diff --git a/device/models.py b/device/models.py index 717af74..59ea09a 100644 --- a/device/models.py +++ b/device/models.py @@ -1,7 +1,7 @@ from django.db import models, connection from utils.constants import ALGOS -from evidence.models import SystemProperty, UserProperty, Property, Evidence +from evidence.models import SystemProperty, UserProperty, Evidence from lot.models import DeviceLot @@ -49,7 +49,6 @@ class Device: return self.properties self.properties = SystemProperty.objects.filter( - type=Property.Type.SYSTEM, value=self.id ).order_by("-created") @@ -66,6 +65,7 @@ class Device: user_properties = UserProperty.objects.filter( uuid__in=self.uuids, owner=self.owner, + type=UserProperty.Type.USER, ) return user_properties @@ -73,10 +73,10 @@ class Device: if not self.uuids: self.get_uuids() - properties = SystemProperty.objects.filter( + properties = UserProperty.objects.filter( uuid__in=self.uuids, owner=self.owner, - type=Property.Type.DOCUMENT + type=UserProperty.Type.DOCUMENT ) return properties @@ -91,7 +91,6 @@ class Device: algos = list(ALGOS.keys()) algos.append('CUSTOM_ID') self.hids = list(set(properties.filter( - type=Property.Type.SYSTEM, key__in=algos, ).values_list("value", flat=True))) @@ -115,10 +114,10 @@ class Device: if not self.uuids: return False - property = SystemProperty.objects.filter( + property = UserProperty.objects.filter( uuid__in=self.uuids, owner=self.owner, - type=Property.Type.ERASE_SERVER + type=UserProperty.Type.ERASE_SERVER ).first() if property: @@ -154,7 +153,6 @@ class Device: LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} - AND t1.type = {type} ) SELECT DISTINCT value @@ -164,7 +162,6 @@ class Device: row_num = 1 """.format( institution=institution.id, - type=Property.Type.SYSTEM, ) if limit: sql += " limit {} offset {}".format(int(limit), int(offset)) @@ -202,7 +199,6 @@ class Device: LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} - And t1.type = '{type}' ) SELECT COUNT(DISTINCT value) @@ -212,7 +208,6 @@ class Device: row_num = 1 """.format( institution=institution.id, - type=Property.Type.SYSTEM, ) with connection.cursor() as cursor: cursor.execute(sql) @@ -239,7 +234,6 @@ class Device: LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id WHERE t2.device_id IS NULL AND t1.owner_id = {institution} - AND t1.type = '{type}' AND t1.uuid = '{uuid}' ) SELECT DISTINCT @@ -251,7 +245,6 @@ class Device: """.format( uuid=uuid.replace("-", ""), institution=institution.id, - type=Property.Type.SYSTEM, ) properties = [] diff --git a/evidence/migrations/0003_systemproperty_userproperty_delete_annotation_and_more.py b/evidence/migrations/0003_systemproperty_userproperty_delete_annotation_and_more.py new file mode 100644 index 0000000..29e5e78 --- /dev/null +++ b/evidence/migrations/0003_systemproperty_userproperty_delete_annotation_and_more.py @@ -0,0 +1,107 @@ +# Generated by Django 5.0.6 on 2024-12-10 19:37 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("evidence", "0002_alter_annotation_type"), + ("user", "0001_initial"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="SystemProperty", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created", models.DateTimeField(auto_now_add=True)), + ("key", models.CharField(max_length=256)), + ("value", models.CharField(max_length=256)), + ("uuid", models.UUIDField()), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="user.institution", + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.CreateModel( + name="UserProperty", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created", models.DateTimeField(auto_now_add=True)), + ("key", models.CharField(max_length=256)), + ("value", models.CharField(max_length=256)), + ("uuid", models.UUIDField()), + ( + "type", + models.SmallIntegerField( + choices=[(1, "User"), (2, "Document"), (3, "EraseServer")], + default=1, + ), + ), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="user.institution", + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.DeleteModel( + name="Annotation", + ), + migrations.AddConstraint( + model_name="systemproperty", + constraint=models.UniqueConstraint( + fields=("key", "uuid"), name="system_unique_type_key_uuid" + ), + ), + migrations.AddConstraint( + model_name="userproperty", + constraint=models.UniqueConstraint( + fields=("key", "uuid", "type"), name="user_unique_type_key_uuid" + ), + ), + ] diff --git a/evidence/models.py b/evidence/models.py index 4abf52d..3457d2b 100644 --- a/evidence/models.py +++ b/evidence/models.py @@ -13,7 +13,6 @@ from user.models import User, Institution class Property(models.Model): created = models.DateTimeField(auto_now_add=True) - uuid = models.UUIDField() owner = models.ForeignKey(Institution, on_delete=models.CASCADE) user = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, blank=True) @@ -26,6 +25,8 @@ class Property(models.Model): class SystemProperty(Property): + uuid = models.UUIDField() + class Meta: constraints = [ models.UniqueConstraint( @@ -34,18 +35,19 @@ class SystemProperty(Property): class UserProperty(Property): + uuid = models.UUIDField() + class Type(models.IntegerChoices): - SYSTEM = 0, "System" USER = 1, "User" DOCUMENT = 2, "Document" ERASE_SERVER = 3, "EraseServer" - type = models.SmallIntegerField(choices=Type, default=Property.Type.USER) + type = models.SmallIntegerField(choices=Type, default=Type.USER) class Meta: constraints = [ models.UniqueConstraint( - fields=["key", "uuid"], name="user_unique_type_key_uuid") + fields=["key", "uuid", "type"], name="user_unique_type_key_uuid") ] @@ -150,7 +152,6 @@ class Evidence: def get_all(cls, user): return SystemProperty.objects.filter( owner=user.institution, - type=Property.Type.SYSTEM, key="hidalgo1", ).order_by("-created").values_list("uuid", "created").distinct() diff --git a/lot/migrations/0003_lotproperty_delete_lotannotation_and_more.py b/lot/migrations/0003_lotproperty_delete_lotannotation_and_more.py new file mode 100644 index 0000000..c10ce93 --- /dev/null +++ b/lot/migrations/0003_lotproperty_delete_lotannotation_and_more.py @@ -0,0 +1,77 @@ +# Generated by Django 5.0.6 on 2024-12-10 19:37 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("lot", "0002_alter_lot_closed"), + ("user", "0001_initial"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="LotProperty", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created", models.DateTimeField(auto_now_add=True)), + ("key", models.CharField(max_length=256)), + ("value", models.CharField(max_length=256)), + ( + "type", + models.SmallIntegerField( + choices=[ + (0, "System"), + (1, "User"), + (2, "Document"), + (3, "EraseServer"), + ], + default=1, + ), + ), + ( + "lot", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, to="lot.lot" + ), + ), + ( + "owner", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="user.institution", + ), + ), + ( + "user", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.DeleteModel( + name="LotAnnotation", + ), + migrations.AddConstraint( + model_name="lotproperty", + constraint=models.UniqueConstraint( + fields=("key", "lot", "type"), name="lot_unique_type_key_lot" + ), + ), + ] diff --git a/lot/models.py b/lot/models.py index d9ef6ef..59ca155 100644 --- a/lot/models.py +++ b/lot/models.py @@ -6,8 +6,8 @@ from utils.constants import ( STR_EXTEND_SIZE, ) -from user.models import User, Institution -from device.models import Property +from user.models import User, Institution +from evidence.models import Property # from device.models import Device @@ -46,7 +46,19 @@ class Lot(models.Model): d.delete() class LotProperty (Property): - #uuid is not needed for id - uuid = None - #lot foreign key is lot = models.ForeignKey(Lot, on_delete=models.CASCADE) + + class Type(models.IntegerChoices): + SYSTEM = 0, "System" + USER = 1, "User" + DOCUMENT = 2, "Document" + ERASE_SERVER = 3, "EraseServer" + + type = models.SmallIntegerField(choices=Type.choices, default=Type.USER) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["key", "lot", "type"], name="lot_unique_type_key_lot" + ) + ]