Issue
I’m building a task management project. I have a APIview in views.py, that gives me report based on characteristics of the given project tasks, but I need to take into account only those, which deadline ends in current week through ORM, how to do it?
#views.py
...
from .managers import get_report_productivity_project
class ProjectReportView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
proj_id = request.data['project']
task_pending_queryset = ProjectTask.objects.filter(project__id=proj_id, task_status__in=[0, 1])
task_overdue_queryset = ProjectTask.objects.filter(project__id=proj_id, task_status__in=[4])
task_completed_queryset = ProjectTask.objects.filter(project__id=proj_id, task_status__in=[3, 5])
high_priority_tasks = ProjectTask.objects.filter(project__id=proj_id, priority=3)
queryset = ProjectTask.objects.filter(project__id=proj_id).exclude(Q(task_status__in=[0, 1, 2]) | Q(priority__in=[0]))
report = get_report_productivity_project(queryset)
status = {}
status['pending'] = len(task_pending_queryset)
status['completed'] = len(task_completed_queryset)
status['overdue'] = len(task_overdue_queryset)
serializer = ProjectTaskSerializer(high_priority_tasks, many=True)
return Response({"status": status, "project_report":report, "high_priority_tasks":serializer.data}, 202)
#models.py
...
class StatusType(models.IntegerChoices):
TO_DO = 0, _("TO DO")
IN_PROGRESS = 1, _("IN PROGRESS")
REVIEW = 2, _("REVIEW")
DONE = 3, _("DONE")
OVERDUE = 4, _("OVERDUE")
AHEAD = 5, _("AHEAD")
class ProjectTask(models.Model):
project = models.ForeignKey(Project, on_delete=models.SET_NULL, blank=False, null=True)
executer = models.ForeignKey(Collabarator, on_delete=models.SET_NULL, blank=False, null=True)
deadline = models.DateTimeField(blank=True, null=True)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
progress = models.PositiveIntegerField(default=0, null=True)
header = models.CharField(max_length=255, null=False, blank=False)
description = models.TextField(blank=True, null=True)
task_status = models.PositiveIntegerField(choices=StatusType.choices, default=StatusType.TO_DO)
priority = models.IntegerField(choices=ImportanceType.choices, default=ImportanceType.LOW)
What I tried
#views.py
date = datetime.date.today()
start_week = date - datetime.timedelta(date.weekday())
end_week = start_week + datetime.timedelta(7)
queryset = ProjectTask.objects.filter(project__id=proj_id, deadline__range=(start_week, end_week)).exclude(Q(task_status__in=[0, 1, 2]) | Q(priority__in=[0]))
Is it a proper way to solve this problem? Because I have a timezone UTC+5 in django settings, but I don’t know if this ORM query take into account timezone detail.