Commit 8cd8a2fdd94a65de6955069bd21b2f23de996abd
1 parent
36f1bc35
Exists in
master
and in
3 other branches
Adjusting subject count
Showing
5 changed files
with
25 additions
and
8 deletions
Show diff stats
subjects/templates/subjects/list.html
1 | 1 | {% extends 'categories/home.html' %} |
2 | 2 | |
3 | 3 | {% load static i18n pagination %} |
4 | -{% load django_bootstrap_breadcrumbs %} | |
4 | +{% load django_bootstrap_breadcrumbs subject_counter %} | |
5 | 5 | |
6 | 6 | {% block javascript%} |
7 | 7 | {{ block.super }} |
... | ... | @@ -55,7 +55,7 @@ |
55 | 55 | <div class="col-md-12 category-header"> |
56 | 56 | <h4 class="panel-title"> |
57 | 57 | <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> |
58 | - <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> {{ category.name }} ({{ category.subject_category.count }}) | |
58 | + <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> {{ category.name }} ({{ category|subject_count:user }}) | |
59 | 59 | </a> |
60 | 60 | </h4> |
61 | 61 | |
... | ... | @@ -101,7 +101,6 @@ |
101 | 101 | <div class="panel-group subject-group" id="{{ category.slug }}-accordion" role="tablist" aria-multiselectable="true"> |
102 | 102 | {% for subject in category.subject_category.all %} |
103 | 103 | {% 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 %} |
104 | - | |
105 | 104 | {% include "subjects/subject_card.html" %} |
106 | 105 | {% endif %} |
107 | 106 | {% endfor %} |
... | ... | @@ -115,7 +114,7 @@ |
115 | 114 | <div class="col-md-12 category-header"> |
116 | 115 | <h4 class="panel-title"> |
117 | 116 | <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> |
118 | - <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> {{category.name}} ({{ category.subject_category.count }}) | |
117 | + <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button> {{category.name}} ({{ category|subject_count:user }}) | |
119 | 118 | </a> |
120 | 119 | </h4> |
121 | 120 | ... | ... |
... | ... | @@ -0,0 +1,13 @@ |
1 | +from django import template | |
2 | + | |
3 | +register = template.Library() | |
4 | + | |
5 | +@register.filter(name = 'subject_count') | |
6 | +def subject_count(category, user): | |
7 | + total = 0 | |
8 | + | |
9 | + for subject in category.subject_category.all(): | |
10 | + if user in subject.students.all() or user in subject.professor.all() or user in subject.category.coordinators.all(): | |
11 | + total += 1 | |
12 | + | |
13 | + return total | |
0 | 14 | \ No newline at end of file | ... | ... |
subjects/utils.py
... | ... | @@ -14,10 +14,15 @@ def has_professor_profile(user, category): |
14 | 14 | |
15 | 15 | return False |
16 | 16 | |
17 | -def count_subjects(categories): | |
17 | +def count_subjects(categories, user, all_subs = True): | |
18 | 18 | total = 0 |
19 | 19 | |
20 | 20 | for category in categories: |
21 | - total += category.subject_category.count() | |
21 | + if not all_subs: | |
22 | + for subject in category.subject_category.all(): | |
23 | + if user in subject.students.all() or user in subject.professor.all() or user in subject.category.coordinators.all(): | |
24 | + total += 1 | |
25 | + else: | |
26 | + total += category.subject_category.count() | |
22 | 27 | |
23 | 28 | return total |
24 | 29 | \ No newline at end of file | ... | ... |
subjects/views.py
... | ... | @@ -68,7 +68,7 @@ class IndexView(LoginRequiredMixin, ListView): |
68 | 68 | def get_queryset(self): |
69 | 69 | categories = Category.objects.all().order_by('name') |
70 | 70 | |
71 | - self.totals['all_subjects'] = count_subjects(categories) | |
71 | + self.totals['all_subjects'] = count_subjects(categories, self.request.user) | |
72 | 72 | self.totals['my_subjects'] = self.totals['all_subjects'] |
73 | 73 | |
74 | 74 | if not self.request.user.is_staff: |
... | ... | @@ -76,7 +76,7 @@ class IndexView(LoginRequiredMixin, ListView): |
76 | 76 | or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] |
77 | 77 | #So I remove all categories that doesn't have the possibility for the user to be on |
78 | 78 | |
79 | - self.totals['my_subjects'] = count_subjects(my_categories) | |
79 | + self.totals['my_subjects'] = count_subjects(my_categories, self.request.user, False) | |
80 | 80 | |
81 | 81 | if not self.kwargs.get('option'): |
82 | 82 | categories = my_categories | ... | ... |