|
126
|
1 |
import datetime |
|
|
2 |
|
|
|
3 |
from auditlog.models import LogEntry |
|
|
4 |
from django.utils import timezone |
|
|
5 |
from notes.models import Note, Session |
|
|
6 |
from rest_framework import permissions |
|
|
7 |
from rest_framework.response import Response |
|
|
8 |
from rest_framework.views import APIView |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
class ListLogsView(APIView): |
|
|
12 |
""" |
|
|
13 |
View to list tle log of changes on Note and Sessions. |
|
|
14 |
|
|
|
15 |
* Only registered users are able to access this view. |
|
|
16 |
* the results are filtered by connected user |
|
|
17 |
""" |
|
|
18 |
permission_classes = (permissions.IsAuthenticated,) |
|
|
19 |
|
|
|
20 |
def __filter_object(self, queryset, modified_since): |
|
|
21 |
log_entries = LogEntry.objects.get_for_objects(queryset) |
|
|
22 |
if modified_since: |
|
|
23 |
log_entries = log_entries.filter(timestamp__gte=modified_since) |
|
|
24 |
return log_entries |
|
|
25 |
|
|
|
26 |
def __process_log_entries(self, queryset, modified_since): |
|
|
27 |
''' |
|
|
28 |
Process log entries |
|
|
29 |
''' |
|
|
30 |
log_entries = self.__filter_object(queryset, modified_since) |
|
|
31 |
|
|
|
32 |
res = {} |
|
|
33 |
for log_entry in log_entries: |
|
|
34 |
ext_id = log_entry.additional_data.get('ext_id') |
|
|
35 |
if not ext_id: |
|
|
36 |
continue |
|
|
37 |
sync_entry = res.get(ext_id, {}) |
|
|
38 |
new_action = { |
|
|
39 |
(None , LogEntry.Action.CREATE): LogEntry.Action.CREATE, |
|
|
40 |
(LogEntry.Action.CREATE, LogEntry.Action.CREATE): LogEntry.Action.CREATE, |
|
|
41 |
(LogEntry.Action.UPDATE, LogEntry.Action.CREATE): LogEntry.Action.UPDATE, |
|
|
42 |
(LogEntry.Action.DELETE, LogEntry.Action.CREATE): LogEntry.Action.UPDATE, |
|
|
43 |
|
|
|
44 |
(None , LogEntry.Action.UPDATE): LogEntry.Action.UPDATE, |
|
|
45 |
(LogEntry.Action.CREATE, LogEntry.Action.UPDATE): LogEntry.Action.CREATE, |
|
|
46 |
(LogEntry.Action.UPDATE, LogEntry.Action.UPDATE): LogEntry.Action.UPDATE, |
|
|
47 |
(LogEntry.Action.DELETE, LogEntry.Action.UPDATE): LogEntry.Action.DELETE, |
|
|
48 |
|
|
|
49 |
(None , LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
|
50 |
(LogEntry.Action.CREATE, LogEntry.Action.DELETE): None, |
|
|
51 |
(LogEntry.Action.UPDATE, LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
|
52 |
(LogEntry.Action.DELETE, LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
|
53 |
|
|
|
54 |
} [(sync_entry.get('action'), log_entry.action)] |
|
|
55 |
if new_action is None: |
|
|
56 |
del res[ext_id] |
|
|
57 |
else: |
|
|
58 |
res[ext_id] = { |
|
|
59 |
'type': log_entry.content_type.model, |
|
|
60 |
'ext_id': ext_id, |
|
|
61 |
'action': new_action, |
|
|
62 |
'timestamp': log_entry.timestamp |
|
|
63 |
} |
|
|
64 |
return res |
|
|
65 |
|
|
|
66 |
def get(self, request, format=None): |
|
|
67 |
""" |
|
|
68 |
Return a list of all users. |
|
|
69 |
""" |
|
|
70 |
modified_since_str = request.query_params.get('modified_since', None) |
|
|
71 |
modified_since = None |
|
|
72 |
if modified_since_str is not None: |
|
|
73 |
modified_since = datetime.datetime.fromtimestamp( |
|
|
74 |
float(modified_since_str), |
|
|
75 |
timezone.utc |
|
|
76 |
) |
|
|
77 |
|
|
|
78 |
user = request.user |
|
|
79 |
res_sessions = self.__process_log_entries(Session.objects.filter(owner=user), modified_since) |
|
|
80 |
res_notes = self.__process_log_entries(Note.objects.filter(session__owner=user), modified_since) |
|
|
81 |
|
|
|
82 |
return Response({ |
|
|
83 |
'sessions': res_sessions.values(), |
|
|
84 |
'notes': res_notes.values() |
|
|
85 |
}) |