property models refactor

This commit is contained in:
Thomas Nahuel Rusiecki 2024-12-09 19:00:47 -03:00
parent 26b9b2e4d9
commit 643cc44824

View file

@ -10,20 +10,13 @@ from evidence.xapian import search
from evidence.parse_details import ParseSnapshot from evidence.parse_details import ParseSnapshot
from user.models import User, Institution from user.models import User, Institution
#TODO: base class is abstract; revise if should be for query efficiency
class Property(models.Model):
class Type(models.IntegerChoices):
SYSTEM = 0, "System"
USER = 1, "User"
DOCUMENT = 2, "Document"
ERASE_SERVER = 3, "EraseServer"
class Property(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
uuid = models.UUIDField() 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)
type = models.SmallIntegerField(choices=Type)
key = models.CharField(max_length=STR_EXTEND_SIZE) key = models.CharField(max_length=STR_EXTEND_SIZE)
value = models.CharField(max_length=STR_EXTEND_SIZE) value = models.CharField(max_length=STR_EXTEND_SIZE)
@ -31,40 +24,31 @@ class Property(models.Model):
#Only for shared behaviour, it is not a table #Only for shared behaviour, it is not a table
abstract = True abstract = True
class SystemProperty(Property):
class SystemProperty(Property):
class Meta: class Meta:
constraints = [
models.CheckConstraint(
check=~Q(type=1), #Enforce that type is not User
name='property_cannot_be_user'
),
]
#Django orm wont inherit constraints to child
#TODO: check if this is needed
constraints = [ constraints = [
models.UniqueConstraint( models.UniqueConstraint(
fields=["type", "key", "uuid"], name="system_unique_type_key_uuid") fields=["key", "uuid"], name="system_unique_type_key_uuid")
] ]
class UserProperty(Property): class UserProperty(Property):
class Type(models.IntegerChoices):
SYSTEM = 0, "System"
USER = 1, "User"
DOCUMENT = 2, "Document"
ERASE_SERVER = 3, "EraseServer"
type = models.SmallIntegerField(default=Property.Type.USER) type = models.SmallIntegerField(choices=Type, default=Property.Type.USER)
class Meta: class Meta:
constraints = [
models.CheckConstraint(
check=Q(type=1), #Enforce that type is User
name='property_needs_to_be_user'
),
]
constraints = [ constraints = [
models.UniqueConstraint( models.UniqueConstraint(
fields=["type", "key", "uuid"], name="user_unique_type_key_uuid") fields=["key", "uuid"], name="user_unique_type_key_uuid")
] ]
class Evidence: class Evidence:
def __init__(self, uuid): def __init__(self, uuid):
self.uuid = uuid self.uuid = uuid