blob: ca89c7913574420b05fcbd7ba7417bcf11e604f5 [file] [log] [blame]
Scott Baker03696ee2015-01-11 13:45:19 -08001from rest_framework import generics
Scott Bakere791dc62014-08-28 14:02:54 -07002from rest_framework import serializers
Scott Baker03696ee2015-01-11 13:45:19 -08003from rest_framework.response import Response
4from rest_framework import status
Scott Bakere791dc62014-08-28 14:02:54 -07005
6""" PlusSerializerMixin
7
8 Implements Serializer fields that are common to all OpenCloud objects. For
9 example, stuff related to backend fields.
10"""
11
12class 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 Bakere5e44eb2014-12-23 11:04:27 -080019 # Rest_framework 3.0 uses _declared_fields instead of base_fields
20 _declared_fields = {"backendIcon": backendIcon, "backendHtml": backendHtml}
Scott Bakere791dc62014-08-28 14:02:54 -070021
22 def getBackendIcon(self, obj):
23 return obj.getBackendIcon()
24
25 def getBackendHtml(self, obj):
26 return obj.getBackendHtml()
27
Scott Baker03696ee2015-01-11 13:45:19 -080028# XXX this is taken from genapi.py
29# XXX find a better way to re-use the code
30class 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