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