| 1 |
|
| 2 |
from __future__ import unicode_literals |
| 3 |
|
| 4 |
from django.db import models |
| 5 |
from django.conf import settings |
| 6 |
from django.utils.duration import duration_string |
| 7 |
|
| 8 |
|
| 9 |
class ImpersonationLog(models.Model): |
| 10 |
''' Stores details of each impersonation session. |
| 11 |
|
| 12 |
This model is used to persist details of impersonations. It hooks |
| 13 |
in to the session_begin and session_end signals to capture the |
| 14 |
details of the user impersonating and the user who is being |
| 15 |
impersonated. It also stores the Django session key. |
| 16 |
|
| 17 |
''' |
| 18 |
impersonator = models.ForeignKey( |
| 19 |
settings.AUTH_USER_MODEL, |
| 20 |
on_delete=models.CASCADE, |
| 21 |
help_text='The user doing the impersonating.', |
| 22 |
related_name='impersonations', |
| 23 |
) |
| 24 |
impersonating = models.ForeignKey( |
| 25 |
settings.AUTH_USER_MODEL, |
| 26 |
on_delete=models.CASCADE, |
| 27 |
related_name='impersonated_by', |
| 28 |
help_text='The user being impersonated.', |
| 29 |
) |
| 30 |
session_key = models.CharField( |
| 31 |
max_length=40, |
| 32 |
help_text='The Django session request key.', |
| 33 |
) |
| 34 |
session_started_at = models.DateTimeField( |
| 35 |
help_text='The time impersonation began.', |
| 36 |
null=True, |
| 37 |
blank=True |
| 38 |
) |
| 39 |
session_ended_at = models.DateTimeField( |
| 40 |
help_text='The time impersonation ended.', |
| 41 |
null=True, |
| 42 |
blank=True |
| 43 |
) |
| 44 |
|
| 45 |
@property |
| 46 |
def duration(self): |
| 47 |
return self._duration() |
| 48 |
|
| 49 |
def _duration(self): |
| 50 |
if all((self.session_started_at, self.session_ended_at)): |
| 51 |
return duration_string( |
| 52 |
self.session_ended_at - self.session_started_at, |
| 53 |
) |
| 54 |
return '' |
| 55 |
_duration.short_description = 'Duration' |