Add Recette.saison and allow search on it
This commit is contained in:
		
							parent
							
								
									d1eef00e7d
								
							
						
					
					
						commit
						ff4d7a5810
					
				
					 6 changed files with 73 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -52,8 +52,9 @@ WSGI_APPLICATION = 'common.wsgi.application'
 | 
			
		|||
 | 
			
		||||
DATABASES = {
 | 
			
		||||
    'default': {
 | 
			
		||||
        'ENGINE': 'django.db.backends.sqlite3',
 | 
			
		||||
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
 | 
			
		||||
        'ENGINE': 'django.db.backends.postgresql',
 | 
			
		||||
        'NAME': 'recettes',
 | 
			
		||||
        'USER': 'claude',
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										26
									
								
								recette/fields.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								recette/fields.py
									
										
									
									
									
										Normal 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)
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,19 @@
 | 
			
		|||
from django import forms
 | 
			
		||||
 | 
			
		||||
from .models import Recette
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SearchForm(forms.Form):
 | 
			
		||||
    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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										17
									
								
								recette/migrations/0002_recette_saison.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								recette/migrations/0002_recette_saison.py
									
										
									
									
									
										Normal 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,
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
from django.db import models
 | 
			
		||||
from django.urls import reverse
 | 
			
		||||
 | 
			
		||||
from .fields import ChoiceArrayField
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Ingredient(models.Model):
 | 
			
		||||
    nom = models.CharField("Nom", max_length=200)
 | 
			
		||||
| 
						 | 
				
			
			@ -17,9 +19,19 @@ class Unite(models.Model):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
class Recette(models.Model):
 | 
			
		||||
    SAISON_CHOICES = (
 | 
			
		||||
        ('printemps', 'Printemps'),
 | 
			
		||||
        ('été', 'Été'),
 | 
			
		||||
        ('automne', 'Automne'),
 | 
			
		||||
        ('hiver', 'Hiver'),
 | 
			
		||||
    )
 | 
			
		||||
    nom = models.CharField("Nom", max_length=200)
 | 
			
		||||
    photo = models.ImageField("Photo", upload_to='photos', blank=True)
 | 
			
		||||
    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)
 | 
			
		||||
    source = models.CharField("Source", max_length=200, blank=True)
 | 
			
		||||
    ingredients = models.ManyToManyField(Ingredient, through='Composition', blank=True)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,8 +8,7 @@ def home(request):
 | 
			
		|||
    form = SearchForm(request.POST or None)
 | 
			
		||||
    recettes = []
 | 
			
		||||
    if request.method == 'POST':
 | 
			
		||||
        if form.is_valid():
 | 
			
		||||
            recettes = Recette.objects.filter(nom__icontains=form.cleaned_data['text'])
 | 
			
		||||
        recettes = form.search()
 | 
			
		||||
        
 | 
			
		||||
    return render(request, 'index.html', context={'form': form, 'recettes': recettes})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue