From 8cd8a2fdd94a65de6955069bd21b2f23de996abd Mon Sep 17 00:00:00 2001 From: Zambom Date: Fri, 13 Jan 2017 16:58:40 -0200 Subject: [PATCH] Adjusting subject count --- subjects/templates/subjects/list.html | 7 +++---- subjects/templatetags/__init__.py | 0 subjects/templatetags/subject_counter.py | 13 +++++++++++++ subjects/utils.py | 9 +++++++-- subjects/views.py | 4 ++-- 5 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 subjects/templatetags/__init__.py create mode 100644 subjects/templatetags/subject_counter.py diff --git a/subjects/templates/subjects/list.html b/subjects/templates/subjects/list.html index cda3372..8dc1d4f 100644 --- a/subjects/templates/subjects/list.html +++ b/subjects/templates/subjects/list.html @@ -1,7 +1,7 @@ {% extends 'categories/home.html' %} {% load static i18n pagination %} -{% load django_bootstrap_breadcrumbs %} +{% load django_bootstrap_breadcrumbs subject_counter %} {% block javascript%} {{ block.super }} @@ -55,7 +55,7 @@

- {{ category.name }} ({{ category.subject_category.count }}) + {{ category.name }} ({{ category|subject_count:user }})

@@ -101,7 +101,6 @@
{% for subject in category.subject_category.all %} {% if request.user in subject.students.all or request.user.is_staff or request.user in subject.professor.all or all or request.user in subject.category.coordinators.all %} - {% include "subjects/subject_card.html" %} {% endif %} {% endfor %} @@ -115,7 +114,7 @@

- {{category.name}} ({{ category.subject_category.count }}) + {{category.name}} ({{ category|subject_count:user }})

diff --git a/subjects/templatetags/__init__.py b/subjects/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/subjects/templatetags/__init__.py diff --git a/subjects/templatetags/subject_counter.py b/subjects/templatetags/subject_counter.py new file mode 100644 index 0000000..1d4e59a --- /dev/null +++ b/subjects/templatetags/subject_counter.py @@ -0,0 +1,13 @@ +from django import template + +register = template.Library() + +@register.filter(name = 'subject_count') +def subject_count(category, user): + total = 0 + + for subject in category.subject_category.all(): + if user in subject.students.all() or user in subject.professor.all() or user in subject.category.coordinators.all(): + total += 1 + + return total \ No newline at end of file diff --git a/subjects/utils.py b/subjects/utils.py index 29bc98f..4a984b3 100644 --- a/subjects/utils.py +++ b/subjects/utils.py @@ -14,10 +14,15 @@ def has_professor_profile(user, category): return False -def count_subjects(categories): +def count_subjects(categories, user, all_subs = True): total = 0 for category in categories: - total += category.subject_category.count() + if not all_subs: + for subject in category.subject_category.all(): + if user in subject.students.all() or user in subject.professor.all() or user in subject.category.coordinators.all(): + total += 1 + else: + total += category.subject_category.count() return total \ No newline at end of file diff --git a/subjects/views.py b/subjects/views.py index 5dba0bf..ea54293 100644 --- a/subjects/views.py +++ b/subjects/views.py @@ -68,7 +68,7 @@ class IndexView(LoginRequiredMixin, ListView): def get_queryset(self): categories = Category.objects.all().order_by('name') - self.totals['all_subjects'] = count_subjects(categories) + self.totals['all_subjects'] = count_subjects(categories, self.request.user) self.totals['my_subjects'] = self.totals['all_subjects'] if not self.request.user.is_staff: @@ -76,7 +76,7 @@ class IndexView(LoginRequiredMixin, ListView): or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] #So I remove all categories that doesn't have the possibility for the user to be on - self.totals['my_subjects'] = count_subjects(my_categories) + self.totals['my_subjects'] = count_subjects(my_categories, self.request.user, False) if not self.kwargs.get('option'): categories = my_categories -- libgit2 0.21.2