Skip to content

Commit

Permalink
Setting finished date correctly on tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
superalex authored and bameda committed Nov 4, 2015
1 parent 06e5492 commit 195c62f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions taiga/export_import/api.py
Expand Up @@ -33,6 +33,7 @@
from taiga.base.api.viewsets import GenericViewSet
from taiga.projects.models import Project, Membership
from taiga.projects.issues.models import Issue
from taiga.projects.tasks.models import Task
from taiga.projects.serializers import ProjectSerializer

from . import mixins
Expand Down Expand Up @@ -238,6 +239,9 @@ def task(self, request, *args, **kwargs):
project = self.get_object_or_none()
self.check_permissions(request, 'import_item', project)

signals.pre_save.disconnect(sender=Task,
dispatch_uid="set_finished_date_when_edit_task")

task = service.store_task(project, request.DATA.copy())

errors = service.get_errors()
Expand Down
4 changes: 4 additions & 0 deletions taiga/projects/tasks/apps.py
Expand Up @@ -23,6 +23,10 @@
from . import signals as handlers

def connect_tasks_signals():
# Finished date
signals.pre_save.connect(handlers.set_finished_date_when_edit_task,
sender=apps.get_model("tasks", "Task"),
dispatch_uid="set_finished_date_when_edit_task")
# Tags
signals.pre_save.connect(generic_handlers.tags_normalization,
sender=apps.get_model("tasks", "Task"),
Expand Down
50 changes: 50 additions & 0 deletions taiga/projects/tasks/migrations/0009_auto_20151104_1131.py
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import connection, migrations, models

def set_finished_date_for_tasks(apps, schema_editor):
# Updates the finished date from tasks according to the history_entries associated
# It takes the last history change updateing the status of a task and if it's a closed
# one it updates the finished_date attribute
sql="""
WITH status_update AS(
WITH status_update AS(
WITH history_entries AS (
SELECT
diff#>>'{status, 1}' new_status_id,
regexp_split_to_array(key, ':') as split_key,
created_at as date
FROM history_historyentry
WHERE diff#>>'{status, 1}' != ''
)
SELECT
split_key[2] as object_id,
new_status_id::int,
MAX(date) as status_change_datetime
FROM history_entries
WHERE split_key[1] = 'tasks.task'
GROUP BY object_id, new_status_id, date
)
SELECT status_update.*
FROM status_update
INNER JOIN projects_taskstatus
ON projects_taskstatus.id = new_status_id AND projects_taskstatus.is_closed = True
)
UPDATE tasks_task
SET finished_date = status_update.status_change_datetime
FROM status_update
WHERE tasks_task.id = status_update.object_id::int
"""
cursor = connection.cursor()
cursor.execute(sql)

class Migration(migrations.Migration):

dependencies = [
('tasks', '0008_remove_task_watchers'),
]

operations = [
migrations.RunPython(set_finished_date_for_tasks),
]
11 changes: 11 additions & 0 deletions taiga/projects/tasks/signals.py
Expand Up @@ -16,6 +16,7 @@

from contextlib import suppress
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone

####################################
# Signals for cached prev task
Expand Down Expand Up @@ -92,3 +93,13 @@ def _try_to_close_milestone_when_delete_task(instance):
with suppress(ObjectDoesNotExist):
if instance.milestone_id and services.calculate_milestone_is_closed(instance.milestone):
services.close_milestone(instance.milestone)

####################################
# Signals for set finished date
####################################

def set_finished_date_when_edit_task(sender, instance, **kwargs):
if instance.status.is_closed and not instance.finished_date:
instance.finished_date = timezone.now()
elif not instance.status.is_closed and instance.finished_date:
instance.finished_date = None

0 comments on commit 195c62f

Please sign in to comment.