You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
import pickle
|
|
from base64 import b64encode, b64decode
|
|
from django.contrib.auth.models import AbstractUser
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
|
|
class User(AbstractUser):
|
|
pass
|
|
public_key = models.TextField()
|
|
private_key = models.TextField()
|
|
|
|
def save(self, *args, **kwargs):
|
|
if hasattr(self, '_phe_public_key') and hasattr(self, '_phe_private_key'):
|
|
self.phe_public_key = b64encode(pickle.dumps(self._phe_public_key)).decode('utf-8')
|
|
self.phe_private_key = b64encode(pickle.dumps(self._phe_private_key)).decode('utf-8')
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
@property
|
|
def deserialized_public_key(self):
|
|
return pickle.loads(b64decode(self.phe_public_key))
|
|
|
|
@property
|
|
def deserialized_private_key(self):
|
|
return pickle.loads(b64decode(self.phe_private_key))
|
|
|
|
class Meta:
|
|
permissions = [
|
|
("can_create_users", "Can create new users"),
|
|
]
|
|
|
|
|
|
class AttributeType(models.Model):
|
|
DATATYPE_CHOICES = [
|
|
('string', 'String'),
|
|
('boolean', 'Boolean'),
|
|
('integer', 'Integer'),
|
|
]
|
|
|
|
is_private = models.BooleanField(default=False)
|
|
datatype = models.CharField(max_length=15)
|
|
significant_digits = models.PositiveIntegerField(null=True, blank=True)
|
|
name = models.CharField(max_length=40)
|
|
|
|
def save(self, *args, **kwargs):
|
|
if self.datatype.startswith('float'):
|
|
if self.significant_digits is None:
|
|
raise ValueError('significant_digits must be set for float datatype')
|
|
self.datatype = f'float_{self.significant_digits}'
|
|
elif self.significant_digits is not None:
|
|
raise ValueError('significant_digits must be None for non-float datatype')
|
|
super().save(*args, **kwargs)
|
|
|
|
def __str__(self) -> str:
|
|
return self.name
|
|
|
|
|
|
class Attribute(models.Model):
|
|
|
|
attribute_type = models.ForeignKey(AttributeType, on_delete=models.CASCADE)
|
|
value = models.IntegerField()
|
|
|
|
|
|
class UserAttribute(Attribute):
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
last_modified = models.DateTimeField(auto_now=True)
|
|
|
|
class File(models.Model):
|
|
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
name = models.CharField(max_length=255)
|
|
file = models.FileField(upload_to='uploads/')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
last_modified = models.DateTimeField(auto_now=True)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
class Meta:
|
|
ordering = ['name', 'created_at']
|
|
|
|
class Rule(models.Model):
|
|
name = models.CharField(max_length=255)
|
|
file = models.ForeignKey(File, on_delete=models.CASCADE)
|
|
read = models.BooleanField(default=False)
|
|
write = models.BooleanField(default=False)
|
|
delete = models.BooleanField(default=False)
|
|
|
|
class RuleAttribute(Attribute):
|
|
OPTION_EQUALS = 'EQ'
|
|
OPTION_DOES_NOT_EQUAL = 'NEQ'
|
|
OPTION_GREATER_THAN = 'GT'
|
|
OPTION_LESS_THAN = 'LT'
|
|
|
|
OPTION_CHOICES = [
|
|
('EQ', 'Equals'),
|
|
('NEQ', "Doesn't Equal"),
|
|
('GT', 'Greater Than'),
|
|
('LT', 'Less Than'),
|
|
]
|
|
|
|
rule = models.ForeignKey(Rule, on_delete=models.CASCADE)
|
|
operator = models.CharField(max_length=3, choices=OPTION_CHOICES, default=OPTION_EQUALS) |