Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion tcms/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ def post_save(self, instance, created, using=None, **kwargs):
# DB query so it is better to use the private field instead!
# In older simple_history version this field wasn't private but was renamed
# in 2.10.0 hence the pylint disable!
instance._change_reason = diff_objects( # pylint: disable=protected-access
diff = diff_objects(
instance.previous, instance, self.fields_included(instance)
)
if diff:
instance._change_reason = diff # pylint: disable=protected-access
super().post_save(instance, created, using, **kwargs)

def finalize(self, sender, **kwargs):
Expand All @@ -134,6 +136,30 @@ class ReadOnlyHistoryAdmin(SimpleHistoryAdmin):
def Diff(self, obj): # pylint: disable=invalid-name
return safe(f"<pre>{obj.history_change_reason}</pre>")

def set_history_delta_changes(
self, request, historical_records, foreign_keys_are_objs=True
):
super().set_history_delta_changes(
request, historical_records, foreign_keys_are_objs
)
for record in historical_records:
if getattr(record, "history_delta_changes", None):
continue

reason = record.history_change_reason or ""
first_line = reason.split("\n")[0]

if first_line.startswith("Tag added: "):
tag_name = first_line[len("Tag added: ") :]
record.history_delta_changes = [
{"field": _("Tags"), "old": "", "new": tag_name}
]
elif first_line.startswith("Tag removed: "):
tag_name = first_line[len("Tag removed: ") :]
record.history_delta_changes = [
{"field": _("Tags"), "old": tag_name, "new": ""}
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this should be solved using the existing functionality in https://django-simple-history.readthedocs.io/en/stable/historical_model.html#tracking-many-to-many-relationships, however I'm not familiar with that part of the library to comment more.

Leaving this open for now for further exploration & review.

def get_readonly_fields(self, request, obj=None):
# make all fields readonly
readonly_fields = list(
Expand Down
12 changes: 12 additions & 0 deletions tcms/rpc/tests/test_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,12 @@ def test_add_tag(self):
).exists()
self.assertTrue(tag_exists)

history_entry = self.testcase.history.latest("history_id")
self.assertIn(self.tag1.name, history_entry.history_change_reason)
self.assertIn(
f"Tag added: {self.tag1.name}", history_entry.history_change_reason
)

def test_add_tag_without_permissions(self):
unauthorized_user = UserFactory()
unauthorized_user.set_password("api-testing")
Expand Down Expand Up @@ -760,6 +766,12 @@ def test_remove_tag(self):
).exists()
self.assertFalse(tag_exists)

history_entry = self.testcase.history.latest("history_id")
self.assertIn(self.tag0.name, history_entry.history_change_reason)
self.assertIn(
f"Tag removed: {self.tag0.name}", history_entry.history_change_reason
)

def test_remove_tag_without_permissions(self):
unauthorized_user = UserFactory()
unauthorized_user.set_password("api-testing")
Expand Down
12 changes: 12 additions & 0 deletions tcms/rpc/tests/test_testplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def test_add_tag(self):
).exists()
self.assertTrue(tag_exists)

history_entry = self.plans[0].history.latest("history_id")
self.assertIn(self.tag1.name, history_entry.history_change_reason)
self.assertIn(
f"Tag added: {self.tag1.name}", history_entry.history_change_reason
)

def test_add_tag_without_permissions(self):
unauthorized_user = UserFactory()
unauthorized_user.set_password("api-testing")
Expand Down Expand Up @@ -142,6 +148,12 @@ def test_remove_tag(self):
).exists()
self.assertFalse(tag_exists)

history_entry = self.plans[0].history.latest("history_id")
self.assertIn(self.tag0.name, history_entry.history_change_reason)
self.assertIn(
f"Tag removed: {self.tag0.name}", history_entry.history_change_reason
)

def test_remove_tag_without_permissions(self):
unauthorized_user = UserFactory()
unauthorized_user.set_password("api-testing")
Expand Down
13 changes: 12 additions & 1 deletion tcms/testcases/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ def add_component(self, component):
return TestCaseComponent.objects.get_or_create(case=self, component=component)

def add_tag(self, tag):
return TestCaseTag.objects.get_or_create(case=self, tag=tag)
result = TestCaseTag.objects.get_or_create(case=self, tag=tag)
self._change_reason = ( # pylint: disable=protected-access
f"Tag added: {tag.name}\n\n"
f"--- tag\n+++ tag\n@@ -1,1 +1,1 @@\n-\n+{tag.name}"
)
self.save()
return result

def get_text_with_version(self, case_text_version=None):
if case_text_version:
Expand All @@ -138,6 +144,11 @@ def remove_component(self, component):

def remove_tag(self, tag):
self.tag.through.objects.filter(case=self.pk, tag=tag.pk).delete()
self._change_reason = ( # pylint: disable=protected-access
f"Tag removed: {tag.name}\n\n"
f"--- tag\n+++ tag\n@@ -1,1 +1,1 @@\n-{tag.name}\n+"
)
self.save()

def _get_absolute_url(self, request=None):
return reverse(
Expand Down
13 changes: 12 additions & 1 deletion tcms/testplans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,21 @@ def add_case(self, case, sortkey=None):
)[0]

def add_tag(self, tag):
return TestPlanTag.objects.get_or_create(plan=self, tag=tag)
result = TestPlanTag.objects.get_or_create(plan=self, tag=tag)
self._change_reason = ( # pylint: disable=protected-access
f"Tag added: {tag.name}\n\n"
f"--- tag\n+++ tag\n@@ -1,1 +1,1 @@\n-\n+{tag.name}"
)
self.save()
return result

def remove_tag(self, tag):
TestPlanTag.objects.filter(plan=self, tag=tag).delete()
self._change_reason = ( # pylint: disable=protected-access
f"Tag removed: {tag.name}\n\n"
f"--- tag\n+++ tag\n@@ -1,1 +1,1 @@\n-{tag.name}\n+"
)
self.save()

def delete_case(self, case):
TestCasePlan.objects.filter(case=case.pk, plan=self.pk).delete()
Expand Down