<reponame>rameshsutaliya/RestAPI<filename>Python/Project/Project/todo/views.py
from django.shortcuts import render, redirect
from .models import TodoList, Category

from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from Project.todo.serializers import UserSerializer, GroupSerializer, TodoSerializer


def index(request):  # the index view
    todos = TodoList.objects.all()  # quering all todos with the object manager
    categories = Category.objects.all()  # getting all categories with object manager
    if request.method == "POST":  # checking if the request method is a POST
        if "taskAdd" in request.POST:  # checking if there is a request to add a todo
            title = request.POST["description"]  # title
            date = str(request.POST["date"])  # date
            category = request.POST["category_select"]  # category
            discription = title + " -- " + date + " " + category  # discription
            Todo = TodoList(title=title, discription=discription, due_date=date,
                            category=Category.objects.get(name=category))
            Todo.save()  # saving the todo
            return redirect("/")  # reloading the page
        if "taskDelete" in request.POST:  # checking if there is a request to delete a todo
            # checked todos to be deleted
            checkedlist = request.POST["checkedbox"]
            for todo_id in checkedlist:
                todo = TodoList.objects.get(id=int(todo_id))  # getting todo id
                todo.delete()  # deleting todo
    return render(request, "index.html", {"todos": todos, "categories": categories})


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]


class TodoViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows todolist to be viewed or edited by everyone {no permission}
    """
    queryset = TodoList.objects.all()
    serializer_class = TodoSerializer
    permissions_classes = None

from django.shortcuts import render, redirect
from .models import TodoList, Category

from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from Project.todo.serializers import UserSerializer, GroupSerializer, TodoSerializer


def index(request):  # the index view
    todos = TodoList.objects.all()  # quering all todos with the object manager
    categories = Category.objects.all()  # getting all categories with object manager
    if request.method == "POST":  # checking if the request method is a POST
        if "taskAdd" in request.POST:  # checking if there is a request to add a todo
            title = request.POST["description"]  # title
            date = str(request.POST["date"])  # date
            category = request.POST["category_select"]  # category
            discription = title + " -- " + date + " " + category  # discription
            Todo = TodoList(title=title, discription=discription, due_date=date,
                            category=Category.objects.get(name=category))
            Todo.save()  # saving the todo
            return redirect("/")  # reloading the page
        if "taskDelete" in request.POST:  # checking if there is a request to delete a todo
            # checked todos to be deleted
            checkedlist = request.POST["checkedbox"]
            for todo_id in checkedlist:
                todo = TodoList.objects.get(id=int(todo_id))  # getting todo id
                todo.delete()  # deleting todo
    return render(request, "index.html", {"todos": todos, "categories": categories})


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]


class TodoViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows todolist to be viewed or edited by everyone {no permission}
    """
    queryset = TodoList.objects.all()
    serializer_class = TodoSerializer
    permissions_classes = None
# the index view # quering all todos with the object manager # getting all categories with object manager # checking if the request method is a POST # checking if there is a request to add a todo # title # date # category # discription # saving the todo # reloading the page # checking if there is a request to delete a todo # checked todos to be deleted # getting todo id # deleting todo API endpoint that allows users to be viewed or edited. API endpoint that allows groups to be viewed or edited. API endpoint that allows todolist to be viewed or edited by everyone {no permission}