Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame^] | 1 | from rest_framework import generics |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 2 | from rest_framework import serializers |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame^] | 3 | from rest_framework.response import Response |
| 4 | from rest_framework import status |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 5 | |
| 6 | """ PlusSerializerMixin |
| 7 | |
| 8 | Implements Serializer fields that are common to all OpenCloud objects. For |
| 9 | example, stuff related to backend fields. |
| 10 | """ |
| 11 |
|
| 12 | class PlusSerializerMixin(): |
| 13 | backendIcon = serializers.SerializerMethodField("getBackendIcon") |
| 14 | backendHtml = serializers.SerializerMethodField("getBackendHtml") |
| 15 | |
| 16 | # This will cause a descendant class to pull in the methods defined |
| 17 | # above. See rest_framework/serializers.py: _get_declared_fields(). |
| 18 | base_fields = {"backendIcon": backendIcon, "backendHtml": backendHtml} |
Scott Baker | e5e44eb | 2014-12-23 11:04:27 -0800 | [diff] [blame] | 19 | # Rest_framework 3.0 uses _declared_fields instead of base_fields |
| 20 | _declared_fields = {"backendIcon": backendIcon, "backendHtml": backendHtml} |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 21 | |
| 22 | def getBackendIcon(self, obj): |
| 23 | return obj.getBackendIcon() |
| 24 | |
| 25 | def getBackendHtml(self, obj): |
| 26 | return obj.getBackendHtml() |
| 27 | |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame^] | 28 | # XXX this is taken from genapi.py |
| 29 | # XXX find a better way to re-use the code |
| 30 | class PlusRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView): |
| 31 | |
| 32 | # To handle fine-grained field permissions, we have to check can_update |
| 33 | # the object has been updated but before it has been saved. |
| 34 | |
| 35 | def update(self, request, *args, **kwargs):
|
| 36 | partial = kwargs.pop('partial', False)
|
| 37 | self.object = self.get_object_or_none()
|
| 38 |
|
| 39 | serializer = self.get_serializer(self.object, data=request.DATA,
|
| 40 | files=request.FILES, partial=partial)
|
| 41 |
|
| 42 | if not serializer.is_valid():
|
| 43 | response = {"error": "validation",
|
| 44 | "specific_error": "not serializer.is_valid()",
|
| 45 | "reasons": serializer.errors}
|
| 46 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
| 47 |
|
| 48 | try:
|
| 49 | self.pre_save(serializer.object)
|
| 50 | except ValidationError as err:
|
| 51 | # full_clean on model instance may be called in pre_save,
|
| 52 | # so we have to handle eventual errors.
|
| 53 | response = {"error": "validation",
|
| 54 | "specific_error": "ValidationError in pre_save",
|
| 55 | "reasons": err.message_dict}
|
| 56 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
| 57 |
|
| 58 | if serializer.object is not None:
|
| 59 | if not serializer.object.can_update(request.user):
|
| 60 | return Response(status=status.HTTP_400_BAD_REQUEST)
|
| 61 |
|
| 62 | if self.object is None:
|
| 63 | self.object = serializer.save(force_insert=True)
|
| 64 | self.post_save(self.object, created=True)
|
| 65 | return Response(serializer.data, status=status.HTTP_201_CREATED)
|
| 66 |
|
| 67 | self.object = serializer.save(force_update=True)
|
| 68 | self.post_save(self.object, created=False)
|
| 69 | return Response(serializer.data, status=status.HTTP_200_OK) |
| 70 | |
| 71 | def destroy(self, request, *args, **kwargs): |
| 72 | obj = self.get_object() |
| 73 | if obj.can_update(request.user): |
| 74 | return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs) |
| 75 | else: |
| 76 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 77 | |