simplify view and add due_date ordering

This commit is contained in:
2022-04-20 18:16:51 +00:00
parent c1b5a2c78d
commit 7ec219f99c
5 changed files with 31 additions and 37 deletions

View File

@@ -2,18 +2,13 @@ from django.db import models
from django.db.models import Q from django.db.models import Q
class TaskQuerySet(models.QuerySet):
def get_ordered(self):
return self.order_by("task_list")
class TaskManager(models.Manager): class TaskManager(models.Manager):
def get_tasks(self, user, completed, task_list, all_tasks): def get_tasks(self, user, assigned_tasks, task_list, completed):
# None ... assigned to all users # None ... assigned to all users
qs_all = self.get_queryset().get_ordered() qs_all = self.get_queryset()
qs = qs_all.filter(assigned_to__id=user) qs = qs_all.filter(assigned_to__id=user)
if all_tasks: if not assigned_tasks:
qs_tmp = qs_all.filter( qs_tmp = qs_all.filter(
Q(assigned_to=None) & Q(task_list__users__id__exact=user) Q(assigned_to=None) & Q(task_list__users__id__exact=user)
) )
@@ -23,6 +18,3 @@ class TaskManager(models.Manager):
qs = qs.filter(task_list=task_list) qs = qs.filter(task_list=task_list)
return qs.filter(completed=completed) return qs.filter(completed=completed)
def get_queryset(self):
return TaskQuerySet(self.model, using=self._db)

View File

@@ -3,6 +3,7 @@ from django.conf import settings
from django.core.validators import ValidationError from django.core.validators import ValidationError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.db.models import F
from django.db.models.constraints import UniqueConstraint from django.db.models.constraints import UniqueConstraint
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@@ -80,6 +81,7 @@ class Task(models.Model):
taskmanager = TaskManager() taskmanager = TaskManager()
class Meta: class Meta:
ordering = ("task_list", F("due_date").desc(nulls_first=True))
verbose_name = "Aufgabe" verbose_name = "Aufgabe"
verbose_name_plural = "Aufgaben" verbose_name_plural = "Aufgaben"

View File

@@ -9,8 +9,8 @@ app_name = apps.TasksConfig.name
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path("task-create/", TaskCreate.as_view(), name="task-create"), path("add/", TaskCreate.as_view(), name="task-create"),
path("<int:pk>/docu-create/", DocumentCreate.as_view(), name="docu-create"),
path("<int:pk>/update/", TaskUpdate.as_view(), name="task-update"), path("<int:pk>/update/", TaskUpdate.as_view(), name="task-update"),
path("<int:pk>/detail/", TaskDetail.as_view(), name="task-detail"), path("<int:pk>/detail/", TaskDetail.as_view(), name="task-detail"),
path("<int:pk>/add/", DocumentCreate.as_view(), name="docu-create"),
] ]

View File

@@ -22,10 +22,12 @@ logger = logging.getLogger(__name__)
@authenticated_user @authenticated_user
def index(request): def index(request):
completed = False
tasklist = None tasklist = None
show_tasklist = None show_tasklist = None
show_all_tasks = True assigned_tasks = None
completed = False
state_open = False
state_closed = False
if request.method == "POST": if request.method == "POST":
if "btn_checkbox" in request.POST: if "btn_checkbox" in request.POST:
@@ -38,38 +40,35 @@ def index(request):
task.save() task.save()
if request.method == "GET": if request.method == "GET":
if "tasklist" in request.GET: if request.GET.get("tasklist"):
if request.GET["tasklist"] != "all": if request.GET.get("tasklist") != "all":
tasklist = TaskList.objects.filter(id=request.GET["tasklist"]).first() tasklist = TaskList.objects.filter(id=request.GET["tasklist"]).first()
show_tasklist = tasklist.id show_tasklist = tasklist.id
if "tasks" in request.GET: if request.GET.get("tasks") == "assigned":
if request.GET["tasks"] == "all": assigned_tasks = True
show_all_tasks = True
else:
show_all_tasks = False
if "open_tasks" in request.GET: if request.GET.get("open_tasks"):
completed = False completed = False
state_open = True
if "closed_tasks" in request.GET: if request.GET.get("closed_tasks"):
completed = True completed = True
state_closed = True
tasks = Task.taskmanager.get_tasks( tasks = Task.taskmanager.get_tasks(
user=request.user.id, user=request.user.id,
completed=completed, assigned_tasks=assigned_tasks,
task_list=tasklist, task_list=tasklist,
all_tasks=show_all_tasks, completed=completed,
) )
tasklists = TaskList.objects.all()
context = { context = {
"tasks": tasks, "tasks": tasks,
"tasklists": tasklists,
"current_user": request.user.id,
"completed": completed,
"show_tasklist": show_tasklist, "show_tasklist": show_tasklist,
"show_all_tasks": show_all_tasks, "assigned_tasks": assigned_tasks,
"state_open": state_open,
"state_closed": state_closed,
} }
return render(request, "tasks/index.html", context) return render(request, "tasks/index.html", context)

