Commit 1313a7d8115836d84c112a8e617636ef927efb72
1 parent
35b73722
Exists in
master
and in
3 other branches
Adjusting my subjects list
Showing
2 changed files
with
18 additions
and
4 deletions
Show diff stats
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +# File used to store useful subject functions # | ||
2 | + | ||
3 | +def has_student_profile(user, category): | ||
4 | + for subject in category.subject_category.all(): | ||
5 | + if user in subject.students.all(): | ||
6 | + return True | ||
7 | + | ||
8 | + return False | ||
9 | + | ||
10 | +def has_professor_profile(user, category): | ||
11 | + for subject in category.subject_category.all(): | ||
12 | + if user in subject.professor.all(): | ||
13 | + return True | ||
14 | + | ||
15 | + return False | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |
subjects/views.py
@@ -25,6 +25,7 @@ from .models import Tag | @@ -25,6 +25,7 @@ from .models import Tag | ||
25 | import time | 25 | import time |
26 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | 26 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
27 | from .forms import CreateSubjectForm | 27 | from .forms import CreateSubjectForm |
28 | +from .utils import has_student_profile, has_professor_profile | ||
28 | from users.models import User | 29 | from users.models import User |
29 | 30 | ||
30 | 31 | ||
@@ -75,10 +76,8 @@ class IndexView(LoginRequiredMixin, ListView): | @@ -75,10 +76,8 @@ class IndexView(LoginRequiredMixin, ListView): | ||
75 | if not self.kwargs.get('option'): | 76 | if not self.kwargs.get('option'): |
76 | categories = Category.objects.all() | 77 | categories = Category.objects.all() |
77 | 78 | ||
78 | - for category in categories: | ||
79 | - category.subjects = Subject.objects.filter(category= category) | ||
80 | - | ||
81 | - categories = [category for category in categories if category.subjects.count() > 0 or self.request.user in category.coordinators.all()] | 79 | + categories = [category for category in categories if self.request.user in category.coordinators.all() \ |
80 | + or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] | ||
82 | #So I remove all categories that doesn't have the possibility for the user to be on | 81 | #So I remove all categories that doesn't have the possibility for the user to be on |
83 | 82 | ||
84 | return categories | 83 | return categories |