Commit 9b2182dd88febc3ba7be8e014eb99c64995f0c23

Authored by Felipe Henrique de Almeida Bormann
1 parent 8a1d0aaa

there is still a query to fix but I fixed most speed problems

Showing 2 changed files with 28 additions and 11 deletions   Show diff stats
subjects/utils.py
  1 +
1 # File used to store useful subject functions # 2 # File used to store useful subject functions #
  3 +from categories.models import Category
  4 +from .models import Subject
  5 +from django.db.models import Q
2 6
3 def has_student_profile(user, category): 7 def has_student_profile(user, category):
4 for subject in category.subject_category.all(): 8 for subject in category.subject_category.all():
@@ -14,15 +18,20 @@ def has_professor_profile(user, category): @@ -14,15 +18,20 @@ def has_professor_profile(user, category):
14 18
15 return False 19 return False
16 20
17 -def count_subjects(categories, user, all_subs = True): 21 +def count_subjects( user, all_subs = True):
18 total = 0 22 total = 0
  23 + pk = user.pk
19 24
20 - for category in categories: 25 + """for category in categories:
21 if not all_subs: 26 if not all_subs:
22 for subject in category.subject_category.all(): 27 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(): 28 if user in subject.students.all() or user in subject.professor.all() or user in subject.category.coordinators.all():
24 total += 1 29 total += 1
25 else: 30 else:
26 - total += category.subject_category.count()  
27 - 31 + total += category.subject_category.count()"""
  32 + if all_subs:
  33 + total += Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).distinct().count()
  34 + else:
  35 +
  36 + total = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct().count()
28 return total 37 return total
29 \ No newline at end of file 38 \ No newline at end of file
subjects/views.py
@@ -68,19 +68,27 @@ class IndexView(LoginRequiredMixin, ListView): @@ -68,19 +68,27 @@ class IndexView(LoginRequiredMixin, ListView):
68 paginate_by = 10 68 paginate_by = 10
69 69
70 def get_queryset(self): 70 def get_queryset(self):
71 - categories = Category.objects.all().order_by('name')  
72 - categories = [category for category in categories if self.request.user.is_staff or self.request.user in category.coordinators.all() or category.visible]  
73 -  
74 - self.totals['all_subjects'] = count_subjects(categories, self.request.user) 71 +
  72 + if self.request.user.is_staff:
  73 + categories = Category.objects.all().order_by('name')
  74 + else:
  75 + pk = self.request.user.pk
  76 +
  77 + categories = Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).order_by('name')
  78 +
  79 + self.totals['all_subjects'] = count_subjects(self.request.user)
  80 +
75 self.totals['my_subjects'] = self.totals['all_subjects'] 81 self.totals['my_subjects'] = self.totals['all_subjects']
76 82
77 if not self.request.user.is_staff: 83 if not self.request.user.is_staff:
  84 +
  85 + #my_categories = Category.objects.filter(Q(coordinators__pk=pk) | Q(subject_professor__pk=pk) | Q())
78 my_categories = [category for category in categories if self.request.user in category.coordinators.all() \ 86 my_categories = [category for category in categories if self.request.user in category.coordinators.all() \
79 or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] 87 or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)]
80 #So I remove all categories that doesn't have the possibility for the user to be on 88 #So I remove all categories that doesn't have the possibility for the user to be on
81 -  
82 - self.totals['my_subjects'] = count_subjects(my_categories, self.request.user, False)  
83 - 89 +
  90 + self.totals['my_subjects'] = count_subjects(self.request.user, False)
  91 +
84 if not self.kwargs.get('option'): 92 if not self.kwargs.get('option'):
85 categories = my_categories 93 categories = my_categories
86 94