Simplification du code avec des méthodes de Django

This commit is contained in:
Hugo Roussille 2017-03-31 03:35:09 +02:00
parent a9c8de7544
commit f3b9266e35
2 changed files with 14 additions and 24 deletions

View file

@ -3,12 +3,10 @@
import random
import hashlib
import time
import json
from datetime import timedelta
from custommail.shortcuts import (
send_mass_custom_mail, send_custom_mail, render_custom_mail
)
from more_itertools import unique_everseen
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib import messages
@ -17,7 +15,7 @@ from django.core import serializers
from django.db.models import Count, Q, Sum
from django.forms.models import inlineformset_factory
from django.http import HttpResponseBadRequest, HttpResponseRedirect,\
JsonResponse, HttpResponse
JsonResponse
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils import timezone, formats
@ -25,7 +23,7 @@ from django.views.generic.list import ListView
from gestioncof.decorators import cof_required, buro_required
from bda.models import Spectacle, Participant, ChoixSpectacle, Attribution,\
Tirage, SpectacleRevente, Salle, Quote
Tirage, SpectacleRevente, Salle, Quote, CategorieSpectacle
from bda.algorithm import Algorithm
from bda.forms import BaseBdaFormSet, TokenForm, ResellForm, AnnulForm,\
InscriptionReventeForm, SoldForm
@ -652,10 +650,8 @@ def catalogue(request, request_type):
"""
if request_type == "list":
# Dans ce cas on retourne la liste des tirages et de leur id en JSON
data_return = [
{'id': tirage.id, 'title': tirage.title}
for tirage in Tirage.objects.filter(appear_catalogue=True).all()
]
data_return = list(
Tirage.objects.filter(appear_catalogue=True).values('id', 'title'))
return JsonResponse(data_return, safe=False)
if request_type == "details":
# Dans ce cas on retourne une liste des catégories et des salles
@ -664,15 +660,14 @@ def catalogue(request, request_type):
tirage = get_object_or_404(Tirage, id=tirage_id)
except:
return HttpResponseBadRequest()
categories = list(unique_everseen([
str(spectacle.category)
for spectacle in tirage.spectacle_set.all()
]))
categories.remove('None')
locations = list(unique_everseen([
str(spectacle.location)
for spectacle in tirage.spectacle_set.all()
]))
categories = list(
CategorieSpectacle.objects.filter(
spectacle__in=tirage.spectacle_set.all())
.distinct())
locations = list(
Salle.objects.filter(
spectacle__in=tirage.spectacle_set.all())
.distinct().values_list('name', flat=True))
data_return = [{'categories': categories, 'locations': locations}]
return JsonResponse(data_return, safe=False)
if request_type == "descriptions":
@ -707,11 +702,6 @@ def catalogue(request, request_type):
except:
return ''
def specquotes(spectacle): [
{'author': str(quote.author), 'text': str(quote.text)}
for quote in Quote.objects.filter(spectacle = spectacle).all()
]
# On convertit les descriptions à envoyer en une liste facilement
# JSONifiable (il devrait y avoir un moyen plus efficace en
# redéfinissant le serializer de JSON)
@ -725,7 +715,8 @@ def catalogue(request, request_type):
'vips': spectacle.vips,
'description': spectacle.description,
'slots_description': spectacle.slots_description,
'quotes': specquotes(spectacle),
'quotes': list(Quote.objects.filter(spectacle=spectacle).values(
'author', 'text')),
'image': getImgUrl(spectacle),
'ext_link': spectacle.ext_link,
'price': spectacle.price,

View file

@ -11,7 +11,6 @@ DBPASSWD="4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4"
apt-get update && apt-get install -y python3-pip python3-dev python3-venv \
libmysqlclient-dev libjpeg-dev git redis-server
pip install -U pip
pip install more_itertools
# Configuration et installation de mysql. Le mot de passe root est le même que
# le mot de passe pour l'utilisateur local - pour rappel, ceci est une instance