Backend Draft
This commit is contained in:
45
backend/analytics/models.py
Normal file
45
backend/analytics/models.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from tenants.managers import TenantScopedManager
|
||||
from tenants.models import Tenant
|
||||
|
||||
class AuditLog(models.Model):
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, related_name='audit_logs')
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name='audit_logs')
|
||||
action = models.CharField(max_length=50) # created, updated, deleted
|
||||
model_name = models.CharField(max_length=100)
|
||||
object_id = models.CharField(max_length=255)
|
||||
changes = models.JSONField(default=dict, blank=True)
|
||||
ip_address = models.GenericIPAddressField(null=True, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
objects = TenantScopedManager()
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created_at']
|
||||
indexes = [
|
||||
models.Index(fields=['tenant', 'created_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.action} on {self.model_name}:{self.object_id} by {self.user}"
|
||||
|
||||
class ActivityLog(models.Model):
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, related_name='activity_logs')
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name='activity_logs')
|
||||
action = models.CharField(max_length=100)
|
||||
target_type = models.CharField(max_length=100)
|
||||
target_id = models.CharField(max_length=255)
|
||||
metadata = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
objects = TenantScopedManager()
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created_at']
|
||||
indexes = [
|
||||
models.Index(fields=['tenant', 'created_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user} {self.action} {self.target_type}:{self.target_id}"
|
||||
Reference in New Issue
Block a user