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.
41 lines
1.5 KiB
41 lines
1.5 KiB
from django.db import models
|
|
from django.utils import timezone
|
|
from django.apps import apps
|
|
import uuid
|
|
|
|
from . import ModelLog, SideStoreModel
|
|
|
|
class DataAccess(models.Model):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
|
owner = models.ForeignKey('CustomUser', related_name='owned_data', on_delete=models.CASCADE)
|
|
shared_with = models.ForeignKey('CustomUser', related_name='shared_data', on_delete=models.CASCADE)
|
|
model_name = models.CharField(max_length=50)
|
|
model_id = models.UUIDField()
|
|
granted_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
def save(self, *args, **kwargs):
|
|
is_new = self._state.adding # Check if this is a new DataAccess
|
|
super().save(*args, **kwargs)
|
|
|
|
if is_new:
|
|
self.create_initial_sync_logs()
|
|
|
|
def create_initial_sync_logs(self):
|
|
|
|
model_class = apps.get_model(self.model_name)
|
|
obj = model_class.objects.get(id=self.model_id)
|
|
parent_model, parent_id = obj.get_parent_reference()
|
|
store_id = None
|
|
if isinstance(obj, SideStoreModel):
|
|
store_id = obj.store_id
|
|
|
|
ModelLog.objects.create(
|
|
user=self.shared_with, # The user receiving access
|
|
model_id=self.model_id,
|
|
model_name=self.model_name,
|
|
operation='SHARE', # New operation type
|
|
date=timezone.now(),
|
|
store_id=store_id,
|
|
parent_model_id=parent_id,
|
|
parent_model_name=parent_model
|
|
)
|
|
|