Commit e8f95108151361888853b455139547d23ada7351
1 parent
00aa6385
Exists in
master
and in
2 other branches
Adding log to mobile operations
Showing
1 changed file
with
65 additions
and
4 deletions
Show diff stats
api/views.py
... | ... | @@ -23,6 +23,9 @@ from security.models import Security |
23 | 23 | from chat.serializers import ChatSerializer |
24 | 24 | from chat.models import TalkMessages, Conversation, ChatVisualizations |
25 | 25 | |
26 | +from log.models import Log | |
27 | +from log.mixins import LogMixin | |
28 | + | |
26 | 29 | from subjects.serializers import SubjectSerializer |
27 | 30 | from subjects.models import Subject |
28 | 31 | |
... | ... | @@ -83,7 +86,7 @@ def getToken(request): |
83 | 86 | |
84 | 87 | return HttpResponse(response) |
85 | 88 | |
86 | -class LoginViewset(viewsets.ReadOnlyModelViewSet): | |
89 | +class LoginViewset(viewsets.ReadOnlyModelViewSet, LogMixin): | |
87 | 90 | """ |
88 | 91 | login: |
89 | 92 | Log a user in the system |
... | ... | @@ -96,6 +99,11 @@ class LoginViewset(viewsets.ReadOnlyModelViewSet): |
96 | 99 | serializer_class = UserSerializer |
97 | 100 | permissions_classes = (IsAuthenticated,) |
98 | 101 | |
102 | + log_component = 'mobile' | |
103 | + log_action = 'access' | |
104 | + log_resource = 'system' | |
105 | + log_context = {} | |
106 | + | |
99 | 107 | @csrf_exempt |
100 | 108 | @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) |
101 | 109 | def login(self, request): |
... | ... | @@ -121,7 +129,9 @@ class LoginViewset(viewsets.ReadOnlyModelViewSet): |
121 | 129 | user_info['extra'] = 0 |
122 | 130 | |
123 | 131 | response = json.dumps(user_info) |
124 | - | |
132 | + | |
133 | + super(LoginViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
134 | + | |
125 | 135 | return HttpResponse(response) |
126 | 136 | |
127 | 137 | @csrf_exempt |
... | ... | @@ -208,7 +218,7 @@ class SubjectViewset(viewsets.ReadOnlyModelViewSet): |
208 | 218 | |
209 | 219 | return HttpResponse(response) |
210 | 220 | |
211 | -class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): | |
221 | +class ParticipantsViewset(viewsets.ReadOnlyModelViewSet, LogMixin): | |
212 | 222 | """ |
213 | 223 | get_participants: |
214 | 224 | Get all users that participates in some subject. Require the logged user email and the subject slug |
... | ... | @@ -218,17 +228,26 @@ class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): |
218 | 228 | serializer_class = UserSerializer |
219 | 229 | permissions_classes = (IsAuthenticated, ) |
220 | 230 | |
231 | + log_component = 'mobile' | |
232 | + log_action = 'view' | |
233 | + log_resource = 'subject_participants' | |
234 | + log_context = {} | |
235 | + | |
221 | 236 | @csrf_exempt |
222 | 237 | @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) |
223 | 238 | def get_participants(self, request): |
224 | 239 | username = request.data['email'] |
225 | 240 | subject_slug = request.data['subject_slug'] |
226 | 241 | |
242 | + user = User.objects.get(email = username) | |
243 | + | |
227 | 244 | participants = None |
228 | 245 | |
229 | 246 | response = "" |
230 | 247 | |
231 | 248 | if not subject_slug == "": |
249 | + subject = Subject.objects.get(slug = subject_slug) | |
250 | + | |
232 | 251 | participants = User.objects.filter(Q(is_staff = True) | Q(subject_student__slug = subject_slug) | Q(professors__slug = subject_slug) | Q(coordinators__subject_category__slug = subject_slug)).exclude(email = username).distinct() |
233 | 252 | |
234 | 253 | serializer = UserSerializer(participants, many = True, context = {"request_user": username}) |
... | ... | @@ -250,9 +269,15 @@ class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): |
250 | 269 | |
251 | 270 | response = json.dumps(info) |
252 | 271 | |
272 | + self.log_context['subject_id'] = subject.id | |
273 | + self.log_context['subject_slug'] = subject_slug | |
274 | + self.log_context['subject_name'] = subject.name | |
275 | + | |
276 | + super(ParticipantsViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
277 | + | |
253 | 278 | return HttpResponse(response) |
254 | 279 | |
255 | -class ChatViewset(viewsets.ModelViewSet): | |
280 | +class ChatViewset(viewsets.ModelViewSet, LogMixin): | |
256 | 281 | """ |
257 | 282 | get_messages: |
258 | 283 | Get messages of a conversation |
... | ... | @@ -265,17 +290,26 @@ class ChatViewset(viewsets.ModelViewSet): |
265 | 290 | serializer_class = ChatSerializer |
266 | 291 | permissions_classes = (IsAuthenticated, ) |
267 | 292 | |
293 | + log_component = 'mobile' | |
294 | + log_action = 'view' | |
295 | + log_resource = 'talk' | |
296 | + log_context = {} | |
297 | + | |
268 | 298 | @csrf_exempt |
269 | 299 | @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) |
270 | 300 | def get_messages(self, request): |
271 | 301 | username = request.data['email'] |
272 | 302 | user_two = request.data['user_two'] |
273 | 303 | |
304 | + user = User.objects.get(email = username) | |
305 | + | |
274 | 306 | messages = None |
275 | 307 | |
276 | 308 | response = "" |
277 | 309 | |
278 | 310 | if not user_two == "": |
311 | + user2 = User.objects.get(email = user_two) | |
312 | + | |
279 | 313 | messages = TalkMessages.objects.filter((Q(talk__user_one__email = username) & Q(talk__user_two__email = user_two)) | (Q(talk__user_one__email = user_two) & Q(talk__user_two__email = username))).order_by('-create_date') |
280 | 314 | |
281 | 315 | serializer = ChatSerializer(messages, many = True) |
... | ... | @@ -298,11 +332,27 @@ class ChatViewset(viewsets.ModelViewSet): |
298 | 332 | |
299 | 333 | response = json.dumps(info) |
300 | 334 | |
335 | + try: | |
336 | + talk = Conversation.objects.get((Q(user_one__email = username) & Q(user_two__email = user_two)) | (Q(user_two__email = username) & Q(user_one__email = user_two))) | |
337 | + self.log_context['talk_id'] = talk.id | |
338 | + except Conversation.DoesNotExist: | |
339 | + pass | |
340 | + | |
341 | + self.log_context['user_id'] = user2.id | |
342 | + self.log_context['user_name'] = str(user2) | |
343 | + self.log_context['user_email'] = user_two | |
344 | + | |
345 | + super(ChatViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
346 | + | |
301 | 347 | return HttpResponse(response) |
302 | 348 | |
303 | 349 | @csrf_exempt |
304 | 350 | @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) |
305 | 351 | def send_message(self, request): |
352 | + self.log_action = 'send' | |
353 | + self.log_resource = 'message' | |
354 | + self.log_context = {} | |
355 | + | |
306 | 356 | if 'file' in request.data: |
307 | 357 | file = request.FILES['file'] |
308 | 358 | |
... | ... | @@ -342,6 +392,10 @@ class ChatViewset(viewsets.ModelViewSet): |
342 | 392 | subject = Subject.objects.get(slug = subject) |
343 | 393 | space = subject.slug |
344 | 394 | space_type = "subject" |
395 | + | |
396 | + self.log_context['subject_id'] = subject.id | |
397 | + self.log_context['subject_slug'] = space | |
398 | + self.log_context['subject_name'] = subject.name | |
345 | 399 | else: |
346 | 400 | subject = None |
347 | 401 | space = 0 |
... | ... | @@ -358,6 +412,11 @@ class ChatViewset(viewsets.ModelViewSet): |
358 | 412 | |
359 | 413 | message.save() |
360 | 414 | |
415 | + self.log_context['talk_id'] = talk.id | |
416 | + self.log_context['user_id'] = user_to.id | |
417 | + self.log_context['user_name'] = str(user_to) | |
418 | + self.log_context['user_email'] = user_two | |
419 | + | |
361 | 420 | if not message.pk is None: |
362 | 421 | simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...") |
363 | 422 | |
... | ... | @@ -393,6 +452,8 @@ class ChatViewset(viewsets.ModelViewSet): |
393 | 452 | info["number"] = 1 |
394 | 453 | |
395 | 454 | sendChatPushNotification(user_to, message) |
455 | + | |
456 | + super(ChatViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
396 | 457 | else: |
397 | 458 | info["message"] = _("Error while sending message!") |
398 | 459 | info["success"] = False | ... | ... |