Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TP2000-1600 Add Subtask filter to Task admin view #1344

Open
wants to merge 4 commits into
base: TP2000-1471--task-workflow
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tasks/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@ def link_to_workbasket(self, workbasket):
)


class SubtaskFilter(admin.SimpleListFilter):
title = "Subtasks"
parameter_name = "is_subtask"

def lookups(self, request, model_admin):
return (
("TRUE", "Subtasks"),
("FALSE", "Tasks"),
)

def queryset(self, request, queryset):
value = self.value()
if value == "TRUE":
return queryset.subtasks()
elif value == "FALSE":
return queryset.parents()


class TaskAdmin(TaskAdminMixin, admin.ModelAdmin):
list_display = [
"id",
"title",
"is_subtask",
"description",
"category",
"progress_state",
Expand All @@ -41,7 +60,11 @@ class TaskAdmin(TaskAdminMixin, admin.ModelAdmin):
"creator",
]
search_fields = ["id", "title", "description"]
list_filter = ["category", "progress_state"]
list_filter = (
SubtaskFilter,
"category",
"progress_state",
)

def parent_task_id(self, obj):
if not obj.parent_task:
Expand Down
13 changes: 13 additions & 0 deletions tasks/models/task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime

from django.conf import settings
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.db import models
from django.db import transaction
Expand Down Expand Up @@ -74,6 +75,17 @@ def top_level(self):
models.Q(taskitem__isnull=True) | models.Q(taskworkflow__isnull=False),
)

def parents(self):
"""Returns a queryset of tasks who do not have subtasks linked to
them."""
return self.filter(
models.Q(parent_task=None),
)

def subtasks(self):
"""Returns a queryset of tasks who have parent tasks linked to them."""
return self.exclude(models.Q(parent_task=None))


class TaskBase(TimestampedMixin):
"""Abstract model mixin containing model fields common to TaskTemplate and
Expand Down Expand Up @@ -122,6 +134,7 @@ class Task(TaskBase):
objects = TaskManager.from_queryset(TaskQueryset)()

@property
@admin.display(boolean=True)
def is_subtask(self) -> bool:
return bool(self.parent_task)

Expand Down