Commit ca7c1550f9d7beb816b5a2d43dfeed205345a9cb
1 parent
2b6e44f7
Exists in
master
and in
3 other branches
finished search subject search constrainsts
Showing
1 changed file
with
575 additions
and
572 deletions
Show diff stats
subjects/views.py
@@ -31,624 +31,627 @@ from users.models import User | @@ -31,624 +31,627 @@ from users.models import User | ||
31 | 31 | ||
32 | 32 | ||
33 | class HomeView(LoginRequiredMixin, ListView): | 33 | class HomeView(LoginRequiredMixin, ListView): |
34 | - login_url = reverse_lazy("users:login") | ||
35 | - redirect_field_name = 'next' | ||
36 | - template_name = 'subjects/initial.html' | ||
37 | - context_object_name = 'subjects' | ||
38 | - paginate_by = 10 | ||
39 | - total = 0 | ||
40 | - | ||
41 | - def get_queryset(self): | ||
42 | - if self.request.user.is_staff: | ||
43 | - subjects = Subject.objects.all().order_by("name") | ||
44 | - else: | ||
45 | - pk = self.request.user.pk | ||
46 | - | ||
47 | - subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct() | ||
48 | - | ||
49 | - self.total = subjects.count() | ||
50 | - | ||
51 | - return subjects | ||
52 | - | ||
53 | - def get_context_data(self, **kwargs): | ||
54 | - context = super(HomeView, self).get_context_data(**kwargs) | ||
55 | - context['title'] = _('Home') | ||
56 | - context['show_buttons'] = True #So it shows subscribe and access buttons | ||
57 | - | ||
58 | - #bringing users | ||
59 | - tag_amount = 50 | ||
60 | - tags = Tag.objects.all() | ||
61 | - tags_list = [] | ||
62 | - for tag in tags: | ||
63 | - if len(tags_list) <= tag_amount: | ||
64 | - tags_list.append((tag.name, Subject.objects.filter(tags__pk = tag.pk).count())) | ||
65 | - tags_list.sort(key= lambda x: x[1], reverse=True) #sort by value | ||
66 | - | ||
67 | - else: | ||
68 | - count = Subject.objects.filter(tags__pk = tag.pk).count() | ||
69 | - if count > tags_list[tag_amount][1]: | ||
70 | - tags_list[tag_amount - 1] = (tag.name, count) | ||
71 | - tags_list.sort(key = lambda x: x[1], reverse=True) | ||
72 | - | ||
73 | - | ||
74 | - i = 0 | ||
75 | - tags = [] | ||
76 | - | ||
77 | - for item in tags_list: | ||
78 | - if i < tag_amount/10: | ||
79 | - tags.append((item[0], 0)) | ||
80 | - elif i < tag_amount/2: | ||
81 | - tags.append((item[0], 1)) | ||
82 | - else: | ||
83 | - tags.append((item[0], 2)) | ||
84 | - i += 1 | ||
85 | - shuffle(tags) | ||
86 | - context['tags'] = tags | ||
87 | - context['total_subs'] = self.total | ||
88 | - | ||
89 | - return context | 34 | + login_url = reverse_lazy("users:login") |
35 | + redirect_field_name = 'next' | ||
36 | + template_name = 'subjects/initial.html' | ||
37 | + context_object_name = 'subjects' | ||
38 | + paginate_by = 10 | ||
39 | + total = 0 | ||
40 | + | ||
41 | + def get_queryset(self): | ||
42 | + if self.request.user.is_staff: | ||
43 | + subjects = Subject.objects.all().order_by("name") | ||
44 | + else: | ||
45 | + pk = self.request.user.pk | ||
46 | + | ||
47 | + subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct() | ||
48 | + | ||
49 | + self.total = subjects.count() | ||
50 | + | ||
51 | + return subjects | ||
52 | + | ||
53 | + def get_context_data(self, **kwargs): | ||
54 | + context = super(HomeView, self).get_context_data(**kwargs) | ||
55 | + context['title'] = _('Home') | ||
56 | + context['show_buttons'] = True #So it shows subscribe and access buttons | ||
57 | + | ||
58 | + #bringing users | ||
59 | + tag_amount = 50 | ||
60 | + tags = Tag.objects.all() | ||
61 | + tags_list = [] | ||
62 | + for tag in tags: | ||
63 | + if len(tags_list) <= tag_amount: | ||
64 | + tags_list.append((tag.name, Subject.objects.filter(tags__pk = tag.pk).count())) | ||
65 | + tags_list.sort(key= lambda x: x[1], reverse=True) #sort by value | ||
66 | + | ||
67 | + else: | ||
68 | + count = Subject.objects.filter(tags__pk = tag.pk).count() | ||
69 | + if count > tags_list[tag_amount][1]: | ||
70 | + tags_list[tag_amount - 1] = (tag.name, count) | ||
71 | + tags_list.sort(key = lambda x: x[1], reverse=True) | ||
72 | + | ||
73 | + | ||
74 | + i = 0 | ||
75 | + tags = [] | ||
76 | + | ||
77 | + for item in tags_list: | ||
78 | + if i < tag_amount/10: | ||
79 | + tags.append((item[0], 0)) | ||
80 | + elif i < tag_amount/2: | ||
81 | + tags.append((item[0], 1)) | ||
82 | + else: | ||
83 | + tags.append((item[0], 2)) | ||
84 | + i += 1 | ||
85 | + shuffle(tags) | ||
86 | + context['tags'] = tags | ||
87 | + context['total_subs'] = self.total | ||
88 | + | ||
89 | + return context | ||
90 | 90 | ||
91 | 91 | ||
92 | class IndexView(LoginRequiredMixin, ListView): | 92 | class IndexView(LoginRequiredMixin, ListView): |
93 | - totals = {} | ||
94 | - | ||
95 | - login_url = reverse_lazy("users:login") | ||
96 | - redirect_field_name = 'next' | ||
97 | - template_name = 'subjects/list.html' | ||
98 | - context_object_name = 'categories' | ||
99 | - paginate_by = 10 | ||
100 | - | ||
101 | - def get_queryset(self): | ||
102 | - self.totals['all_subjects'] = count_subjects(self.request.user) | ||
103 | - | ||
104 | - self.totals['my_subjects'] = self.totals['all_subjects'] | ||
105 | - | ||
106 | - if self.request.user.is_staff: | ||
107 | - categories = Category.objects.all().order_by('name') | ||
108 | - else: | ||
109 | - pk = self.request.user.pk | ||
110 | - | ||
111 | - self.totals['my_subjects'] = count_subjects(self.request.user, False) | ||
112 | - | ||
113 | - if not self.kwargs.get('option'): | ||
114 | - my_categories = Category.objects.filter(Q(coordinators__pk=pk) | Q(subject_category__professor__pk=pk) | Q(subject_category__students__pk = pk, visible = True)).distinct().order_by('name') | ||
115 | - | ||
116 | - categories = my_categories | ||
117 | - else: | ||
118 | - categories = Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).distinct().order_by('name') | ||
119 | - | ||
120 | - #if not self.request.user.is_staff: | ||
121 | - | ||
122 | - #my_categories = [category for category in categories if self.request.user in category.coordinators.all() \ | ||
123 | - #or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] | ||
124 | - #So I remove all categories that doesn't have the possibility for the user to be on | ||
125 | - | ||
126 | - | ||
127 | - return categories | ||
128 | - | ||
129 | - def paginate_queryset(self, queryset, page_size): | ||
130 | - paginator = self.get_paginator( | ||
131 | - queryset, page_size, orphans=self.get_paginate_orphans(), | ||
132 | - allow_empty_first_page=self.get_allow_empty()) | ||
133 | - | ||
134 | - page_kwarg = self.page_kwarg | ||
135 | - | ||
136 | - page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1 | ||
137 | - | ||
138 | - if self.kwargs.get('slug'): | ||
139 | - categories = queryset | ||
140 | - | ||
141 | - paginator = Paginator(categories, self.paginate_by) | ||
142 | - | ||
143 | - page = get_category_page(categories, self.kwargs.get('slug'), self.paginate_by) | ||
144 | - | ||
145 | - try: | ||
146 | - page_number = int(page) | ||
147 | - except ValueError: | ||
148 | - if page == 'last': | ||
149 | - page_number = paginator.num_pages | ||
150 | - else: | ||
151 | - raise Http404(_("Page is not 'last', nor can it be converted to an int.")) | ||
152 | - | ||
153 | - try: | ||
154 | - page = paginator.page(page_number) | ||
155 | - return (paginator, page, page.object_list, page.has_other_pages()) | ||
156 | - except InvalidPage as e: | ||
157 | - raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { | ||
158 | - 'page_number': page_number, | ||
159 | - 'message': str(e) | ||
160 | - }) | ||
161 | - | ||
162 | - def render_to_response(self, context, **response_kwargs): | ||
163 | - if self.request.user.is_staff: | ||
164 | - context['page_template'] = "categories/home_admin_content.html" | ||
165 | - else: | ||
166 | - context['page_template'] = "categories/home_teacher_student.html" | ||
167 | - | ||
168 | - if self.request.is_ajax(): | ||
169 | - if self.request.user.is_staff: | ||
170 | - self.template_name = "categories/home_admin_content.html" | ||
171 | - else: | ||
172 | - self.template_name = "categories/home_teacher_student_content.html" | ||
173 | - | ||
174 | - return self.response_class(request = self.request, template = self.template_name, context = context, using = self.template_engine, **response_kwargs) | ||
175 | - | ||
176 | - def get_context_data(self, **kwargs): | ||
177 | - context = super(IndexView, self).get_context_data(**kwargs) | ||
178 | - | ||
179 | - context['all'] = False | ||
180 | - context['title'] = _('My Subjects') | ||
181 | - | ||
182 | - context['show_buttons'] = True #So it shows subscribe and access buttons | ||
183 | - context['totals'] = self.totals | ||
184 | - | ||
185 | - if self.kwargs.get('option'): | ||
186 | - context['all'] = True | ||
187 | - context['title'] = _('All Subjects') | ||
188 | - | ||
189 | - if self.kwargs.get('slug'): | ||
190 | - context['cat_slug'] = self.kwargs.get('slug') | ||
191 | - | ||
192 | - context['subjects_menu_active'] = 'subjects_menu_active' | ||
193 | - | ||
194 | - return context | 93 | + totals = {} |
94 | + | ||
95 | + login_url = reverse_lazy("users:login") | ||
96 | + redirect_field_name = 'next' | ||
97 | + template_name = 'subjects/list.html' | ||
98 | + context_object_name = 'categories' | ||
99 | + paginate_by = 10 | ||
100 | + | ||
101 | + def get_queryset(self): | ||
102 | + self.totals['all_subjects'] = count_subjects(self.request.user) | ||
103 | + | ||
104 | + self.totals['my_subjects'] = self.totals['all_subjects'] | ||
105 | + | ||
106 | + if self.request.user.is_staff: | ||
107 | + categories = Category.objects.all().order_by('name') | ||
108 | + else: | ||
109 | + pk = self.request.user.pk | ||
110 | + | ||
111 | + self.totals['my_subjects'] = count_subjects(self.request.user, False) | ||
112 | + | ||
113 | + if not self.kwargs.get('option'): | ||
114 | + my_categories = Category.objects.filter(Q(coordinators__pk=pk) | Q(subject_category__professor__pk=pk) | Q(subject_category__students__pk = pk, visible = True)).distinct().order_by('name') | ||
115 | + | ||
116 | + categories = my_categories | ||
117 | + else: | ||
118 | + categories = Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).distinct().order_by('name') | ||
119 | + | ||
120 | + #if not self.request.user.is_staff: | ||
121 | + | ||
122 | + #my_categories = [category for category in categories if self.request.user in category.coordinators.all() \ | ||
123 | + #or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] | ||
124 | + #So I remove all categories that doesn't have the possibility for the user to be on | ||
125 | + | ||
126 | + | ||
127 | + return categories | ||
128 | + | ||
129 | + def paginate_queryset(self, queryset, page_size): | ||
130 | + paginator = self.get_paginator( | ||
131 | + queryset, page_size, orphans=self.get_paginate_orphans(), | ||
132 | + allow_empty_first_page=self.get_allow_empty()) | ||
133 | + | ||
134 | + page_kwarg = self.page_kwarg | ||
135 | + | ||
136 | + page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1 | ||
137 | + | ||
138 | + if self.kwargs.get('slug'): | ||
139 | + categories = queryset | ||
140 | + | ||
141 | + paginator = Paginator(categories, self.paginate_by) | ||
142 | + | ||
143 | + page = get_category_page(categories, self.kwargs.get('slug'), self.paginate_by) | ||
144 | + | ||
145 | + try: | ||
146 | + page_number = int(page) | ||
147 | + except ValueError: | ||
148 | + if page == 'last': | ||
149 | + page_number = paginator.num_pages | ||
150 | + else: | ||
151 | + raise Http404(_("Page is not 'last', nor can it be converted to an int.")) | ||
152 | + | ||
153 | + try: | ||
154 | + page = paginator.page(page_number) | ||
155 | + return (paginator, page, page.object_list, page.has_other_pages()) | ||
156 | + except InvalidPage as e: | ||
157 | + raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { | ||
158 | + 'page_number': page_number, | ||
159 | + 'message': str(e) | ||
160 | + }) | ||
161 | + | ||
162 | + def render_to_response(self, context, **response_kwargs): | ||
163 | + if self.request.user.is_staff: | ||
164 | + context['page_template'] = "categories/home_admin_content.html" | ||
165 | + else: | ||
166 | + context['page_template'] = "categories/home_teacher_student.html" | ||
167 | + | ||
168 | + if self.request.is_ajax(): | ||
169 | + if self.request.user.is_staff: | ||
170 | + self.template_name = "categories/home_admin_content.html" | ||
171 | + else: | ||
172 | + self.template_name = "categories/home_teacher_student_content.html" | ||
173 | + | ||
174 | + return self.response_class(request = self.request, template = self.template_name, context = context, using = self.template_engine, **response_kwargs) | ||
175 | + | ||
176 | + def get_context_data(self, **kwargs): | ||
177 | + context = super(IndexView, self).get_context_data(**kwargs) | ||
178 | + | ||
179 | + context['all'] = False | ||
180 | + context['title'] = _('My Subjects') | ||
181 | + | ||
182 | + context['show_buttons'] = True #So it shows subscribe and access buttons | ||
183 | + context['totals'] = self.totals | ||
184 | + | ||
185 | + if self.kwargs.get('option'): | ||
186 | + context['all'] = True | ||
187 | + context['title'] = _('All Subjects') | ||
188 | + | ||
189 | + if self.kwargs.get('slug'): | ||
190 | + context['cat_slug'] = self.kwargs.get('slug') | ||
191 | + | ||
192 | + context['subjects_menu_active'] = 'subjects_menu_active' | ||
193 | + | ||
194 | + return context | ||
195 | 195 | ||
196 | class GetSubjectList(LoginRequiredMixin, ListView): | 196 | class GetSubjectList(LoginRequiredMixin, ListView): |
197 | - login_url = reverse_lazy("users:login") | ||
198 | - redirect_field_name = 'next' | 197 | + login_url = reverse_lazy("users:login") |
198 | + redirect_field_name = 'next' | ||
199 | 199 | ||
200 | - template_name = 'subjects/_list.html' | ||
201 | - model = Subject | ||
202 | - context_object_name = 'subjects' | 200 | + template_name = 'subjects/_list.html' |
201 | + model = Subject | ||
202 | + context_object_name = 'subjects' | ||
203 | 203 | ||
204 | - def get_queryset(self): | ||
205 | - slug = self.kwargs.get('slug') | ||
206 | - category = get_object_or_404(Category, slug = slug) | 204 | + def get_queryset(self): |
205 | + slug = self.kwargs.get('slug') | ||
206 | + category = get_object_or_404(Category, slug = slug) | ||
207 | 207 | ||
208 | - return category.subject_category.all() | 208 | + return category.subject_category.all() |
209 | 209 | ||
210 | - def get_context_data(self, **kwargs): | ||
211 | - context = super(GetSubjectList, self).get_context_data(**kwargs) | 210 | + def get_context_data(self, **kwargs): |
211 | + context = super(GetSubjectList, self).get_context_data(**kwargs) | ||
212 | 212 | ||
213 | - context['show_buttons'] = True #So it shows subscribe and access buttons | 213 | + context['show_buttons'] = True #So it shows subscribe and access buttons |
214 | 214 | ||
215 | - if 'all' in self.request.META.get('HTTP_REFERER'): | ||
216 | - context['all'] = True | 215 | + if 'all' in self.request.META.get('HTTP_REFERER'): |
216 | + context['all'] = True | ||
217 | 217 | ||
218 | - return context | 218 | + return context |
219 | 219 | ||
220 | class SubjectCreateView(LoginRequiredMixin, LogMixin, CreateView): | 220 | class SubjectCreateView(LoginRequiredMixin, LogMixin, CreateView): |
221 | - log_component = 'subject' | ||
222 | - log_action = 'create' | ||
223 | - log_resource = 'subject' | ||
224 | - log_context = {} | ||
225 | - | ||
226 | - model = Subject | ||
227 | - template_name = "subjects/create.html" | ||
228 | - | ||
229 | - login_url = reverse_lazy('users:login') | ||
230 | - redirect_field_name = 'next' | ||
231 | - form_class = CreateSubjectForm | ||
232 | - | ||
233 | - success_url = reverse_lazy('subject:index') | ||
234 | - | ||
235 | - def dispatch(self, request, *args, **kwargs): | ||
236 | - user = request.user | ||
237 | - pk = user.pk | ||
238 | - | ||
239 | - if kwargs.get('subject_slug'): | ||
240 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('subject_slug'))) | ||
241 | - if not user.is_staff: | ||
242 | - if subject.count() == 0: | ||
243 | - if request.META.get('HTTP_REFERER'): | ||
244 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
245 | - else: | ||
246 | - return redirect('subjects:index') | ||
247 | - | ||
248 | - | ||
249 | - if kwargs.get('slug'): | ||
250 | - if not user.is_staff: | ||
251 | - category = Category.objects.filter(Q(coordinators__pk=pk) & Q(slug= kwargs.get('slug'))) | ||
252 | - if category.count() == 0: | ||
253 | - if request.META.get('HTTP_REFERER'): | ||
254 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
255 | - else: | ||
256 | - return redirect('subjects:index') | ||
257 | - if request.method.lower() in self.http_method_names: | ||
258 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
259 | - else: | ||
260 | - handler = self.http_method_not_allowed | ||
261 | - return handler(request, *args, **kwargs) | 221 | + log_component = 'subject' |
222 | + log_action = 'create' | ||
223 | + log_resource = 'subject' | ||
224 | + log_context = {} | ||
225 | + | ||
226 | + model = Subject | ||
227 | + template_name = "subjects/create.html" | ||
228 | + | ||
229 | + login_url = reverse_lazy('users:login') | ||
230 | + redirect_field_name = 'next' | ||
231 | + form_class = CreateSubjectForm | ||
232 | + | ||
233 | + success_url = reverse_lazy('subject:index') | ||
234 | + | ||
235 | + def dispatch(self, request, *args, **kwargs): | ||
236 | + user = request.user | ||
237 | + pk = user.pk | ||
238 | + | ||
239 | + if kwargs.get('subject_slug'): | ||
240 | + subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('subject_slug'))) | ||
241 | + if not user.is_staff: | ||
242 | + if subject.count() == 0: | ||
243 | + if request.META.get('HTTP_REFERER'): | ||
244 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
245 | + else: | ||
246 | + return redirect('subjects:index') | ||
247 | + | ||
248 | + | ||
249 | + if kwargs.get('slug'): | ||
250 | + if not user.is_staff: | ||
251 | + category = Category.objects.filter(Q(coordinators__pk=pk) & Q(slug= kwargs.get('slug'))) | ||
252 | + if category.count() == 0: | ||
253 | + if request.META.get('HTTP_REFERER'): | ||
254 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
255 | + else: | ||
256 | + return redirect('subjects:index') | ||
257 | + if request.method.lower() in self.http_method_names: | ||
258 | + handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
259 | + else: | ||
260 | + handler = self.http_method_not_allowed | ||
261 | + return handler(request, *args, **kwargs) | ||
262 | 262 | ||
263 | 263 | ||
264 | - def get_initial(self): | ||
265 | - initial = super(SubjectCreateView, self).get_initial() | ||
266 | - | ||
267 | - if self.kwargs.get('slug'): #when the user creates a subject | ||
268 | - initial['category'] = Category.objects.all().filter(slug=self.kwargs['slug']) | ||
269 | - | ||
270 | - if self.kwargs.get('subject_slug'): #when the user replicate a subject | ||
271 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
272 | - initial = initial.copy() | ||
273 | - initial['category'] = subject.category | ||
274 | - initial['description'] = subject.description | ||
275 | - initial['name'] = subject.name | ||
276 | - initial['visible'] = subject.visible | ||
277 | - initial['professor'] = subject.professor.all() | ||
278 | - initial['tags'] = ", ".join(subject.tags.all().values_list("name", flat = True)) | ||
279 | - initial['init_date'] = subject.init_date | ||
280 | - initial['end_date'] = subject.end_date | ||
281 | - initial['students'] = subject.students.all() | ||
282 | - initial['description_brief'] = subject.description_brief | ||
283 | - | ||
284 | - self.log_action = 'replicate' | ||
285 | - | ||
286 | - self.log_context['replicated_subject_id'] = subject.id | ||
287 | - self.log_context['replicated_subject_name'] = subject.name | ||
288 | - self.log_context['replicated_subject_slug'] = subject.slug | ||
289 | - | ||
290 | - return initial | ||
291 | - | ||
292 | - def get_context_data(self, **kwargs): | ||
293 | - context = super(SubjectCreateView, self).get_context_data(**kwargs) | ||
294 | - context['title'] = _('Create Subject') | ||
295 | - | ||
296 | - if self.kwargs.get('slug'): | ||
297 | - context['slug'] = self.kwargs['slug'] | ||
298 | - | ||
299 | - if self.kwargs.get('subject_slug'): | ||
300 | - context['title'] = _('Replicate Subject') | ||
301 | - | ||
302 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
303 | - | ||
304 | - context['slug'] = subject.category.slug | ||
305 | - context['replicate'] = True | ||
306 | - | ||
307 | - context['subject'] = subject | ||
308 | - | ||
309 | - context['subjects_menu_active'] = 'subjects_menu_active' | ||
310 | - | ||
311 | - return context | ||
312 | - | ||
313 | - def form_valid(self, form): | ||
314 | - | ||
315 | - self.object = form.save() | ||
316 | - | ||
317 | - if self.kwargs.get('slug'): | ||
318 | - self.object.category = Category.objects.get(slug=self.kwargs['slug']) | ||
319 | - | ||
320 | - if self.kwargs.get('subject_slug'): | ||
321 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
322 | - self.object.category = subject.category | ||
323 | - | ||
324 | - self.object.save() | ||
325 | - | ||
326 | - self.log_context['category_id'] = self.object.category.id | ||
327 | - self.log_context['category_name'] = self.object.category.name | ||
328 | - self.log_context['category_slug'] = self.object.category.slug | ||
329 | - self.log_context['subject_id'] = self.object.id | ||
330 | - self.log_context['subject_name'] = self.object.name | ||
331 | - self.log_context['subject_slug'] = self.object.slug | ||
332 | - | ||
333 | - super(SubjectCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
334 | - | ||
335 | - return super(SubjectCreateView, self).form_valid(form) | ||
336 | - | ||
337 | - def get_success_url(self): | ||
338 | - if not self.object.category.visible: | ||
339 | - self.object.visible = False | ||
340 | - self.object.save() | ||
341 | - | ||
342 | - messages.success(self.request, _('Subject "%s" was registered on "%s" successfully!')%(self.object.name, self.object.category.name )) | ||
343 | - return reverse_lazy('subjects:index') | 264 | + def get_initial(self): |
265 | + initial = super(SubjectCreateView, self).get_initial() | ||
266 | + | ||
267 | + if self.kwargs.get('slug'): #when the user creates a subject | ||
268 | + initial['category'] = Category.objects.all().filter(slug=self.kwargs['slug']) | ||
269 | + | ||
270 | + if self.kwargs.get('subject_slug'): #when the user replicate a subject | ||
271 | + subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
272 | + initial = initial.copy() | ||
273 | + initial['category'] = subject.category | ||
274 | + initial['description'] = subject.description | ||
275 | + initial['name'] = subject.name | ||
276 | + initial['visible'] = subject.visible | ||
277 | + initial['professor'] = subject.professor.all() | ||
278 | + initial['tags'] = ", ".join(subject.tags.all().values_list("name", flat = True)) | ||
279 | + initial['init_date'] = subject.init_date | ||
280 | + initial['end_date'] = subject.end_date | ||
281 | + initial['students'] = subject.students.all() | ||
282 | + initial['description_brief'] = subject.description_brief | ||
283 | + | ||
284 | + self.log_action = 'replicate' | ||
285 | + | ||
286 | + self.log_context['replicated_subject_id'] = subject.id | ||
287 | + self.log_context['replicated_subject_name'] = subject.name | ||
288 | + self.log_context['replicated_subject_slug'] = subject.slug | ||
289 | + | ||
290 | + return initial | ||
291 | + | ||
292 | + def get_context_data(self, **kwargs): | ||
293 | + context = super(SubjectCreateView, self).get_context_data(**kwargs) | ||
294 | + context['title'] = _('Create Subject') | ||
295 | + | ||
296 | + if self.kwargs.get('slug'): | ||
297 | + context['slug'] = self.kwargs['slug'] | ||
298 | + | ||
299 | + if self.kwargs.get('subject_slug'): | ||
300 | + context['title'] = _('Replicate Subject') | ||
301 | + | ||
302 | + subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
303 | + | ||
304 | + context['slug'] = subject.category.slug | ||
305 | + context['replicate'] = True | ||
306 | + | ||
307 | + context['subject'] = subject | ||
308 | + | ||
309 | + context['subjects_menu_active'] = 'subjects_menu_active' | ||
310 | + | ||
311 | + return context | ||
312 | + | ||
313 | + def form_valid(self, form): | ||
314 | + | ||
315 | + self.object = form.save() | ||
316 | + | ||
317 | + if self.kwargs.get('slug'): | ||
318 | + self.object.category = Category.objects.get(slug=self.kwargs['slug']) | ||
319 | + | ||
320 | + if self.kwargs.get('subject_slug'): | ||
321 | + subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | ||
322 | + self.object.category = subject.category | ||
323 | + | ||
324 | + self.object.save() | ||
325 | + | ||
326 | + self.log_context['category_id'] = self.object.category.id | ||
327 | + self.log_context['category_name'] = self.object.category.name | ||
328 | + self.log_context['category_slug'] = self.object.category.slug | ||
329 | + self.log_context['subject_id'] = self.object.id | ||
330 | + self.log_context['subject_name'] = self.object.name | ||
331 | + self.log_context['subject_slug'] = self.object.slug | ||
332 | + | ||
333 | + super(SubjectCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
334 | + | ||
335 | + return super(SubjectCreateView, self).form_valid(form) | ||
336 | + | ||
337 | + def get_success_url(self): | ||
338 | + if not self.object.category.visible: | ||
339 | + self.object.visible = False | ||
340 | + self.object.save() | ||
341 | + | ||
342 | + messages.success(self.request, _('Subject "%s" was registered on "%s" successfully!')%(self.object.name, self.object.category.name )) | ||
343 | + return reverse_lazy('subjects:index') | ||
344 | 344 | ||
345 | class SubjectUpdateView(LoginRequiredMixin, LogMixin, UpdateView): | 345 | class SubjectUpdateView(LoginRequiredMixin, LogMixin, UpdateView): |
346 | - log_component = 'subject' | ||
347 | - log_action = 'update' | ||
348 | - log_resource = 'subject' | ||
349 | - log_context = {} | ||
350 | - | ||
351 | - model = Subject | ||
352 | - form_class = CreateSubjectForm | ||
353 | - template_name = 'subjects/update.html' | ||
354 | - | ||
355 | - login_url = reverse_lazy("users:login") | ||
356 | - redirect_field_name = 'next' | ||
357 | - | ||
358 | - def dispatch(self, request, *args, **kwargs): | ||
359 | - user = self.request.user | ||
360 | - | ||
361 | - pk = user.pk | ||
362 | - | ||
363 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
364 | - if not user.is_staff: | ||
365 | - if subject.count() == 0: | ||
366 | - if request.META.get('HTTP_REFERER'): | ||
367 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
368 | - else: | ||
369 | - return redirect('subjects:index') | ||
370 | - | ||
371 | - if request.method.lower() in self.http_method_names: | ||
372 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
373 | - else: | ||
374 | - handler = self.http_method_not_allowed | ||
375 | - return handler(request, *args, **kwargs) | ||
376 | - | ||
377 | - def get_context_data(self, **kwargs): | ||
378 | - context = super(SubjectUpdateView, self).get_context_data(**kwargs) | ||
379 | - context['title'] = _('Update Subject') | ||
380 | - context['template_extends'] = 'categories/home.html' | ||
381 | - context['subjects_menu_active'] = 'subjects_menu_active' | ||
382 | - | ||
383 | - return context | ||
384 | - | ||
385 | - def get_success_url(self): | ||
386 | - if not self.object.category.visible: | ||
387 | - self.object.visible = False | ||
388 | - self.object.save() | ||
389 | - | ||
390 | - self.log_context['category_id'] = self.object.category.id | ||
391 | - self.log_context['category_name'] = self.object.category.name | ||
392 | - self.log_context['category_slug'] = self.object.category.slug | ||
393 | - self.log_context['subject_id'] = self.object.id | ||
394 | - self.log_context['subject_name'] = self.object.name | ||
395 | - self.log_context['subject_slug'] = self.object.slug | ||
396 | - | ||
397 | - super(SubjectUpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
398 | - | ||
399 | - messages.success(self.request, _('Subject "%s" was updated on "%s" successfully!')%(self.object.name, self.object.category.name )) | ||
400 | - return reverse_lazy('subjects:index') | 346 | + log_component = 'subject' |
347 | + log_action = 'update' | ||
348 | + log_resource = 'subject' | ||
349 | + log_context = {} | ||
350 | + | ||
351 | + model = Subject | ||
352 | + form_class = CreateSubjectForm | ||
353 | + template_name = 'subjects/update.html' | ||
354 | + | ||
355 | + login_url = reverse_lazy("users:login") | ||
356 | + redirect_field_name = 'next' | ||
357 | + | ||
358 | + def dispatch(self, request, *args, **kwargs): | ||
359 | + user = self.request.user | ||
360 | + | ||
361 | + pk = user.pk | ||
362 | + | ||
363 | + subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
364 | + if not user.is_staff: | ||
365 | + if subject.count() == 0: | ||
366 | + if request.META.get('HTTP_REFERER'): | ||
367 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
368 | + else: | ||
369 | + return redirect('subjects:index') | ||
370 | + | ||
371 | + if request.method.lower() in self.http_method_names: | ||
372 | + handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
373 | + else: | ||
374 | + handler = self.http_method_not_allowed | ||
375 | + return handler(request, *args, **kwargs) | ||
376 | + | ||
377 | + def get_context_data(self, **kwargs): | ||
378 | + context = super(SubjectUpdateView, self).get_context_data(**kwargs) | ||
379 | + context['title'] = _('Update Subject') | ||
380 | + context['template_extends'] = 'categories/home.html' | ||
381 | + context['subjects_menu_active'] = 'subjects_menu_active' | ||
382 | + | ||
383 | + return context | ||
384 | + | ||
385 | + def get_success_url(self): | ||
386 | + if not self.object.category.visible: | ||
387 | + self.object.visible = False | ||
388 | + self.object.save() | ||
389 | + | ||
390 | + self.log_context['category_id'] = self.object.category.id | ||
391 | + self.log_context['category_name'] = self.object.category.name | ||
392 | + self.log_context['category_slug'] = self.object.category.slug | ||
393 | + self.log_context['subject_id'] = self.object.id | ||
394 | + self.log_context['subject_name'] = self.object.name | ||
395 | + self.log_context['subject_slug'] = self.object.slug | ||
396 | + | ||
397 | + super(SubjectUpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
398 | + | ||
399 | + messages.success(self.request, _('Subject "%s" was updated on "%s" successfully!')%(self.object.name, self.object.category.name )) | ||
400 | + return reverse_lazy('subjects:index') | ||
401 | 401 | ||
402 | class SubjectDeleteView(LoginRequiredMixin, LogMixin, DeleteView): | 402 | class SubjectDeleteView(LoginRequiredMixin, LogMixin, DeleteView): |
403 | - log_component = 'subject' | ||
404 | - log_action = 'delete' | ||
405 | - log_resource = 'subject' | ||
406 | - log_context = {} | 403 | + log_component = 'subject' |
404 | + log_action = 'delete' | ||
405 | + log_resource = 'subject' | ||
406 | + log_context = {} | ||
407 | 407 | ||
408 | - login_url = reverse_lazy("users:login") | ||
409 | - redirect_field_name = 'next' | ||
410 | - model = Subject | ||
411 | - template_name = 'subjects/delete.html' | ||
412 | - | ||
413 | - def dispatch(self, request, *args, **kwargs): | ||
414 | - user = self.request.user | ||
415 | - | ||
416 | - pk = user.pk | ||
417 | - | ||
418 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
419 | - if not user.is_staff: | ||
420 | - if subject.count() == 0: | ||
421 | - if request.META.get('HTTP_REFERER'): | ||
422 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
423 | - else: | ||
424 | - return redirect('subjects:index') | ||
425 | - return super(SubjectDeleteView, self).dispatch(request, *args, **kwargs) | ||
426 | - | ||
427 | - def get(self, request, *args, **kwargs): | ||
428 | - self.object = self.get_object() | ||
429 | - if self.object.students.all().count() > 0: | ||
430 | - messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated")) | ||
431 | - | ||
432 | - return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')}) | ||
433 | - context = self.get_context_data(object=self.object) | ||
434 | - return self.render_to_response(context) | ||
435 | - | ||
436 | - def get_context_data(self, **kwargs): | ||
437 | - context = super(SubjectDeleteView, self).get_context_data(**kwargs) | ||
438 | - subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) | ||
439 | - context['subject'] = subject | ||
440 | - | ||
441 | - if (self.request.GET.get('view') == 'index'): | ||
442 | - context['index'] = True | ||
443 | - else: | ||
444 | - context['index'] = False | ||
445 | - | ||
446 | - return context | ||
447 | - | ||
448 | - def get_success_url(self): | ||
449 | - self.log_context['category_id'] = self.object.category.id | ||
450 | - self.log_context['category_name'] = self.object.category.name | ||
451 | - self.log_context['category_slug'] = self.object.category.slug | ||
452 | - self.log_context['subject_id'] = self.object.id | ||
453 | - self.log_context['subject_name'] = self.object.name | ||
454 | - self.log_context['subject_slug'] = self.object.slug | ||
455 | - | ||
456 | - super(SubjectDeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
457 | - | ||
458 | - | ||
459 | - messages.success(self.request, _('Subject "%s" removed successfully!')%(self.object.name)) | ||
460 | - | ||
461 | - return reverse_lazy('subjects:index') | 408 | + login_url = reverse_lazy("users:login") |
409 | + redirect_field_name = 'next' | ||
410 | + model = Subject | ||
411 | + template_name = 'subjects/delete.html' | ||
412 | + | ||
413 | + def dispatch(self, request, *args, **kwargs): | ||
414 | + user = self.request.user | ||
415 | + | ||
416 | + pk = user.pk | ||
417 | + | ||
418 | + subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
419 | + if not user.is_staff: | ||
420 | + if subject.count() == 0: | ||
421 | + if request.META.get('HTTP_REFERER'): | ||
422 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
423 | + else: | ||
424 | + return redirect('subjects:index') | ||
425 | + return super(SubjectDeleteView, self).dispatch(request, *args, **kwargs) | ||
426 | + | ||
427 | + def get(self, request, *args, **kwargs): | ||
428 | + self.object = self.get_object() | ||
429 | + if self.object.students.all().count() > 0: | ||
430 | + messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated")) | ||
431 | + | ||
432 | + return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')}) | ||
433 | + context = self.get_context_data(object=self.object) | ||
434 | + return self.render_to_response(context) | ||
435 | + | ||
436 | + def get_context_data(self, **kwargs): | ||
437 | + context = super(SubjectDeleteView, self).get_context_data(**kwargs) | ||
438 | + subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) | ||
439 | + context['subject'] = subject | ||
440 | + | ||
441 | + if (self.request.GET.get('view') == 'index'): | ||
442 | + context['index'] = True | ||
443 | + else: | ||
444 | + context['index'] = False | ||
445 | + | ||
446 | + return context | ||
447 | + | ||
448 | + def get_success_url(self): | ||
449 | + self.log_context['category_id'] = self.object.category.id | ||
450 | + self.log_context['category_name'] = self.object.category.name | ||
451 | + self.log_context['category_slug'] = self.object.category.slug | ||
452 | + self.log_context['subject_id'] = self.object.id | ||
453 | + self.log_context['subject_name'] = self.object.name | ||
454 | + self.log_context['subject_slug'] = self.object.slug | ||
455 | + | ||
456 | + super(SubjectDeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
457 | + | ||
458 | + | ||
459 | + messages.success(self.request, _('Subject "%s" removed successfully!')%(self.object.name)) | ||
460 | + | ||
461 | + return reverse_lazy('subjects:index') | ||
462 | 462 | ||
463 | 463 | ||
464 | class SubjectDetailView(LoginRequiredMixin, LogMixin, DetailView): | 464 | class SubjectDetailView(LoginRequiredMixin, LogMixin, DetailView): |
465 | - log_component = 'subject' | ||
466 | - log_action = 'access' | ||
467 | - log_resource = 'subject' | ||
468 | - log_context = {} | ||
469 | - | ||
470 | - login_url = reverse_lazy("users:login") | ||
471 | - redirect_field_name = 'next' | ||
472 | - | ||
473 | - model = Subject | ||
474 | - template_name = 'subjects/view.html' | ||
475 | - context_object_name = 'subject' | ||
476 | - | ||
477 | - def dispatch(self, request, *args,**kwargs): | ||
478 | - user = request.user | ||
479 | - pk = user.pk | ||
480 | - if kwargs.get('slug') and not user.is_staff: | ||
481 | - subject = get_object_or_404(Subject, slug = kwargs.get('slug')) | ||
482 | - | ||
483 | - subject = Subject.objects.filter((Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
484 | - | ||
485 | - if subject.count() == 0: | ||
486 | - if request.META.get('HTTP_REFERER'): | ||
487 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
488 | - else: | ||
489 | - return redirect('subjects:home') | ||
490 | - | ||
491 | - if request.method.lower() in self.http_method_names: | ||
492 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
493 | - else: | ||
494 | - handler = self.http_method_not_allowed | ||
495 | - return handler(request, *args, **kwargs) | ||
496 | - | ||
497 | - def get_context_data(self, **kwargs): | ||
498 | - context = super(SubjectDetailView, self).get_context_data(**kwargs) | ||
499 | - context['title'] = self.object.name | ||
500 | - | ||
501 | - resources = self.request.session.get('resources', None) | ||
502 | - | ||
503 | - if resources: | ||
504 | - context['resource_new_page'] = resources['new_page'] | ||
505 | - context['resource_new_page_url'] = resources['new_page_url'] | ||
506 | - | ||
507 | - self.request.session['resources'] = None | ||
508 | - | ||
509 | - if self.kwargs.get('topic_slug'): | ||
510 | - context['topic_slug'] = self.kwargs.get('topic_slug') | ||
511 | - | ||
512 | - self.log_context['category_id'] = self.object.category.id | ||
513 | - self.log_context['category_name'] = self.object.category.name | ||
514 | - self.log_context['category_slug'] = self.object.category.slug | ||
515 | - self.log_context['subject_id'] = self.object.id | ||
516 | - self.log_context['subject_name'] = self.object.name | ||
517 | - self.log_context['subject_slug'] = self.object.slug | ||
518 | - self.log_context['timestamp_start'] = str(int(time.time())) | ||
519 | - | ||
520 | - super(SubjectDetailView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
521 | - | ||
522 | - self.request.session['log_id'] = Log.objects.latest('id').id | ||
523 | - | ||
524 | - return context | 465 | + log_component = 'subject' |
466 | + log_action = 'access' | ||
467 | + log_resource = 'subject' | ||
468 | + log_context = {} | ||
469 | + | ||
470 | + login_url = reverse_lazy("users:login") | ||
471 | + redirect_field_name = 'next' | ||
472 | + | ||
473 | + model = Subject | ||
474 | + template_name = 'subjects/view.html' | ||
475 | + context_object_name = 'subject' | ||
476 | + | ||
477 | + def dispatch(self, request, *args,**kwargs): | ||
478 | + user = request.user | ||
479 | + pk = user.pk | ||
480 | + if kwargs.get('slug') and not user.is_staff: | ||
481 | + subject = get_object_or_404(Subject, slug = kwargs.get('slug')) | ||
482 | + | ||
483 | + subject = Subject.objects.filter((Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | ||
484 | + | ||
485 | + if subject.count() == 0: | ||
486 | + if request.META.get('HTTP_REFERER'): | ||
487 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
488 | + else: | ||
489 | + return redirect('subjects:home') | ||
490 | + | ||
491 | + if request.method.lower() in self.http_method_names: | ||
492 | + handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
493 | + else: | ||
494 | + handler = self.http_method_not_allowed | ||
495 | + return handler(request, *args, **kwargs) | ||
496 | + | ||
497 | + def get_context_data(self, **kwargs): | ||
498 | + context = super(SubjectDetailView, self).get_context_data(**kwargs) | ||
499 | + context['title'] = self.object.name | ||
500 | + | ||
501 | + resources = self.request.session.get('resources', None) | ||
502 | + | ||
503 | + if resources: | ||
504 | + context['resource_new_page'] = resources['new_page'] | ||
505 | + context['resource_new_page_url'] = resources['new_page_url'] | ||
506 | + | ||
507 | + self.request.session['resources'] = None | ||
508 | + | ||
509 | + if self.kwargs.get('topic_slug'): | ||
510 | + context['topic_slug'] = self.kwargs.get('topic_slug') | ||
511 | + | ||
512 | + self.log_context['category_id'] = self.object.category.id | ||
513 | + self.log_context['category_name'] = self.object.category.name | ||
514 | + self.log_context['category_slug'] = self.object.category.slug | ||
515 | + self.log_context['subject_id'] = self.object.id | ||
516 | + self.log_context['subject_name'] = self.object.name | ||
517 | + self.log_context['subject_slug'] = self.object.slug | ||
518 | + self.log_context['timestamp_start'] = str(int(time.time())) | ||
519 | + | ||
520 | + super(SubjectDetailView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
521 | + | ||
522 | + self.request.session['log_id'] = Log.objects.latest('id').id | ||
523 | + | ||
524 | + return context | ||
525 | 525 | ||
526 | class SubjectSubscribeView(LoginRequiredMixin, LogMixin, TemplateView): | 526 | class SubjectSubscribeView(LoginRequiredMixin, LogMixin, TemplateView): |
527 | - log_component = 'subject' | ||
528 | - log_action = 'subscribe' | ||
529 | - log_resource = 'subject' | ||
530 | - log_context = {} | 527 | + log_component = 'subject' |
528 | + log_action = 'subscribe' | ||
529 | + log_resource = 'subject' | ||
530 | + log_context = {} | ||
531 | 531 | ||
532 | - login_url = reverse_lazy("users:login") | ||
533 | - redirect_field_name = 'next' | 532 | + login_url = reverse_lazy("users:login") |
533 | + redirect_field_name = 'next' | ||
534 | 534 | ||
535 | - template_name = 'subjects/subscribe.html' | 535 | + template_name = 'subjects/subscribe.html' |
536 | 536 | ||
537 | - def get_context_data(self, **kwargs): | ||
538 | - context = super(SubjectSubscribeView, self).get_context_data(**kwargs) | ||
539 | - context['subject'] = get_object_or_404(Subject, slug= kwargs.get('slug')) | 537 | + def get_context_data(self, **kwargs): |
538 | + context = super(SubjectSubscribeView, self).get_context_data(**kwargs) | ||
539 | + context['subject'] = get_object_or_404(Subject, slug= kwargs.get('slug')) | ||
540 | 540 | ||
541 | - return context | 541 | + return context |
542 | 542 | ||
543 | - def post(self, request, *args, **kwargs): | ||
544 | - subject = get_object_or_404(Subject, slug= kwargs.get('slug')) | 543 | + def post(self, request, *args, **kwargs): |
544 | + subject = get_object_or_404(Subject, slug= kwargs.get('slug')) | ||
545 | 545 | ||
546 | - if subject.subscribe_end < datetime.datetime.today().date(): | ||
547 | - messages.error(self.request, _('Subscription date is due!')) | ||
548 | - else: | ||
549 | - self.log_context['category_id'] = subject.category.id | ||
550 | - self.log_context['category_name'] = subject.category.name | ||
551 | - self.log_context['category_slug'] = subject.category.slug | ||
552 | - self.log_context['subject_id'] = subject.id | ||
553 | - self.log_context['subject_name'] = subject.name | ||
554 | - self.log_context['subject_slug'] = subject.slug | 546 | + if subject.subscribe_end < datetime.datetime.today().date(): |
547 | + messages.error(self.request, _('Subscription date is due!')) | ||
548 | + else: | ||
549 | + self.log_context['category_id'] = subject.category.id | ||
550 | + self.log_context['category_name'] = subject.category.name | ||
551 | + self.log_context['category_slug'] = subject.category.slug | ||
552 | + self.log_context['subject_id'] = subject.id | ||
553 | + self.log_context['subject_name'] = subject.name | ||
554 | + self.log_context['subject_slug'] = subject.slug | ||
555 | 555 | ||
556 | - super(SubjectSubscribeView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 556 | + super(SubjectSubscribeView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
557 | 557 | ||
558 | - subject.students.add(request.user) | ||
559 | - subject.save() | ||
560 | - messages.success(self.request, _('Subscription was successfull!')) | ||
561 | - | ||
562 | - return JsonResponse({'url':reverse_lazy('subjects:index')}) | 558 | + subject.students.add(request.user) |
559 | + subject.save() | ||
560 | + messages.success(self.request, _('Subscription was successfull!')) | ||
561 | + | ||
562 | + return JsonResponse({'url':reverse_lazy('subjects:index')}) | ||
563 | 563 | ||
564 | 564 | ||
565 | class SubjectSearchView(LoginRequiredMixin, LogMixin, ListView): | 565 | class SubjectSearchView(LoginRequiredMixin, LogMixin, ListView): |
566 | - log_component = 'subject' | ||
567 | - log_action = 'search' | ||
568 | - log_resource = 'subject/resources' | ||
569 | - log_context = {} | ||
570 | - | ||
571 | - login_url = reverse_lazy("users:login") | ||
572 | - redirect_field_name = 'next' | ||
573 | - | ||
574 | - template_name = 'subjects/list_search.html' | ||
575 | - context_object_name = 'subjects' | ||
576 | - paginate_by = 10 | ||
577 | - | ||
578 | - def dispatch(self, request, *args, **kwargs): | ||
579 | - # Try to dispatch to the right method; if a method doesn't exist, | ||
580 | - # defer to the error handler. Also defer to the error handler if the | ||
581 | - # request method isn't on the approved list. | ||
582 | - tags = request.GET.get('search') | ||
583 | - tags = tags.split(" ") | ||
584 | - | ||
585 | - if tags[0] == '': | ||
586 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
587 | - if request.method.lower() in self.http_method_names: | ||
588 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
589 | - else: | ||
590 | - handler = self.http_method_not_allowed | ||
591 | - return handler(request, *args, **kwargs) | 566 | + log_component = 'subject' |
567 | + log_action = 'search' | ||
568 | + log_resource = 'subject/resources' | ||
569 | + log_context = {} | ||
570 | + | ||
571 | + login_url = reverse_lazy("users:login") | ||
572 | + redirect_field_name = 'next' | ||
573 | + | ||
574 | + template_name = 'subjects/list_search.html' | ||
575 | + context_object_name = 'subjects' | ||
576 | + paginate_by = 10 | ||
577 | + | ||
578 | + def dispatch(self, request, *args, **kwargs): | ||
579 | + # Try to dispatch to the right method; if a method doesn't exist, | ||
580 | + # defer to the error handler. Also defer to the error handler if the | ||
581 | + # request method isn't on the approved list. | ||
582 | + tags = request.GET.get('search') | ||
583 | + tags = tags.split(" ") | ||
584 | + | ||
585 | + if tags[0] == '': | ||
586 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | ||
587 | + if request.method.lower() in self.http_method_names: | ||
588 | + handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | ||
589 | + else: | ||
590 | + handler = self.http_method_not_allowed | ||
591 | + return handler(request, *args, **kwargs) | ||
592 | 592 | ||
593 | - def get_queryset(self): | ||
594 | - | ||
595 | - tags = self.request.GET.get('search') | ||
596 | - | ||
597 | - self.tags = tags | ||
598 | - tags = tags.split(" ") | ||
599 | - | ||
600 | - subjects = Subject.objects.filter(tags__name__unaccent__in=tags) | ||
601 | - #pk = self.request.user.pk | ||
602 | - #my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct() | ||
603 | - | ||
604 | - self.totals = {'all_subjects': subjects.count(), 'my_subjects': subjects.count()} | ||
605 | - #if self.kwargs.get('option'): | ||
606 | - # subjects = my_subjects | ||
607 | - return subjects | 593 | + def get_queryset(self): |
594 | + | ||
595 | + tags = self.request.GET.get('search') | ||
596 | + | ||
597 | + self.tags = tags | ||
598 | + tags = tags.split(" ") | ||
599 | + q = Q() | ||
600 | + for tag in tags: | ||
601 | + q = q | Q(tags__name__unaccent__iexact=tag) | ||
602 | + | ||
603 | + subjects = Subject.objects.filter(q) | ||
604 | + #pk = self.request.user.pk | ||
605 | + #my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct() | ||
606 | + | ||
607 | + self.totals = {'all_subjects': subjects.count(), 'my_subjects': subjects.count()} | ||
608 | + #if self.kwargs.get('option'): | ||
609 | + # subjects = my_subjects | ||
610 | + return subjects | ||
608 | 611 | ||
609 | - def get_context_data(self, **kwargs): | ||
610 | - context = super(SubjectSearchView, self).get_context_data(**kwargs) | ||
611 | - | ||
612 | - context['tags'] = self.tags | ||
613 | - context['all'] = False | ||
614 | - context['title'] = _('Subjects') | ||
615 | - | ||
616 | - context['show_buttons'] = True #So it shows subscribe and access buttons | ||
617 | - context['totals'] = self.totals | ||
618 | - | ||
619 | - if self.kwargs.get('option'): | ||
620 | - context['all'] = True | ||
621 | - context['title'] = _('Subjects') | ||
622 | - | ||
623 | - context['subjects_menu_active'] = 'subjects_menu_active' | ||
624 | - | ||
625 | - self.log_context['search_for'] = self.tags | ||
626 | - super(SubjectSearchView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
627 | - | ||
628 | - return context | 612 | + def get_context_data(self, **kwargs): |
613 | + context = super(SubjectSearchView, self).get_context_data(**kwargs) | ||
614 | + | ||
615 | + context['tags'] = self.tags | ||
616 | + context['all'] = False | ||
617 | + context['title'] = _('Subjects') | ||
618 | + | ||
619 | + context['show_buttons'] = True #So it shows subscribe and access buttons | ||
620 | + context['totals'] = self.totals | ||
621 | + | ||
622 | + if self.kwargs.get('option'): | ||
623 | + context['all'] = True | ||
624 | + context['title'] = _('Subjects') | ||
625 | + | ||
626 | + context['subjects_menu_active'] = 'subjects_menu_active' | ||
627 | + | ||
628 | + self.log_context['search_for'] = self.tags | ||
629 | + super(SubjectSearchView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
630 | + | ||
631 | + return context | ||
629 | 632 | ||
630 | @log_decorator_ajax('subject', 'view', 'subject') | 633 | @log_decorator_ajax('subject', 'view', 'subject') |
631 | def subject_view_log(request, subject): | 634 | def subject_view_log(request, subject): |
632 | - action = request.GET.get('action') | 635 | + action = request.GET.get('action') |
633 | 636 | ||
634 | - if action == 'open': | ||
635 | - subject = get_object_or_404(Subject, id = subject) | 637 | + if action == 'open': |
638 | + subject = get_object_or_404(Subject, id = subject) | ||
636 | 639 | ||
637 | - log_context = {} | ||
638 | - log_context['category_id'] = subject.category.id | ||
639 | - log_context['category_name'] = subject.category.name | ||
640 | - log_context['category_slug'] = subject.category.slug | ||
641 | - log_context['subject_id'] = subject.id | ||
642 | - log_context['subject_name'] = subject.name | ||
643 | - log_context['subject_slug'] = subject.slug | ||
644 | - log_context['timestamp_start'] = str(int(time.time())) | ||
645 | - log_context['timestamp_end'] = '-1' | 640 | + log_context = {} |
641 | + log_context['category_id'] = subject.category.id | ||
642 | + log_context['category_name'] = subject.category.name | ||
643 | + log_context['category_slug'] = subject.category.slug | ||
644 | + log_context['subject_id'] = subject.id | ||
645 | + log_context['subject_name'] = subject.name | ||
646 | + log_context['subject_slug'] = subject.slug | ||
647 | + log_context['timestamp_start'] = str(int(time.time())) | ||
648 | + log_context['timestamp_end'] = '-1' | ||
646 | 649 | ||
647 | - request.log_context = log_context | 650 | + request.log_context = log_context |
648 | 651 | ||
649 | - log_id = Log.objects.latest('id').id | 652 | + log_id = Log.objects.latest('id').id |
650 | 653 | ||
651 | - return JsonResponse({'message': 'ok', 'log_id': log_id}) | 654 | + return JsonResponse({'message': 'ok', 'log_id': log_id}) |
652 | 655 | ||
653 | - return JsonResponse({'message': 'ok'}) | 656 | + return JsonResponse({'message': 'ok'}) |
654 | 657 |