From 4f1ae20bb419763b91f1cc154f8696442e27d702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 24 Sep 2016 00:55:01 +0200 Subject: [PATCH] Suppression d'erreurs sur les get On remplace les `MyModel.objects.get(...)` par des `get_object_or_404(MyModel, ...)` --- Ernestophone/settings-example.py | 1 + calendrier/views.py | 22 +++++++++++----------- partitions/views.py | 20 ++++++++++++-------- propositions/views.py | 7 +++---- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Ernestophone/settings-example.py b/Ernestophone/settings-example.py index 91fdd19..b6e83fe 100644 --- a/Ernestophone/settings-example.py +++ b/Ernestophone/settings-example.py @@ -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/' diff --git a/calendrier/views.py b/calendrier/views.py index 0033066..4d232a6 100644 --- a/calendrier/views.py +++ b/calendrier/views.py @@ -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() diff --git a/partitions/views.py b/partitions/views.py index 3d9c6bf..aa3a74c 100644 --- a/partitions/views.py +++ b/partitions/views.py @@ -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")) diff --git a/propositions/views.py b/propositions/views.py index 44a7055..8e45452 100644 --- a/propositions/views.py +++ b/propositions/views.py @@ -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.