Commit 6ddcd43107bb67816f610216740c9822b222852b
1 parent
db4bd233
Exists in
master
and in
2 other branches
Broadcast dificulties from bulletin
Showing
1 changed file
with
57 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
1 | +import json | ||
2 | +import textwrap | ||
3 | + | ||
4 | +from django.db.models import Q | ||
5 | +from django.utils import timezone | ||
6 | +from django.utils import formats | ||
7 | +from django.utils.html import strip_tags | ||
8 | +from django.template.loader import render_to_string | ||
9 | +from django.core.urlresolvers import reverse | ||
10 | +from django.utils.translation import ugettext_lazy as _ | ||
11 | + | ||
12 | +from channels import Group | ||
13 | + | ||
14 | + | ||
15 | +from chat.models import Conversation, TalkMessages, ChatVisualizations | ||
16 | +from users.models import User | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | +def brodcast_dificulties(request, message, subject): | ||
23 | + msg = TalkMessages() | ||
24 | + msg.text = message | ||
25 | + msg.user = request.user | ||
26 | + msg.subject = subject | ||
27 | + | ||
28 | + simple_notify = textwrap.shorten(strip_tags(msg.text), width = 30, placeholder = "...") | ||
29 | + | ||
30 | + for p in subject.professor.all(): | ||
31 | + 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))) | ||
32 | + | ||
33 | + if talks.count() > 0: | ||
34 | + msg.talk = talks[0] | ||
35 | + else: | ||
36 | + msg.talk = Conversation.objects.create(user_one = request.user, user_two = p) | ||
37 | + | ||
38 | + msg.save() | ||
39 | + | ||
40 | + notification = { | ||
41 | + "type": "chat", | ||
42 | + "subtype": subject.slug, | ||
43 | + "space": "subject", | ||
44 | + "user_icon": request.user.image_url, | ||
45 | + "notify_title": str(request.user), | ||
46 | + "simple_notify": simple_notify, | ||
47 | + "view_url": reverse("chat:view_message", args = (msg.id, ), kwargs = {}), | ||
48 | + "complete": render_to_string("chat/_message.html", {"talk_msg": msg}, request), | ||
49 | + "container": "chat-" + str(request.user.id), | ||
50 | + "last_date": _("Last message in %s")%(formats.date_format(msg.create_date, "SHORT_DATETIME_FORMAT")) | ||
51 | + } | ||
52 | + | ||
53 | + notification = json.dumps(notification) | ||
54 | + | ||
55 | + Group("user-%s" % p.id).send({'text': notification}) | ||
56 | + | ||
57 | + ChatVisualizations.objects.create(viewed = False, message = msg, user = p) |