Scott Baker | dcf6fbf | 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 | dcf6fbf | 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 | a4ac3a8 | 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 | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 28 | # XXX this was lifted and hacked up a bit from genapi.py |
| 29 | class PlusListCreateAPIView(generics.ListCreateAPIView): |
| 30 | def create(self, request, *args, **kwargs): |
| 31 | serializer = self.get_serializer(data=request.DATA, files=request.FILES) |
| 32 | if not (serializer.is_valid()): |
| 33 | response = {"error": "validation", |
| 34 | "specific_error": "not serializer.is_valid()",
|
| 35 | "reasons": serializer.errors}
|
| 36 | return Response(response, status=status.HTTP_400_BAD_REQUEST) |
Scott Baker | cba0ffe | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 37 | |
| 38 | # now do XOS can_update permission checking |
| 39 | |
Scott Baker | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 40 | obj = serializer.object |
| 41 | obj.caller = request.user |
Scott Baker | 447eb50 | 2015-02-03 14:47:08 -0800 | [diff] [blame] | 42 | if not obj.can_update(request.user): |
Scott Baker | cba0ffe | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 43 | response = {"error": "validation", |
| 44 | "specific_error": "failed can_update",
|
| 45 | "reasons": []}
|
| 46 | return Response(response, status=status.HTTP_400_BAD_REQUEST) |
Scott Baker | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 47 | |
Scott Baker | cba0ffe | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 48 | # stuff below is from generics.ListCreateAPIView |
Scott Baker | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 49 | |
Scott Baker | cba0ffe | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 50 | if (hasattr(self, "pre_save")): |
| 51 | # rest_framework 2.x |
| 52 | self.pre_save(serializer.object) |
| 53 | self.object = serializer.save(force_insert=True) |
| 54 | self.post_save(self.object, created=True) |
| 55 | else: |
| 56 | # rest_framework 3.x |
| 57 | self.perform_create(serializer) |
| 58 | |
| 59 | headers = self.get_success_headers(serializer.data) |
| 60 | return Response(serializer.data, status=status.HTTP_201_CREATED,
|
| 61 | headers=headers) |
Scott Baker | bbc8a5b | 2015-01-12 12:57:22 -0800 | [diff] [blame] | 62 | |
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 63 | # XXX this is taken from genapi.py |
| 64 | # XXX find a better way to re-use the code |
| 65 | class PlusRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView): |
| 66 | |
| 67 | # To handle fine-grained field permissions, we have to check can_update |
| 68 | # the object has been updated but before it has been saved. |
| 69 | |
| 70 | def update(self, request, *args, **kwargs):
|
| 71 | partial = kwargs.pop('partial', False)
|
| 72 | self.object = self.get_object_or_none()
|
| 73 |
|
| 74 | serializer = self.get_serializer(self.object, data=request.DATA,
|
| 75 | files=request.FILES, partial=partial)
|
| 76 |
|
| 77 | if not serializer.is_valid():
|
| 78 | response = {"error": "validation",
|
| 79 | "specific_error": "not serializer.is_valid()",
|
| 80 | "reasons": serializer.errors}
|
| 81 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
| 82 |
|
| 83 | try:
|
| 84 | self.pre_save(serializer.object)
|
| 85 | except ValidationError as err:
|
| 86 | # full_clean on model instance may be called in pre_save,
|
| 87 | # so we have to handle eventual errors.
|
| 88 | response = {"error": "validation",
|
| 89 | "specific_error": "ValidationError in pre_save",
|
| 90 | "reasons": err.message_dict}
|
| 91 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
| 92 |
|
| 93 | if serializer.object is not None:
|
| 94 | if not serializer.object.can_update(request.user):
|
| 95 | return Response(status=status.HTTP_400_BAD_REQUEST)
|
| 96 |
|
| 97 | if self.object is None:
|
Scott Baker | cba0ffe | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 98 | raise Exception("Use the List API for creating objects")
|
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 99 |
|
| 100 | self.object = serializer.save(force_update=True)
|
Scott Baker | 447eb50 | 2015-02-03 14:47:08 -0800 | [diff] [blame] | 101 | self.object.caller = request.user
|
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 102 | self.post_save(self.object, created=False)
|
| 103 | return Response(serializer.data, status=status.HTTP_200_OK) |
| 104 | |
| 105 | def destroy(self, request, *args, **kwargs): |
| 106 | obj = self.get_object() |
| 107 | if obj.can_update(request.user): |
| 108 | return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs) |
| 109 | else: |
| 110 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 111 | |