Add Recette.saison and allow search on it

This commit is contained in:
Claude Paroz 2018-05-26 17:59:13 +02:00
parent d1eef00e7d
commit ff4d7a5810
6 changed files with 73 additions and 4 deletions

View file

@ -52,8 +52,9 @@ WSGI_APPLICATION = 'common.wsgi.application'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'NAME': 'recettes',
'USER': 'claude',
} }
} }

26
recette/fields.py Normal file
View file

@ -0,0 +1,26 @@
from django.contrib.postgres.fields import ArrayField
from django.forms import CheckboxSelectMultiple, TypedMultipleChoiceField
class ChoiceArrayField(ArrayField):
"""
From https://blogs.gnome.org/danni/2016/03/08/multiple-choice-using-djangos-postgres-arrayfield/
A field that allows us to store an array of choices.
Uses Django's postgres ArrayField and a MultipleChoiceField for its formfield.
See also https://code.djangoproject.com/ticket/27704
"""
widget = CheckboxSelectMultiple
def formfield(self, **kwargs):
defaults = {
'form_class': TypedMultipleChoiceField,
'coerce': self.base_field.to_python,
'choices': self.base_field.choices,
'widget': self.widget,
}
defaults.update(kwargs)
# Skip our parent's formfield implementation completely as we don't
# care for it.
# pylint:disable=bad-super-call
return super(ArrayField, self).formfield(**defaults)

View file

@ -1,5 +1,19 @@
from django import forms from django import forms
from .models import Recette
class SearchForm(forms.Form): class SearchForm(forms.Form):
text = forms.CharField(widget=forms.TextInput(attrs={'autofocus': True})) text = forms.CharField(widget=forms.TextInput(attrs={'autofocus': True}))
saison = forms.ChoiceField(choices=(('all', "Toutes"),) + Recette.SAISON_CHOICES)
def search(self):
if self.is_valid():
qs = Recette.objects.all()
if self.cleaned_data['text']:
qs = qs.filter(nom__icontains=self.cleaned_data['text'])
if self.cleaned_data['saison'] != 'all':
qs = qs.filter(saison__contains=[self.cleaned_data['saison']])
return qs
else:
return Recette.objects.none()

View file

@ -0,0 +1,17 @@
from django.db import migrations, models
from recette.fields import ChoiceArrayField
class Migration(migrations.Migration):
dependencies = [
('recette', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='recette',
name='saison',
field=ChoiceArrayField(base_field=models.CharField(blank=True, choices=[('printemps', 'Printemps'), ('été', 'Été'), ('automne', 'Automne'), ('hiver', 'Hiver')], max_length=10), default=[], size=None, verbose_name='Saison'),
preserve_default=False,
),
]

View file

@ -1,6 +1,8 @@
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from .fields import ChoiceArrayField
class Ingredient(models.Model): class Ingredient(models.Model):
nom = models.CharField("Nom", max_length=200) nom = models.CharField("Nom", max_length=200)
@ -17,9 +19,19 @@ class Unite(models.Model):
class Recette(models.Model): class Recette(models.Model):
SAISON_CHOICES = (
('printemps', 'Printemps'),
('été', 'Été'),
('automne', 'Automne'),
('hiver', 'Hiver'),
)
nom = models.CharField("Nom", max_length=200) nom = models.CharField("Nom", max_length=200)
photo = models.ImageField("Photo", upload_to='photos', blank=True) photo = models.ImageField("Photo", upload_to='photos', blank=True)
nb_pers = models.IntegerField(default=4) nb_pers = models.IntegerField(default=4)
saison = ChoiceArrayField(
models.CharField(max_length=10, choices=SAISON_CHOICES, blank=True),
verbose_name="Saison",
)
prep = models.TextField("Préparation", blank=True) prep = models.TextField("Préparation", blank=True)
source = models.CharField("Source", max_length=200, blank=True) source = models.CharField("Source", max_length=200, blank=True)
ingredients = models.ManyToManyField(Ingredient, through='Composition', blank=True) ingredients = models.ManyToManyField(Ingredient, through='Composition', blank=True)

View file

@ -8,8 +8,7 @@ def home(request):
form = SearchForm(request.POST or None) form = SearchForm(request.POST or None)
recettes = [] recettes = []
if request.method == 'POST': if request.method == 'POST':
if form.is_valid(): recettes = form.search()
recettes = Recette.objects.filter(nom__icontains=form.cleaned_data['text'])
return render(request, 'index.html', context={'form': form, 'recettes': recettes}) return render(request, 'index.html', context={'form': form, 'recettes': recettes})