Commit 86d62f12 authored by Sli's avatar Sli

Fully functionnal Matmatronch

parent 349475cd
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<form action="{{ url('matmat:search') }}" method="post" enctype="multipart/form-data"> <form action="{{ url('matmat:search') }}" method="post" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{% for field in form %} {% for field in form %}
{% if field.name != 'phone' %} {% if field.name not in ('phone', 'quick') %}
<p> <p>
{{ field.errors }} {{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label> <label for="{{ field.id_for_label }}">{{ field.label }}</label>
...@@ -59,13 +59,16 @@ ...@@ -59,13 +59,16 @@
<p><input type="submit" value="{% trans %}Search{% endtrans %}" /></p> <p><input type="submit" value="{% trans %}Search{% endtrans %}" /></p>
</p> </p>
</form> </form>
<h3>{% trans %}Simple search{% endtrans %}</h3> <h3>{% trans %}Quick search{% endtrans %}</h3>
<form action="{{ url('matmat:search_quick') }}" method="post">
{% csrf_token %} {% csrf_token %}
<form action="{{ url('core:search') }}" method="GET"> <p>
<label for="search">{% trans %}Last/First name or nickname{% endtrans %}</label> {{ form.quick.errors }}
<input type="text" name="query" id="search" /> <label for="{{ form.quick.id_for_label }}">{{ form.quick.label }}</label>
<p><input type="submit" value="{% trans %}Search{% endtrans %}" /></p> {{ form.quick }}
</form> <p><input type="submit" value="{% trans %}Search{% endtrans %}" /></p>
</p>
</form>
{% endblock %} {% endblock %}
{% block script %} {% block script %}
......
...@@ -27,7 +27,8 @@ from django.conf.urls import url ...@@ -27,7 +27,8 @@ from django.conf.urls import url
from matmat.views import * from matmat.views import *
urlpatterns = [ urlpatterns = [
url(r'^search$', SearchFormView.as_view(), name="search"), url(r'^$', SearchNormalFormView.as_view(), name="search"),
url(r'^search/reverse$', SearchReverseFormView.as_view(), name="search_reverse"), url(r'^reverse$', SearchReverseFormView.as_view(), name="search_reverse"),
url(r'^quick$', SearchQuickFormView.as_view(), name="search_quick"),
url(r'^clear$', SearchClearFormView.as_view(), name="search_clear"), url(r'^clear$', SearchClearFormView.as_view(), name="search_clear"),
] ]
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
# #
# #
from ast import literal_eval from ast import literal_eval
from enum import Enum
from django.views.generic import ListView, View from django.views.generic import ListView, View
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
...@@ -34,8 +35,18 @@ from django import forms ...@@ -34,8 +35,18 @@ from django import forms
from core.models import User from core.models import User
from core.views import WasSuscribed from core.views import WasSuscribed
from core.views.forms import SelectDate from core.views.forms import SelectDate
from core.views import search_user
from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget
# Enum to select search type
class SearchType(Enum):
NORMAL = 1
REVERSE = 2
QUICK = 3
# Custom form # Custom form
...@@ -64,6 +75,8 @@ class SearchForm(forms.ModelForm): ...@@ -64,6 +75,8 @@ class SearchForm(forms.ModelForm):
("INDIFFERENT", _("Indifferent")) ("INDIFFERENT", _("Indifferent"))
], widget=forms.RadioSelect, initial="INDIFFERENT") ], widget=forms.RadioSelect, initial="INDIFFERENT")
quick = forms.CharField(label=_('Last/First name or nickname'), max_length=255)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SearchForm, self).__init__(*args, **kwargs) super(SearchForm, self).__init__(*args, **kwargs)
for key in self.fields.keys(): for key in self.fields.keys():
...@@ -82,12 +95,13 @@ class SearchForm(forms.ModelForm): ...@@ -82,12 +95,13 @@ class SearchForm(forms.ModelForm):
class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView): class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
model = User model = User
ordering = ["-id"]
paginate_by = 12
template_name = 'matmat/search_form.jinja' template_name = 'matmat/search_form.jinja'
paginate_by = 3
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.form_class = kwargs['form'] self.form_class = kwargs['form']
self.reverse = kwargs['reverse'] self.search_type = kwargs['search_type']
self.session = request.session self.session = request.session
self.last_search = self.session.get('matmat_search_result', str([])) self.last_search = self.session.get('matmat_search_result', str([]))
self.last_search = literal_eval(self.last_search) self.last_search = literal_eval(self.last_search)
...@@ -97,7 +111,9 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView): ...@@ -97,7 +111,9 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
self.valid_form = None self.valid_form = None
self.init_query = self.model.objects self.init_query = self.model.objects
self.can_see_hidden = True
if not (request.user.is_board_member or request.user.is_root): if not (request.user.is_board_member or request.user.is_root):
self.can_see_hidden = False
self.init_query = self.init_query.exclude(is_subscriber_viewable=False) self.init_query = self.init_query.exclude(is_subscriber_viewable=False)
return super(SearchFormListView, self).dispatch(request, *args, **kwargs) return super(SearchFormListView, self).dispatch(request, *args, **kwargs)
...@@ -115,17 +131,24 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView): ...@@ -115,17 +131,24 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
def get_queryset(self): def get_queryset(self):
q = self.init_query q = self.init_query
if self.valid_form is not None: if self.valid_form is not None:
if self.reverse: if self.search_type == SearchType.REVERSE:
q = q.filter(phone=self.valid_form['phone']).all() q = q.filter(phone=self.valid_form['phone']).all()
elif self.search_type == SearchType.QUICK:
q = search_user(self.valid_form['quick'])
if not self.can_see_hidden:
q = [user for user in q if user.is_subscriber_viewable]
else: else:
search_dict = {} search_dict = {}
for key, value in self.valid_form.items(): for key, value in self.valid_form.items():
if key != 'phone' and not (value == '' or value is None or value == 'INDIFFERENT'): if key not in ('phone', 'quick') and not (value == '' or value is None or value == 'INDIFFERENT'):
search_dict[key + "__icontains"] = value search_dict[key + "__icontains"] = value
q = q.filter(**search_dict).all() q = q.filter(**search_dict).all()
else: else:
q = q.filter(pk__in=self.last_search).all() q = q.filter(pk__in=self.last_search).all()
self.result_exists = q.exists() if isinstance(q, list):
self.result_exists = len(q) > 0
else:
self.result_exists = q.exists()
self.last_search = [] self.last_search = []
for user in q: for user in q:
self.last_search.append(user.id) self.last_search.append(user.id)
...@@ -138,13 +161,12 @@ class SearchFormView(WasSuscribed, FormView): ...@@ -138,13 +161,12 @@ class SearchFormView(WasSuscribed, FormView):
Allows users to search inside the user list Allows users to search inside the user list
""" """
form_class = SearchForm form_class = SearchForm
reverse = False
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.session = request.session self.session = request.session
self.init_query = User.objects self.init_query = User.objects
kwargs['form'] = self.get_form() kwargs['form'] = self.get_form()
kwargs['reverse'] = self.reverse kwargs['search_type'] = self.search_type
return super(SearchFormView, self).dispatch(request, *args, **kwargs) return super(SearchFormView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
...@@ -163,8 +185,16 @@ class SearchFormView(WasSuscribed, FormView): ...@@ -163,8 +185,16 @@ class SearchFormView(WasSuscribed, FormView):
return self.session.get('matmat_search_form', {}) return self.session.get('matmat_search_form', {})
class SearchNormalFormView(SearchFormView):
search_type = SearchType.NORMAL
class SearchReverseFormView(SearchFormView): class SearchReverseFormView(SearchFormView):
reverse = True search_type = SearchType.REVERSE
class SearchQuickFormView(SearchFormView):
search_type = SearchType.QUICK
class SearchClearFormView(WasSuscribed, View): class SearchClearFormView(WasSuscribed, View):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment