Files
fet2020/fet2020/tasks/views.py

163 lines
4.5 KiB
Python

import logging
from collections import deque
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy, reverse
from django.utils import timezone
from authentications.decorators import authenticated_user
from documents.api import get_pad_link
from documents.etherpadlib import add_ep_cookie
from .forms import TaskForm, DocumentForm
from .models import Task, TaskList, Document
logger = logging.getLogger(__name__)
@authenticated_user
def index(request):
current_action = False
tasklist = None
show_tasklist = None
show_all_tasks = True
if request.method == "POST":
if "btn_checkbox" in request.POST:
for task_id in request.POST.getlist("checkbox"):
task = Task.objects.get(id=task_id)
if not task.completed:
task.completed = True
task.completed_date = timezone.now().date()
task.save()
if "btn_user" in request.POST:
if request.POST["action"] == "show_incompleted":
current_action = False
else:
current_action = True
if request.POST["tasklist"] != "all":
tasklist = TaskList.objects.filter(id=request.POST["tasklist"]).first()
show_tasklist = tasklist.id
if request.POST["tasks"] == "all":
show_all_tasks = True
else:
show_all_tasks = False
form = TaskForm()
tasks = deque(
Task.taskmanager.get_tasks(
user=request.user.id,
completed=current_action,
task_list=tasklist,
all_tasks=show_all_tasks,
)
)
tasklists = deque(TaskList.objects.all())
context = {
"formset": form,
"tasks": tasks,
"tasklists": tasklists,
"current_user": request.user.id,
"current_action": current_action,
"show_tasklist": show_tasklist,
"show_all_tasks": show_all_tasks,
}
return render(request, "tasks/index.html", context)
class TaskCreate(LoginRequiredMixin, CreateView):
model = Task
success_url = reverse_lazy("tasks")
template_name = "tasks/task_create.html"
form_class = TaskForm
def form_valid(self, form):
form.instance.created_by = self.request.user
return super().form_valid(form)
class TaskUpdate(LoginRequiredMixin, UpdateView):
model = Task
success_url = reverse_lazy("tasks")
template_name = "tasks/task_create.html"
form_class = TaskForm
class TaskDetail(LoginRequiredMixin, DetailView):
model = Task
success_url = reverse_lazy("tasks")
template_name = "tasks/task_detail.html"
def get(self, request, *args, **kwargs):
response = super().get(request, *args, **kwargs)
try:
response = add_ep_cookie(self.request, response)
except Exception as e:
logger.info("Etherpad Server doesn't work. Error: %s", e)
return response
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
docus = deque(
Document.objects.filter(task__id=self.object.id).order_by("-date")
)
documents = deque([])
# list of etherpad url-link of any documents
for elem in docus:
documents.append(
{
"title": elem.title,
"date": elem.date,
"etherpad_key": get_pad_link(elem.etherpad_key),
}
)
context["documents"] = documents
return context
class DocumentCreate(LoginRequiredMixin, CreateView):
model = Document
# success_url = reverse_lazy('tasks')
template_name = "tasks/docu_create.html"
form_class = DocumentForm
task_id = None
def get_initial(self):
self.task_id = self.kwargs.get("pk")
task = Task.objects.get(pk=self.task_id)
context = {
"task": task,
}
return context
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["task_id"] = self.task_id
return context
def get_success_url(self):
context = {
"task_id": self.task_id,
}
return reverse("task-detail", kwargs=context)