Commit 8cd8a2fdd94a65de6955069bd21b2f23de996abd

Authored by Zambom
1 parent 36f1bc35

Adjusting subject count

subjects/templates/subjects/list.html
1 {% extends 'categories/home.html' %} 1 {% extends 'categories/home.html' %}
2 2
3 {% load static i18n pagination %} 3 {% load static i18n pagination %}
4 -{% load django_bootstrap_breadcrumbs %} 4 +{% load django_bootstrap_breadcrumbs subject_counter %}
5 5
6 {% block javascript%} 6 {% block javascript%}
7 {{ block.super }} 7 {{ block.super }}
@@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
55 <div class="col-md-12 category-header"> 55 <div class="col-md-12 category-header">
56 <h4 class="panel-title"> 56 <h4 class="panel-title">
57 <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> 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 </a> 59 </a>
60 </h4> 60 </h4>
61 61
@@ -101,7 +101,6 @@ @@ -101,7 +101,6 @@
101 <div class="panel-group subject-group" id="{{ category.slug }}-accordion" role="tablist" aria-multiselectable="true"> 101 <div class="panel-group subject-group" id="{{ category.slug }}-accordion" role="tablist" aria-multiselectable="true">
102 {% for subject in category.subject_category.all %} 102 {% for subject in category.subject_category.all %}
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 %} 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 {% include "subjects/subject_card.html" %} 104 {% include "subjects/subject_card.html" %}
106 {% endif %} 105 {% endif %}
107 {% endfor %} 106 {% endfor %}
@@ -115,7 +114,7 @@ @@ -115,7 +114,7 @@
115 <div class="col-md-12 category-header"> 114 <div class="col-md-12 category-header">
116 <h4 class="panel-title"> 115 <h4 class="panel-title">
117 <a class="category-course-link pull-left" data-parent="#accordion" data-toggle="collapse" href="#{{category.slug}}"> 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 </a> 118 </a>
120 </h4> 119 </h4>
121 120
subjects/templatetags/__init__.py 0 → 100644
subjects/templatetags/subject_counter.py 0 → 100644
@@ -0,0 +1,13 @@ @@ -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 \ No newline at end of file 14 \ No newline at end of file
subjects/utils.py
@@ -14,10 +14,15 @@ def has_professor_profile(user, category): @@ -14,10 +14,15 @@ def has_professor_profile(user, category):
14 14
15 return False 15 return False
16 16
17 -def count_subjects(categories): 17 +def count_subjects(categories, user, all_subs = True):
18 total = 0 18 total = 0
19 19
20 for category in categories: 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 return total 28 return total
24 \ No newline at end of file 29 \ No newline at end of file
subjects/views.py
@@ -68,7 +68,7 @@ class IndexView(LoginRequiredMixin, ListView): @@ -68,7 +68,7 @@ class IndexView(LoginRequiredMixin, ListView):
68 def get_queryset(self): 68 def get_queryset(self):
69 categories = Category.objects.all().order_by('name') 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 self.totals['my_subjects'] = self.totals['all_subjects'] 72 self.totals['my_subjects'] = self.totals['all_subjects']
73 73
74 if not self.request.user.is_staff: 74 if not self.request.user.is_staff:
@@ -76,7 +76,7 @@ class IndexView(LoginRequiredMixin, ListView): @@ -76,7 +76,7 @@ class IndexView(LoginRequiredMixin, ListView):
76 or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] 76 or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)]
77 #So I remove all categories that doesn't have the possibility for the user to be on 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 if not self.kwargs.get('option'): 81 if not self.kwargs.get('option'):
82 categories = my_categories 82 categories = my_categories