View File

@@ -43,24 +43,25 @@
<span class="text-gray-700 dark:text-gray-200">Task-Gruppe</span> <span class="text-gray-700 dark:text-gray-200">Task-Gruppe</span>
<select id="id_tasklist" name="tasklist" class="block w-full mt-1 rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50"> <select id="id_tasklist" name="tasklist" class="block w-full mt-1 rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
<option value="all" {% if not show_tasklist %}selected{% endif %}>alle Task-Gruppen</option> <option value="all" {% if not show_tasklist %}selected{% endif %}>alle Task-Gruppen</option>
{% for elem in tasklists %} {% regroup tasks by task_list as section_list %}
<option value="{{ elem.id }}" {% if show_tasklist == elem.id %}selected{% endif %}>{{ elem.name }}</option> {% for group in section_list %}
<option value="{{ group.grouper.id }}" {% if show_tasklist == group.grouper.id %}selected{% endif %}>{{ group.grouper.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</label> </label>
<label> <label>
<span class="text-gray-700 dark:text-gray-200">Tasks</span> <span class="text-gray-700 dark:text-gray-200">Tasks</span>
<select id="id_task" name="tasks" class="block w-full mt-1 rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50"> <select id="id_task" name="tasks" class="block w-full mt-1 rounded-md border-gray-300 dark:border-none shadow-sm focus:border-none focus:ring focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
<option value="own" {% if not show_all_tasks %}selected{% endif %}>eigenen Tasks</option> <option value="assigned" {% if assigned_tasks %}selected{% endif %}>dir zugewiesene</option>
<option value="all" {% if show_all_tasks %}selected{% endif %}>alle Tasks</option> <option value="all" {% if not assigned_tasks %}selected{% endif %}>alle</option>
</select> </select>
</label> </label>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="open_tasks" {% if not completed %}checked{% endif %} class="rounded border-gray-300 dark:border-none text-proprietary shadow-sm focus:border-blue-300 focus:ring focus:ring-offset-0 focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50"> <input type="checkbox" name="open_tasks" {% if state_open %}checked{% endif %} class="rounded border-gray-300 dark:border-none text-proprietary shadow-sm focus:border-blue-300 focus:ring focus:ring-offset-0 focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
<span class="ml-2 text-gray-700 dark:text-gray-200">Offene Tasks</span> <span class="ml-2 text-gray-700 dark:text-gray-200">Offene Tasks</span>
</label> </label>
<label class="inline-flex items-center"> <label class="inline-flex items-center">
<input type="checkbox" name="closed_tasks" {% if completed %}checked{% endif %} class="rounded border-gray-300 dark:border-none text-proprietary shadow-sm focus:border-blue-300 focus:ring focus:ring-offset-0 focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50"> <input type="checkbox" name="closed_tasks" {% if state_closed %}checked{% endif %} class="rounded border-gray-300 dark:border-none text-proprietary shadow-sm focus:border-blue-300 focus:ring focus:ring-offset-0 focus:ring-blue-200 dark:focus:ring-sky-700 focus:ring-opacity-50">
<span class="ml-2 text-gray-700 dark:text-gray-200">Geschlossene Tasks</span> <span class="ml-2 text-gray-700 dark:text-gray-200">Geschlossene Tasks</span>
</label> </label>
<input type="submit" class="block btn btn-primary" value="Anzeigen"> <input type="submit" class="block btn btn-primary" value="Anzeigen">