From ecdc72f16c8f51b381292523dd15103cde39bf8e Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 25 May 2011 02:11:12 -0400 Subject: [PATCH] If a document doesn't have a metadatatype don't raise error, just don't save it. Fixed multi doc heterogeneous metadata editing. --- apps/metadata/api.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/apps/metadata/api.py b/apps/metadata/api.py index eebdd2eb87..c62c03062a 100644 --- a/apps/metadata/api.py +++ b/apps/metadata/api.py @@ -33,14 +33,18 @@ def decode_metadata_from_url(url_dict): def save_metadata_list(metadata_list, document, create=False): """ - Take a list of metadata values and associate a document to it + Take a list of metadata dictionaries and associate them to a + document """ for item in metadata_list: save_metadata(item, document, create) def save_metadata(metadata_dict, document, create=False): - """save metadata_dict""" + """ + Take a dictionary of metadata type & value and associate it to a + document + """ if create: # Use matched metadata now to create document metadata document_metadata, created = DocumentMetadata.objects.get_or_create( @@ -51,21 +55,26 @@ def save_metadata(metadata_dict, document, create=False): ), ) else: - document_metadata = DocumentMetadata.objects.get( - document=document, - metadata_type=get_object_or_404( - MetadataType, - pk=metadata_dict['id'] - ), - ) + try: + document_metadata = DocumentMetadata.objects.get( + document=document, + metadata_type=get_object_or_404( + MetadataType, + pk=metadata_dict['id'] + ), + ) + except DocumentMetadata.DoesNotExist: + # TODO: Maybe return warning to caller? + document_metadata = None # Handle 'plus sign as space' in the url # unquote_plus handles utf-8?!? # http://stackoverflow.com/questions/4382875/handling-iri-in-django #.decode('utf-8') - document_metadata.value = unquote_plus(metadata_dict['value']) - document_metadata.save() + if document_metadata: + document_metadata.value = unquote_plus(metadata_dict['value']) + document_metadata.save() def metadata_repr(metadata_list):