Add api_token field to log entries

This commit is contained in:
Raphael Michel
2017-10-11 00:07:47 +02:00
parent f342e46f53
commit 07473f854e
3 changed files with 37 additions and 2 deletions

View File

@@ -36,7 +36,7 @@ def cached_file_delete(sender, instance, **kwargs):
class LoggingMixin:
def log_action(self, action, data=None, user=None):
def log_action(self, action, data=None, user=None, api_token=None):
"""
Create a LogEntry object that is related to this object.
See the LogEntry documentation for details.
@@ -53,7 +53,9 @@ class LoggingMixin:
event = self
elif hasattr(self, 'event'):
event = self.event
l = LogEntry(content_object=self, user=user, action_type=action, event=event)
if not user.is_authenticated:
user = None
l = LogEntry(content_object=self, user=user, action_type=action, event=event, api_token=api_token)
if data:
l.data = json.dumps(data, cls=CustomJSONEncoder)
l.save()

View File

@@ -35,6 +35,7 @@ class LogEntry(models.Model):
content_object = GenericForeignKey('content_type', 'object_id')
datetime = models.DateTimeField(auto_now_add=True, db_index=True)
user = models.ForeignKey('User', null=True, blank=True, on_delete=models.PROTECT)
api_token = models.ForeignKey('TeamAPIToken', null=True, blank=True, on_delete=models.PROTECT)
event = models.ForeignKey('Event', null=True, blank=True, on_delete=models.CASCADE)
action_type = models.CharField(max_length=255)
data = models.TextField(default='{}')