diff --git a/subjects/models.py b/subjects/models.py index 5c0f667..9f5fcd9 100644 --- a/subjects/models.py +++ b/subjects/models.py @@ -42,6 +42,7 @@ class Subject(models.Model): class Meta: verbose_name = "Subject" verbose_name_plural = "Subjects" + ordering = ['name'] def __str__(self): return self.name diff --git a/subjects/static/subjects/js/modal_subject.js b/subjects/static/subjects/js/modal_subject.js index 7088b0e..413b938 100644 --- a/subjects/static/subjects/js/modal_subject.js +++ b/subjects/static/subjects/js/modal_subject.js @@ -20,8 +20,12 @@ var delete_subject = { if($(id_modal).length){ $(id_div_modal).empty(); } - $(id_div_modal).append(data); - $(id_modal).modal('show'); + if (!data['error']){ //If there is no error in the removing process, no message is shown + $(id_div_modal).append(data); + $(id_modal).modal('show'); + }else{ + window.location.href = data['url']; // If there is a error, we redirect to another URL + } }); } }; \ No newline at end of file diff --git a/subjects/templates/subjects/list.html b/subjects/templates/subjects/list.html index b493a56..3bb5f51 100644 --- a/subjects/templates/subjects/list.html +++ b/subjects/templates/subjects/list.html @@ -98,7 +98,7 @@ {% endif %}
- {% for subject in category.subject_category.all %} + {% for subject in category.subject_category.all %} {% if request.user in subject.students.all or request.user.is_staff or request.user in subject.professor.all %} {% include "subjects/subject_card.html" %} @@ -171,7 +171,7 @@ - + {% endblock %} diff --git a/subjects/views.py b/subjects/views.py index 0fa32f2..b8a054f 100644 --- a/subjects/views.py +++ b/subjects/views.py @@ -32,7 +32,7 @@ from users.models import User class HomeView(LoginRequiredMixin, ListView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' - queryset = Subject.objects.all() + queryset = Subject.objects.all().order_by('name') template_name = 'subjects/initial.html' context_object_name = 'subjects' paginate_by = 10 @@ -63,7 +63,7 @@ class IndexView(LoginRequiredMixin, ListView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' - queryset = Category.objects.all() + queryset = Category.objects.all().order_by('name') template_name = 'subjects/list.html' context_object_name = 'categories' paginate_by = 10 @@ -73,7 +73,7 @@ class IndexView(LoginRequiredMixin, ListView): if not self.request.user.is_staff: if not self.kwargs.get('option'): - categories = Category.objects.all() + categories = Category.objects.all().order_by('name') categories = [category for category in categories if self.request.user in category.coordinators.all() \ 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): template_name = 'subjects/delete.html' def dispatch(self, *args, **kwargs): - + return super(SubjectDeleteView, self).dispatch(*args, **kwargs) + def get(self, request, *args, **kwargs): + self.object = self.get_object() + if self.object.students.all().count() > 0: + messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated")) + + return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')}) + context = self.get_context_data(object=self.object) + return self.render_to_response(context) def get_context_data(self, **kwargs): context = super(SubjectDeleteView, self).get_context_data(**kwargs) - context['subject'] = get_object_or_404(Subject, slug = self.kwargs.get('slug')) + subject = get_object_or_404(Subject, slug = self.kwargs.get('slug')) + context['subject'] = subject + if (self.request.GET.get('view') == 'index'): context['index'] = True else: -- libgit2 0.21.2