def brodcast_dificulties(request, message, subject): msg = TalkMessages() msg.text = message msg.user = request.user msg.subject = subject simple_notify = textwrap.shorten(strip_tags(msg.text), width=30, placeholder="...") for p in subject.professor.all(): talks = Conversation.objects.filter( (Q(user_one=request.user) & Q(user_two__email=p.email)) | (Q(user_two=request.user) & Q(user_one__email=p.email))) if talks.count() > 0: msg.talk = talks[0] else: msg.talk = Conversation.objects.create(user_one=request.user, user_two=p) msg.save() notification = { "type": "chat", "subtype": subject.slug, "space": "subject", "user_icon": request.user.image_url, "notify_title": str(request.user), "simple_notify": simple_notify, "view_url": reverse("chat:view_message", args=(msg.id, ), kwargs={}), "complete": render_to_string("chat/_message.html", {"talk_msg": msg}, request), "container": "chat-" + str(request.user.id), "last_date": _("Last message in %s") % (formats.date_format(msg.create_date, "SHORT_DATETIME_FORMAT")) } notification = json.dumps(notification) Group("user-%s" % p.id).send({'text': notification}) sendChatPushNotification(p, msg) ChatVisualizations.objects.create(viewed=False, message=msg, user=p)
def brodcast_dificulties(request, message, subject): msg = TalkMessages() msg.text = message msg.user = request.user msg.subject = subject simple_notify = textwrap.shorten(strip_tags(msg.text), width = 30, placeholder = "...") for p in subject.professor.all(): talks = Conversation.objects.filter((Q(user_one = request.user) & Q(user_two__email = p.email)) | (Q(user_two = request.user) & Q(user_one__email = p.email))) if talks.count() > 0: msg.talk = talks[0] else: msg.talk = Conversation.objects.create(user_one = request.user, user_two = p) msg.save() notification = { "type": "chat", "subtype": subject.slug, "space": "subject", "user_icon": request.user.image_url, "notify_title": str(request.user), "simple_notify": simple_notify, "view_url": reverse("chat:view_message", args = (msg.id, ), kwargs = {}), "complete": render_to_string("chat/_message.html", {"talk_msg": msg}, request), "container": "chat-" + str(request.user.id), "last_date": _("Last message in %s")%(formats.date_format(msg.create_date, "SHORT_DATETIME_FORMAT")) } notification = json.dumps(notification) Group("user-%s" % p.id).send({'text': notification}) sendChatPushNotification(p, msg) ChatVisualizations.objects.create(viewed = False, message = msg, user = p)
def test_message_data_from_view(self): """ test if I'm capturing the message data correctly in all scenarios """ #scenario 01: the number of messages a student receives from other students topic = Topic.objects.create(name="novo topico", subject= self.subject, visible = True) topic.save() student02 = User.objects.create(username="******", email= "*****@*****.**", password="******") student02.save() conversation_01 = Conversation.objects.create(user_one = self.student, user_two = student02) #building messages message01 = TalkMessages(text="hi", talk = conversation_01, subject = self.subject, user = self.student) message01.save() message02 = TalkMessages(text="hello, how are you?", talk = conversation_01, subject = self.subject, user = student02) message02.save() #get all conversations where a student of the subject is in and the amount of messages the hey sent to other students conversations = Conversation.objects.filter(Q(user_one = self.student) | Q(user_two = self.student) ) amount_of_messages = TalkMessages.objects.filter(talk__in= conversations).count() print(amount_of_messages) self.assertEqual(3,3)
def send_message(self, request): self.log_action = "send" self.log_resource = "message" self.log_context = {} if "file" in request.data: file = request.FILES["file"] data = json.loads(request.data["data"]) username = data["email"] user_two = data["user_two"] subject = data["subject"] msg_text = data["text"] create_date = data["create_date"] else: file = None data = ( request.data if request.data else json.loads(request.body.decode("utf-8")) ) username = data["email"] user_two = data["user_two"] subject = data["subject"] msg_text = data["text"] create_date = data["create_date"] info = {} if not user_two == "" and not username == "": user = User.objects.get(email=username) user_to = User.objects.get(email=user_two) 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)) ) if talks.count() > 0: talk = talks[0] else: talk = Conversation() talk.user_one = user talk.user_two = user_to talk.save() if subject != "": subject = Subject.objects.get(slug=subject) space = subject.slug space_type = "subject" self.log_context["subject_id"] = subject.id self.log_context["subject_slug"] = space self.log_context["subject_name"] = subject.name else: subject = None space = 0 space_type = "general" message = TalkMessages() message.text = "<p>" + msg_text + "</p>" message.user = user message.talk = talk message.subject = subject if not file is None: message.image = file message.save() self.log_context["talk_id"] = talk.id self.log_context["user_id"] = user_to.id self.log_context["user_name"] = str(user_to) self.log_context["user_email"] = user_two if not message.pk is None: simple_notify = textwrap.shorten( strip_tags(message.text), width=30, placeholder="..." ) notification = { "type": "chat", "subtype": space_type, "space": space, "user_icon": message.user.image_url, "notify_title": str(message.user), "simple_notify": simple_notify, "view_url": reverse( "chat:view_message", args=(message.id,), kwargs={} ), "complete": render_to_string( "chat/_message.html", {"talk_msg": message}, request ), "container": "chat-" + str(message.user.id), "last_date": _("Last message in %s") % ( formats.date_format( message.create_date, "SHORT_DATETIME_FORMAT" ) ), } notification = json.dumps(notification) Group("user-%s" % user_to.id).send({"text": notification}) ChatVisualizations.objects.create( viewed=False, message=message, user=user_to ) serializer = ChatSerializer(message) json_r = json.dumps(serializer.data) json_r = json.loads(json_r) info["data"] = {} info["data"]["message_sent"] = json_r info["message"] = _("Message sent successfully!") info["success"] = True info["number"] = 1 sendChatPushNotification(user_to, message) super(ChatViewset, self).createLog( user, self.log_component, self.log_action, self.log_resource, self.log_context, ) else: info["message"] = _("Error while sending message!") info["success"] = False info["number"] = 0 else: info["data"] = {} info["data"]["message_sent"] = {} info["message"] = _("No information received!") info["success"] = False info["number"] = 0 info["data"]["messages"] = [] info["type"] = "" info["title"] = _("Amadeus") info["extra"] = 0 response = json.dumps(info) return HttpResponse(response)
def send_message(self, request): self.log_action = 'send' self.log_resource = 'message' self.log_context = {} if 'file' in request.data: file = request.FILES['file'] data = json.loads(request.data['data']) username = data['email'] user_two = data['user_two'] subject = data['subject'] msg_text = data['text'] create_date = data['create_date'] else: file = None data = request.data if request.data else json.loads(request.body.decode('utf-8')) username = data['email'] user_two = data['user_two'] subject = data['subject'] msg_text = data['text'] create_date = data['create_date'] info = {} if not user_two == "" and not username == "": user = User.objects.get(email = username) user_to = User.objects.get(email = user_two) 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))) if talks.count() > 0: talk = talks[0] else: talk = Conversation() talk.user_one = user talk.user_two = user_to talk.save() if subject != "": subject = Subject.objects.get(slug = subject) space = subject.slug space_type = "subject" self.log_context['subject_id'] = subject.id self.log_context['subject_slug'] = space self.log_context['subject_name'] = subject.name else: subject = None space = 0 space_type = "general" message = TalkMessages() message.text = "<p>" + msg_text + "</p>" message.user = user message.talk = talk message.subject = subject if not file is None: message.image = file message.save() self.log_context['talk_id'] = talk.id self.log_context['user_id'] = user_to.id self.log_context['user_name'] = str(user_to) self.log_context['user_email'] = user_two if not message.pk is None: simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...") notification = { "type": "chat", "subtype": space_type, "space": space, "user_icon": message.user.image_url, "notify_title": str(message.user), "simple_notify": simple_notify, "view_url": reverse("chat:view_message", args = (message.id, ), kwargs = {}), "complete": render_to_string("chat/_message.html", {"talk_msg": message}, request), "container": "chat-" + str(message.user.id), "last_date": _("Last message in %s")%(formats.date_format(message.create_date, "SHORT_DATETIME_FORMAT")) } notification = json.dumps(notification) Group("user-%s" % user_to.id).send({'text': notification}) ChatVisualizations.objects.create(viewed = False, message = message, user = user_to) serializer = ChatSerializer(message) json_r = json.dumps(serializer.data) json_r = json.loads(json_r) info["data"] = {} info["data"]["message_sent"] = json_r info["message"] = _("Message sent successfully!") info["success"] = True info["number"] = 1 sendChatPushNotification(user_to, message) super(ChatViewset, self).createLog(user, self.log_component, self.log_action, self.log_resource, self.log_context) else: info["message"] = _("Error while sending message!") info["success"] = False info["number"] = 0 else: info["data"] = {} info["data"]["message_sent"] = {} info["message"] = _("No information received!") info["success"] = False info["number"] = 0 info["data"]["messages"] = [] info["type"] = "" info["title"] = _("Amadeus") info['extra'] = 0 response = json.dumps(info) return HttpResponse(response)