Commit 242d62ce573e1c3316d367033d7a4c37162f3f8d
1 parent
328ac689
Exists in
master
and in
3 other branches
removing subject is 100% complete, function as expected
Showing
4 changed files
with
24 additions
and
9 deletions
Show diff stats
subjects/models.py
subjects/static/subjects/js/modal_subject.js
... | ... | @@ -20,8 +20,12 @@ var delete_subject = { |
20 | 20 | if($(id_modal).length){ |
21 | 21 | $(id_div_modal).empty(); |
22 | 22 | } |
23 | - $(id_div_modal).append(data); | |
24 | - $(id_modal).modal('show'); | |
23 | + if (!data['error']){ //If there is no error in the removing process, no message is shown | |
24 | + $(id_div_modal).append(data); | |
25 | + $(id_modal).modal('show'); | |
26 | + }else{ | |
27 | + window.location.href = data['url']; // If there is a error, we redirect to another URL | |
28 | + } | |
25 | 29 | }); |
26 | 30 | } |
27 | 31 | }; |
28 | 32 | \ No newline at end of file | ... | ... |
subjects/templates/subjects/list.html
... | ... | @@ -98,7 +98,7 @@ |
98 | 98 | {% endif %} |
99 | 99 | |
100 | 100 | <div class="panel-group subject-group" id="{{ category.slug }}-accordion" role="tablist" aria-multiselectable="true"> |
101 | - {% for subject in category.subject_category.all %} | |
101 | + {% for subject in category.subject_category.all %} | |
102 | 102 | {% if request.user in subject.students.all or request.user.is_staff or request.user in subject.professor.all %} |
103 | 103 | |
104 | 104 | {% include "subjects/subject_card.html" %} |
... | ... | @@ -171,7 +171,7 @@ |
171 | 171 | <div id="modal_course"> |
172 | 172 | </div> |
173 | 173 | |
174 | - <div id="modal_subjct"></div> | |
174 | + <div id="modal_subject"></div> | |
175 | 175 | |
176 | 176 | <script type="text/javascript" src="{% static 'js/course.js' %}"></script> |
177 | 177 | {% endblock %} | ... | ... |
subjects/views.py
... | ... | @@ -32,7 +32,7 @@ from users.models import User |
32 | 32 | class HomeView(LoginRequiredMixin, ListView): |
33 | 33 | login_url = reverse_lazy("users:login") |
34 | 34 | redirect_field_name = 'next' |
35 | - queryset = Subject.objects.all() | |
35 | + queryset = Subject.objects.all().order_by('name') | |
36 | 36 | template_name = 'subjects/initial.html' |
37 | 37 | context_object_name = 'subjects' |
38 | 38 | paginate_by = 10 |
... | ... | @@ -63,7 +63,7 @@ class IndexView(LoginRequiredMixin, ListView): |
63 | 63 | |
64 | 64 | login_url = reverse_lazy("users:login") |
65 | 65 | redirect_field_name = 'next' |
66 | - queryset = Category.objects.all() | |
66 | + queryset = Category.objects.all().order_by('name') | |
67 | 67 | template_name = 'subjects/list.html' |
68 | 68 | context_object_name = 'categories' |
69 | 69 | paginate_by = 10 |
... | ... | @@ -73,7 +73,7 @@ class IndexView(LoginRequiredMixin, ListView): |
73 | 73 | |
74 | 74 | if not self.request.user.is_staff: |
75 | 75 | if not self.kwargs.get('option'): |
76 | - categories = Category.objects.all() | |
76 | + categories = Category.objects.all().order_by('name') | |
77 | 77 | |
78 | 78 | categories = [category for category in categories if self.request.user in category.coordinators.all() \ |
79 | 79 | or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)] |
... | ... | @@ -205,13 +205,23 @@ class SubjectDeleteView(LoginRequiredMixin, LogMixin, DeleteView): |
205 | 205 | template_name = 'subjects/delete.html' |
206 | 206 | |
207 | 207 | def dispatch(self, *args, **kwargs): |
208 | - | |
208 | + | |
209 | 209 | return super(SubjectDeleteView, self).dispatch(*args, **kwargs) |
210 | 210 | |
211 | + def get(self, request, *args, **kwargs): | |
212 | + self.object = self.get_object() | |
213 | + if self.object.students.all().count() > 0: | |
214 | + messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated")) | |
215 | + | |
216 | + return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')}) | |
217 | + context = self.get_context_data(object=self.object) | |
218 | + return self.render_to_response(context) | |
211 | 219 | def get_context_data(self, **kwargs): |
212 | 220 | context = super(SubjectDeleteView, self).get_context_data(**kwargs) |
213 | - context['subject'] = get_object_or_404(Subject, slug = self.kwargs.get('slug')) | |
221 | + subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) | |
222 | + context['subject'] = subject | |
214 | 223 | |
224 | + | |
215 | 225 | if (self.request.GET.get('view') == 'index'): |
216 | 226 | context['index'] = True |
217 | 227 | else: | ... | ... |