Commit 4624161eb52843ab1bfabf618ef9b182061deb73

Authored by Zambom
1 parent 1c837b10

Creating function to send chat notifications to app

api/utils.py 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +import json
  2 +import textwrap
  3 +from django.utils.html import strip_tags
  4 +from django.utils.translation import ugettext as _
  5 +
  6 +from fcm_django.models import FCMDevice
  7 +from fcm_django.fcm import fcm_send_message
  8 +
  9 +from chat.serializers import ChatSerializer
  10 +
  11 +def sendChatPushNotification(user, message):
  12 + device = FCMDevice.objects.filter(user = user, active = True).first()
  13 +
  14 + if not device is None:
  15 + serializer = ChatSerializer(message)
  16 +
  17 + json_r = json.dumps(serializer.data)
  18 + json_r = json.loads(json_r)
  19 +
  20 + info = {}
  21 +
  22 + info["data"] = {}
  23 + info["data"]["messages"] = []
  24 + info["data"]["message_sent"] = json_r
  25 +
  26 + info["message"] = ""
  27 + info["type"] = ""
  28 + info["title"] = ""
  29 + info["success"] = True
  30 + info["number"] = 1
  31 + info['extra'] = 0
  32 +
  33 + response = json.dumps(info)
  34 +
  35 + title = str(user).join(_(" sent a message"))
  36 +
  37 + simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...")
  38 +
  39 + if message.image:
  40 + simple_notify += " ".join(_("[Photo]"))
  41 +
  42 + device.send_message(title = title, body = simple_notify, data = {"response": response})
0 43 \ No newline at end of file
... ...
api/views.py
... ... @@ -35,6 +35,8 @@ from django.http import HttpResponse
35 35  
36 36 from fcm_django.models import FCMDevice
37 37  
  38 +from .utils import sendChatPushNotification
  39 +
38 40 @csrf_exempt
39 41 def getToken(request):
40 42 oauth = Application.objects.filter(name = "amadeus-droid")
... ... @@ -349,6 +351,8 @@ class ChatViewset(viewsets.ModelViewSet):
349 351 info["message"] = _("Message sent successfully!")
350 352 info["success"] = True
351 353 info["number"] = 1
  354 +
  355 + sendChatPushNotification(user_to, message)
352 356 else:
353 357 info["message"] = _("Error while sending message!")
354 358 info["success"] = False
... ...
bulletin/utils.py
... ... @@ -11,14 +11,11 @@ from django.utils.translation import ugettext_lazy as _
11 11  
12 12 from channels import Group
13 13  
  14 +from api.utils import sendChatPushNotification
14 15  
15 16 from chat.models import Conversation, TalkMessages, ChatVisualizations
16 17 from users.models import User
17 18  
18   -
19   -
20   -
21   -
22 19 def brodcast_dificulties(request, message, subject):
23 20 msg = TalkMessages()
24 21 msg.text = message
... ... @@ -54,4 +51,6 @@ def brodcast_dificulties(request, message, subject):
54 51  
55 52 Group("user-%s" % p.id).send({'text': notification})
56 53  
  54 + sendChatPushNotification(p, msg)
  55 +
57 56 ChatVisualizations.objects.create(viewed = False, message = msg, user = p)
... ...
chat/views.py
... ... @@ -24,15 +24,13 @@ from log.models import Log
24 24 from log.mixins import LogMixin
25 25 import time
26 26  
27   -from fcm_django.models import FCMDevice
28   -from fcm_django.fcm import fcm_send_message
29   -
30 27 from categories.models import Category
31 28 from subjects.models import Subject
32 29 from users.models import User
33 30  
  31 +from api.utils import sendChatPushNotification
  32 +
34 33 from .models import Conversation, TalkMessages, ChatVisualizations, ChatFavorites
35   -from .serializers import ChatSerializer
36 34 from .forms import ChatMessageForm
37 35  
38 36 class GeneralIndex(LoginRequiredMixin, LogMixin, generic.ListView):
... ... @@ -373,44 +371,7 @@ class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView):
373 371  
374 372 Group("user-%s" % user.id).send({'text': notification})
375 373  
376   - device = FCMDevice.objects.filter(user = user, active = True).first()
377   -
378   - if not device is None:
379   - data = {
380   - "user_icon": self.object.user.image_url,
381   - "notify_title": str(self.object.user),
382   - "text_notify": simple_notify,
383   - "image_url": "",
384   - "message": strip_tags(self.object.text),
385   - "datetime": str(self.object.create_date)
386   - }
387   -
388   - if self.object.image:
389   - data['image_url'] = self.object.image.url
390   -
391   - data = ChatSerializer(self.object)
392   -
393   - json_r = json.dumps(data.data)
394   - json_r = json.loads(json_r)
395   -
396   - info = {}
397   -
398   - info["data"] = {}
399   - info["data"]["messages"] = []
400   - info["data"]["message_sent"] = json_r
401   -
402   - info["message"] = ""
403   - info["type"] = ""
404   - info["title"] = ""
405   - info["success"] = True
406   - info["number"] = 1
407   - info['extra'] = 0
408   -
409   - response = json.dumps(info)
410   -
411   - title = str(self.object.user)
412   -
413   - device.send_message(title = title, body = simple_notify, data = {"response": response})
  374 + sendChatPushNotification(user, self.object)
414 375  
415 376 ChatVisualizations.objects.create(viewed = False, message = self.object, user = user)
416 377  
... ...
goals/utils.py
... ... @@ -10,6 +10,8 @@ from django.utils.translation import ugettext_lazy as _
10 10  
11 11 from channels import Group
12 12  
  13 +from api.utils import sendChatPushNotification
  14 +
13 15 from chat.models import Conversation, TalkMessages, ChatVisualizations
14 16 from users.models import User
15 17  
... ... @@ -63,5 +65,7 @@ def brodcast_dificulties(request, message, subject):
63 65  
64 66 Group("user-%s" % p.id).send({'text': notification})
65 67  
  68 + sendChatPushNotification(p, msg)
  69 +
66 70 ChatVisualizations.objects.create(viewed = False, message = msg, user = p)
67 71  
... ...