Commit cdaefc85e5133d81cc51ae3d54a4d13f89b2b98b

Authored by Felipe Henrique de Almeida Bormann
1 parent 4096a888

fixed subject count and case where the search field is empty now does not goes f…

…urther, it stays on the same page
Showing 1 changed file with 23 additions and 6 deletions   Show diff stats
subjects/views.py
... ... @@ -6,7 +6,7 @@ from django.core.urlresolvers import reverse_lazy
6 6 from rolepermissions.verifications import has_role
7 7 from django.db.models import Q
8 8 from django.contrib import messages
9   -from django.http import HttpResponse, JsonResponse
  9 +from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
10 10 from django.utils.translation import ugettext_lazy as _
11 11  
12 12 from django.contrib.auth.mixins import LoginRequiredMixin
... ... @@ -346,20 +346,37 @@ class SubjectSearchView(LoginRequiredMixin, ListView):
346 346 template_name = 'subjects/list_search.html'
347 347 context_object_name = 'subjects'
348 348 paginate_by = 10
  349 +
  350 + def dispatch(self, request, *args, **kwargs):
  351 + # Try to dispatch to the right method; if a method doesn't exist,
  352 + # defer to the error handler. Also defer to the error handler if the
  353 + # request method isn't on the approved list.
  354 + tags = request.GET.get('search')
  355 + tags = tags.split(" ")
  356 +
  357 + if tags[0] == '':
  358 + return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
  359 + if request.method.lower() in self.http_method_names:
  360 + handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
  361 + else:
  362 + handler = self.http_method_not_allowed
  363 + return handler(request, *args, **kwargs)
349 364  
350 365 def get_queryset(self):
351 366  
352 367 tags = self.request.GET.get('search')
  368 +
353 369 self.tags = tags
354 370 tags = tags.split(" ")
355 371  
  372 +
356 373 subjects = Subject.objects.filter(tags__name__in=tags)
357   - pk = self.request.user.pk
358   - my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct()
  374 + #pk = self.request.user.pk
  375 + #my_subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk) & Q(tags__name__in=tags) ).distinct()
359 376  
360   - self.totals = {'all_subjects': subjects.count(), 'my_subjects': my_subjects.count()}
361   - if self.kwargs.get('option'):
362   - subjects = my_subjects
  377 + self.totals = {'all_subjects': subjects.count(), 'my_subjects': subjects.count()}
  378 + #if self.kwargs.get('option'):
  379 + # subjects = my_subjects
363 380 return subjects
364 381  
365 382 def get_context_data(self, **kwargs):
... ...