Commit 8cde917a3c9f0e8932f2e435b9e88a0da28deb5c
1 parent
cc5c45e4
Exists in
master
and in
2 other branches
Included reports and send message views for webconference
Showing
1 changed file
with
178 additions
and
0 deletions
Show diff stats
webconference/views.py
@@ -6,6 +6,18 @@ from django.utils.translation import ugettext_lazy as _ | @@ -6,6 +6,18 @@ from django.utils.translation import ugettext_lazy as _ | ||
6 | from django.contrib.auth.mixins import LoginRequiredMixin | 6 | from django.contrib.auth.mixins import LoginRequiredMixin |
7 | from django.http import JsonResponse | 7 | from django.http import JsonResponse |
8 | 8 | ||
9 | +from webpage.forms import FormModalMessage | ||
10 | +from chat.models import Conversation, TalkMessages, ChatVisualizations | ||
11 | +import textwrap | ||
12 | +import json | ||
13 | +from channels import Group | ||
14 | +import datetime | ||
15 | +from users.models import User | ||
16 | +from django.utils.html import strip_tags | ||
17 | +from django.template.loader import render_to_string | ||
18 | +from django.utils import formats | ||
19 | +from subjects.models import Subject | ||
20 | + | ||
9 | from amadeus.permissions import has_subject_permissions, has_resource_permissions | 21 | from amadeus.permissions import has_subject_permissions, has_resource_permissions |
10 | 22 | ||
11 | import time | 23 | import time |
@@ -533,3 +545,169 @@ class ConferenceSettings(braces_mixins.LoginRequiredMixin, braces_mixins.Staffus | @@ -533,3 +545,169 @@ class ConferenceSettings(braces_mixins.LoginRequiredMixin, braces_mixins.Staffus | ||
533 | context['title'] = _('Web Conference Settings') | 545 | context['title'] = _('Web Conference Settings') |
534 | 546 | ||
535 | return context | 547 | return context |
548 | +class StatisticsView(LoginRequiredMixin, LogMixin, generic.DetailView): | ||
549 | + log_component = 'resources' | ||
550 | + log_action = 'view_statistics' | ||
551 | + log_resource = 'webconference' | ||
552 | + log_context = {} | ||
553 | + | ||
554 | + login_url = reverse_lazy("users:login") | ||
555 | + redirect_field_name = 'next' | ||
556 | + model = Webconference | ||
557 | + template_name = 'webconference/relatorios.html' | ||
558 | + | ||
559 | + def dispatch(self, request, *args, **kwargs): | ||
560 | + slug = self.kwargs.get('slug', '') | ||
561 | + webconference = get_object_or_404(Webconference, slug = slug) | ||
562 | + | ||
563 | + if not has_subject_permissions(request.user, webconference.topic.subject): | ||
564 | + return redirect(reverse_lazy('subjects:home')) | ||
565 | + | ||
566 | + return super(StatisticsView, self).dispatch(request, *args, **kwargs) | ||
567 | + | ||
568 | + def get_context_data(self, **kwargs): | ||
569 | + context = super(StatisticsView, self).get_context_data(**kwargs) | ||
570 | + | ||
571 | + self.log_context['category_id'] = self.object.topic.subject.category.id | ||
572 | + self.log_context['category_name'] = self.object.topic.subject.category.name | ||
573 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | ||
574 | + self.log_context['subject_id'] = self.object.topic.subject.id | ||
575 | + self.log_context['subject_name'] = self.object.topic.subject.name | ||
576 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | ||
577 | + self.log_context['topic_id'] = self.object.topic.id | ||
578 | + self.log_context['topic_name'] = self.object.topic.name | ||
579 | + self.log_context['topic_slug'] = self.object.topic.slug | ||
580 | + self.log_context['webconference_id'] = self.object.id | ||
581 | + self.log_context['webconference_name'] = self.object.name | ||
582 | + self.log_context['webconference_slug'] = self.object.slug | ||
583 | + | ||
584 | + super(StatisticsView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
585 | + | ||
586 | + | ||
587 | + context['title'] = _('Webconference Reports') | ||
588 | + | ||
589 | + slug = self.kwargs.get('slug') | ||
590 | + webconference = get_object_or_404(Webconference, slug = slug) | ||
591 | + | ||
592 | + date_format = "%d/%m/%Y %H:%M" if self.request.GET.get('language','') == 'pt-br' else "%m/%d/%Y %I:%M %p" | ||
593 | + if self.request.GET.get('language','') == "": | ||
594 | + start_date = datetime.datetime.now() - datetime.timedelta(30) | ||
595 | + end_date = datetime.datetime.now() | ||
596 | + else : | ||
597 | + start_date = datetime.datetime.strptime(self.request.GET.get('init_date',''),date_format) | ||
598 | + end_date = datetime.datetime.strptime(self.request.GET.get('end_date',''),date_format) | ||
599 | + context["init_date"] = start_date | ||
600 | + context["end_date"] = end_date | ||
601 | + alunos = webconference.students.all() | ||
602 | + if webconference.all_students : | ||
603 | + alunos = webconference.topic.subject.students.all() | ||
604 | + | ||
605 | + vis_ou = Log.objects.filter(context__contains={'webconference_id':webconference.id},resource="webconference",action="view",user_email__in=(aluno.email for aluno in alunos), datetime__range=(start_date,end_date + datetime.timedelta(minutes = 1))) | ||
606 | + did,n_did,history = str(_("Realized")),str(_("Unrealized")),str(_("Historic")) | ||
607 | + re = [] | ||
608 | + data_n_did,data_history = [],[] | ||
609 | + json_n_did, json_history = {},{} | ||
610 | + | ||
611 | + for log_al in vis_ou.order_by("datetime"): | ||
612 | + data_history.append([str(alunos.get(email=log_al.user_email)), | ||
613 | + ", ".join([str(x) for x in webconference.topic.subject.group_subject.filter(participants__email=log_al.user_email)]), | ||
614 | + log_al.action,log_al.datetime]) | ||
615 | + | ||
616 | + json_history["data"] = data_history | ||
617 | + | ||
618 | + not_view = alunos.exclude(email__in=[log.user_email for log in vis_ou.distinct("user_email")]) | ||
619 | + index = 0 | ||
620 | + for alun in not_view: | ||
621 | + data_n_did.append([index,str(alun),", ".join([str(x) for x in webconference.topic.subject.group_subject.filter(participants__email=alun.email)]),str(_('View')), str(alun.email)]) | ||
622 | + index += 1 | ||
623 | + json_n_did["data"] = data_n_did | ||
624 | + | ||
625 | + | ||
626 | + context["json_n_did"] = json_n_did | ||
627 | + context["json_history"] = json_history | ||
628 | + c_visualizou = vis_ou.distinct("user_email").count() | ||
629 | + column_view = str(_('View')) | ||
630 | + re.append([str(_('File link')),did,n_did]) | ||
631 | + re.append([column_view,c_visualizou, alunos.count() - c_visualizou]) | ||
632 | + context['topic'] = webconference.topic | ||
633 | + context['subject'] = webconference.topic.subject | ||
634 | + context['db_data'] = re | ||
635 | + context['title_chart'] = _('Actions about resource') | ||
636 | + context['title_vAxis'] = _('Quantity') | ||
637 | + context['view'] = column_view | ||
638 | + context["n_did_table"] = n_did | ||
639 | + context["did_table"] = did | ||
640 | + context["history_table"] = history | ||
641 | + return context | ||
642 | + | ||
643 | +from django.http import HttpResponse #used to send HTTP 404 error to ajax | ||
644 | + | ||
645 | +class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.FormView): | ||
646 | + log_component = 'resources' | ||
647 | + log_action = 'send' | ||
648 | + log_resource = 'webconference' | ||
649 | + log_context = {} | ||
650 | + | ||
651 | + login_url = reverse_lazy("users:login") | ||
652 | + redirect_field_name = 'next' | ||
653 | + | ||
654 | + template_name = 'webconference/send_message.html' | ||
655 | + form_class = FormModalMessage | ||
656 | + | ||
657 | + def dispatch(self, request, *args, **kwargs): | ||
658 | + slug = self.kwargs.get('slug', '') | ||
659 | + webconference = get_object_or_404(Webconference, slug = slug) | ||
660 | + self.webconference = webconference | ||
661 | + | ||
662 | + if not has_subject_permissions(request.user, webconference.topic.subject): | ||
663 | + return redirect(reverse_lazy('subjects:home')) | ||
664 | + | ||
665 | + return super(SendMessage, self).dispatch(request, *args, **kwargs) | ||
666 | + | ||
667 | + def form_valid(self, form): | ||
668 | + message = form.cleaned_data.get('comment') | ||
669 | + image = form.cleaned_data.get("image") | ||
670 | + users = (self.request.POST.get('users[]','')).split(",") | ||
671 | + user = self.request.user | ||
672 | + subject = self.webconference.topic.subject | ||
673 | + | ||
674 | + if (users[0] is not ''): | ||
675 | + for u in users: | ||
676 | + to_user = User.objects.get(email=u) | ||
677 | + talk, create = Conversation.objects.get_or_create(user_one=user,user_two=to_user) | ||
678 | + created = TalkMessages.objects.create(text=message,talk=talk,user=user,subject=subject,image=image) | ||
679 | + | ||
680 | + simple_notify = textwrap.shorten(strip_tags(message), width = 30, placeholder = "...") | ||
681 | + | ||
682 | + if image is not '': | ||
683 | + simple_notify += " ".join(_("[Photo]")) | ||
684 | + | ||
685 | + notification = { | ||
686 | + "type": "chat", | ||
687 | + "subtype": "subject", | ||
688 | + "space": subject.slug, | ||
689 | + "user_icon": created.user.image_url, | ||
690 | + "notify_title": str(created.user), | ||
691 | + "simple_notify": simple_notify, | ||
692 | + "view_url": reverse("chat:view_message", args = (created.id, ), kwargs = {}), | ||
693 | + "complete": render_to_string("chat/_message.html", {"talk_msg": created}, self.request), | ||
694 | + "container": "chat-" + str(created.user.id), | ||
695 | + "last_date": _("Last message in %s")%(formats.date_format(created.create_date, "SHORT_DATETIME_FORMAT")) | ||
696 | + } | ||
697 | + | ||
698 | + notification = json.dumps(notification) | ||
699 | + | ||
700 | + Group("user-%s" % to_user.id).send({'text': notification}) | ||
701 | + | ||
702 | + ChatVisualizations.objects.create(viewed = False, message = created, user = to_user) | ||
703 | + | ||
704 | + success = str(_('The message was successfull sent!')) | ||
705 | + return JsonResponse({"message":success}) | ||
706 | + erro = HttpResponse(str(_("No user selected!"))) | ||
707 | + erro.status_code = 404 | ||
708 | + return erro | ||
709 | + | ||
710 | + def get_context_data(self, **kwargs): | ||
711 | + context = super(SendMessage,self).get_context_data() | ||
712 | + context["webconference"] = get_object_or_404(Webconference, slug=self.kwargs.get('slug', '')) | ||
713 | + return context |