Commit 522215a29e73a74791ec8b91e68d870d03bbde9e
1 parent
02272cd4
Exists in
master
and in
2 other branches
Adding dificulties field to goals submission
Showing
3 changed files
with
90 additions
and
1 deletions
Show diff stats
goals/templates/goals/_form_submit.html
... | ... | @@ -46,6 +46,15 @@ |
46 | 46 | {% endif %} |
47 | 47 | </div> |
48 | 48 | {% endfor %} |
49 | + | |
50 | + <hr /> | |
51 | + | |
52 | + <div class="form-group"> | |
53 | + <div class="col-md-12"> | |
54 | + <label for="dificulties">{% trans 'There are obstacles to your activities?' %}</label> | |
55 | + <textarea class="form-control" id="dificulties" name="dificulties" placeholder="{% trans 'Type here the dificulties that may harm your performance' %}"></textarea> | |
56 | + </div> | |
57 | + </div> | |
49 | 58 | </form> |
50 | 59 | |
51 | 60 | <hr /> | ... | ... |
goals/utils.py
1 | +import json | |
2 | +import textwrap | |
3 | +from django.db.models import Q | |
1 | 4 | from django.utils import timezone |
5 | +from django.utils import formats | |
6 | +from django.utils.html import strip_tags | |
7 | +from django.template.loader import render_to_string | |
8 | +from django.core.urlresolvers import reverse | |
9 | +from django.utils.translation import ugettext_lazy as _ | |
2 | 10 | |
11 | +from channels import Group | |
12 | + | |
13 | +from chat.models import Conversation, TalkMessages, ChatVisualizations | |
3 | 14 | from users.models import User |
4 | 15 | |
5 | 16 | from .models import GoalItem, MyGoals |
... | ... | @@ -16,3 +27,41 @@ def set_goals(): |
16 | 27 | entries.append(MyGoals(user = user, item = goal, value = goal.ref_value)) |
17 | 28 | |
18 | 29 | MyGoals.objects.bulk_create(entries) |
30 | + | |
31 | +def brodcast_dificulties(request, message, subject): | |
32 | + msg = TalkMessages() | |
33 | + msg.text = message | |
34 | + msg.user = request.user | |
35 | + msg.subject = subject | |
36 | + | |
37 | + simple_notify = textwrap.shorten(strip_tags(msg.text), width = 30, placeholder = "...") | |
38 | + | |
39 | + for p in subject.professor.all(): | |
40 | + 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))) | |
41 | + | |
42 | + if talks.count() > 0: | |
43 | + msg.talk = talks[0] | |
44 | + else: | |
45 | + msg.talk = Conversation.objects.create(user_one = request.user, user_two = p) | |
46 | + | |
47 | + msg.save() | |
48 | + | |
49 | + notification = { | |
50 | + "type": "chat", | |
51 | + "subtype": subject.slug, | |
52 | + "space": "subject", | |
53 | + "user_icon": request.user.image_url, | |
54 | + "notify_title": str(request.user), | |
55 | + "simple_notify": simple_notify, | |
56 | + "view_url": reverse("chat:view_message", args = (msg.id, ), kwargs = {}), | |
57 | + "complete": render_to_string("chat/_message.html", {"talk_msg": msg}, request), | |
58 | + "container": "chat-" + str(request.user.id), | |
59 | + "last_date": _("Last message in %s")%(formats.date_format(msg.create_date, "SHORT_DATETIME_FORMAT")) | |
60 | + } | |
61 | + | |
62 | + notification = json.dumps(notification) | |
63 | + | |
64 | + Group("user-%s" % p.id).send({'text': notification}) | |
65 | + | |
66 | + ChatVisualizations.objects.create(viewed = False, message = msg, user = p) | |
67 | + | ... | ... |
goals/views.py
... | ... | @@ -17,6 +17,7 @@ from amadeus.permissions import has_subject_permissions, has_resource_permission |
17 | 17 | from topics.models import Topic |
18 | 18 | from users.models import User |
19 | 19 | |
20 | +from .utils import brodcast_dificulties | |
20 | 21 | from .forms import GoalsForm, MyGoalsForm, InlinePendenciesFormset, InlineGoalItemFormset |
21 | 22 | from .models import Goals, MyGoals |
22 | 23 | |
... | ... | @@ -344,6 +345,16 @@ class NewWindowSubmit(LoginRequiredMixin, LogMixin, generic.edit.CreateView): |
344 | 345 | |
345 | 346 | form.save() |
346 | 347 | |
348 | + dificulties = self.request.POST.get('dificulties', None) | |
349 | + | |
350 | + if not dificulties is None: | |
351 | + slug = self.kwargs.get('slug', '') | |
352 | + goals = get_object_or_404(Goals, slug = slug) | |
353 | + | |
354 | + message = _("#Dificulty(ies) found in %s")%(str(goals)) + ":<p>" + dificulties + "</p>" | |
355 | + | |
356 | + brodcast_dificulties(self.request, message, goals.topic.subject) | |
357 | + | |
347 | 358 | return redirect(self.get_success_url()) |
348 | 359 | |
349 | 360 | def get_context_data(self, **kwargs): |
... | ... | @@ -445,7 +456,7 @@ class SubmitView(LoginRequiredMixin, LogMixin, generic.edit.CreateView): |
445 | 456 | |
446 | 457 | def post(self, request, *args, **kwargs): |
447 | 458 | self.object = None |
448 | - | |
459 | + | |
449 | 460 | form_class = self.get_form_class() |
450 | 461 | form = self.get_form(form_class) |
451 | 462 | |
... | ... | @@ -470,6 +481,16 @@ class SubmitView(LoginRequiredMixin, LogMixin, generic.edit.CreateView): |
470 | 481 | |
471 | 482 | form.save() |
472 | 483 | |
484 | + dificulties = self.request.POST.get('dificulties', None) | |
485 | + | |
486 | + if not dificulties is None: | |
487 | + slug = self.kwargs.get('slug', '') | |
488 | + goals = get_object_or_404(Goals, slug = slug) | |
489 | + | |
490 | + message = _("#Dificulty(ies) found in %s")%(str(goals)) + ":<p>" + dificulties + "</p>" | |
491 | + | |
492 | + brodcast_dificulties(self.request, message, goals.topic.subject) | |
493 | + | |
473 | 494 | return redirect(self.get_success_url()) |
474 | 495 | |
475 | 496 | def get_context_data(self, **kwargs): |
... | ... | @@ -578,6 +599,16 @@ class UpdateSubmit(LoginRequiredMixin, LogMixin, generic.UpdateView): |
578 | 599 | |
579 | 600 | form.save() |
580 | 601 | |
602 | + dificulties = self.request.POST.get('dificulties', None) | |
603 | + | |
604 | + if not dificulties is None: | |
605 | + slug = self.kwargs.get('slug', '') | |
606 | + goals = get_object_or_404(Goals, slug = slug) | |
607 | + | |
608 | + message = _("#Dificulty(ies) found in %s")%(str(goals)) + ":<p>" + dificulties + "</p>" | |
609 | + | |
610 | + brodcast_dificulties(self.request, message, goals.topic.subject) | |
611 | + | |
581 | 612 | return redirect(self.get_success_url()) |
582 | 613 | |
583 | 614 | def get_context_data(self, **kwargs): | ... | ... |