prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  500 on bad certificate number
  # Problem
  
  When I try and add a bad certificate number, then I get a 500, I should get a 404.
  See:
  http://staging.changelog.qgis.org/en/qgis/certificate/0246242/
  
  # Proposed Solution
  
  Return a 404
  
  </issue>
  <code>
  [start of django_project/certification/views/certificate.py]
  1 # coding=utf-8
  2 from django.http import Http404
  3 from django.views.generic import CreateView, DetailView
  4 from django.core.urlresolvers import reverse
  5 from braces.views import LoginRequiredMixin
  6 from ..models import Certificate, Course, Attendee
  7 from ..forms import CertificateForm
  8 
  9 
  10 class CertificateMixin(object):
  11     """Mixin class to provide standard settings for Certificate."""
  12 
  13     model = Certificate
  14     form_class = CertificateForm
  15 
  16 
  17 class CertificateCreateView(
  18         LoginRequiredMixin, CertificateMixin, CreateView):
  19     """Create view for Certificate."""
  20 
  21     context_object_name = 'certificate'
  22     template_name = 'certificate/create.html'
  23 
  24     def get_success_url(self):
  25         """Define the redirect URL.
  26 
  27         After successful creation of the object, the User will be redirected
  28         to the Course detail page.
  29 
  30        :returns: URL
  31        :rtype: HttpResponse
  32        """
  33 
  34         return reverse('course-detail', kwargs={
  35             'project_slug': self.project_slug,
  36             'organisation_slug': self.organisation_slug,
  37             'slug': self.course_slug
  38         })
  39 
  40     def get_context_data(self, **kwargs):
  41         """Get the context data which is passed to a template.
  42 
  43         :param kwargs: Any arguments to pass to the superclass.
  44         :type kwargs: dict
  45 
  46         :returns: Context data which will be passed to the template.
  47         :rtype: dict
  48         """
  49 
  50         context = super(
  51             CertificateCreateView, self).get_context_data(**kwargs)
  52         context['course'] = Course.objects.get(slug=self.course_slug)
  53         context['attendee'] = Attendee.objects.get(pk=self.pk)
  54         return context
  55 
  56     def get_form_kwargs(self):
  57         """Get keyword arguments from form.
  58 
  59         :returns keyword argument from the form
  60         :rtype: dict
  61         """
  62 
  63         kwargs = super(CertificateCreateView, self).get_form_kwargs()
  64         self.project_slug = self.kwargs.get('project_slug', None)
  65         self.organisation_slug = self.kwargs.get('organisation_slug', None)
  66         self.course_slug = self.kwargs.get('course_slug', None)
  67         self.pk = self.kwargs.get('pk', None)
  68         self.course = Course.objects.get(slug=self.course_slug)
  69         self.attendee = Attendee.objects.get(pk=self.pk)
  70         kwargs.update({
  71             'user': self.request.user,
  72             'course': self.course,
  73             'attendee': self.attendee,
  74         })
  75         return kwargs
  76 
  77 
  78 class CertificateDetailView(DetailView):
  79     """Detail view for Certificate."""
  80 
  81     model = Certificate
  82     context_object_name = 'certificate'
  83     template_name = 'certificate/detail.html'
  84 
  85     def get_context_data(self, **kwargs):
  86         """Get the context data which is passed to a template.
  87 
  88         :param kwargs: Any arguments to pass to the superclass.
  89         :type kwargs: dict
  90 
  91         :returns: Context data which will be passed to the template.
  92         :rtype: dict
  93         """
  94 
  95         self.certificateID = self.kwargs.get('id', None)
  96         context = super(
  97             CertificateDetailView, self).get_context_data(**kwargs)
  98         context['certificate'] = \
  99             Certificate.objects.get(certificateID=self.certificateID)
  100         return context
  101 
  102     def get_queryset(self):
  103         """Get the queryset for this view.
  104 
  105         :returns: Queryset which is all certificate in the
  106             corresponding organisation.
  107         :rtype: QuerySet
  108         """
  109 
  110         qs = Certificate.objects.all()
  111         return qs
  112 
  113     def get_object(self, queryset=None):
  114         """Get the object for this view.
  115 
  116         :param queryset: A query set
  117         :type queryset: QuerySet
  118 
  119         :returns: Queryset which is filtered to only show a certificate
  120             depends on the input certificate ID.
  121         :rtype: QuerySet
  122         :raises: Http404
  123         """
  124 
  125         if queryset is None:
  126             queryset = self.get_queryset()
  127             certificateID = self.kwargs.get('id', None)
  128             if certificateID:
  129                 obj = queryset.get(
  130                     certificateID=certificateID)
  131                 return obj
  132             else:
  133                 raise Http404('Sorry! Certificate by this ID is not exist.')
  134
  [end of django_project/certification/views/certificate.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/django_project/certification/views/certificate.py b/django_project/certification/views/certificate.py
  --- a/django_project/certification/views/certificate.py
  +++ b/django_project/certification/views/certificate.py
  @@ -93,10 +93,15 @@
           """
   
           self.certificateID = self.kwargs.get('id', None)
  +        self.project_slug = self.kwargs.get('project_slug', None)
           context = super(
               CertificateDetailView, self).get_context_data(**kwargs)
  -        context['certificate'] = \
  -            Certificate.objects.get(certificateID=self.certificateID)
  +        issued_id = \
  +            Certificate.objects.all().values_list('certificateID', flat=True)
  +        if self.certificateID in issued_id:
  +            context['certificate'] = \
  +                Certificate.objects.get(certificateID=self.certificateID)
  +        context['project_slug'] = self.project_slug
           return context
   
       def get_queryset(self):
  @@ -126,8 +131,10 @@
               queryset = self.get_queryset()
               certificateID = self.kwargs.get('id', None)
               if certificateID:
  -                obj = queryset.get(
  -                    certificateID=certificateID)
  -                return obj
  +                try:
  +                    obj = queryset.get(certificateID=certificateID)
  +                    return obj
  +                except Certificate.DoesNotExist:
  +                    return None
               else:
                   raise Http404('Sorry! Certificate by this ID is not exist.')
  
verification_info: {"golden_diff": "diff --git a/django_project/certification/views/certificate.py b/django_project/certification/views/certificate.py\n--- a/django_project/certification/views/certificate.py\n+++ b/django_project/certification/views/certificate.py\n@@ -93,10 +93,15 @@\n         \"\"\"\n \n         self.certificateID = self.kwargs.get('id', None)\n+        self.project_slug = self.kwargs.get('project_slug', None)\n         context = super(\n             CertificateDetailView, self).get_context_data(**kwargs)\n-        context['certificate'] = \\\n-            Certificate.objects.get(certificateID=self.certificateID)\n+        issued_id = \\\n+            Certificate.objects.all().values_list('certificateID', flat=True)\n+        if self.certificateID in issued_id:\n+            context['certificate'] = \\\n+                Certificate.objects.get(certificateID=self.certificateID)\n+        context['project_slug'] = self.project_slug\n         return context\n \n     def get_queryset(self):\n@@ -126,8 +131,10 @@\n             queryset = self.get_queryset()\n             certificateID = self.kwargs.get('id', None)\n             if certificateID:\n-                obj = queryset.get(\n-                    certificateID=certificateID)\n-                return obj\n+                try:\n+                    obj = queryset.get(certificateID=certificateID)\n+                    return obj\n+                except Certificate.DoesNotExist:\n+                    return None\n             else:\n                 raise Http404('Sorry! Certificate by this ID is not exist.')\n", "issue": "500 on bad certificate number\n# Problem\r\n\r\nWhen I try and add a bad certificate number, then I get a 500, I should get a 404.\r\nSee:\r\nhttp://staging.changelog.qgis.org/en/qgis/certificate/0246242/\r\n\r\n# Proposed Solution\r\n\r\nReturn a 404\n", "before_files": [{"content": "# coding=utf-8\nfrom django.http import Http404\nfrom django.views.generic import CreateView, DetailView\nfrom django.core.urlresolvers import reverse\nfrom braces.views import LoginRequiredMixin\nfrom ..models import Certificate, Course, Attendee\nfrom ..forms import CertificateForm\n\n\nclass CertificateMixin(object):\n    \"\"\"Mixin class to provide standard settings for Certificate.\"\"\"\n\n    model = Certificate\n    form_class = CertificateForm\n\n\nclass CertificateCreateView(\n        LoginRequiredMixin, CertificateMixin, CreateView):\n    \"\"\"Create view for Certificate.\"\"\"\n\n    context_object_name = 'certificate'\n    template_name = 'certificate/create.html'\n\n    def get_success_url(self):\n        \"\"\"Define the redirect URL.\n\n        After successful creation of the object, the User will be redirected\n        to the Course detail page.\n\n       :returns: URL\n       :rtype: HttpResponse\n       \"\"\"\n\n        return reverse('course-detail', kwargs={\n            'project_slug': self.project_slug,\n            'organisation_slug': self.organisation_slug,\n            'slug': self.course_slug\n        })\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        context = super(\n            CertificateCreateView, self).get_context_data(**kwargs)\n        context['course'] = Course.objects.get(slug=self.course_slug)\n        context['attendee'] = Attendee.objects.get(pk=self.pk)\n        return context\n\n    def get_form_kwargs(self):\n        \"\"\"Get keyword arguments from form.\n\n        :returns keyword argument from the form\n        :rtype: dict\n        \"\"\"\n\n        kwargs = super(CertificateCreateView, self).get_form_kwargs()\n        self.project_slug = self.kwargs.get('project_slug', None)\n        self.organisation_slug = self.kwargs.get('organisation_slug', None)\n        self.course_slug = self.kwargs.get('course_slug', None)\n        self.pk = self.kwargs.get('pk', None)\n        self.course = Course.objects.get(slug=self.course_slug)\n        self.attendee = Attendee.objects.get(pk=self.pk)\n        kwargs.update({\n            'user': self.request.user,\n            'course': self.course,\n            'attendee': self.attendee,\n        })\n        return kwargs\n\n\nclass CertificateDetailView(DetailView):\n    \"\"\"Detail view for Certificate.\"\"\"\n\n    model = Certificate\n    context_object_name = 'certificate'\n    template_name = 'certificate/detail.html'\n\n    def get_context_data(self, **kwargs):\n        \"\"\"Get the context data which is passed to a template.\n\n        :param kwargs: Any arguments to pass to the superclass.\n        :type kwargs: dict\n\n        :returns: Context data which will be passed to the template.\n        :rtype: dict\n        \"\"\"\n\n        self.certificateID = self.kwargs.get('id', None)\n        context = super(\n            CertificateDetailView, self).get_context_data(**kwargs)\n        context['certificate'] = \\\n            Certificate.objects.get(certificateID=self.certificateID)\n        return context\n\n    def get_queryset(self):\n        \"\"\"Get the queryset for this view.\n\n        :returns: Queryset which is all certificate in the\n            corresponding organisation.\n        :rtype: QuerySet\n        \"\"\"\n\n        qs = Certificate.objects.all()\n        return qs\n\n    def get_object(self, queryset=None):\n        \"\"\"Get the object for this view.\n\n        :param queryset: A query set\n        :type queryset: QuerySet\n\n        :returns: Queryset which is filtered to only show a certificate\n            depends on the input certificate ID.\n        :rtype: QuerySet\n        :raises: Http404\n        \"\"\"\n\n        if queryset is None:\n            queryset = self.get_queryset()\n            certificateID = self.kwargs.get('id', None)\n            if certificateID:\n                obj = queryset.get(\n                    certificateID=certificateID)\n                return obj\n            else:\n                raise Http404('Sorry! Certificate by this ID is not exist.')\n", "path": "django_project/certification/views/certificate.py"}]}