Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/webhook_launcher/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions src/webhook_launcher/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 :
Expand Down