diff --git a/core/tickets/api_v2.py b/core/tickets/api_v2.py index a4beaaa..8cadcb1 100644 --- a/core/tickets/api_v2.py +++ b/core/tickets/api_v2.py @@ -22,8 +22,8 @@ class IssueSerializer(serializers.ModelSerializer): class Meta: model = IssueThread - fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity') - read_only_fields = ('id', 'timeline', 'last_activity') + fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid') + read_only_fields = ('id', 'timeline', 'last_activity', 'uuid') def to_internal_value(self, data): ret = super().to_internal_value(data) diff --git a/core/tickets/migrations/0006_issuethread_uuid.py b/core/tickets/migrations/0006_issuethread_uuid.py new file mode 100644 index 0000000..146809a --- /dev/null +++ b/core/tickets/migrations/0006_issuethread_uuid.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.7 on 2024-01-12 21:28 + +from django.db import migrations, models + +from tickets.models import IssueThread + + +class Migration(migrations.Migration): + + dependencies = [ + ('tickets', '0005_remove_issuethread_last_activity'), + ] + + def set_uuid(apps, schema_editor): + import uuid + for issue_thread in IssueThread.objects.all(): + issue_thread.uuid = str(uuid.uuid4()) + issue_thread.save() + + operations = [ + migrations.AddField( + model_name='issuethread', + name='uuid', + field=models.CharField(max_length=255, null=True), + ), + migrations.RunPython(set_uuid), + migrations.AlterField( + model_name='issuethread', + name='uuid', + field=models.CharField(max_length=255, unique=True, null=False, blank=False), + ), + ] diff --git a/core/tickets/models.py b/core/tickets/models.py index a721385..8616c92 100644 --- a/core/tickets/models.py +++ b/core/tickets/models.py @@ -2,7 +2,7 @@ from django.db import models from django_softdelete.models import SoftDeleteModel from inventory.models import Event -from django.db.models.signals import post_save +from django.db.models.signals import post_save, pre_save from django.dispatch import receiver STATE_CHOICES = ( @@ -25,10 +25,14 @@ STATE_CHOICES = ( class IssueThread(SoftDeleteModel): id = models.AutoField(primary_key=True) + uuid = models.CharField(max_length=255, unique=True, null=False, blank=False) name = models.CharField(max_length=255) assigned_to = models.CharField(max_length=255, null=True) manually_created = models.BooleanField(default=False) + def short_uuid(self): + return self.uuid[:8] + @property def state(self): try: @@ -49,6 +53,13 @@ class IssueThread(SoftDeleteModel): ] +@receiver(pre_save, sender=IssueThread) +def set_uuid(sender, instance, **kwargs): + import uuid + if instance.uuid is None or instance.uuid == '': + instance.uuid = str(uuid.uuid4()) + + @receiver(post_save, sender=IssueThread) def create_issue_thread(sender, instance, created, **kwargs): if created: diff --git a/core/tickets/tests/v2/test_tickets.py b/core/tickets/tests/v2/test_tickets.py index 54a2e4a..09cc298 100644 --- a/core/tickets/tests/v2/test_tickets.py +++ b/core/tickets/tests/v2/test_tickets.py @@ -51,7 +51,10 @@ class IssueApiTest(TestCase): comment="test", timestamp=now + timedelta(seconds=3), ) - + self.assertEqual('pending_new', issue.state) + self.assertEqual('test issue', issue.name) + self.assertEqual(None, issue.assigned_to) + self.assertEqual(36, len(issue.uuid)) response = self.client.get('/api/2/tickets/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) @@ -59,6 +62,7 @@ class IssueApiTest(TestCase): self.assertEqual(response.json()[0]['name'], "test issue") self.assertEqual(response.json()[0]['state'], "pending_new") self.assertEqual(response.json()[0]['assigned_to'], None) + self.assertEqual(response.json()[0]['uuid'], issue.uuid) self.assertEqual(response.json()[0]['last_activity'], comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) self.assertEqual(len(response.json()[0]['timeline']), 4) self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')