From a95fffbdf5f7bf3420cc1b53c1fd3452a6cad546 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 15 Nov 2024 11:03:49 +0100 Subject: [PATCH] feat: Introduces Char32UUIDField for model UUIDs Adds a custom UUIDField to use 32-character format for IDs to optimize database storage and integration with existing systems. Refactors model definitions to replace default UUIDField with the new Char32UUIDField to maintain consistent implementation across the codebase. --- src/pix360core/fields.py | 11 +++++++++++ src/pix360core/models/content.py | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/pix360core/fields.py diff --git a/src/pix360core/fields.py b/src/pix360core/fields.py new file mode 100644 index 0000000..2cdc84c --- /dev/null +++ b/src/pix360core/fields.py @@ -0,0 +1,11 @@ +from django.db import models + +class Char32UUIDField(models.UUIDField): + def db_type(self, connection): + return "char(32)" + + def get_db_prep_value(self, value, connection, prepared=False): + value = super().get_db_prep_value(value, connection, prepared) + if value is not None: + value = value.hex + return value diff --git a/src/pix360core/models/content.py b/src/pix360core/models/content.py index dcd9314..ff105f7 100644 --- a/src/pix360core/models/content.py +++ b/src/pix360core/models/content.py @@ -1,6 +1,8 @@ from django.db import models from django.contrib.auth import get_user_model +from ..fields import Char32UUIDField as UUIDField + import mimetypes import uuid @@ -27,7 +29,7 @@ class File(models.Model): is_result (BooleanField): Whether this file is the result of a conversion """ - id = models.UUIDField(primary_key=True, default=uuid.uuid4) + id = UUIDField(primary_key=True, default=uuid.uuid4) file = models.FileField(upload_to=file_upload_path) mime_type = models.CharField(max_length=256, default="application/octet-stream") conversion = models.ForeignKey(to='Conversion', on_delete=models.SET_NULL, null=True, blank=True) @@ -65,7 +67,7 @@ class Conversion(models.Model): log (TextField): Log of the conversion """ - id = models.UUIDField(primary_key=True, default=uuid.uuid4) + id = UUIDField(primary_key=True, default=uuid.uuid4) title = models.CharField(max_length=256, null=True, blank=True) url = models.URLField() downloader = models.CharField(max_length=256, null=True, blank=True)