blob: 2fd5729fec6973e00488848ee3aa1700e0b1f64b [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 Baker77eba942015-01-12 12:57:22 -080028# XXX this was lifted and hacked up a bit from genapi.py
29class PlusListCreateAPIView(generics.ListCreateAPIView):
Scott Bakerce7fc0a2015-02-03 14:47:08 -080030 # rest_framework 2.x
31 # create() calls pre_save, then serializer.save, then post_save
32 def pre_save(self, obj):
33 super(PlusListCreateAPIView,self).pre_save(obj)
34 obj.caller = self.request.user
35
36 # rest_framework 3.x
37 # pre_save/serializer.save/post_save is replaced with perform_save
38 # *** UNTESTED ***
39 def perform_create(self, serializer):
40 self.pre_save(serializer.object)
41 super(PlusListCreateAPIView,self).perform_save(serializer)
42
Scott Baker77eba942015-01-12 12:57:22 -080043 def create(self, request, *args, **kwargs):
44 serializer = self.get_serializer(data=request.DATA, files=request.FILES)
45 if not (serializer.is_valid()):
46 response = {"error": "validation",
47 "specific_error": "not serializer.is_valid()",
48 "reasons": serializer.errors}
49 return Response(response, status=status.HTTP_400_BAD_REQUEST)
50 obj = serializer.object
51 obj.caller = request.user
Scott Bakerce7fc0a2015-02-03 14:47:08 -080052 if not obj.can_update(request.user):
Scott Baker77eba942015-01-12 12:57:22 -080053 raise Exception("failed obj.can_update")
54
55 ret = super(PlusListCreateAPIView, self).create(request, *args, **kwargs)
Scott Baker77eba942015-01-12 12:57:22 -080056
57 return ret
58
Scott Baker03696ee2015-01-11 13:45:19 -080059# XXX this is taken from genapi.py
60# XXX find a better way to re-use the code
61class PlusRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
62
63 # To handle fine-grained field permissions, we have to check can_update
64 # the object has been updated but before it has been saved.
65
66 def update(self, request, *args, **kwargs):
67 partial = kwargs.pop('partial', False)
68 self.object = self.get_object_or_none()
69
70 serializer = self.get_serializer(self.object, data=request.DATA,
71 files=request.FILES, partial=partial)
72
73 if not serializer.is_valid():
74 response = {"error": "validation",
75 "specific_error": "not serializer.is_valid()",
76 "reasons": serializer.errors}
77 return Response(response, status=status.HTTP_400_BAD_REQUEST)
78
79 try:
80 self.pre_save(serializer.object)
81 except ValidationError as err:
82 # full_clean on model instance may be called in pre_save,
83 # so we have to handle eventual errors.
84 response = {"error": "validation",
85 "specific_error": "ValidationError in pre_save",
86 "reasons": err.message_dict}
87 return Response(response, status=status.HTTP_400_BAD_REQUEST)
88
89 if serializer.object is not None:
90 if not serializer.object.can_update(request.user):
91 return Response(status=status.HTTP_400_BAD_REQUEST)
92
93 if self.object is None:
94 self.object = serializer.save(force_insert=True)
Scott Bakerce7fc0a2015-02-03 14:47:08 -080095 self.object.caller = request.user
Scott Baker03696ee2015-01-11 13:45:19 -080096 self.post_save(self.object, created=True)
97 return Response(serializer.data, status=status.HTTP_201_CREATED)
98
99 self.object = serializer.save(force_update=True)
Scott Bakerce7fc0a2015-02-03 14:47:08 -0800100 self.object.caller = request.user
Scott Baker03696ee2015-01-11 13:45:19 -0800101 self.post_save(self.object, created=False)
102 return Response(serializer.data, status=status.HTTP_200_OK)
103
104 def destroy(self, request, *args, **kwargs):
105 obj = self.get_object()
106 if obj.can_update(request.user):
107 return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs)
108 else:
109 return Response(status=status.HTTP_400_BAD_REQUEST)
110