from django.db import models from django.conf import settings class ModelOperation(models.TextChoices): POST = 'POST', 'POST' PUT = 'PUT', 'PUT' DELETE = 'DELETE', 'DELETE' GRANT_ACCESS = 'GRANT_ACCESS', 'GRANT_ACCESS' REVOKE_ACCESS = 'REVOKE_ACCESS', 'REVOKE_ACCESS' SHARED_RELATIONSHIP_SET = 'SHARED_RELATIONSHIP_SET', 'SHARED_RELATIONSHIP_SET' SHARED_RELATIONSHIP_REMOVED = 'SHARED_RELATIONSHIP_REMOVED', 'SHARED_RELATIONSHIP_REMOVED' class ModelLog(models.Model): # id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) # id = models.BigAutoField(primary_key=True) # users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='model_logs', blank=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='model_logs') model_id = models.UUIDField() operation = models.CharField(choices=ModelOperation.choices, max_length=50) date = models.DateTimeField(auto_now_add=True) model_name = models.CharField(max_length=50) store_id = models.CharField(max_length=200, blank=True, null=True) device_id = models.CharField(max_length=200, blank=True, null=True) count = models.IntegerField(default=0) class Meta: ordering = ['date'] indexes = [ models.Index(fields=['date', 'user']), models.Index(fields=['date', 'user', 'device_id']), ] def formatted_time(self): return self.date.strftime('%H:%M:%S.%f') def save(self, *args, **kwargs): # Round microseconds to milliseconds (3 decimals to match Swift precision) if self.date: microseconds = round(self.date.microsecond, -3) # Round to nearest thousand self.date = self.date.replace(microsecond=microseconds) super().save(*args, **kwargs) def data_identifier_dict(self): return { 'model_id': self.model_id, 'store_id': self.store_id } def retrieved(self): self.count += 1