| author | ymh <ymh.work@gmail.com> |
| Tue, 01 Aug 2017 12:20:14 +0200 | |
| changeset 132 | 906a6c7c7943 |
| parent 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 |
||
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
22 |
def __filter_object(self, model, user, modified_since, client_id): |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
23 |
""" |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
24 |
Log entries are filtered by model, actor and timestamp. |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
25 |
If a client id is given, log entries from this client are ignored. |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
26 |
""" |
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
27 |
log_entries = LogEntry.objects.get_for_model(model).filter(actor=user) |
| 126 | 28 |
if modified_since: |
29 |
log_entries = log_entries.filter(timestamp__gte=modified_since) |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
30 |
if client_id: |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
31 |
log_entries = log_entries.exclude(client=client_id) |
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
126
diff
changeset
|
32 |
return log_entries.order_by('timestamp') |
| 126 | 33 |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
34 |
def __process_log_entries(self, model, user, modified_since, client_id): |
| 126 | 35 |
''' |
36 |
Process log entries |
|
37 |
''' |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
38 |
log_entries = self.__filter_object(model, user, modified_since, client_id) |
| 126 | 39 |
|
40 |
res = {} |
|
41 |
for log_entry in log_entries: |
|
42 |
ext_id = log_entry.additional_data.get('ext_id') |
|
43 |
if not ext_id: |
|
44 |
continue |
|
45 |
sync_entry = res.get(ext_id, {}) |
|
46 |
new_action = { |
|
47 |
(None , LogEntry.Action.CREATE): LogEntry.Action.CREATE, |
|
48 |
(LogEntry.Action.CREATE, LogEntry.Action.CREATE): LogEntry.Action.CREATE, |
|
49 |
(LogEntry.Action.UPDATE, LogEntry.Action.CREATE): LogEntry.Action.UPDATE, |
|
50 |
(LogEntry.Action.DELETE, LogEntry.Action.CREATE): LogEntry.Action.UPDATE, |
|
51 |
||
52 |
(None , LogEntry.Action.UPDATE): LogEntry.Action.UPDATE, |
|
53 |
(LogEntry.Action.CREATE, LogEntry.Action.UPDATE): LogEntry.Action.CREATE, |
|
54 |
(LogEntry.Action.UPDATE, LogEntry.Action.UPDATE): LogEntry.Action.UPDATE, |
|
55 |
(LogEntry.Action.DELETE, LogEntry.Action.UPDATE): LogEntry.Action.DELETE, |
|
56 |
||
57 |
(None , LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
58 |
(LogEntry.Action.CREATE, LogEntry.Action.DELETE): None, |
|
59 |
(LogEntry.Action.UPDATE, LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
60 |
(LogEntry.Action.DELETE, LogEntry.Action.DELETE): LogEntry.Action.DELETE, |
|
61 |
||
62 |
} [(sync_entry.get('action'), log_entry.action)] |
|
63 |
if new_action is None: |
|
64 |
del res[ext_id] |
|
65 |
else: |
|
66 |
res[ext_id] = { |
|
67 |
'type': log_entry.content_type.model, |
|
68 |
'ext_id': ext_id, |
|
69 |
'action': new_action, |
|
70 |
'timestamp': log_entry.timestamp |
|
71 |
} |
|
72 |
return res |
|
73 |
||
74 |
def get(self, request, format=None): |
|
75 |
""" |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
76 |
Return an aggregations of all changes. |
| 126 | 77 |
""" |
78 |
modified_since_str = request.query_params.get('modified_since', None) |
|
79 |
modified_since = None |
|
80 |
if modified_since_str is not None: |
|
81 |
modified_since = datetime.datetime.fromtimestamp( |
|
82 |
float(modified_since_str), |
|
83 |
timezone.utc |
|
84 |
) |
|
85 |
||
86 |
user = request.user |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
87 |
client_id = request.META.get('HTTP_AUDITLOG_CLIENT', None) |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
88 |
res_sessions = self.__process_log_entries(Session, user, modified_since, client_id) |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
128
diff
changeset
|
89 |
res_notes = self.__process_log_entries(Note, user, modified_since, client_id) |
| 126 | 90 |
|
91 |
return Response({ |
|
92 |
'sessions': res_sessions.values(), |
|
93 |
'notes': res_notes.values() |
|
94 |
}) |