more property model refactoring

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-10 16:52:29 -03:00
parent 643cc44824
commit fe5abd0299
5 changed files with 213 additions and 23 deletions

View file

@ -1,7 +1,7 @@
from django.db import models, connection from django.db import models, connection
from utils.constants import ALGOS 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 from lot.models import DeviceLot
@ -49,7 +49,6 @@ class Device:
return self.properties return self.properties
self.properties = SystemProperty.objects.filter( self.properties = SystemProperty.objects.filter(
type=Property.Type.SYSTEM,
value=self.id value=self.id
).order_by("-created") ).order_by("-created")
@ -66,6 +65,7 @@ class Device:
user_properties = UserProperty.objects.filter( user_properties = UserProperty.objects.filter(
uuid__in=self.uuids, uuid__in=self.uuids,
owner=self.owner, owner=self.owner,
type=UserProperty.Type.USER,
) )
return user_properties return user_properties
@ -73,10 +73,10 @@ class Device:
if not self.uuids: if not self.uuids:
self.get_uuids() self.get_uuids()
properties = SystemProperty.objects.filter( properties = UserProperty.objects.filter(
uuid__in=self.uuids, uuid__in=self.uuids,
owner=self.owner, owner=self.owner,
type=Property.Type.DOCUMENT type=UserProperty.Type.DOCUMENT
) )
return properties return properties
@ -91,7 +91,6 @@ class Device:
algos = list(ALGOS.keys()) algos = list(ALGOS.keys())
algos.append('CUSTOM_ID') algos.append('CUSTOM_ID')
self.hids = list(set(properties.filter( self.hids = list(set(properties.filter(
type=Property.Type.SYSTEM,
key__in=algos, key__in=algos,
).values_list("value", flat=True))) ).values_list("value", flat=True)))
@ -115,10 +114,10 @@ class Device:
if not self.uuids: if not self.uuids:
return False return False
property = SystemProperty.objects.filter( property = UserProperty.objects.filter(
uuid__in=self.uuids, uuid__in=self.uuids,
owner=self.owner, owner=self.owner,
type=Property.Type.ERASE_SERVER type=UserProperty.Type.ERASE_SERVER
).first() ).first()
if property: if property:
@ -154,7 +153,6 @@ class Device:
LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id
WHERE t2.device_id IS NULL WHERE t2.device_id IS NULL
AND t1.owner_id = {institution} AND t1.owner_id = {institution}
AND t1.type = {type}
) )
SELECT DISTINCT SELECT DISTINCT
value value
@ -164,7 +162,6 @@ class Device:
row_num = 1 row_num = 1
""".format( """.format(
institution=institution.id, institution=institution.id,
type=Property.Type.SYSTEM,
) )
if limit: if limit:
sql += " limit {} offset {}".format(int(limit), int(offset)) 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 LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id
WHERE t2.device_id IS NULL WHERE t2.device_id IS NULL
AND t1.owner_id = {institution} AND t1.owner_id = {institution}
And t1.type = '{type}'
) )
SELECT SELECT
COUNT(DISTINCT value) COUNT(DISTINCT value)
@ -212,7 +208,6 @@ class Device:
row_num = 1 row_num = 1
""".format( """.format(
institution=institution.id, institution=institution.id,
type=Property.Type.SYSTEM,
) )
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute(sql) cursor.execute(sql)
@ -239,7 +234,6 @@ class Device:
LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id LEFT JOIN lot_devicelot AS t2 ON t1.value = t2.device_id
WHERE t2.device_id IS NULL WHERE t2.device_id IS NULL
AND t1.owner_id = {institution} AND t1.owner_id = {institution}
AND t1.type = '{type}'
AND t1.uuid = '{uuid}' AND t1.uuid = '{uuid}'
) )
SELECT DISTINCT SELECT DISTINCT
@ -251,7 +245,6 @@ class Device:
""".format( """.format(
uuid=uuid.replace("-", ""), uuid=uuid.replace("-", ""),
institution=institution.id, institution=institution.id,
type=Property.Type.SYSTEM,
) )
properties = [] properties = []

View file

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

View file

@ -13,7 +13,6 @@ from user.models import User, Institution
class Property(models.Model): class Property(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField()
owner = models.ForeignKey(Institution, on_delete=models.CASCADE) owner = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey( user = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, blank=True) User, on_delete=models.SET_NULL, null=True, blank=True)
@ -26,6 +25,8 @@ class Property(models.Model):
class SystemProperty(Property): class SystemProperty(Property):
uuid = models.UUIDField()
class Meta: class Meta:
constraints = [ constraints = [
models.UniqueConstraint( models.UniqueConstraint(
@ -34,18 +35,19 @@ class SystemProperty(Property):
class UserProperty(Property): class UserProperty(Property):
uuid = models.UUIDField()
class Type(models.IntegerChoices): class Type(models.IntegerChoices):
SYSTEM = 0, "System"
USER = 1, "User" USER = 1, "User"
DOCUMENT = 2, "Document" DOCUMENT = 2, "Document"
ERASE_SERVER = 3, "EraseServer" ERASE_SERVER = 3, "EraseServer"
type = models.SmallIntegerField(choices=Type, default=Property.Type.USER) type = models.SmallIntegerField(choices=Type, default=Type.USER)
class Meta: class Meta:
constraints = [ constraints = [
models.UniqueConstraint( 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): def get_all(cls, user):
return SystemProperty.objects.filter( return SystemProperty.objects.filter(
owner=user.institution, owner=user.institution,
type=Property.Type.SYSTEM,
key="hidalgo1", key="hidalgo1",
).order_by("-created").values_list("uuid", "created").distinct() ).order_by("-created").values_list("uuid", "created").distinct()

View file

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

View file

@ -6,8 +6,8 @@ from utils.constants import (
STR_EXTEND_SIZE, STR_EXTEND_SIZE,
) )
from user.models import User, Institution from user.models import User, Institution
from device.models import Property from evidence.models import Property
# from device.models import Device # from device.models import Device
@ -46,7 +46,19 @@ class Lot(models.Model):
d.delete() d.delete()
class LotProperty (Property): class LotProperty (Property):
#uuid is not needed for id
uuid = None
#lot foreign key is
lot = models.ForeignKey(Lot, on_delete=models.CASCADE) 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"
)
]