Commit f8992ca0 authored by Maël Kervella's avatar Maël Kervella

Passage du search en GET + proprification du nom des variables

parent 0ed60c64
......@@ -28,18 +28,18 @@ from django import forms
from django.forms import Form
from django.forms import ModelForm
CHOICES = (
CHOICES_USER = (
('0', 'Actifs'),
('1', 'Désactivés'),
('2', 'Archivés'),
)
CHOICES2 = (
CHOICES_CO = (
(1, 'Active'),
("", 'Désactivée'),
)
CHOICES3 = (
CHOICES_AFF = (
('0', 'Utilisateurs'),
('1', 'Machines'),
('2', 'Factures'),
......@@ -51,12 +51,12 @@ CHOICES3 = (
class SearchForm(Form):
search_field = forms.CharField(label = 'Search', max_length = 100)
query = forms.CharField(label = 'Search', max_length = 100)
class SearchFormPlus(Form):
search_field = forms.CharField(label = 'Search', max_length = 100, required=False)
filtre = forms.MultipleChoiceField(label="Filtre utilisateurs", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES)
connexion = forms.MultipleChoiceField(label="Filtre connexion", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES2)
affichage = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES3)
date_deb = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'])
date_fin = forms.DateField(required=False, help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'], label="Date de fin")
query = forms.CharField(label = 'Search', max_length = 100, required=False)
user_state = forms.MultipleChoiceField(label="Filtre utilisateurs", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_USER)
co_state = forms.MultipleChoiceField(label="Filtre connexion", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_CO)
aff = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_AFF)
start = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'])
end = forms.DateField(required=False, help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'], label="Date de fin")
......@@ -28,11 +28,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block title %}Recherche{% endblock %}
{% block content %}
{% bootstrap_form_errors searchform %}
{% bootstrap_form_errors search_form %}
<form class="form" method="post">
{% csrf_token %}
{% bootstrap_form searchform %}
<form class="form">
{% bootstrap_form search_form %}
{% bootstrap_button "Search" button_type="submit" icon="search" %}
</form>
<br />
......
......@@ -40,33 +40,28 @@ from cotisations.models import Facture
from search.models import SearchForm, SearchFormPlus
from preferences.models import GeneralOption
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
return render(request, template, c)
def search_result(search, type, request):
date_deb = None
date_fin = None
states=[]
co=[]
aff=[]
def search_result(search_form, type, request):
start = None
end = None
user_state = []
co_state = []
aff = []
if(type):
aff = search.cleaned_data['affichage']
co = search.cleaned_data['connexion']
states = search.cleaned_data['filtre']
date_deb = search.cleaned_data['date_deb']
date_fin = search.cleaned_data['date_fin']
aff = search_form.cleaned_data['aff']
co_state = search_form.cleaned_data['co_state']
user_state = search_form.cleaned_data['user_state']
start = search_form.cleaned_data['start']
end = search_form.cleaned_data['end']
date_query = Q()
if aff==[]:
aff = ['0','1','2','3','4','5','6']
if date_deb != None:
date_query = date_query & Q(date__gte=date_deb)
if date_fin != None:
date_query = date_query & Q(date__lte=date_fin)
search = search.cleaned_data['search_field']
if start != None:
date_query = date_query & Q(date__gte=start)
if end != None:
date_query = date_query & Q(date__lte=end)
search = search_form.cleaned_data['query']
query1 = Q()
for s in states:
for s in user_state:
query1 = query1 | Q(state = s)
connexion = []
......@@ -121,14 +116,14 @@ def search_result(search, type, request):
@login_required
def search(request):
search = SearchForm(request.POST or None)
if search.is_valid():
return form(search_result(search, False, request), 'search/index.html',request)
return form({'searchform' : search}, 'search/search.html', request)
search_form = SearchForm(request.GET or None)
if search_form.is_valid():
return render(request, 'search/index.html', search_result(search_form, False, request))
return render(request, 'search/search.html', {'search_form' : search_form})
@login_required
def searchp(request):
search = SearchFormPlus(request.POST or None)
if search.is_valid():
return form(search_result(search, True, request), 'search/index.html',request)
return form({'searchform' : search}, 'search/search.html', request)
search_form = SearchFormPlus(request.GET or None)
if search_form.is_valid():
return render(request, 'search/index.html', search_result(search_form, True, request))
return render(request, 'search/search.html', {'search_form' : search_form})
......@@ -72,8 +72,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% endif %}
</ul>
<div class="col-sm-3 col-md-3 navbar-right">
<form action="{% url "search:search"%}" method="POST" class="navbar-form" role="search">
{% csrf_token %}
<form action="{% url "search:search"%}" class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="search_field" id="search-term">
<div class="input-group-btn">
......
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