prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  IntegrityError in ratings
  See:
  - https://sentry.liqd.net/sentry/debattenportalspdde/issues/84/
  - https://sentry.liqd.net/sentry/meinberlin-prod/issues/5/
  
  </issue>
  <code>
  [start of adhocracy4/ratings/api.py]
  1 from django.conf import settings
  2 
  3 from django_filters import rest_framework as filters
  4 from rest_framework import mixins, viewsets
  5 from rest_framework.response import Response
  6 
  7 from adhocracy4.api.mixins import ContentTypeMixin
  8 from adhocracy4.api.permissions import ViewSetRulesPermission
  9 
  10 from .models import Rating
  11 from .serializers import RatingSerializer
  12 
  13 
  14 class RatingViewSet(mixins.CreateModelMixin,
  15                     mixins.UpdateModelMixin,
  16                     ContentTypeMixin,
  17                     viewsets.GenericViewSet):
  18 
  19     queryset = Rating.objects.all()
  20     serializer_class = RatingSerializer
  21     permission_classes = (ViewSetRulesPermission,)
  22     filter_backends = (filters.DjangoFilterBackend,)
  23     filter_fields = ('object_pk', 'content_type')
  24     content_type_filter = settings.A4_RATEABLES
  25 
  26     def perform_create(self, serializer):
  27         serializer.save(
  28             content_object=self.content_object,
  29             creator=self.request.user
  30         )
  31 
  32     def get_permission_object(self):
  33         return self.content_object
  34 
  35     @property
  36     def rules_method_map(self):
  37         return ViewSetRulesPermission.default_rules_method_map._replace(
  38             POST='{app_label}.rate_{model}'.format(
  39                 app_label=self.content_type.app_label,
  40                 model=self.content_type.model
  41             )
  42         )
  43 
  44     def destroy(self, request, content_type, object_pk, pk=None):
  45         """
  46         Sets value to zero
  47         NOTE: Rating is NOT deleted.
  48         """
  49         rating = self.get_object()
  50         rating.update(0)
  51         serializer = self.get_serializer(rating)
  52         return Response(serializer.data)
  53
  [end of adhocracy4/ratings/api.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/adhocracy4/ratings/api.py b/adhocracy4/ratings/api.py
  --- a/adhocracy4/ratings/api.py
  +++ b/adhocracy4/ratings/api.py
  @@ -3,6 +3,7 @@
   from django_filters import rest_framework as filters
   from rest_framework import mixins, viewsets
   from rest_framework.response import Response
  +from rest_framework.serializers import ValidationError
   
   from adhocracy4.api.mixins import ContentTypeMixin
   from adhocracy4.api.permissions import ViewSetRulesPermission
  @@ -24,6 +25,11 @@
       content_type_filter = settings.A4_RATEABLES
   
       def perform_create(self, serializer):
  +        queryset = Rating.objects.filter(content_type_id=self.content_type.pk,
  +                                         creator=self.request.user,
  +                                         object_pk=self.content_object.pk)
  +        if queryset.exists():
  +            raise ValidationError(queryset[0].pk)
           serializer.save(
               content_object=self.content_object,
               creator=self.request.user
  
verification_info: {"golden_diff": "diff --git a/adhocracy4/ratings/api.py b/adhocracy4/ratings/api.py\n--- a/adhocracy4/ratings/api.py\n+++ b/adhocracy4/ratings/api.py\n@@ -3,6 +3,7 @@\n from django_filters import rest_framework as filters\n from rest_framework import mixins, viewsets\n from rest_framework.response import Response\n+from rest_framework.serializers import ValidationError\n \n from adhocracy4.api.mixins import ContentTypeMixin\n from adhocracy4.api.permissions import ViewSetRulesPermission\n@@ -24,6 +25,11 @@\n     content_type_filter = settings.A4_RATEABLES\n \n     def perform_create(self, serializer):\n+        queryset = Rating.objects.filter(content_type_id=self.content_type.pk,\n+                                         creator=self.request.user,\n+                                         object_pk=self.content_object.pk)\n+        if queryset.exists():\n+            raise ValidationError(queryset[0].pk)\n         serializer.save(\n             content_object=self.content_object,\n             creator=self.request.user\n", "issue": "IntegrityError in ratings\nSee:\r\n- https://sentry.liqd.net/sentry/debattenportalspdde/issues/84/\r\n- https://sentry.liqd.net/sentry/meinberlin-prod/issues/5/\n", "before_files": [{"content": "from django.conf import settings\n\nfrom django_filters import rest_framework as filters\nfrom rest_framework import mixins, viewsets\nfrom rest_framework.response import Response\n\nfrom adhocracy4.api.mixins import ContentTypeMixin\nfrom adhocracy4.api.permissions import ViewSetRulesPermission\n\nfrom .models import Rating\nfrom .serializers import RatingSerializer\n\n\nclass RatingViewSet(mixins.CreateModelMixin,\n                    mixins.UpdateModelMixin,\n                    ContentTypeMixin,\n                    viewsets.GenericViewSet):\n\n    queryset = Rating.objects.all()\n    serializer_class = RatingSerializer\n    permission_classes = (ViewSetRulesPermission,)\n    filter_backends = (filters.DjangoFilterBackend,)\n    filter_fields = ('object_pk', 'content_type')\n    content_type_filter = settings.A4_RATEABLES\n\n    def perform_create(self, serializer):\n        serializer.save(\n            content_object=self.content_object,\n            creator=self.request.user\n        )\n\n    def get_permission_object(self):\n        return self.content_object\n\n    @property\n    def rules_method_map(self):\n        return ViewSetRulesPermission.default_rules_method_map._replace(\n            POST='{app_label}.rate_{model}'.format(\n                app_label=self.content_type.app_label,\n                model=self.content_type.model\n            )\n        )\n\n    def destroy(self, request, content_type, object_pk, pk=None):\n        \"\"\"\n        Sets value to zero\n        NOTE: Rating is NOT deleted.\n        \"\"\"\n        rating = self.get_object()\n        rating.update(0)\n        serializer = self.get_serializer(rating)\n        return Response(serializer.data)\n", "path": "adhocracy4/ratings/api.py"}]}