Skip to content

Commit

Permalink
Make one-shots dismissable
Browse files Browse the repository at this point in the history
  • Loading branch information
wildjames committed Nov 6, 2023
1 parent 66715e5 commit f850eef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.5 on 2023-11-06 20:50

import datetime
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tasks', '0007_alter_oneshottask_due_date'),
]

operations = [
migrations.AddField(
model_name='oneshottask',
name='last_completed',
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2023, 11, 6, 20, 50, 34, 981370, tzinfo=datetime.timezone.utc)),
preserve_default=False,
),
]
6 changes: 6 additions & 0 deletions todoqueue_backend/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class OneShotTask(models.Model):
Household, on_delete=models.CASCADE, related_name="oneshot_tasks"
)
frozen = models.BooleanField(default=False)
last_completed = models.DateTimeField(auto_now_add=True)
has_completed = models.BooleanField(default=False)

@property
Expand Down Expand Up @@ -338,6 +339,11 @@ def save(self, *args, **kwargs):
try:
# Tasks MUST have a last_completed field
self.content_object.last_completed = timezone.now()

# Check if the task is a OneShotTask and set has_completed to True
if isinstance(self.content_object, OneShotTask):
self.content_object.has_completed = True

self.content_object.save()
except AttributeError:
logger.error(
Expand Down
34 changes: 26 additions & 8 deletions todoqueue_backend/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ class OneShotTaskViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = OneShotTask.objects.all().order_by("-task_name")
serializer_class = OneShotTaskSerializer

def get_queryset(self):
"""When a user requests a task list, only get the ones part of that household"""
logger.info(f"Getting tasks for user: {self.request.user}")
user = self.request.user
household_id = self.request.query_params.get("household", None)

if household_id is None:
logger.info("No household provided")
return OneShotTask.objects.none()

household = get_object_or_404(Household, id=household_id)

if user in household.users.all():
return OneShotTask.objects.filter(household=household).order_by(
"-task_name"
Expand All @@ -142,15 +142,23 @@ def get_queryset(self):
if user in household.users.all():
scheduled_tasks = ScheduledTask.objects.filter(household=household)
flexible_tasks = FlexibleTask.objects.filter(household=household)
oneshot_tasks = OneShotTask.objects.filter(household=household)
# Only incomplete one-shots are listed
oneshot_tasks = OneShotTask.objects.filter(
household=household, has_completed=False
)
return list(scheduled_tasks) + list(flexible_tasks) + list(oneshot_tasks)
else:
# Return an empty queryset if the user does not belong to the household
return []

def retrieve(self, request, *args, **kwargs):
task_id = kwargs.get("pk")
task, task_type = get_task_by_id(task_id)
try:
task_id = kwargs.get("pk")
task, task_type = get_task_by_id(task_id)
except:
return Response(
{"detail": "Task not found."}, status=status.HTTP_404_NOT_FOUND
)

if not task:
return Response(
Expand Down Expand Up @@ -204,7 +212,17 @@ def dismiss_task(request, taskId):

# Set the last_completed field to the current time
task.last_completed = timezone.now()
task.save(update_fields=["last_completed"])

# One-shots also need to have their "has_completed" field set to true
if isinstance(task, OneShotTask):
logger.debug(
f"This is a one-shot task, so setting the has_completed field to True"
)
task.has_completed = True
else:
logger.debug(f"Task is of type {task.__class__}")

task.save()

return Response({"last_completed": task.last_completed}, status=status.HTTP_200_OK)

Expand Down

0 comments on commit f850eef

Please sign in to comment.