Sith/matmat/views.py

165 lines
5.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.
#
#
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
from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget
# 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-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
template_name = 'matmat/search_form.jinja'
2017-07-23 10:54:12 +00:00
paginate_by = 3
2017-07-22 20:35:01 +00:00
def dispatch(self, request, *args, **kwargs):
self.form_class = kwargs['form']
self.reverse = kwargs['reverse']
if 'valid_form' in kwargs.keys():
self.valid_form = kwargs['valid_form']
else:
self.valid_form = None
self.init_query = self.model.objects
if not (request.user.is_board_member or request.user.is_root):
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
2017-07-22 20:35:01 +00:00
return kwargs
def get_queryset(self):
if self.valid_form is not None:
if self.reverse:
return self.init_query.filter(phone=self.valid_form['phone']).all()
else:
q = self.init_query
# f = self.valid_form
return q.all()
else:
return self.model.objects.none()
class SearchFormView(WasSuscribed, FormView):
"""
Allows users to search inside the user list
"""
form_class = SearchForm
reverse = False
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-22 20:35:01 +00:00
kwargs['reverse'] = self.reverse
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()
2017-07-23 10:54:12 +00:00
request.session['matmat_search'] = form.cleaned_data_json
2017-07-22 20:35:01 +00:00
return view(request, *args, **kwargs)
def get_initial(self):
2017-07-23 10:54:12 +00:00
return self.session.get('matmat_search', {})
2017-07-22 20:35:01 +00:00
class SearchReverseFormView(SearchFormView):
reverse = True
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)
request.session.pop('matmat_search')
return HttpResponseRedirect(reverse('matmat:search'))