diff --git a/subjects/utils.py b/subjects/utils.py index c2532c8..f97b645 100644 --- a/subjects/utils.py +++ b/subjects/utils.py @@ -1,4 +1,8 @@ + # File used to store useful subject functions # +from categories.models import Category +from .models import Subject +from django.db.models import Q def has_student_profile(user, category): for subject in category.subject_category.all(): @@ -14,15 +18,20 @@ def has_professor_profile(user, category): return False -def count_subjects(categories, user, all_subs = True): +def count_subjects( user, all_subs = True): total = 0 + pk = user.pk - for category in categories: + """for category in categories: 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() - + total += category.subject_category.count()""" + if all_subs: + total += Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).distinct().count() + else: + + total = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct().count() return total \ No newline at end of file diff --git a/subjects/views.py b/subjects/views.py index a42ca2d..d55bdf8 100644 --- a/subjects/views.py +++ b/subjects/views.py @@ -68,19 +68,27 @@ class IndexView(LoginRequiredMixin, ListView): paginate_by = 10 def get_queryset(self): - categories = Category.objects.all().order_by('name') - categories = [category for category in categories if self.request.user.is_staff or self.request.user in category.coordinators.all() or category.visible] - - self.totals['all_subjects'] = count_subjects(categories, self.request.user) + + if self.request.user.is_staff: + categories = Category.objects.all().order_by('name') + else: + pk = self.request.user.pk + + categories = Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).order_by('name') + + self.totals['all_subjects'] = count_subjects(self.request.user) + self.totals['my_subjects'] = self.totals['all_subjects'] if not self.request.user.is_staff: + + #my_categories = Category.objects.filter(Q(coordinators__pk=pk) | Q(subject_professor__pk=pk) | Q()) my_categories = [category for category in categories if self.request.user in category.coordinators.all() \ 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.request.user, False) - + + self.totals['my_subjects'] = count_subjects(self.request.user, False) + if not self.kwargs.get('option'): categories = my_categories -- libgit2 0.21.2