From 4dec832ab124083008758199166c590b19360f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Mon, 14 Oct 2024 20:58:15 +0200 Subject: [PATCH] Fix user status in translation comments (#3408) --- pontoon/base/models/comment.py | 3 ++- pontoon/base/tests/models/test_comment.py | 33 +++++++++++++++++++++++ pontoon/test/factories.py | 20 ++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pontoon/base/tests/models/test_comment.py diff --git a/pontoon/base/models/comment.py b/pontoon/base/models/comment.py index 7ecb88f29c..384a594fd1 100644 --- a/pontoon/base/models/comment.py +++ b/pontoon/base/models/comment.py @@ -27,10 +27,11 @@ def __str__(self): return self.content def serialize(self): + locale = self.locale or self.translation.locale return { "author": self.author.name_or_email, "username": self.author.username, - "user_status": self.author.status(self.locale), + "user_status": self.author.status(locale), "user_gravatar_url_small": self.author.gravatar_url(88), "created_at": self.timestamp.strftime("%b %d, %Y %H:%M"), "date_iso": self.timestamp.isoformat(), diff --git a/pontoon/base/tests/models/test_comment.py b/pontoon/base/tests/models/test_comment.py new file mode 100644 index 0000000000..b76ae6cebd --- /dev/null +++ b/pontoon/base/tests/models/test_comment.py @@ -0,0 +1,33 @@ +import pytest + +from pontoon.test.factories import TeamCommentFactory, TranslationCommentFactory + + +@pytest.mark.django_db +def test_serialize_comments(): + tr = TranslationCommentFactory.create() + team = TeamCommentFactory.create() + + assert tr.serialize() == { + "author": tr.author.name_or_email, + "username": tr.author.username, + "user_status": tr.author.status(tr.translation.locale), + "user_gravatar_url_small": tr.author.gravatar_url(88), + "created_at": tr.timestamp.strftime("%b %d, %Y %H:%M"), + "date_iso": tr.timestamp.isoformat(), + "content": tr.content, + "pinned": tr.pinned, + "id": tr.id, + } + + assert team.serialize() == { + "author": team.author.name_or_email, + "username": team.author.username, + "user_status": team.author.status(team.locale), + "user_gravatar_url_small": team.author.gravatar_url(88), + "created_at": team.timestamp.strftime("%b %d, %Y %H:%M"), + "date_iso": team.timestamp.isoformat(), + "content": team.content, + "pinned": team.pinned, + "id": team.id, + } diff --git a/pontoon/test/factories.py b/pontoon/test/factories.py index bc7ab07837..19c37c2cf2 100644 --- a/pontoon/test/factories.py +++ b/pontoon/test/factories.py @@ -9,6 +9,7 @@ from pontoon.base.models import ( ChangedEntityLocale, + Comment, Entity, Locale, LocaleCodeHistory, @@ -142,6 +143,25 @@ class IdenticalTranslationFactory(TranslationFactory): entity = SubFactory(EntityFactory, string=SelfAttribute("..string")) +class TranslationCommentFactory(DjangoModelFactory): + content = Sequence(lambda n: f"Comment {n}") + author = SubFactory(UserFactory) + translation = SubFactory(TranslationFactory) + + class Meta: + model = Comment + + +class TeamCommentFactory(DjangoModelFactory): + content = Sequence(lambda n: f"Comment {n}") + author = SubFactory(UserFactory) + entity = SubFactory(EntityFactory) + locale = SubFactory(LocaleFactory) + + class Meta: + model = Comment + + class TranslationMemoryFactory(DjangoModelFactory): source = Sequence(lambda n: f"source {n}") target = Sequence(lambda n: f"target {n}")