From 9589bba63b3bf634cc7fe2cfcc3a23bd9112d2a0 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 8 Apr 2015 07:34:20 +0200 Subject: [PATCH] Adding ref data to timeline API --- taiga/timeline/service.py | 59 +++++++++++++++++ taiga/timeline/signals.py | 8 ++- taiga/timeline/timeline_implementations.py | 75 ++++------------------ tests/integration/test_timeline.py | 12 ++-- 4 files changed, 84 insertions(+), 70 deletions(-) diff --git a/taiga/timeline/service.py b/taiga/timeline/service.py index da039a57..19087412 100644 --- a/taiga/timeline/service.py +++ b/taiga/timeline/service.py @@ -24,6 +24,7 @@ from functools import partial, wraps from taiga.base.utils.db import get_typename_for_model_class from taiga.celery import app +from taiga.users.services import get_photo_or_gravatar_url, get_big_photo_or_gravatar_url _timeline_impl_map = {} @@ -165,3 +166,61 @@ def register_timeline_implementation(typename:str, event_type:str, fn=None): _timeline_impl_map[key] = _wrapper return _wrapper + + + +def extract_project_info(instance): + return { + "id": instance.pk, + "slug": instance.slug, + "name": instance.name, + } + + +def extract_user_info(instance): + return { + "id": instance.pk, + "name": instance.get_full_name(), + "photo": get_photo_or_gravatar_url(instance), + "big_photo": get_big_photo_or_gravatar_url(instance), + "username": instance.username, + } + + +def extract_milestone_info(instance): + return { + "id": instance.pk, + "slug": instance.slug, + "name": instance.name, + } + + +def extract_userstory_info(instance): + return { + "id": instance.pk, + "ref": instance.ref, + "subject": instance.subject, + } + + +def extract_issue_info(instance): + return { + "id": instance.pk, + "ref": instance.ref, + "subject": instance.subject, + } + + +def extract_task_info(instance): + return { + "id": instance.pk, + "ref": instance.ref, + "subject": instance.subject, + } + + +def extract_wiki_page_info(instance): + return { + "id": instance.pk, + "slug": instance.slug, + } diff --git a/taiga/timeline/signals.py b/taiga/timeline/signals.py index 8361048a..cb9c9334 100644 --- a/taiga/timeline/signals.py +++ b/taiga/timeline/signals.py @@ -20,7 +20,8 @@ from taiga.projects.history import services as history_services from taiga.projects.models import Project from taiga.users.models import User from taiga.projects.history.choices import HistoryType -from taiga.timeline.service import push_to_timeline, build_user_namespace, build_project_namespace +from taiga.timeline.service import (push_to_timeline, build_user_namespace, + build_project_namespace, extract_user_info) # TODO: Add events to followers timeline when followers are implemented. # TODO: Add events to project watchers timeline when project watchers are implemented. @@ -72,13 +73,14 @@ def on_new_history_entry(sender, instance, created, **kwargs): elif instance.type == HistoryType.delete: event_type = "delete" + user = User.objects.get(id=instance.user["pk"]) + extra_data = { "values_diff": instance.values_diff, - "user": instance.user, + "user": extract_user_info(user), "comment": instance.comment, } - user = User.objects.get(id=instance.user["pk"]) _push_to_timelines(project, user, obj, event_type, extra_data=extra_data) diff --git a/taiga/timeline/timeline_implementations.py b/taiga/timeline/timeline_implementations.py index 364027a2..79c93371 100644 --- a/taiga/timeline/timeline_implementations.py +++ b/taiga/timeline/timeline_implementations.py @@ -15,18 +15,14 @@ # along with this program. If not, see . from taiga.timeline.service import register_timeline_implementation - +from . import service @register_timeline_implementation("projects.project", "create") @register_timeline_implementation("projects.project", "change") @register_timeline_implementation("projects.project", "delete") def project_timeline(instance, extra_data={}): result ={ - "project": { - "id": instance.pk, - "slug": instance.slug, - "name": instance.name, - }, + "project": service.extract_project_info(instance), } result.update(extra_data) return result @@ -37,16 +33,8 @@ def project_timeline(instance, extra_data={}): @register_timeline_implementation("milestones.milestone", "delete") def project_timeline(instance, extra_data={}): result ={ - "milestone": { - "id": instance.pk, - "slug": instance.slug, - "name": instance.name, - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - } + "milestone": service.extract_milestone_info(instance), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result @@ -57,15 +45,8 @@ def project_timeline(instance, extra_data={}): @register_timeline_implementation("userstories.userstory", "delete") def userstory_timeline(instance, extra_data={}): result ={ - "userstory": { - "id": instance.pk, - "subject": instance.subject, - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - } + "userstory": service.extract_userstory_info(instance), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result @@ -76,15 +57,8 @@ def userstory_timeline(instance, extra_data={}): @register_timeline_implementation("issues.issue", "delete") def issue_timeline(instance, extra_data={}): result ={ - "issue": { - "id": instance.pk, - "subject": instance.subject, - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - } + "issue": service.extract_issue_info(instance), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result @@ -95,15 +69,8 @@ def issue_timeline(instance, extra_data={}): @register_timeline_implementation("tasks.task", "delete") def task_timeline(instance, extra_data={}): result ={ - "task": { - "id": instance.pk, - "subject": instance.subject, - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - } + "task": service.extract_task_info(instance), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result @@ -114,15 +81,8 @@ def task_timeline(instance, extra_data={}): @register_timeline_implementation("wiki.wikipage", "delete") def wiki_page_timeline(instance, extra_data={}): result ={ - "wiki_page": { - "id": instance.pk, - "slug": instance.slug, - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - } + "wiki_page": service.extract_wiki_page_info(instance), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result @@ -132,15 +92,8 @@ def wiki_page_timeline(instance, extra_data={}): @register_timeline_implementation("projects.membership", "delete") def membership_create_timeline(instance, extra_data={}): result = { - "user": { - "id": instance.user.pk, - "name": instance.user.get_full_name(), - }, - "project": { - "id": instance.project.pk, - "slug": instance.project.slug, - "name": instance.project.name, - }, + "user": service.extract_user_info(instance.user), + "project": service.extract_project_info(instance.project), } result.update(extra_data) return result diff --git a/tests/integration/test_timeline.py b/tests/integration/test_timeline.py index 4439fe8b..c90ff72e 100644 --- a/tests/integration/test_timeline.py +++ b/tests/integration/test_timeline.py @@ -134,7 +134,7 @@ def test_create_project_timeline(): project_timeline = service.get_project_timeline(project) assert project_timeline[0].event_type == "projects.project.create" assert project_timeline[0].data["project"]["name"] == "test project timeline" - assert project_timeline[0].data["user"]["pk"] == project.owner.id + assert project_timeline[0].data["user"]["id"] == project.owner.id assert project_timeline[0].data["user"]["name"] == project.owner.get_full_name() @@ -144,7 +144,7 @@ def test_create_milestone_timeline(): milestone_timeline = service.get_project_timeline(milestone.project) assert milestone_timeline[0].event_type == "milestones.milestone.create" assert milestone_timeline[0].data["milestone"]["name"] == "test milestone timeline" - assert milestone_timeline[0].data["user"]["pk"] == milestone.owner.id + assert milestone_timeline[0].data["user"]["id"] == milestone.owner.id assert milestone_timeline[0].data["user"]["name"] == milestone.owner.get_full_name() @@ -154,7 +154,7 @@ def test_create_user_story_timeline(): project_timeline = service.get_project_timeline(user_story.project) assert project_timeline[0].event_type == "userstories.userstory.create" assert project_timeline[0].data["userstory"]["subject"] == "test us timeline" - assert project_timeline[0].data["user"]["pk"] == user_story.owner.id + assert project_timeline[0].data["user"]["id"] == user_story.owner.id assert project_timeline[0].data["user"]["name"] == user_story.owner.get_full_name() @@ -164,7 +164,7 @@ def test_create_issue_timeline(): project_timeline = service.get_project_timeline(issue.project) assert project_timeline[0].event_type == "issues.issue.create" assert project_timeline[0].data["issue"]["subject"] == "test issue timeline" - assert project_timeline[0].data["user"]["pk"] == issue.owner.id + assert project_timeline[0].data["user"]["id"] == issue.owner.id assert project_timeline[0].data["user"]["name"] == issue.owner.get_full_name() @@ -174,7 +174,7 @@ def test_create_task_timeline(): project_timeline = service.get_project_timeline(task.project) assert project_timeline[0].event_type == "tasks.task.create" assert project_timeline[0].data["task"]["subject"] == "test task timeline" - assert project_timeline[0].data["user"]["pk"] == task.owner.id + assert project_timeline[0].data["user"]["id"] == task.owner.id assert project_timeline[0].data["user"]["name"] == task.owner.get_full_name() @@ -184,7 +184,7 @@ def test_create_wiki_page_timeline(): project_timeline = service.get_project_timeline(page.project) assert project_timeline[0].event_type == "wiki.wikipage.create" assert project_timeline[0].data["wiki_page"]["slug"] == "test wiki page timeline" - assert project_timeline[0].data["user"]["pk"] == page.owner.id + assert project_timeline[0].data["user"]["id"] == page.owner.id assert project_timeline[0].data["user"]["name"] == page.owner.get_full_name()