You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.9 KiB
48 lines
1.9 KiB
from django.db import models
|
|
from django.utils import timezone
|
|
from django.apps import apps
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from django.conf import settings
|
|
|
|
|
|
import uuid
|
|
|
|
from . import ModelLog, SideStoreModel, BaseModel
|
|
|
|
class DataAccess(BaseModel):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
|
owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='owned_data', on_delete=models.CASCADE)
|
|
shared_with = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='shared_data')
|
|
model_name = models.CharField(max_length=50)
|
|
model_id = models.UUIDField()
|
|
granted_at = models.DateTimeField(auto_now_add=True)
|
|
# last_hierarchy_update = models.DateTimeField(default=timezone.now)
|
|
|
|
def create_revoke_access_log(self):
|
|
self.create_access_log(self.shared_with.all(), 'REVOKE_ACCESS')
|
|
|
|
def create_access_log(self, users, operation):
|
|
"""Create an access log for a list of users """
|
|
model_class = apps.get_model('tournaments', self.model_name)
|
|
try:
|
|
obj = model_class.objects.get(id=self.model_id)
|
|
store_id = None
|
|
if isinstance(obj, SideStoreModel):
|
|
store_id = obj.store_id
|
|
|
|
existing_log = ModelLog.objects.filter(users__in=users, model_id=self.model_id, operation=operation).first()
|
|
if existing_log:
|
|
existing_log.date = timezone.now()
|
|
existing_log.model_operation = operation
|
|
existing_log.save()
|
|
else:
|
|
model_log = ModelLog.objects.create(
|
|
model_id=self.model_id,
|
|
model_name=self.model_name,
|
|
operation=operation,
|
|
date=timezone.now(),
|
|
store_id=store_id
|
|
)
|
|
model_log.users.set(users)
|
|
except ObjectDoesNotExist:
|
|
pass
|
|
|