src/notes/api/views/sync.py
changeset 128 34a75bd8d0b9
parent 126 ba8bc0199464
child 129 d48946d164c6
equal deleted inserted replaced
127:006c5270128c 128:34a75bd8d0b9
     1 import datetime
     1 import datetime
       
     2 import logging
     2 
     3 
     3 from auditlog.models import LogEntry
     4 from auditlog.models import LogEntry
     4 from django.utils import timezone
     5 from django.utils import timezone
     5 from notes.models import Note, Session
     6 from notes.models import Note, Session
     6 from rest_framework import permissions
     7 from rest_framework import permissions
     7 from rest_framework.response import Response
     8 from rest_framework.response import Response
     8 from rest_framework.views import APIView
     9 from rest_framework.views import APIView
     9 
    10 
       
    11 logger = logging.getLogger(__name__)
    10 
    12 
    11 class ListLogsView(APIView):
    13 class ListLogsView(APIView):
    12     """
    14     """
    13     View to list tle log of changes on Note and Sessions.
    15     View to list tle log of changes on Note and Sessions.
    14 
    16 
    15     * Only registered users are able to access this view.
    17     * Only registered users are able to access this view.
    16     * the results are filtered by connected user
    18     * the results are filtered by connected user
    17     """
    19     """
    18     permission_classes = (permissions.IsAuthenticated,)
    20     permission_classes = (permissions.IsAuthenticated,)
    19 
    21 
    20     def __filter_object(self, queryset, modified_since):
    22     def __filter_object(self, model, user, modified_since):
    21         log_entries = LogEntry.objects.get_for_objects(queryset)
    23         log_entries = LogEntry.objects.get_for_model(model).filter(actor=user)
    22         if modified_since:
    24         if modified_since:
    23             log_entries = log_entries.filter(timestamp__gte=modified_since)
    25             log_entries = log_entries.filter(timestamp__gte=modified_since)
    24         return log_entries
    26         return log_entries.order_by('timestamp')
    25 
    27 
    26     def __process_log_entries(self, queryset, modified_since):
    28     def __process_log_entries(self, model, user, modified_since):
    27         '''
    29         '''
    28         Process log entries
    30         Process log entries
    29         '''
    31         '''
    30         log_entries = self.__filter_object(queryset, modified_since)
    32         log_entries = self.__filter_object(model, user, modified_since)
       
    33         logger.debug("LOG ENTRies %r", list(log_entries))
    31 
    34 
    32         res = {}
    35         res = {}
    33         for log_entry in log_entries:
    36         for log_entry in log_entries:
    34             ext_id = log_entry.additional_data.get('ext_id')
    37             ext_id = log_entry.additional_data.get('ext_id')
    35             if not ext_id:
    38             if not ext_id:
    74                 float(modified_since_str),
    77                 float(modified_since_str),
    75                 timezone.utc
    78                 timezone.utc
    76             )
    79             )
    77 
    80 
    78         user = request.user
    81         user = request.user
    79         res_sessions = self.__process_log_entries(Session.objects.filter(owner=user), modified_since)
    82         res_sessions = self.__process_log_entries(Session, user, modified_since)
    80         res_notes = self.__process_log_entries(Note.objects.filter(session__owner=user), modified_since)
    83         res_notes = self.__process_log_entries(Note, user, modified_since)
    81 
    84 
    82         return Response({
    85         return Response({
    83             'sessions': res_sessions.values(),
    86             'sessions': res_sessions.values(),
    84             'notes': res_notes.values()
    87             'notes': res_notes.values()
    85         })
    88         })