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.
79 lines
2.9 KiB
79 lines
2.9 KiB
from django.db import models
|
|
from django.utils import timezone
|
|
from django.apps import apps
|
|
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 save(self, *args, **kwargs):
|
|
# is_new = self._state.adding
|
|
|
|
# print('>>> save DA')
|
|
# if not is_new:
|
|
# # Store old shared_with users before save
|
|
# old_instance = DataAccess.objects.get(pk=self.pk)
|
|
# self._old_shared_with = set(old_instance.shared_with.all())
|
|
|
|
# super().save(*args, **kwargs)
|
|
|
|
# if is_new:
|
|
# # For new instances, create logs for all shared users
|
|
# self.create_access_logs('GRANT_ACCESS')
|
|
# else:
|
|
# # For updates, handle differences
|
|
# new_shared_with = set(self.shared_with.all())
|
|
|
|
# # Users that were added
|
|
# added_users = new_shared_with - self._old_shared_with
|
|
# for user in added_users:
|
|
# self.create_access_log(user, 'GRANT_ACCESS')
|
|
|
|
# # Users that were removed
|
|
# removed_users = self._old_shared_with - new_shared_with
|
|
# for user in removed_users:
|
|
# self.create_access_log(user, 'REVOKE_ACCESS')
|
|
|
|
# def delete(self, *args, **kwargs):
|
|
# # Store users before deletion
|
|
# users_to_revoke = list(self.shared_with.all())
|
|
|
|
# # First delete the instance
|
|
# super().delete(*args, **kwargs)
|
|
|
|
# # Then create revoke logs for all users
|
|
# for user in users_to_revoke:
|
|
# self.create_access_log(user, 'REVOKE_ACCESS')
|
|
|
|
# def create_access_logs(self, operation):
|
|
# """Create logs for all shared users"""
|
|
# users = self.shared_with.all()
|
|
# print(f'>>> create logs for users = {len(users)}')
|
|
|
|
# for user in self.shared_with.all():
|
|
# self.create_access_log(user, operation)
|
|
|
|
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)
|
|
obj = model_class.objects.get(id=self.model_id)
|
|
store_id = None
|
|
if isinstance(obj, SideStoreModel):
|
|
store_id = obj.store_id
|
|
|
|
ModelLog.objects.create(
|
|
user=user,
|
|
model_id=self.model_id,
|
|
model_name=self.model_name,
|
|
operation=operation,
|
|
date=timezone.now(),
|
|
store_id=store_id
|
|
)
|
|
|