Ajoute des filtres sur la page description.

Les variables `GET` `location` et `category` permettent de filtrer sur
les salles et catégories dans le résultats de `/bda/descriptions/<id>`
This commit is contained in:
Martin Pépin 2016-08-26 06:06:45 +02:00
parent ab4e7ec084
commit 3bca778734
3 changed files with 20 additions and 6 deletions

View file

@ -16,7 +16,8 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True)),
('name', models.CharField(max_length=300, verbose_name='Nom')),
('name', models.CharField(max_length=300, verbose_name='Nom',
unique=True)),
],
options={
'verbose_name': 'Cat\xe9gorie',
@ -25,9 +26,11 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Quote',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True)),
('text', models.TextField(verbose_name='Citation')),
('author', models.CharField(max_length=200, verbose_name='Auteur')),
('author', models.CharField(max_length=200,
verbose_name='Auteur')),
],
),
migrations.AlterModelOptions(

View file

@ -49,7 +49,7 @@ class Salle(models.Model):
@python_2_unicode_compatible
class CategorieSpectacle(models.Model):
name = models.CharField('Nom', max_length=300)
name = models.CharField('Nom', max_length=100, unique=True)
def __str__(self):
return self.name

View file

@ -10,6 +10,7 @@ from django.db import models
from django.db.models import Count
from django.core import serializers
from django.forms.models import inlineformset_factory
from django.http import HttpResponseBadRequest
import hashlib
from django.core.mail import send_mail
@ -368,5 +369,15 @@ def send_rappel(request, spectacle_id):
def descriptions_spectacles(request, tirage_id):
tirage = get_object_or_404(Tirage, id=tirage_id)
shows = tirage.spectacle_set.all()
return render(request, 'descriptions.html', {'shows': shows})
shows_qs = tirage.spectacle_set
category_name = request.GET.get('category', '')
location_id = request.GET.get('location', '')
if category_name:
shows_qs = shows_qs.filter(category__name=category_name)
if location_id:
try:
shows_qs = shows_qs.filter(location__id=int(location_id))
except ValueError:
return HttpResponseBadRequest(
"La variable GET 'location' doit contenir un entier")
return render(request, 'descriptions.html', {'shows': shows_qs.all()})