Sith/matmat/views.py

218 lines
7.1 KiB
Python
Raw Normal View History

2017-07-22 20:35:01 +00:00
# -*- coding:utf-8 -*
#
# Copyright 2017
# - Sli <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from ast import literal_eval
2017-07-23 13:03:04 +00:00
from enum import Enum
2017-07-22 20:35:01 +00:00
2017-07-23 10:54:12 +00:00
from django.views.generic import ListView, View
2017-07-22 20:35:01 +00:00
from django.views.generic.edit import FormView
from django.utils.translation import ugettext_lazy as _
from django.views.generic.detail import SingleObjectMixin
2017-07-23 10:54:12 +00:00
from django.http.response import HttpResponseRedirect
from django.core.urlresolvers import reverse
2017-07-22 20:35:01 +00:00
from django import forms
from core.models import User
from core.views import WasSuscribed
from core.views.forms import SelectDate
2017-07-23 13:03:04 +00:00
from core.views import search_user
2017-07-22 20:35:01 +00:00
from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget
2017-07-23 13:03:04 +00:00
# Enum to select search type
class SearchType(Enum):
NORMAL = 1
REVERSE = 2
QUICK = 3
2017-07-22 20:35:01 +00:00
# Custom form
class SearchForm(forms.ModelForm):
class Meta:
model = User
fields = [
'first_name',
'last_name',
'nick_name',
'role',
'department',
'semester',
'promo',
'date_of_birth',
'phone',
]
widgets = {
'date_of_birth': SelectDate,
'phone': PhoneNumberInternationalFallbackWidget,
}
2017-07-23 10:54:12 +00:00
sex = forms.ChoiceField([
("MAN", _("Man")),
("WOMAN", _("Woman")),
("INDIFFERENT", _("Indifferent"))
], widget=forms.RadioSelect, initial="INDIFFERENT")
2017-07-23 13:03:04 +00:00
quick = forms.CharField(label=_('Last/First name or nickname'), max_length=255)
2017-07-22 20:35:01 +00:00
def __init__(self, *args, **kwargs):
super(SearchForm, self).__init__(*args, **kwargs)
for key in self.fields.keys():
self.fields[key].required = False
2017-07-23 10:54:12 +00:00
@property
def cleaned_data_json(self):
data = self.cleaned_data
for key in data.keys():
if key in ('date_of_birth', 'phone') and data[key] is not None:
data[key] = str(data[key])
return data
2017-07-22 20:35:01 +00:00
# Views
2017-07-23 10:54:12 +00:00
2017-07-22 20:35:01 +00:00
class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
model = User
2017-07-23 13:03:04 +00:00
ordering = ["-id"]
paginate_by = 12
2017-07-22 20:35:01 +00:00
template_name = 'matmat/search_form.jinja'
def dispatch(self, request, *args, **kwargs):
self.form_class = kwargs['form']
2017-07-23 13:03:04 +00:00
self.search_type = kwargs['search_type']
self.session = request.session
self.last_search = self.session.get('matmat_search_result', str([]))
self.last_search = literal_eval(self.last_search)
2017-07-22 20:35:01 +00:00
if 'valid_form' in kwargs.keys():
self.valid_form = kwargs['valid_form']
else:
self.valid_form = None
self.init_query = self.model.objects
2017-07-23 13:03:04 +00:00
self.can_see_hidden = True
2017-07-22 20:35:01 +00:00
if not (request.user.is_board_member or request.user.is_root):
2017-07-23 13:03:04 +00:00
self.can_see_hidden = False
2017-07-22 20:35:01 +00:00
self.init_query = self.init_query.exclude(is_subscriber_viewable=False)
return super(SearchFormListView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
self.object = None
kwargs = super(SearchFormListView, self).get_context_data(**kwargs)
2017-07-23 10:54:12 +00:00
kwargs['form'] = self.form_class
kwargs['result_exists'] = self.result_exists
2017-07-22 20:35:01 +00:00
return kwargs
def get_queryset(self):
q = self.init_query
2017-07-22 20:35:01 +00:00
if self.valid_form is not None:
2017-07-23 13:03:04 +00:00
if self.search_type == SearchType.REVERSE:
q = q.filter(phone=self.valid_form['phone']).all()
2017-07-23 13:03:04 +00:00
elif self.search_type == SearchType.QUICK:
if self.valid_form['quick'].strip():
q = search_user(self.valid_form['quick'])
else:
q = []
if not self.can_see_hidden and len(q) > 0:
2017-07-23 13:03:04 +00:00
q = [user for user in q if user.is_subscriber_viewable]
2017-07-22 20:35:01 +00:00
else:
2017-07-23 11:42:13 +00:00
search_dict = {}
for key, value in self.valid_form.items():
2017-07-23 13:03:04 +00:00
if key not in ('phone', 'quick') and not (value == '' or value is None or value == 'INDIFFERENT'):
2017-07-23 11:42:13 +00:00
search_dict[key + "__icontains"] = value
q = q.filter(**search_dict).all()
2017-07-22 20:35:01 +00:00
else:
q = q.filter(pk__in=self.last_search).all()
2017-07-23 13:03:04 +00:00
if isinstance(q, list):
self.result_exists = len(q) > 0
else:
self.result_exists = q.exists()
self.last_search = []
for user in q:
self.last_search.append(user.id)
self.session['matmat_search_result'] = str(self.last_search)
return q
2017-07-22 20:35:01 +00:00
class SearchFormView(WasSuscribed, FormView):
"""
Allows users to search inside the user list
"""
form_class = SearchForm
def dispatch(self, request, *args, **kwargs):
2017-07-23 10:54:12 +00:00
self.session = request.session
2017-07-22 20:35:01 +00:00
self.init_query = User.objects
2017-07-23 10:54:12 +00:00
kwargs['form'] = self.get_form()
2017-07-23 13:03:04 +00:00
kwargs['search_type'] = self.search_type
2017-07-22 20:35:01 +00:00
return super(SearchFormView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
view = SearchFormListView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
view = SearchFormListView.as_view()
if form.is_valid():
kwargs['valid_form'] = form.clean()
request.session['matmat_search_form'] = form.cleaned_data_json
2017-07-22 20:35:01 +00:00
return view(request, *args, **kwargs)
def get_initial(self):
init = self.session.get('matmat_search_form', {})
if not init:
init['department'] = ''
return init
2017-07-22 20:35:01 +00:00
2017-07-23 13:03:04 +00:00
class SearchNormalFormView(SearchFormView):
search_type = SearchType.NORMAL
2017-07-22 20:35:01 +00:00
class SearchReverseFormView(SearchFormView):
2017-07-23 13:03:04 +00:00
search_type = SearchType.REVERSE
class SearchQuickFormView(SearchFormView):
search_type = SearchType.QUICK
2017-07-23 10:54:12 +00:00
class SearchClearFormView(WasSuscribed, View):
"""
Clear SearchFormView and redirect to it
"""
def dispatch(self, request, *args, **kwargs):
super(SearchClearFormView, self).dispatch(request, *args, **kwargs)
if 'matmat_search_form' in request.session.keys():
request.session.pop('matmat_search_form')
if 'matmat_search_result' in request.session.keys():
request.session.pop('matmat_search_result')
2017-07-23 10:54:12 +00:00
return HttpResponseRedirect(reverse('matmat:search'))