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.
 
 
 
 
padelclub_backend/tournaments/models/data_access.py

43 lines
1.7 KiB

from django.db import models
from django.utils import timezone
from django.apps import apps
from django.core.exceptions import ObjectDoesNotExist
import uuid
from . import ModelLog, SideStoreModel, BaseModel
class DataAccess(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
owner = models.ForeignKey('CustomUser', related_name='owned_data', on_delete=models.CASCADE)
shared_with = models.ManyToManyField('CustomUser', 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_access_log(self, user, operation):
"""Create a single access log for a specific user"""
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(user=user, 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:
ModelLog.objects.create(
user=user,
model_id=self.model_id,
model_name=self.model_name,
operation=operation,
date=timezone.now(),
store_id=store_id
)
except ObjectDoesNotExist:
pass