Commit 4d316ef2bbf1a184fbb66ad260db2d6d56fdef5e
1 parent
547930ac
Exists in
master
and in
3 other branches
Adding subject log
Showing
4 changed files
with
616 additions
and
509 deletions
Show diff stats
log/middleware.py
| ... | ... | @@ -14,35 +14,36 @@ class TimeSpentMiddleware(object): |
| 14 | 14 | |
| 15 | 15 | if not 'admin' in app_names: |
| 16 | 16 | if not request.is_ajax(): |
| 17 | - log_id = request.session.get('log_id', None) | |
| 17 | + if not request.path.startswith('/uploads/'): | |
| 18 | + log_id = request.session.get('log_id', None) | |
| 18 | 19 | |
| 19 | - if not log_id is None: | |
| 20 | - log = get_object_or_404(Log, id = log_id) | |
| 20 | + if not log_id is None: | |
| 21 | + log = get_object_or_404(Log, id = log_id) | |
| 21 | 22 | |
| 22 | - if type(log.context) == dict: | |
| 23 | - log_context = log.context | |
| 24 | - else: | |
| 25 | - log_context = json.loads(log.context) | |
| 23 | + if type(log.context) == dict: | |
| 24 | + log_context = log.context | |
| 25 | + else: | |
| 26 | + log_context = json.loads(log.context) | |
| 26 | 27 | |
| 27 | - log_context['timestamp_end'] = str(int(time.time())) | |
| 28 | + log_context['timestamp_end'] = str(int(time.time())) | |
| 28 | 29 | |
| 29 | - log.context = log_context | |
| 30 | + log.context = log_context | |
| 30 | 31 | |
| 31 | - log.save() | |
| 32 | + log.save() | |
| 32 | 33 | |
| 33 | - request.session['log_id'] = None | |
| 34 | + request.session['log_id'] = None | |
| 34 | 35 | |
| 35 | - if request.user.is_authenticated: | |
| 36 | - oppened_logs = Log.objects.filter(user = request.user, context__contains={'timestamp_end': '-1'}) | |
| 36 | + if request.user.is_authenticated: | |
| 37 | + oppened_logs = Log.objects.filter(user = request.user, context__contains={'timestamp_end': '-1'}) | |
| 37 | 38 | |
| 38 | - for op_log in oppened_logs: | |
| 39 | - if type(op_log.context) == dict: | |
| 40 | - log_context = op_log.context | |
| 41 | - else: | |
| 42 | - log_context = json.loads(op_log.context) | |
| 39 | + for op_log in oppened_logs: | |
| 40 | + if type(op_log.context) == dict: | |
| 41 | + log_context = op_log.context | |
| 42 | + else: | |
| 43 | + log_context = json.loads(op_log.context) | |
| 43 | 44 | |
| 44 | - log_context['timestamp_end'] = str(int(time.time())) | |
| 45 | + log_context['timestamp_end'] = str(int(time.time())) | |
| 45 | 46 | |
| 46 | - op_log.context = log_context | |
| 47 | + op_log.context = log_context | |
| 47 | 48 | |
| 48 | - op_log.save() | |
| 49 | 49 | \ No newline at end of file |
| 50 | + op_log.save() | |
| 50 | 51 | \ No newline at end of file | ... | ... |
subjects/templates/subjects/subject_card.html
| ... | ... | @@ -42,6 +42,9 @@ |
| 42 | 42 | </div> |
| 43 | 43 | </div> |
| 44 | 44 | <div id="{{subject.slug}}" class="panel-collapse collapse category-panel-content"> |
| 45 | + <input type="hidden" class="log_url" value="{% url 'subjects:view_log' subject.id %}" /> | |
| 46 | + <input type="hidden" class="log_id" value="" /> | |
| 47 | + | |
| 45 | 48 | <div class="row"> |
| 46 | 49 | <div class="col-md-6"> |
| 47 | 50 | {% if subject.professor.all|length > 0 %} | ... | ... |
subjects/urls.py
| ... | ... | @@ -15,5 +15,6 @@ urlpatterns = [ |
| 15 | 15 | url(r'^search/$', views.SubjectSearchView.as_view(), name='search'), |
| 16 | 16 | url(r'^search/(?P<option>[\w_-]+)/$', views.SubjectSearchView.as_view(), name='search'), |
| 17 | 17 | url(r'^load_subs/(?P<slug>[\w_-]+)/$', views.GetSubjectList.as_view(), name='load_view'), |
| 18 | + url(r'^view_log/(?P<subject>[\w_-]+)/$', views.subject_view_log, name = 'view_log'), | |
| 18 | 19 | url(r'^(?P<option>[\w_-]+)/$', views.IndexView.as_view(), name='index'), |
| 19 | 20 | ] |
| 20 | 21 | \ No newline at end of file | ... | ... |
subjects/views.py
| ... | ... | @@ -31,510 +31,612 @@ from users.models import User |
| 31 | 31 | |
| 32 | 32 | |
| 33 | 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 = len(subjects) | |
| 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 = len(subjects) | |
| 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 | 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 | 196 | class GetSubjectList(LoginRequiredMixin, ListView): |
| 197 | - login_url = reverse_lazy("users:login") | |
| 198 | - redirect_field_name = 'next' | |
| 199 | - | |
| 200 | - template_name = 'subjects/_list.html' | |
| 201 | - model = Subject | |
| 202 | - context_object_name = 'subjects' | |
| 203 | - | |
| 204 | - def get_queryset(self): | |
| 205 | - slug = self.kwargs.get('slug') | |
| 206 | - category = get_object_or_404(Category, slug = slug) | |
| 207 | - | |
| 208 | - return category.subject_category.all() | |
| 209 | - | |
| 210 | - def get_context_data(self, **kwargs): | |
| 211 | - context = super(GetSubjectList, self).get_context_data(**kwargs) | |
| 212 | - | |
| 213 | - context['show_buttons'] = True #So it shows subscribe and access buttons | |
| 214 | - | |
| 215 | - if 'all' in self.request.META.get('HTTP_REFERER'): | |
| 216 | - context['all'] = True | |
| 217 | - | |
| 218 | - return context | |
| 219 | - | |
| 220 | -class SubjectCreateView(LoginRequiredMixin, CreateView): | |
| 221 | - model = Subject | |
| 222 | - template_name = "subjects/create.html" | |
| 223 | - | |
| 224 | - login_url = reverse_lazy('users:login') | |
| 225 | - redirect_field_name = 'next' | |
| 226 | - form_class = CreateSubjectForm | |
| 227 | - | |
| 228 | - success_url = reverse_lazy('subject:index') | |
| 229 | - | |
| 230 | - def dispatch(self, request, *args, **kwargs): | |
| 231 | - user = request.user | |
| 232 | - pk = user.pk | |
| 233 | - | |
| 234 | - if kwargs.get('subject_slug'): | |
| 235 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('subject_slug'))) | |
| 236 | - if not user.is_staff: | |
| 237 | - if subject.count() == 0: | |
| 238 | - if request.META.get('HTTP_REFERER'): | |
| 239 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 240 | - else: | |
| 241 | - return redirect('subjects:index') | |
| 242 | - | |
| 243 | - | |
| 244 | - if kwargs.get('slug'): | |
| 245 | - if not user.is_staff: | |
| 246 | - category = Category.objects.filter(Q(coordinators__pk=pk) & Q(slug= kwargs.get('slug'))) | |
| 247 | - if category.count() == 0: | |
| 248 | - if request.META.get('HTTP_REFERER'): | |
| 249 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 250 | - else: | |
| 251 | - return redirect('subjects:index') | |
| 252 | - if request.method.lower() in self.http_method_names: | |
| 253 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | |
| 254 | - else: | |
| 255 | - handler = self.http_method_not_allowed | |
| 256 | - return handler(request, *args, **kwargs) | |
| 197 | + login_url = reverse_lazy("users:login") | |
| 198 | + redirect_field_name = 'next' | |
| 199 | + | |
| 200 | + template_name = 'subjects/_list.html' | |
| 201 | + model = Subject | |
| 202 | + context_object_name = 'subjects' | |
| 203 | + | |
| 204 | + def get_queryset(self): | |
| 205 | + slug = self.kwargs.get('slug') | |
| 206 | + category = get_object_or_404(Category, slug = slug) | |
| 207 | + | |
| 208 | + return category.subject_category.all() | |
| 209 | + | |
| 210 | + def get_context_data(self, **kwargs): | |
| 211 | + context = super(GetSubjectList, self).get_context_data(**kwargs) | |
| 212 | + | |
| 213 | + context['show_buttons'] = True #So it shows subscribe and access buttons | |
| 214 | + | |
| 215 | + if 'all' in self.request.META.get('HTTP_REFERER'): | |
| 216 | + context['all'] = True | |
| 217 | + | |
| 218 | + return context | |
| 219 | + | |
| 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) | |
| 257 | 262 | |
| 258 | 263 | |
| 259 | - def get_initial(self): | |
| 260 | - initial = super(SubjectCreateView, self).get_initial() | |
| 261 | - if self.kwargs.get('slug'): #when the user creates a subject | |
| 262 | - initial['category'] = Category.objects.all().filter(slug=self.kwargs['slug']) | |
| 263 | - | |
| 264 | - if self.kwargs.get('subject_slug'): #when the user replicate a subject | |
| 265 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | |
| 266 | - initial = initial.copy() | |
| 267 | - initial['category'] = subject.category | |
| 268 | - initial['description'] = subject.description | |
| 269 | - initial['name'] = subject.name | |
| 270 | - initial['visible'] = subject.visible | |
| 271 | - initial['professor'] = subject.professor.all() | |
| 272 | - initial['tags'] = ", ".join(subject.tags.all().values_list("name", flat = True)) | |
| 273 | - initial['init_date'] = subject.init_date | |
| 274 | - initial['end_date'] = subject.end_date | |
| 275 | - initial['students'] = subject.students.all() | |
| 276 | - initial['description_brief'] = subject.description_brief | |
| 277 | - | |
| 278 | - return initial | |
| 279 | - | |
| 280 | - def get_context_data(self, **kwargs): | |
| 281 | - context = super(SubjectCreateView, self).get_context_data(**kwargs) | |
| 282 | - context['title'] = _('Create Subject') | |
| 283 | - | |
| 284 | - if self.kwargs.get('slug'): | |
| 285 | - context['slug'] = self.kwargs['slug'] | |
| 286 | - | |
| 287 | - if self.kwargs.get('subject_slug'): | |
| 288 | - context['title'] = _('Replicate Subject') | |
| 289 | - | |
| 290 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | |
| 291 | - | |
| 292 | - context['slug'] = subject.category.slug | |
| 293 | - context['replicate'] = True | |
| 294 | - | |
| 295 | - context['subject'] = subject | |
| 296 | - | |
| 297 | - | |
| 298 | - context['subjects_menu_active'] = 'subjects_menu_active' | |
| 299 | - | |
| 300 | - return context | |
| 301 | - | |
| 302 | - def form_valid(self, form): | |
| 303 | - | |
| 304 | - self.object = form.save() | |
| 305 | - if self.kwargs.get('slug'): | |
| 306 | - self.object.category = Category.objects.get(slug=self.kwargs['slug']) | |
| 307 | - if self.kwargs.get('subject_slug'): | |
| 308 | - subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug']) | |
| 309 | - self.object.category = subject.category | |
| 310 | - self.object.save() | |
| 311 | - | |
| 312 | - return super(SubjectCreateView, self).form_valid(form) | |
| 313 | - | |
| 314 | - def get_success_url(self): | |
| 315 | - if not self.object.category.visible: | |
| 316 | - self.object.visible = False | |
| 317 | - self.object.save() | |
| 318 | - | |
| 319 | - messages.success(self.request, _('Subject "%s" was registered on "%s" successfully!')%(self.object.name, self.object.category.name )) | |
| 320 | - 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') | |
| 321 | 344 | |
| 322 | 345 | class SubjectUpdateView(LoginRequiredMixin, LogMixin, UpdateView): |
| 323 | - model = Subject | |
| 324 | - form_class = CreateSubjectForm | |
| 325 | - template_name = 'subjects/update.html' | |
| 326 | - | |
| 327 | - login_url = reverse_lazy("users:login") | |
| 328 | - redirect_field_name = 'next' | |
| 329 | - | |
| 330 | - def dispatch(self, request, *args, **kwargs): | |
| 331 | - user = self.request.user | |
| 332 | - | |
| 333 | - pk = user.pk | |
| 334 | - | |
| 335 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | |
| 336 | - if not user.is_staff: | |
| 337 | - if subject.count() == 0: | |
| 338 | - if request.META.get('HTTP_REFERER'): | |
| 339 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 340 | - else: | |
| 341 | - return redirect('subjects:index') | |
| 342 | - | |
| 343 | - if request.method.lower() in self.http_method_names: | |
| 344 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | |
| 345 | - else: | |
| 346 | - handler = self.http_method_not_allowed | |
| 347 | - return handler(request, *args, **kwargs) | |
| 348 | - | |
| 349 | - def get_context_data(self, **kwargs): | |
| 350 | - context = super(SubjectUpdateView, self).get_context_data(**kwargs) | |
| 351 | - context['title'] = _('Update Subject') | |
| 352 | - context['template_extends'] = 'categories/home.html' | |
| 353 | - context['subjects_menu_active'] = 'subjects_menu_active' | |
| 354 | - | |
| 355 | - return context | |
| 356 | - | |
| 357 | - def get_success_url(self): | |
| 358 | - if not self.object.category.visible: | |
| 359 | - self.object.visible = False | |
| 360 | - self.object.save() | |
| 361 | - | |
| 362 | - messages.success(self.request, _('Subject "%s" was updated on "%s" successfully!')%(self.object.name, self.object.category.name )) | |
| 363 | - 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') | |
| 364 | 401 | |
| 365 | 402 | class SubjectDeleteView(LoginRequiredMixin, LogMixin, DeleteView): |
| 403 | + log_component = 'subject' | |
| 404 | + log_action = 'delete' | |
| 405 | + log_resource = 'subject' | |
| 406 | + log_context = {} | |
| 366 | 407 | |
| 367 | - login_url = reverse_lazy("users:login") | |
| 368 | - redirect_field_name = 'next' | |
| 369 | - model = Subject | |
| 370 | - template_name = 'subjects/delete.html' | |
| 371 | - | |
| 372 | - def dispatch(self, request, *args, **kwargs): | |
| 373 | - user = self.request.user | |
| 374 | - | |
| 375 | - pk = user.pk | |
| 376 | - | |
| 377 | - subject = Subject.objects.filter((Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | |
| 378 | - if not user.is_staff: | |
| 379 | - if subject.count() == 0: | |
| 380 | - if request.META.get('HTTP_REFERER'): | |
| 381 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 382 | - else: | |
| 383 | - return redirect('subjects:index') | |
| 384 | - return super(SubjectDeleteView, self).dispatch(request, *args, **kwargs) | |
| 385 | - | |
| 386 | - def get(self, request, *args, **kwargs): | |
| 387 | - self.object = self.get_object() | |
| 388 | - if self.object.students.all().count() > 0: | |
| 389 | - messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated")) | |
| 390 | - | |
| 391 | - return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')}) | |
| 392 | - context = self.get_context_data(object=self.object) | |
| 393 | - return self.render_to_response(context) | |
| 394 | - def get_context_data(self, **kwargs): | |
| 395 | - context = super(SubjectDeleteView, self).get_context_data(**kwargs) | |
| 396 | - subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) | |
| 397 | - context['subject'] = subject | |
| 398 | - | |
| 399 | - | |
| 400 | - if (self.request.GET.get('view') == 'index'): | |
| 401 | - context['index'] = True | |
| 402 | - else: | |
| 403 | - context['index'] = False | |
| 404 | - | |
| 405 | - return context | |
| 406 | - | |
| 407 | - def get_success_url(self): | |
| 408 | - | |
| 409 | - | |
| 410 | - | |
| 411 | - messages.success(self.request, _('Subject "%s" removed successfully!')%(self.object.name)) | |
| 412 | - | |
| 413 | - return reverse_lazy('subjects:index') | |
| 414 | - | |
| 415 | - | |
| 416 | -class SubjectDetailView(LoginRequiredMixin, DetailView): | |
| 417 | - login_url = reverse_lazy("users:login") | |
| 418 | - redirect_field_name = 'next' | |
| 419 | - | |
| 420 | - model = Subject | |
| 421 | - template_name = 'subjects/view.html' | |
| 422 | - context_object_name = 'subject' | |
| 423 | - | |
| 424 | - def dispatch(self, request, *args,**kwargs): | |
| 425 | - user = request.user | |
| 426 | - pk = user.pk | |
| 427 | - if kwargs.get('slug') and not user.is_staff: | |
| 428 | - subject = get_object_or_404(Subject, slug = kwargs.get('slug')) | |
| 429 | - | |
| 430 | - subject = Subject.objects.filter((Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)) & Q(slug = kwargs.get('slug'))) | |
| 431 | - | |
| 432 | - if subject.count() == 0: | |
| 433 | - if request.META.get('HTTP_REFERER'): | |
| 434 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 435 | - else: | |
| 436 | - return redirect('subjects:home') | |
| 437 | - | |
| 438 | - if request.method.lower() in self.http_method_names: | |
| 439 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | |
| 440 | - else: | |
| 441 | - handler = self.http_method_not_allowed | |
| 442 | - return handler(request, *args, **kwargs) | |
| 443 | - | |
| 444 | - def get_context_data(self, **kwargs): | |
| 445 | - context = super(SubjectDetailView, self).get_context_data(**kwargs) | |
| 446 | - context['title'] = self.object.name | |
| 447 | - | |
| 448 | - resources = self.request.session.get('resources', None) | |
| 449 | - | |
| 450 | - if resources: | |
| 451 | - context['resource_new_page'] = resources['new_page'] | |
| 452 | - context['resource_new_page_url'] = resources['new_page_url'] | |
| 453 | - | |
| 454 | - self.request.session['resources'] = None | |
| 455 | - | |
| 456 | - if self.kwargs.get('topic_slug'): | |
| 457 | - context['topic_slug'] = self.kwargs.get('topic_slug') | |
| 458 | - | |
| 459 | - return context | |
| 460 | - | |
| 461 | - | |
| 462 | -class SubjectSubscribeView(LoginRequiredMixin, TemplateView): | |
| 463 | - | |
| 464 | - template_name = 'subjects/subscribe.html' | |
| 465 | - | |
| 466 | - def get_context_data(self, **kwargs): | |
| 467 | - context = super(SubjectSubscribeView, self).get_context_data(**kwargs) | |
| 468 | - context['subject'] = get_object_or_404(Subject, slug= kwargs.get('slug')) | |
| 469 | - | |
| 470 | - return context | |
| 471 | - | |
| 472 | - def post(self, request, *args, **kwargs): | |
| 473 | - subject = get_object_or_404(Subject, slug= kwargs.get('slug')) | |
| 474 | - if subject.subscribe_end < datetime.datetime.today().date(): | |
| 475 | - messages.error(self.request, _('Subscription date is due!')) | |
| 476 | - else: | |
| 477 | - subject.students.add(request.user) | |
| 478 | - subject.save() | |
| 479 | - messages.success(self.request, _('Subscription was successfull!')) | |
| 480 | - | |
| 481 | - return JsonResponse({'url':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 | + | |
| 463 | + | |
| 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 | |
| 525 | + | |
| 526 | +class SubjectSubscribeView(LoginRequiredMixin, LogMixin, TemplateView): | |
| 527 | + log_component = 'subject' | |
| 528 | + log_action = 'subscribe' | |
| 529 | + log_resource = 'subject' | |
| 530 | + log_context = {} | |
| 531 | + | |
| 532 | + template_name = 'subjects/subscribe.html' | |
| 533 | + | |
| 534 | + def get_context_data(self, **kwargs): | |
| 535 | + context = super(SubjectSubscribeView, self).get_context_data(**kwargs) | |
| 536 | + context['subject'] = get_object_or_404(Subject, slug= kwargs.get('slug')) | |
| 537 | + | |
| 538 | + return context | |
| 539 | + | |
| 540 | + def post(self, request, *args, **kwargs): | |
| 541 | + subject = get_object_or_404(Subject, slug= kwargs.get('slug')) | |
| 542 | + | |
| 543 | + if subject.subscribe_end < datetime.datetime.today().date(): | |
| 544 | + messages.error(self.request, _('Subscription date is due!')) | |
| 545 | + else: | |
| 546 | + self.log_context['category_id'] = subject.category.id | |
| 547 | + self.log_context['category_name'] = subject.category.name | |
| 548 | + self.log_context['category_slug'] = subject.category.slug | |
| 549 | + self.log_context['subject_id'] = subject.id | |
| 550 | + self.log_context['subject_name'] = subject.name | |
| 551 | + self.log_context['subject_slug'] = subject.slug | |
| 552 | + | |
| 553 | + super(SubjectSubscribeView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
| 554 | + | |
| 555 | + subject.students.add(request.user) | |
| 556 | + subject.save() | |
| 557 | + messages.success(self.request, _('Subscription was successfull!')) | |
| 558 | + | |
| 559 | + return JsonResponse({'url':reverse_lazy('subjects:index')}) | |
| 482 | 560 | |
| 483 | 561 | |
| 484 | 562 | class SubjectSearchView(LoginRequiredMixin, ListView): |
| 485 | - login_url = reverse_lazy("users:login") | |
| 486 | - template_name = 'subjects/list_search.html' | |
| 487 | - context_object_name = 'subjects' | |
| 488 | - paginate_by = 10 | |
| 489 | - | |
| 490 | - def dispatch(self, request, *args, **kwargs): | |
| 491 | - # Try to dispatch to the right method; if a method doesn't exist, | |
| 492 | - # defer to the error handler. Also defer to the error handler if the | |
| 493 | - # request method isn't on the approved list. | |
| 494 | - tags = request.GET.get('search') | |
| 495 | - tags = tags.split(" ") | |
| 496 | - | |
| 497 | - if tags[0] == '': | |
| 498 | - return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 499 | - if request.method.lower() in self.http_method_names: | |
| 500 | - handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | |
| 501 | - else: | |
| 502 | - handler = self.http_method_not_allowed | |
| 503 | - return handler(request, *args, **kwargs) | |
| 563 | + login_url = reverse_lazy("users:login") | |
| 564 | + template_name = 'subjects/list_search.html' | |
| 565 | + context_object_name = 'subjects' | |
| 566 | + paginate_by = 10 | |
| 567 | + | |
| 568 | + def dispatch(self, request, *args, **kwargs): | |
| 569 | + # Try to dispatch to the right method; if a method doesn't exist, | |
| 570 | + # defer to the error handler. Also defer to the error handler if the | |
| 571 | + # request method isn't on the approved list. | |
| 572 | + tags = request.GET.get('search') | |
| 573 | + tags = tags.split(" ") | |
| 574 | + | |
| 575 | + if tags[0] == '': | |
| 576 | + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) | |
| 577 | + if request.method.lower() in self.http_method_names: | |
| 578 | + handler = getattr(self, request.method.lower(), self.http_method_not_allowed) | |
| 579 | + else: | |
| 580 | + handler = self.http_method_not_allowed | |
| 581 | + return handler(request, *args, **kwargs) | |
| 504 | 582 | |
| 505 | - def get_queryset(self): | |
| 506 | - | |
| 507 | - tags = self.request.GET.get('search') | |
| 508 | - | |
| 509 | - self.tags = tags | |
| 510 | - tags = tags.split(" ") | |
| 511 | - | |
| 512 | - | |
| 513 | - subjects = Subject.objects.filter(tags__name__in=tags) | |
| 514 | - #pk = self.request.user.pk | |
| 515 | - #my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct() | |
| 516 | - | |
| 517 | - self.totals = {'all_subjects': subjects.count(), 'my_subjects': subjects.count()} | |
| 518 | - #if self.kwargs.get('option'): | |
| 519 | - # subjects = my_subjects | |
| 520 | - return subjects | |
| 583 | + def get_queryset(self): | |
| 584 | + | |
| 585 | + tags = self.request.GET.get('search') | |
| 586 | + | |
| 587 | + self.tags = tags | |
| 588 | + tags = tags.split(" ") | |
| 589 | + | |
| 590 | + | |
| 591 | + subjects = Subject.objects.filter(tags__name__in=tags) | |
| 592 | + #pk = self.request.user.pk | |
| 593 | + #my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct() | |
| 594 | + | |
| 595 | + self.totals = {'all_subjects': subjects.count(), 'my_subjects': subjects.count()} | |
| 596 | + #if self.kwargs.get('option'): | |
| 597 | + # subjects = my_subjects | |
| 598 | + return subjects | |
| 521 | 599 | |
| 522 | - def get_context_data(self, **kwargs): | |
| 523 | - context = super(SubjectSearchView, self).get_context_data(**kwargs) | |
| 524 | - | |
| 525 | - context['tags'] = self.tags | |
| 526 | - context['all'] = False | |
| 527 | - context['title'] = _('Subjects') | |
| 528 | - | |
| 529 | - context['show_buttons'] = True #So it shows subscribe and access buttons | |
| 530 | - context['totals'] = self.totals | |
| 531 | - | |
| 532 | - if self.kwargs.get('option'): | |
| 533 | - context['all'] = True | |
| 534 | - context['title'] = _('Subjects') | |
| 535 | - | |
| 536 | - context['subjects_menu_active'] = 'subjects_menu_active' | |
| 537 | - | |
| 538 | - return context | |
| 600 | + def get_context_data(self, **kwargs): | |
| 601 | + context = super(SubjectSearchView, self).get_context_data(**kwargs) | |
| 602 | + | |
| 603 | + context['tags'] = self.tags | |
| 604 | + context['all'] = False | |
| 605 | + context['title'] = _('Subjects') | |
| 606 | + | |
| 607 | + context['show_buttons'] = True #So it shows subscribe and access buttons | |
| 608 | + context['totals'] = self.totals | |
| 609 | + | |
| 610 | + if self.kwargs.get('option'): | |
| 611 | + context['all'] = True | |
| 612 | + context['title'] = _('Subjects') | |
| 613 | + | |
| 614 | + context['subjects_menu_active'] = 'subjects_menu_active' | |
| 615 | + | |
| 616 | + return context | |
| 539 | 617 | |
| 618 | +@log_decorator_ajax('subject', 'view', 'subject') | |
| 619 | +def subject_view_log(request, subject): | |
| 620 | + action = request.GET.get('action') | |
| 621 | + | |
| 622 | + if action == 'open': | |
| 623 | + subject = get_object_or_404(Subject, id = subject) | |
| 624 | + | |
| 625 | + log_context = {} | |
| 626 | + log_context['category_id'] = subject.category.id | |
| 627 | + log_context['category_name'] = subject.category.name | |
| 628 | + log_context['category_slug'] = subject.category.slug | |
| 629 | + log_context['subject_id'] = subject.id | |
| 630 | + log_context['subject_name'] = subject.name | |
| 631 | + log_context['subject_slug'] = subject.slug | |
| 632 | + log_context['timestamp_start'] = str(int(time.time())) | |
| 633 | + log_context['timestamp_end'] = '-1' | |
| 634 | + | |
| 635 | + request.log_context = log_context | |
| 636 | + | |
| 637 | + log_id = Log.objects.latest('id').id | |
| 638 | + | |
| 639 | + return JsonResponse({'message': 'ok', 'log_id': log_id}) | |
| 640 | + | |
| 641 | + return JsonResponse({'message': 'ok'}) | |
| 540 | 642 | ... | ... |