Set main choice between plat and dessert

This commit is contained in:
Claude Paroz 2021-07-03 18:54:58 +02:00
parent 4172a5f5fe
commit d7276ca9f0
3 changed files with 15 additions and 8 deletions

View File

@ -11,16 +11,18 @@ class SearchForm(forms.Form):
label="Texte", required=False label="Texte", required=False
) )
saison = forms.ChoiceField(choices=(('all', "Toutes"),) + Recette.SAISON_CHOICES) saison = forms.ChoiceField(choices=(('all', "Toutes"),) + Recette.SAISON_CHOICES)
type_princ = forms.ChoiceField(
label='',
choices=(('plat', 'Plat principal'), ('dessert', 'Dessert')),
widget=forms.RadioSelect
)
veget = forms.BooleanField(label="Végétarien", required=False) veget = forms.BooleanField(label="Végétarien", required=False)
dessert = forms.BooleanField(label="Dessert", required=False)
def search(self): def search(self):
if self.is_valid(): if self.is_valid():
qs = Recette.objects.all() qs = Recette.objects.filter(is_dessert=self.cleaned_data['type_princ'] == 'dessert')
if self.cleaned_data['veget']: if self.cleaned_data['veget']:
qs = qs.filter(is_veget=True) qs = qs.filter(is_veget=True)
if self.cleaned_data['dessert']:
qs = qs.filter(is_dessert=True)
if self.cleaned_data['text']: if self.cleaned_data['text']:
terms = [t for t in self.cleaned_data['text'].split() if t not in STOP_WORDS] terms = [t for t in self.cleaned_data['text'].split() if t not in STOP_WORDS]
for term in terms: for term in terms:

View File

@ -5,7 +5,7 @@ from .models import Recette
def home(request): def home(request):
form = SearchForm(request.POST or None) form = SearchForm(request.POST or None, initial={'type_princ': 'plat'})
recettes = [] recettes = []
num_results = None num_results = None
if request.method == 'POST': if request.method == 'POST':

View File

@ -5,12 +5,17 @@
{% block extrahead %} {% block extrahead %}
<style> <style>
ul.results { list-style-type: none; padding: 0; } ul { list-style-type: none; padding: 0; }
input#id_text { padding: 10px 0; }
form#search-form p, form#search-form ul {
display: inline-block;
margin-right: 1em;
vertical-align: top;
}
ul.results li { margin: 0; padding: 2px; } ul.results li { margin: 0; padding: 2px; }
ul.results li, ul.results li * { vertical-align: middle; } ul.results li, ul.results li * { vertical-align: middle; }
ul.results li:nth-child(odd) { background-color: #eee; } ul.results li:nth-child(odd) { background-color: #eee; }
input#id_text { padding: 10px 0; }
form#search-form p { display: inline-block; margin-right: 1em; }
</style> </style>
{% endblock %} {% endblock %}