Commit 396d4690f183b30e5be49668eb69e4736ec12c90
1 parent
336aaa96
Exists in
master
and in
2 other branches
Added suport to provide (un)favorite message action on app
Showing
2 changed files
with
49 additions
and
2 deletions
Show diff stats
api/views.py
@@ -21,7 +21,7 @@ from django.db.models import Q | @@ -21,7 +21,7 @@ from django.db.models import Q | ||
21 | from security.models import Security | 21 | from security.models import Security |
22 | 22 | ||
23 | from chat.serializers import ChatSerializer | 23 | from chat.serializers import ChatSerializer |
24 | -from chat.models import TalkMessages, Conversation, ChatVisualizations | 24 | +from chat.models import TalkMessages, Conversation, ChatVisualizations, ChatFavorites |
25 | 25 | ||
26 | from log.models import Log | 26 | from log.models import Log |
27 | from log.mixins import LogMixin | 27 | from log.mixins import LogMixin |
@@ -473,4 +473,51 @@ class ChatViewset(viewsets.ModelViewSet, LogMixin): | @@ -473,4 +473,51 @@ class ChatViewset(viewsets.ModelViewSet, LogMixin): | ||
473 | 473 | ||
474 | response = json.dumps(info) | 474 | response = json.dumps(info) |
475 | 475 | ||
476 | + return HttpResponse(response) | ||
477 | + | ||
478 | + @csrf_exempt | ||
479 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | ||
480 | + def favorite_messages(self, request): | ||
481 | + username = request.data['email'] | ||
482 | + favor = request.data['favor'] | ||
483 | + message_id = int(request.data['id']) | ||
484 | + | ||
485 | + user = User.objects.get(email = username) | ||
486 | + | ||
487 | + message = get_object_or_404(TalkMessages, id = message_id) | ||
488 | + | ||
489 | + response = "" | ||
490 | + | ||
491 | + if favor == "true": | ||
492 | + if not ChatFavorites.objects.filter(Q(user = user) & Q(message__id = message_id)).exists(): | ||
493 | + #Insert on table | ||
494 | + ChatFavorites.objects.create(message = message, user = user) | ||
495 | + | ||
496 | + info = {} | ||
497 | + | ||
498 | + info["message"] = "" | ||
499 | + info["type"] = "" | ||
500 | + info["title"] = "" | ||
501 | + info["success"] = True | ||
502 | + info["number"] = 1 | ||
503 | + info['extra'] = 0 | ||
504 | + | ||
505 | + response = json.dumps(info) | ||
506 | + | ||
507 | + elif favor == "false": | ||
508 | + if ChatFavorites.objects.filter(Q(user = user) & Q(message__id = message_id)).exists(): | ||
509 | + #Delete row | ||
510 | + ChatFavorites.objects.filter(message = message, user = user).delete() | ||
511 | + | ||
512 | + info = {} | ||
513 | + | ||
514 | + info["message"] = "" | ||
515 | + info["type"] = "" | ||
516 | + info["title"] = "" | ||
517 | + info["success"] = True | ||
518 | + info["number"] = 1 | ||
519 | + info['extra'] = 0 | ||
520 | + | ||
521 | + response = json.dumps(info) | ||
522 | + | ||
476 | return HttpResponse(response) | 523 | return HttpResponse(response) |
477 | \ No newline at end of file | 524 | \ No newline at end of file |
chat/serializers.py
@@ -22,4 +22,4 @@ class ChatSerializer(serializers.ModelSerializer): | @@ -22,4 +22,4 @@ class ChatSerializer(serializers.ModelSerializer): | ||
22 | 22 | ||
23 | class Meta: | 23 | class Meta: |
24 | model = TalkMessages | 24 | model = TalkMessages |
25 | - fields = ('text', 'user', 'subject', 'image_url', 'create_date', 'favorite') | 25 | + fields = ('text', 'user', 'subject', 'image_url', 'create_date', 'favorite', 'id') |