Skip to content

Commit

Permalink
ignore empty attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Jan 8, 2016
1 parent 8f7ff7e commit e517b7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
30 changes: 24 additions & 6 deletions patterns/base/serializers.py
Expand Up @@ -22,9 +22,19 @@ def get_value(self, dictionary):
# Ideal case: an array of dicts; this can be handled by the default
# implementation. HTML JSON forms will use this approach.
if self.field_name in dictionary:
return super(TypedAttachmentListSerializer, self).get_value(
value = super(TypedAttachmentListSerializer, self).get_value(
dictionary
)
for i, row in enumerate(value):
empty = True
for key, val in row.items():
if key == self.child.type_field:
continue
elif val:
empty = False
if empty:
value[i] = None
return value

# Deprecated/"classic" form style, where each attachment is submitted
# as form fields with names in the format [model]_[typeid] e.g.
Expand Down Expand Up @@ -61,6 +71,10 @@ def get_value(self, dictionary):
class AttachmentSerializer(ModelSerializer):
id = serializers.IntegerField(required=False)

def __init__(self, *args, **kwargs):
kwargs['allow_null'] = True
super(AttachmentSerializer, self).__init__(*args, **kwargs)


class TypedAttachmentSerializer(AttachmentSerializer):
attachment_fields = ['id']
Expand Down Expand Up @@ -132,6 +146,8 @@ def create(self, validated_data):
for name in attachment_data:
model = fields[name].child.Meta.model
for attachment in attachment_data[name]:
if not attachment:
continue
self.set_parent_object(attachment, instance, name)
self.create_attachment(model, attachment, name)
return instance
Expand All @@ -146,6 +162,8 @@ def update(self, instance, validated_data):
for name in attachment_data:
model = fields[name].child.Meta.model
for attachment in attachment_data[name]:
if not attachment:
continue
self.set_parent_object(attachment, instance, name)
if 'id' in attachment:
exist = self.get_attachment(model, attachment['id'])
Expand All @@ -171,13 +189,13 @@ def get_attachment(self, model, pk):
return model.objects.get(pk=pk)

def update_attachment(self, exist, attachment, name):
for key, val in attachment.items():
if key != 'id':
setattr(exist, key, val)
exist.save()
field = self.get_fields()[name]
attachment.pop('id')
field.child.update(exist, attachment)

def create_attachment(self, model, attachment, name):
return model.objects.create(**attachment)
field = self.get_fields()[name]
field.child.create(attachment)


class NaturalKeyValidator(serializers.UniqueTogetherValidator):
Expand Down
10 changes: 8 additions & 2 deletions patterns/identify/serializers.py
Expand Up @@ -7,9 +7,15 @@
class IdentifierListSerializer(base.TypedAttachmentListSerializer):
def to_internal_value(self, data):
data = super(IdentifierListSerializer, self).to_internal_value(data)
primary = [ident for ident in data if ident.get('is_primary', None)]
primary = [
ident for ident in data
if ident and ident.get('is_primary', None)
]
if not any(primary) and len(data) > 0:
data[0]['is_primary'] = True
for ident in data:
if ident:
ident['is_primary'] = True
break
return data


Expand Down

0 comments on commit e517b7d

Please sign in to comment.