Don't crash on redirect when multiple CFs are inprogress or open
authorMagnus Hagander <magnus@hagander.net>
Mon, 31 Aug 2015 14:44:57 +0000 (16:44 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 31 Aug 2015 14:44:57 +0000 (16:44 +0200)
This is an incorrect state, but we shouldn't crash anyway. Instead show
an error message and redirect to the root page.

Reported by Bruce Momjian

pgcommitfest/commitfest/views.py

index 02567f3a096804c459826d6b197c07259cfd47b8..93bd4213befa64d4d919ec7573cb746894f76e13 100644 (file)
@@ -80,13 +80,20 @@ def activity(request, cfid=None, rss=None):
 
 def redir(request, what):
        if what == 'open':
-               cf = get_object_or_404(CommitFest, status=CommitFest.STATUS_OPEN)
+               cfs = list(CommitFest.objects.filter(status=CommitFest.STATUS_OPEN))
        elif what == 'inprogress':
-               cf = get_object_or_404(CommitFest, status=CommitFest.STATUS_INPROGRESS)
+               cfs = list(CommitFest.objects.filter(status=CommitFest.STATUS_INPROGRESS))
        else:
                raise Http404()
 
-       return HttpResponseRedirect("/%s/" % cf.id)
+       if len(cfs) == 0:
+               messages.warning(request, "No {0} commitfests exist, redirecting to startpage.".format(what))
+               return HttpResponseRedirect("/")
+       if len(cfs) != 1:
+               messages.warning(request, "More than one {0} commitfest exists, redirecting to startpage instead.".format(what))
+               return HttpResponseRedirect("/")
+
+       return HttpResponseRedirect("/%s/" % cfs[0].id)
 
 def commitfest(request, cfid):
        # Find ourselves