use uuid in tickets

This commit is contained in:
j3d1 2024-01-12 22:59:57 +01:00
parent 4664d6255d
commit f7002c5548
4 changed files with 51 additions and 4 deletions

View file

@ -22,8 +22,8 @@ class IssueSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = IssueThread model = IssueThread
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity') fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid')
read_only_fields = ('id', 'timeline', 'last_activity') read_only_fields = ('id', 'timeline', 'last_activity', 'uuid')
def to_internal_value(self, data): def to_internal_value(self, data):
ret = super().to_internal_value(data) ret = super().to_internal_value(data)

View file

@ -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),
),
]

View file

@ -2,7 +2,7 @@ from django.db import models
from django_softdelete.models import SoftDeleteModel from django_softdelete.models import SoftDeleteModel
from inventory.models import Event 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 from django.dispatch import receiver
STATE_CHOICES = ( STATE_CHOICES = (
@ -25,10 +25,14 @@ STATE_CHOICES = (
class IssueThread(SoftDeleteModel): class IssueThread(SoftDeleteModel):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
uuid = models.CharField(max_length=255, unique=True, null=False, blank=False)
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
assigned_to = models.CharField(max_length=255, null=True) assigned_to = models.CharField(max_length=255, null=True)
manually_created = models.BooleanField(default=False) manually_created = models.BooleanField(default=False)
def short_uuid(self):
return self.uuid[:8]
@property @property
def state(self): def state(self):
try: 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) @receiver(post_save, sender=IssueThread)
def create_issue_thread(sender, instance, created, **kwargs): def create_issue_thread(sender, instance, created, **kwargs):
if created: if created:

View file

@ -51,7 +51,10 @@ class IssueApiTest(TestCase):
comment="test", comment="test",
timestamp=now + timedelta(seconds=3), 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/') response = self.client.get('/api/2/tickets/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 1) 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]['name'], "test issue")
self.assertEqual(response.json()[0]['state'], "pending_new") self.assertEqual(response.json()[0]['state'], "pending_new")
self.assertEqual(response.json()[0]['assigned_to'], None) 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(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(len(response.json()[0]['timeline']), 4)
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state') self.assertEqual(response.json()[0]['timeline'][0]['type'], 'state')