Commit cf6a65a52745ade15d2340fae21cc45c12005f55

Authored by Zambom
1 parent c1b04946

Adding send message in api chat

Showing 1 changed file with 89 additions and 1 deletions   Show diff stats
api/views.py
... ... @@ -2,6 +2,14 @@ import requests, json
2 2 from django.shortcuts import get_object_or_404, reverse
3 3 from django.contrib.auth import authenticate
4 4 from django.views.decorators.csrf import csrf_exempt
  5 +from django.utils.translation import ugettext as _
  6 +from django.template.loader import render_to_string
  7 +import textwrap
  8 +from datetime import datetime
  9 +from django.utils import formats
  10 +from django.utils.html import strip_tags
  11 +
  12 +from channels import Group
5 13  
6 14 from rest_framework import viewsets
7 15 from rest_framework.response import Response
... ... @@ -13,7 +21,7 @@ from django.db.models import Q
13 21 from security.models import Security
14 22  
15 23 from chat.serializers import ChatSerializer
16   -from chat.models import TalkMessages
  24 +from chat.models import TalkMessages, Conversation, ChatVisualizations
17 25  
18 26 from subjects.serializers import SubjectSerializer
19 27 from subjects.models import Subject
... ... @@ -221,4 +229,84 @@ class ChatViewset(viewsets.ModelViewSet):
221 229  
222 230 response = json.dumps(info)
223 231  
  232 + return HttpResponse(response)
  233 +
  234 + @csrf_exempt
  235 + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
  236 + def send_message(self, request):
  237 + username = request.data['email']
  238 + user_two = request.data['user_two']
  239 + subject = request.data['subject']
  240 + msg_text = request.data['text']
  241 + create_date = request.data['create_date']
  242 +
  243 + info = {}
  244 +
  245 + if not user_two == "" and not username == "":
  246 + user = User.objects.get(email = username)
  247 + user_to = User.objects.get(email = user_two)
  248 +
  249 + talks = Conversation.objects.filter((Q(user_one__email = username) & Q(user_two__email = user_two)) | (Q(user_two__email = username) & Q(user_one__email = user_two)))
  250 +
  251 + if talks.count() > 0:
  252 + talk = talks[0]
  253 + else:
  254 + talk = Conversation()
  255 + talk.user_one = user
  256 + talk.user_two = user_to
  257 +
  258 + talk.save()
  259 +
  260 + subject = Subject.objects.get(slug = subject)
  261 +
  262 + message = TalkMessages()
  263 + message.text = "<p>" + msg_text + "</p>"
  264 + message.user = user
  265 + message.talk = talk
  266 +
  267 + message.save()
  268 +
  269 + if not message.pk is None:
  270 + simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...")
  271 +
  272 + notification = {
  273 + "type": "chat",
  274 + "subtype": "subject",
  275 + "space": subject.slug,
  276 + "user_icon": message.user.image_url,
  277 + "notify_title": str(message.user),
  278 + "simple_notify": simple_notify,
  279 + "view_url": reverse("chat:view_message", args = (message.id, ), kwargs = {}),
  280 + "complete": render_to_string("chat/_message.html", {"talk_msg": message}, request),
  281 + "container": "chat-" + str(message.user.id),
  282 + "last_date": _("Last message in %s")%(formats.date_format(message.create_date, "SHORT_DATETIME_FORMAT"))
  283 + }
  284 +
  285 + notification = json.dumps(notification)
  286 +
  287 + Group("user-%s" % user_to.id).send({'text': notification})
  288 +
  289 + ChatVisualizations.objects.create(viewed = False, message = message, user = user_to)
  290 +
  291 + info["message"] = _("Message sent successfully!")
  292 + info["success"] = True
  293 + info["number"] = 1
  294 + else:
  295 + info["message"] = _("Error while sending message!")
  296 + info["success"] = False
  297 + info["number"] = 0
  298 + else:
  299 + info["message"] = _("No information received!")
  300 + info["success"] = False
  301 + info["number"] = 0
  302 +
  303 + info["data"] = {}
  304 + info["data"]["messages"] = []
  305 +
  306 + info["type"] = ""
  307 + info["title"] = _("Amadeus")
  308 + info['extra'] = 0
  309 +
  310 + response = json.dumps(info)
  311 +
224 312 return HttpResponse(response)
225 313 \ No newline at end of file
... ...