Updating timeline serializer
parent
0048f4d297
commit
6b281f1839
|
@ -14,14 +14,47 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.forms import widgets
|
||||||
|
|
||||||
from taiga.base.api import serializers
|
from taiga.base.api import serializers
|
||||||
from taiga.base.fields import JsonField
|
from taiga.base.fields import JsonField
|
||||||
|
from taiga.users.services import get_photo_or_gravatar_url, get_big_photo_or_gravatar_url
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
from . import service
|
||||||
|
|
||||||
|
class TimelineDataJsonField(serializers.WritableField):
|
||||||
|
"""
|
||||||
|
Timeline Json objects serializer.
|
||||||
|
"""
|
||||||
|
widget = widgets.Textarea
|
||||||
|
|
||||||
|
def to_native(self, obj):
|
||||||
|
#Updates the data user info saved if the user exists
|
||||||
|
User = apps.get_model("users", "User")
|
||||||
|
userData = obj.get("user", None)
|
||||||
|
if userData:
|
||||||
|
try:
|
||||||
|
user = User.objects.get(id=userData["id"])
|
||||||
|
obj["user"] = {
|
||||||
|
"id": user.pk,
|
||||||
|
"name": user.get_full_name(),
|
||||||
|
"photo": get_photo_or_gravatar_url(user),
|
||||||
|
"big_photo": get_big_photo_or_gravatar_url(user),
|
||||||
|
"username": user.username,
|
||||||
|
}
|
||||||
|
except User.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def from_native(self, data):
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class TimelineSerializer(serializers.ModelSerializer):
|
class TimelineSerializer(serializers.ModelSerializer):
|
||||||
data = JsonField()
|
data = TimelineDataJsonField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Timeline
|
model = models.Timeline
|
||||||
|
|
|
@ -180,11 +180,7 @@ def extract_project_info(instance):
|
||||||
|
|
||||||
def extract_user_info(instance):
|
def extract_user_info(instance):
|
||||||
return {
|
return {
|
||||||
"id": instance.pk,
|
"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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,6 @@ def test_create_project_timeline():
|
||||||
assert project_timeline[0].event_type == "projects.project.create"
|
assert project_timeline[0].event_type == "projects.project.create"
|
||||||
assert project_timeline[0].data["project"]["name"] == "test project timeline"
|
assert project_timeline[0].data["project"]["name"] == "test project timeline"
|
||||||
assert project_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_milestone_timeline():
|
def test_create_milestone_timeline():
|
||||||
|
@ -145,7 +144,6 @@ def test_create_milestone_timeline():
|
||||||
assert milestone_timeline[0].event_type == "milestones.milestone.create"
|
assert milestone_timeline[0].event_type == "milestones.milestone.create"
|
||||||
assert milestone_timeline[0].data["milestone"]["name"] == "test milestone timeline"
|
assert milestone_timeline[0].data["milestone"]["name"] == "test milestone timeline"
|
||||||
assert milestone_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_user_story_timeline():
|
def test_create_user_story_timeline():
|
||||||
|
@ -155,7 +153,6 @@ def test_create_user_story_timeline():
|
||||||
assert project_timeline[0].event_type == "userstories.userstory.create"
|
assert project_timeline[0].event_type == "userstories.userstory.create"
|
||||||
assert project_timeline[0].data["userstory"]["subject"] == "test us timeline"
|
assert project_timeline[0].data["userstory"]["subject"] == "test us timeline"
|
||||||
assert project_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_issue_timeline():
|
def test_create_issue_timeline():
|
||||||
|
@ -165,7 +162,6 @@ def test_create_issue_timeline():
|
||||||
assert project_timeline[0].event_type == "issues.issue.create"
|
assert project_timeline[0].event_type == "issues.issue.create"
|
||||||
assert project_timeline[0].data["issue"]["subject"] == "test issue timeline"
|
assert project_timeline[0].data["issue"]["subject"] == "test issue timeline"
|
||||||
assert project_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_task_timeline():
|
def test_create_task_timeline():
|
||||||
|
@ -175,7 +171,6 @@ def test_create_task_timeline():
|
||||||
assert project_timeline[0].event_type == "tasks.task.create"
|
assert project_timeline[0].event_type == "tasks.task.create"
|
||||||
assert project_timeline[0].data["task"]["subject"] == "test task timeline"
|
assert project_timeline[0].data["task"]["subject"] == "test task timeline"
|
||||||
assert project_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_wiki_page_timeline():
|
def test_create_wiki_page_timeline():
|
||||||
|
@ -185,7 +180,6 @@ def test_create_wiki_page_timeline():
|
||||||
assert project_timeline[0].event_type == "wiki.wikipage.create"
|
assert project_timeline[0].event_type == "wiki.wikipage.create"
|
||||||
assert project_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline"
|
assert project_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline"
|
||||||
assert project_timeline[0].data["user"]["id"] == 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()
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_membership_timeline():
|
def test_create_membership_timeline():
|
||||||
|
@ -195,11 +189,9 @@ def test_create_membership_timeline():
|
||||||
assert project_timeline[0].event_type == "projects.membership.create"
|
assert project_timeline[0].event_type == "projects.membership.create"
|
||||||
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert project_timeline[0].data["user"]["id"] == membership.user.id
|
assert project_timeline[0].data["user"]["id"] == membership.user.id
|
||||||
assert project_timeline[0].data["user"]["name"] == membership.user.get_full_name()
|
|
||||||
assert user_timeline[0].event_type == "projects.membership.create"
|
assert user_timeline[0].event_type == "projects.membership.create"
|
||||||
assert user_timeline[0].data["project"]["id"] == membership.project.id
|
assert user_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert user_timeline[0].data["user"]["id"] == membership.user.id
|
assert user_timeline[0].data["user"]["id"] == membership.user.id
|
||||||
assert user_timeline[0].data["user"]["name"] == membership.user.get_full_name()
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_project_timeline():
|
def test_update_project_timeline():
|
||||||
|
@ -292,19 +284,15 @@ def test_update_membership_timeline():
|
||||||
assert project_timeline[0].event_type == "projects.membership.delete"
|
assert project_timeline[0].event_type == "projects.membership.delete"
|
||||||
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert project_timeline[0].data["user"]["id"] == user_1.id
|
assert project_timeline[0].data["user"]["id"] == user_1.id
|
||||||
assert project_timeline[0].data["user"]["name"] == user_1.get_full_name()
|
|
||||||
assert project_timeline[1].event_type == "projects.membership.create"
|
assert project_timeline[1].event_type == "projects.membership.create"
|
||||||
assert project_timeline[1].data["project"]["id"] == membership.project.id
|
assert project_timeline[1].data["project"]["id"] == membership.project.id
|
||||||
assert project_timeline[1].data["user"]["id"] == user_2.id
|
assert project_timeline[1].data["user"]["id"] == user_2.id
|
||||||
assert project_timeline[1].data["user"]["name"] == user_2.get_full_name()
|
|
||||||
assert user_1_timeline[0].event_type == "projects.membership.delete"
|
assert user_1_timeline[0].event_type == "projects.membership.delete"
|
||||||
assert user_1_timeline[0].data["project"]["id"] == membership.project.id
|
assert user_1_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert user_1_timeline[0].data["user"]["id"] == user_1.id
|
assert user_1_timeline[0].data["user"]["id"] == user_1.id
|
||||||
assert user_1_timeline[0].data["user"]["name"] == user_1.get_full_name()
|
|
||||||
assert user_2_timeline[0].event_type == "projects.membership.create"
|
assert user_2_timeline[0].event_type == "projects.membership.create"
|
||||||
assert user_2_timeline[0].data["project"]["id"] == membership.project.id
|
assert user_2_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert user_2_timeline[0].data["user"]["id"] == user_2.id
|
assert user_2_timeline[0].data["user"]["id"] == user_2.id
|
||||||
assert user_2_timeline[0].data["user"]["name"] == user_2.get_full_name()
|
|
||||||
|
|
||||||
|
|
||||||
def test_delete_project_timeline():
|
def test_delete_project_timeline():
|
||||||
|
@ -363,11 +351,9 @@ def test_delete_membership_timeline():
|
||||||
assert project_timeline[0].event_type == "projects.membership.delete"
|
assert project_timeline[0].event_type == "projects.membership.delete"
|
||||||
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
assert project_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert project_timeline[0].data["user"]["id"] == membership.user.id
|
assert project_timeline[0].data["user"]["id"] == membership.user.id
|
||||||
assert project_timeline[0].data["user"]["name"] == membership.user.get_full_name()
|
|
||||||
assert user_timeline[0].event_type == "projects.membership.delete"
|
assert user_timeline[0].event_type == "projects.membership.delete"
|
||||||
assert user_timeline[0].data["project"]["id"] == membership.project.id
|
assert user_timeline[0].data["project"]["id"] == membership.project.id
|
||||||
assert user_timeline[0].data["user"]["id"] == membership.user.id
|
assert user_timeline[0].data["user"]["id"] == membership.user.id
|
||||||
assert user_timeline[0].data["user"]["name"] == membership.user.get_full_name()
|
|
||||||
|
|
||||||
|
|
||||||
def test_comment_user_story_timeline():
|
def test_comment_user_story_timeline():
|
||||||
|
|
Loading…
Reference in New Issue