From 1e152bf2b70a24f0c925dc52f66230e897c72ed5 Mon Sep 17 00:00:00 2001 From: Zambom Date: Wed, 8 Mar 2017 23:29:23 -0300 Subject: [PATCH] Adding goals history report --- amadeus/static/js/goals_reports.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ goals/templates/goals/_history.html | 40 ++++++++++++++++++++++++++++++++++++++++ goals/templates/goals/reports.html | 15 +++++++-------- goals/templatetags/goals_filters.py | 36 +++++++++++++++++++++++++++++++++++- goals/urls.py | 1 + goals/views.py | 36 ++++++++++++++++++++++++++++++++++-- 6 files changed, 173 insertions(+), 11 deletions(-) create mode 100644 goals/templates/goals/_history.html diff --git a/amadeus/static/js/goals_reports.js b/amadeus/static/js/goals_reports.js index 9dd7b0a..c99a4fe 100644 --- a/amadeus/static/js/goals_reports.js +++ b/amadeus/static/js/goals_reports.js @@ -46,6 +46,9 @@ function getAnswered() { container.find('.unanswered_link').removeClass('active'); container.find('.unanswered').hide(); + container.find('.history_link').removeClass('active'); + container.find('.history').hide(); + setBreadcrumb(answeredBread); } @@ -106,9 +109,62 @@ function getUnanswered() { container.find('.unanswered_link').addClass('active'); container.find('.unanswered').show(); + container.find('.history_link').removeClass('active'); + container.find('.history').hide(); + setBreadcrumb(unansweredBread); } +function getHistory() { + var container = $("#reports"), + list = container.find('.history_data'); + + if (list.children().length == 0) { + var url = list.parent().data('url'); + + $.ajax({ + url: url, + success: function (data) { + list.html(data); + + $('#history_table').DataTable({ + "dom": "Bfrtip", + "language": dataTablei18n, + buttons: { + dom: { + container: { + className: 'col-md-3' + }, + buttonContainer: { + tag: 'h4', + className: 'history-header' + }, + }, + buttons: [ + { + extend: 'csv', + text: csvBtnLabeli18n, + filename: 'report-history' + } + ], + }, + }); + } + }); + } + + container.find('.answered_link').removeClass('active'); + container.find('.answered').hide(); + + container.find('.unanswered_link').removeClass('active'); + container.find('.unanswered').hide(); + + container.find('.history_link').addClass('active'); + container.find('.history').show(); + + setBreadcrumb(historyBread); +} + function setBreadcrumb(text) { var breadcrumb = $(".breadcrumb")[0], li = $(breadcrumb).find('li:last-child'); diff --git a/goals/templates/goals/_history.html b/goals/templates/goals/_history.html new file mode 100644 index 0000000..c54927e --- /dev/null +++ b/goals/templates/goals/_history.html @@ -0,0 +1,40 @@ +{% load i18n goals_filters %} + +
+
+ + + + + + + + + + + {% for row in records %} + + + + + + + + + {% endfor %} + +
+ {% trans 'Date/Hour' %} + + {% trans 'Student' %} + + {% trans 'Group' %} + + {% trans 'Action' %} + + {% trans 'Object' %} + + {% trans 'Resource' %} +
{{ row.datetime }}{{ row.user }}{{ row.user_id|log_groups }}{{ row.action|log_action }}{{ row|log_object }}{{ row.context.goals_name }}
+
+
\ No newline at end of file diff --git a/goals/templates/goals/reports.html b/goals/templates/goals/reports.html index 25ba529..1cdef92 100644 --- a/goals/templates/goals/reports.html +++ b/goals/templates/goals/reports.html @@ -56,22 +56,20 @@
-
-
-
-
-
-
+
+ +
+
@@ -104,7 +102,8 @@ var csvBtnLabeli18n = " {% trans 'Download .csv file' %}", answeredBread = "{% trans 'Reports: Answered' %}", - unansweredBread = "{% trans 'Reports: Unanswered' %}"; + unansweredBread = "{% trans 'Reports: Unanswered' %}", + historyBread = "{% trans 'Reports: History' %}"; {% endblock %} \ No newline at end of file diff --git a/goals/templatetags/goals_filters.py b/goals/templatetags/goals_filters.py index ab70449..9990116 100644 --- a/goals/templatetags/goals_filters.py +++ b/goals/templatetags/goals_filters.py @@ -3,6 +3,7 @@ from django.utils.translation import ugettext_lazy as _ from goals.models import MyGoals from log.models import Log +from students_group.models import StudentsGroup register = template.Library() @@ -17,6 +18,17 @@ def groups(user): else: return "---" +@register.filter(name = 'log_groups') +def log_groups(user_id): + groups = StudentsGroup.objects.filter(participants__id = user_id).values_list('name', flat = True) + + if groups.count() > 0: + groups = list(groups) + + return ", ".join(groups) + else: + return "---" + @register.filter(name = 'creation_date') def creation_date(user, goal): log = Log.objects.filter(user_id = user.id, action = 'submit', resource = 'goals', context__contains = {"goals_id": goal.id}) @@ -39,4 +51,26 @@ def update_date(user, goal): def my_goals(user, goal): mine = list(MyGoals.objects.filter(user = user, item__goal = goal).values_list('value', flat = True)) - return ', '.join(str(x) for x in mine) \ No newline at end of file + return ', '.join(str(x) for x in mine) + +@register.filter(name = 'log_action') +def log_action(action): + if action == 'view': + return _('Visualized') + elif action == 'create': + return _('Added') + elif action == 'update': + return _('Updated') + elif action == 'submit': + return _('Submitted') + + return '---' + +@register.filter(name = 'log_object') +def log_object(log): + if log.resource == 'my_goals': + return _('My Goals') + + name = log.context['goals_name'] + + return _("%s Instance")%(name) \ No newline at end of file diff --git a/goals/urls.py b/goals/urls.py index fd93433..3daac48 100644 --- a/goals/urls.py +++ b/goals/urls.py @@ -14,4 +14,5 @@ urlpatterns = [ url(r'^reports/(?P[\w_-]+)/$', views.Reports.as_view(), name = 'reports'), url(r'^answered_report/(?P[\w_-]+)/$', views.AnsweredReport.as_view(), name = 'answered_report'), url(r'^unanswered_report/(?P[\w_-]+)/$', views.UnansweredReport.as_view(), name = 'unanswered_report'), + url(r'^history_report/(?P[\w_-]+)/$', views.HistoryReport.as_view(), name = 'history_report'), ] diff --git a/goals/views.py b/goals/views.py index 4e4b201..8db4edd 100644 --- a/goals/views.py +++ b/goals/views.py @@ -71,7 +71,6 @@ class AnsweredReport(LoginRequiredMixin, generic.ListView): redirect_field_name = 'next' template_name = 'goals/_answered.html' - model = MyGoals context_object_name = 'students' def get_queryset(self): @@ -110,7 +109,6 @@ class UnansweredReport(LoginRequiredMixin, generic.ListView): redirect_field_name = 'next' template_name = 'goals/_unanswered.html' - model = MyGoals context_object_name = 'students' def get_queryset(self): @@ -146,6 +144,40 @@ class UnansweredReport(LoginRequiredMixin, generic.ListView): return context +class HistoryReport(LoginRequiredMixin, generic.ListView): + login_url = reverse_lazy("users:login") + redirect_field_name = 'next' + + template_name = 'goals/_history.html' + context_object_name = 'records' + + def get_queryset(self): + slug = self.kwargs.get('slug', '') + goal = get_object_or_404(Goals, slug = slug) + + rows = Log.objects.filter(context__contains = {"goals_id": goal.id}) + + return rows + + def dispatch(self, request, *args, **kwargs): + slug = self.kwargs.get('slug', '') + goals = get_object_or_404(Goals, slug = slug) + + if not has_resource_permissions(request.user, goals): + return redirect(reverse_lazy('subjects:home')) + + return super(HistoryReport, self).dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = super(HistoryReport, self).get_context_data(**kwargs) + + slug = self.kwargs.get('slug', '') + goals = get_object_or_404(Goals, slug = slug) + + context['goal'] = goals + + return context + class InsideView(LoginRequiredMixin, LogMixin, generic.ListView): log_component = "resources" log_action = "view" -- libgit2 0.21.2