From ef77219486aca3c208d3fbe97cef61b832210d14 Mon Sep 17 00:00:00 2001 From: David Greaves Date: Sat, 1 Apr 2017 18:54:09 +0100 Subject: [PATCH 1/2] The object was just found, pass it into the partial_update Signed-off-by: David Greaves --- src/webhook_launcher/app/views.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/webhook_launcher/app/views.py b/src/webhook_launcher/app/views.py index b4f0101..0dcf6c9 100644 --- a/src/webhook_launcher/app/views.py +++ b/src/webhook_launcher/app/views.py @@ -172,11 +172,7 @@ def update(self, request, pk=None): return Response({ 'WebHookMapping Triggered by API': msg }) # PATCH / update webhook - def partial_update(self, request, pk=None): - try: - hook = WebHookMapping.objects.get(pk=pk) - except WebHookMapping.DoesNotExist: - return Response(status=status.HTTP_404_NOT_FOUND) + def partial_update(self, hook, request): serializer = WebHookMappingSerializer(hook) #first take the original data @@ -239,8 +235,7 @@ def find(self, request, obsname, project, package): # The decorator stored our kwargs and doesn's support # chaining very well so append 'pk' to self.kwargs and # then call update() - self.kwargs['pk'] = obj.id - return self.update(request=request, pk=obj.id) + return self.partial_update(obj, request=request) except WebHookMapping.DoesNotExist: return self.create(request=request) else : From ed2ff90673216bb7e823d21dc74defad9f37d075 Mon Sep 17 00:00:00 2001 From: David Greaves Date: Sat, 1 Apr 2017 18:58:01 +0100 Subject: [PATCH 2/2] It appears that to_internal_value() should return an OrderedDict() providing a value for the appropriate attr; in this case that's the _lsr private attribute of a WebHookMapping Signed-off-by: David Greaves --- src/webhook_launcher/app/serializers.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/webhook_launcher/app/serializers.py b/src/webhook_launcher/app/serializers.py index 04004c5..600f93c 100644 --- a/src/webhook_launcher/app/serializers.py +++ b/src/webhook_launcher/app/serializers.py @@ -4,6 +4,7 @@ from rest_framework import serializers from StringIO import StringIO from rest_framework.parsers import JSONParser +from collections import OrderedDict class BuildServiceSerializer(serializers.ModelSerializer): class Meta: @@ -72,27 +73,32 @@ def get_value(self, obj): def to_internal_value(self, data): field_name="lsr" if field_name not in data: - return - mydata = data[field_name] + raise Exception("No 'lsr' in data: %s" % data) + lsrdata = data[field_name] # An OrderedDict # Try and get our existing lsr - if self.parent.validated_data is None: - print "Can't set an lsr on object creation since the lsr needs the id of the object which hasn't been created at the time the lsr is created :(" - return - lsr = self.parent.validated_data.lsr + if self.parent.initial_data is None: + raise Exception("Can't set an lsr on object creation since the lsr needs the id of the object which hasn't been created at the time the lsr is created :(") + + lsr = None + if "id" in lsrdata: + lsr = LastSeenRevision.objects.get(pk=lsrdata["id"]) if not lsr: # create a new lsr - lsr = LastSeenRevision(mapping = self.parent.validated_data) + lsr = LastSeenRevision(mapping = lsrdata["id"]) # update it with the data and ensure it's valid # Passing lsr into LastSeenRevisionSerializer() updates it in place # and returns a Serializer reference to it which we use for the useful # functions - lsr_ = LastSeenRevisionSerializer(lsr, data=mydata, partial=True) + lsr_ = LastSeenRevisionSerializer(lsr, data=lsrdata, partial=True) if not lsr_.is_valid() : raise Exception(lsr_.errors) # and just absolutely ensure the mapping is still to us - lsr_.mapping = self.parent.validated_data + lsr_.mapping = data["id"] lsr_.save() + r=OrderedDict() + r["_lsr"]=lsr_.instance + return r class WebHookMappingSerializer(serializers.ModelSerializer): # lsr = LastSeenRevisionSerializer(many=False, read_only=True)