From 9a93e2d6adc0e7aef11304d79f2192e643c1c13f Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 12 Jun 2025 15:58:59 +0200 Subject: [PATCH] adds store_id for DataAccess --- sync/migrations/0007_dataaccess_store_id.py | 18 +++++++++ sync/models/data_access.py | 43 ++++++++------------- 2 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 sync/migrations/0007_dataaccess_store_id.py diff --git a/sync/migrations/0007_dataaccess_store_id.py b/sync/migrations/0007_dataaccess_store_id.py new file mode 100644 index 0000000..8113e21 --- /dev/null +++ b/sync/migrations/0007_dataaccess_store_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1 on 2025-06-12 13:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sync', '0006_alter_modellog_operation'), + ] + + operations = [ + migrations.AddField( + model_name='dataaccess', + name='store_id', + field=models.CharField(default='', max_length=100), + ), + ] diff --git a/sync/models/data_access.py b/sync/models/data_access.py index 5a38ae3..5f54b11 100644 --- a/sync/models/data_access.py +++ b/sync/models/data_access.py @@ -17,6 +17,7 @@ class DataAccess(BaseModel): shared_with = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='shared_data') model_name = models.CharField(max_length=50) model_id = models.UUIDField() + store_id = models.CharField(max_length=100, default="") # a value matching LeStorage directory sub-stores. Matches the name of the directory. granted_at = models.DateTimeField(auto_now_add=True) def delete_dependencies(self): @@ -35,33 +36,23 @@ class DataAccess(BaseModel): """Create an access log for a list of users """ model_class = model_registry.get_model(self.model_name) if model_class: - try: - obj = model_class.objects.get(id=self.model_id) - store_id = None - if isinstance(obj, SideStoreModel): - store_id = obj.store_id - - for user in users: + for user in users: + logger.info(f'=== create ModelLog for: {operation} > {users}') - logger.info(f'=== create ModelLog for: {operation} > {users}') - - 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: - logger.warn(f'!!! object does not exists any more: {self.model_name} : {self.model_id} : {operation}') - pass + 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=self.store_id + ) else: logger.warn(f'!!!model not found: {self.model_name}')