Commit 9bc93f76ebd2ee44111edde23a1f9cadfe94885c
1 parent
3a6a41bd
Exists in
master
and in
5 other branches
Search view Finished [Issue:#251]
Showing
1 changed file
with
29 additions
and
39 deletions
Show diff stats
users/views.py
| @@ -176,63 +176,53 @@ class SearchView(LoginRequiredMixin, generic.ListView): | @@ -176,63 +176,53 @@ class SearchView(LoginRequiredMixin, generic.ListView): | ||
| 176 | 176 | ||
| 177 | def get_context_data(self, **kwargs): | 177 | def get_context_data(self, **kwargs): |
| 178 | context = super(SearchView, self).get_context_data(**kwargs) | 178 | context = super(SearchView, self).get_context_data(**kwargs) |
| 179 | - #result_list = [] | ||
| 180 | search = self.request.GET.get('search', None) | 179 | search = self.request.GET.get('search', None) |
| 181 | link_list = [] | 180 | link_list = [] |
| 182 | file_list = [] | 181 | file_list = [] |
| 183 | poll_list = [] | 182 | poll_list = [] |
| 184 | exam_list = [] | 183 | exam_list = [] |
| 185 | forum_list = [] | 184 | forum_list = [] |
| 186 | - if has_role(self.request.user,'professor'): | 185 | + qtd = 0 |
| 186 | + | ||
| 187 | + if has_role(self.request.user,'system_admin'): | ||
| 188 | + if search != '': | ||
| 189 | + link_list = Link.objects.filter( Q(name__icontains=search)).order_by('name') | ||
| 190 | + file_list = TopicFile.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 191 | + poll_list = Poll.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 192 | + exam_list = Exam.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 193 | + forum_list = Forum.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 194 | + qtd = len(link_list) + len(file_list) + len(poll_list) + len(exam_list) + len(forum_list) | ||
| 195 | + | ||
| 196 | + elif has_role(self.request.user,'professor'): | ||
| 187 | topics = Topic.objects.filter(owner = self.request.user) | 197 | topics = Topic.objects.filter(owner = self.request.user) |
| 188 | links = Link.objects.all() | 198 | links = Link.objects.all() |
| 189 | files = TopicFile.objects.all() | 199 | files = TopicFile.objects.all() |
| 190 | polls = Poll.objects.all() | 200 | polls = Poll.objects.all() |
| 191 | exams = Exam.objects.all() | 201 | exams = Exam.objects.all() |
| 192 | forums = Forum.objects.all() | 202 | forums = Forum.objects.all() |
| 193 | - link_list = sorted([link for link in links for topic in topics if link.topic == topic],key = lambda x:x.name) | ||
| 194 | - exam_list = sorted([exam for exam in exams for topic in topics if exam.topic == topic],key = lambda x:x.name) | ||
| 195 | - file_list = sorted([arquivo for arquivo in files for topic in topics if arquivo.topic == topic],key = lambda x:x.name) | ||
| 196 | - poll_list = sorted([poll for poll in polls for topic in topics if poll.topic == topic],key = lambda x:x.name) | ||
| 197 | - forum_list = sorted([forum for forum in forums for topic in topics if forum.topic == topic],key = lambda x:x.name) | ||
| 198 | - elif has_role(self.request.user,'system_admin'): | ||
| 199 | - link_list = Link.objects.filter( Q(name__icontains=search)).order_by('name') | ||
| 200 | - file_list = TopicFile.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 201 | - poll_list = Poll.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 202 | - exam_list = Exam.objects.filter(Q(name__icontains=search)).order_by('name') | ||
| 203 | - forum_list = Forum.objects.filter(Q(name__icontains=search)).order_by('name') | 203 | + if search != '': |
| 204 | + link_list = sorted([link for link in links for topic in topics if (link.topic == topic) and ( search in link.name ) ],key = lambda x:x.name) | ||
| 205 | + exam_list = sorted([exam for exam in exams for topic in topics if (exam.topic == topic) and ( search in exam.name ) ],key = lambda x:x.name) | ||
| 206 | + file_list = sorted([arquivo for arquivo in files for topic in topics if (arquivo.topic == topic) and (search in arquivo.name ) ],key = lambda x:x.name) | ||
| 207 | + poll_list = sorted([poll for poll in polls for topic in topics if (poll.topic == topic) and ( search in poll.name ) ],key = lambda x:x.name) | ||
| 208 | + forum_list = sorted([forum for forum in forums for topic in topics if (forum.topic == topic) and ( search in forum.name ) ],key = lambda x:x.name) | ||
| 209 | + qtd = len(link_list) + len(file_list) + len(poll_list) + len(exam_list) + len(forum_list) | ||
| 210 | + | ||
| 204 | elif has_role(self.request.user, 'student'): | 211 | elif has_role(self.request.user, 'student'): |
| 205 | - link_list = Link.objects.filter( Q(name__icontains=search) and Q(students__name = self.request.user.name)).order_by('name') | ||
| 206 | - file_list = TopicFile.objects.filter(Q(name__icontains=search) and Q(students__name = self.request.user.name)).order_by('name') | ||
| 207 | - poll_list = Poll.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 208 | - exam_list = Exam.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 209 | - forum_list = Forum.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 210 | - | ||
| 211 | - ''' | ||
| 212 | - result_list = list(chain(link_list,file_list,poll_list,exam_list,forum_list)) | ||
| 213 | - for x in result_list: | ||
| 214 | - if isinstance(x,Link): | ||
| 215 | - print(x) | ||
| 216 | - paginator = Paginator(result_list, self.paginate_by) | ||
| 217 | - print(paginator.num_pages) | ||
| 218 | - print(paginator.count) | ||
| 219 | - page = self.request.GET.get('page') | ||
| 220 | - try: | ||
| 221 | - result_list = paginator.page(page) | ||
| 222 | - print (result_list) | ||
| 223 | - except PageNotAnInteger: | ||
| 224 | - result_list = paginator.page(1) | ||
| 225 | - except EmptyPage: | ||
| 226 | - #print ((paginator.num_pages)) | ||
| 227 | - result_list = paginator.page(paginator.num_pages) | ||
| 228 | - | ||
| 229 | - ''' | 212 | + if search != '': |
| 213 | + link_list = Link.objects.filter( Q(name__icontains=search) and Q(students__name = self.request.user.name)).order_by('name') | ||
| 214 | + file_list = TopicFile.objects.filter(Q(name__icontains=search) and Q(students__name = self.request.user.name)).order_by('name') | ||
| 215 | + poll_list = Poll.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 216 | + exam_list = Exam.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 217 | + forum_list = Forum.objects.filter(Q(name__icontains=search)and Q(students__name = self.request.user.name)).order_by('name') | ||
| 218 | + qtd = len(link_list) + len(file_list) + len(poll_list) + len(exam_list) + len(forum_list) | ||
| 219 | + | ||
| 230 | 220 | ||
| 231 | context['link_list'] = link_list | 221 | context['link_list'] = link_list |
| 232 | context['file_list'] = file_list | 222 | context['file_list'] = file_list |
| 233 | context['poll_list'] = poll_list | 223 | context['poll_list'] = poll_list |
| 234 | context['exam_list'] = exam_list | 224 | context['exam_list'] = exam_list |
| 235 | context['forum_list'] = forum_list | 225 | context['forum_list'] = forum_list |
| 236 | - #context['result_list'] = result_list | 226 | + context['qtd'] = qtd |
| 237 | 227 | ||
| 238 | return context | 228 | return context |