Suppression d'erreurs sur les get

On remplace les `MyModel.objects.get(...)` par des
`get_object_or_404(MyModel, ...)`
This commit is contained in:
Martin Pépin 2016-09-24 00:55:01 +02:00
parent 6af610dabc
commit 4f1ae20bb4
4 changed files with 27 additions and 23 deletions

View file

@ -16,6 +16,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(__file__))
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'

View file

@ -1,4 +1,4 @@
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from calendrier.forms import ModifEventForm, EventForm, ParticipantsForm, \
SendMail, ChangeDoodleName
from calendrier.models import Event, Participants
@ -69,7 +69,7 @@ def calendar(request, pYear, pMonth):
def view_event(request, pYear, pMonth, id):
ev = Event.objects.get(id=id)
ev = get_object_or_404(Event, id=id)
if not request.user.is_authenticated() and not ev.calendrier:
return redirect('calendrier.views.home')
nom = ev.nom.capitalize
@ -87,7 +87,7 @@ def view_event(request, pYear, pMonth, id):
def view_eventbis(request, id):
ev = Event.objects.get(id=id)
ev = get_object_or_404(Event, id=id)
if not request.user.is_authenticated() and not ev.calendrier:
return redirect('calendrier.views.home')
part = ev.participants_set.all()
@ -109,7 +109,7 @@ def resend(request, id):
if request.method == "POST":
form = SendMail(request.POST)
if form.is_valid():
ev = Event.objects.get(id=id)
ev = get_object_or_404(Event, id=id)
users = ErnestoUser.objects.filter(is_ernesto=True)
message = form.cleaned_data["message"]
try:
@ -228,7 +228,7 @@ def create_event(request):
@login_required
def reponse(request, id):
part = request.user.profile
ev = Event.objects.get(id=id)
ev = get_object_or_404(Event, id=id)
if request.method == "POST":
form = ParticipantsForm(request.POST)
if form.is_valid():
@ -248,8 +248,8 @@ def reponse(request, id):
def repouidir(request, codeus, codeev):
part = ErnestoUser.objects.get(slug=codeus)
ev = Event.objects.get(slug=codeev)
part = get_object_or_404(ErnestoUser, slug=codeus)
ev = get_object_or_404(Event, slug=codeev)
try:
p = Participants.objects.get(event=ev, participant=part)
p.delete()
@ -265,8 +265,8 @@ def repouidir(request, codeus, codeev):
def reppedir(request, codeus, codeev):
part = ErnestoUser.objects.get(slug=codeus)
ev = Event.objects.get(slug=codeev)
part = get_object_or_404(ErnestoUser, slug=codeus)
ev = get_object_or_404(Event, slug=codeev)
try:
p = Participants.objects.get(event=ev, participant=part)
p.delete()
@ -282,8 +282,8 @@ def reppedir(request, codeus, codeev):
def repnondir(request, codeus, codeev):
part = ErnestoUser.objects.get(slug=codeus)
ev = Event.objects.get(slug=codeev)
part = get_object_or_404(ErnestoUser, slug=codeus)
ev = get_object_or_404(Event, slug=codeev)
try:
p = Participants.objects.get(event=ev, participant=part)
p.delete()

View file

@ -4,6 +4,7 @@ from django.utils.encoding import smart_str
from django.forms.models import modelform_factory
from django.core.files import File
from django.utils.safestring import mark_safe
from django.http import Http404
import os
@ -35,7 +36,7 @@ def upload(request, nom, auteur):
error = "Le caractère / n'est pas autorisé dans le nom"
form = UploadFileForm()
return render(request, "partitions/upload.html", locals())
mor = PartitionSet.objects.get(nom=nom, auteur=auteur)
mor = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
partition.morceau = mor
try:
mor.partition_set.get(nom=partition.nom)
@ -69,7 +70,7 @@ def see(request, nom, auteur, partition_id):
response["Content-Length"] = myfile.size
return response
else:
partitions = PartitionSet.objects.get(nom=nom, auteur=auteur)
partitions = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
part = partitions.partition_set.all()
return render(request, 'partitions/listepart.html', locals())
@ -113,24 +114,27 @@ def ajouter_morceau(request):
@chef_required
def delete(request, nom, auteur, id):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
part = p.partition_set.get(id=id)
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
try:
part = p.partition_set.get(id=id)
except Partition.DoesNotExist:
raise Http404
part.delete()
suppression = "Partition supprimée"
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
p.refresh_from_db()
part = p.partition_set.all()
return render(request, 'partitions/listepart.html', locals())
@chef_required
def conf_delete(request, nom, auteur, id):
part = Partition.objects.get(id=id)
part = get_object_or_404(Partition, id=id)
return render(request, 'partitions/conf_delete.html', locals())
@chef_required
def delete_morc(request, nom, auteur):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
part = p.partition_set.all()
for pa in part:
pa.delete()
@ -146,7 +150,7 @@ def conf_delete_morc(request, nom, auteur):
@login_required
def listepart(request, nom, auteur):
p = PartitionSet.objects.get(nom=nom, auteur=auteur)
p = get_object_or_404(PartitionSet, nom=nom, auteur=auteur)
part = p.partition_set.all()
ChefEditForm = modelform_factory(PartitionSet,
fields=("category", "infos"))

View file

@ -1,4 +1,4 @@
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.views.generic import DeleteView
@ -35,7 +35,7 @@ def liste(request):
@login_required
def repoui(request, id):
prop = Prop.objects.get(id=id)
prop = get_object_or_404(Prop, id=id)
participant = request.user.profile
try:
p = Reponses.objects.get(prop=prop, part=participant)
@ -86,7 +86,7 @@ def test(request, pk):
@login_required
def repnon(request, id):
prop = Prop.objects.get(id=id)
prop = get_object_or_404(Prop, id=id)
participant = request.user.profile
try:
p = Reponses.objects.get(prop=prop, part=participant)
@ -105,4 +105,3 @@ def repnon(request, id):
prop.nbnon += 1
prop.save()
return redirect('propositions.views.liste')
# Create your views here.