def updateMetadata(self, obj, dom):
     """ Metadata as described in:
         SWORD V2 Spec for Publishing Modules in Connexions
         Section: Metadata
     """
     self.update_semantics = 'merge'
     metadata = {}
     metadata.update(self.getMetadata(dom, METADATA_MAPPING))
     for oerdc_name, cnx_name in METADATA_MAPPING.items():
         if cnx_name in ['keywords', 'subject',]:
             current_values = getattr(obj, cnx_name)
             if current_values:
                 if type(current_values) == TupleType:
                     current_values = list(current_values)
                 new_values = metadata.get(cnx_name, [])
                 if type(new_values) == StringType:
                     new_values = [new_values,]
                 for value in new_values:
                     if value not in current_values:
                         current_values.extend(value)
                 metadata[cnx_name] = new_values
     if metadata:
         self.validate_metadata(metadata)
         metadata = self.fixEntities(metadata, ATTRIBUTES_TO_FIX)
         if ICollection.providedBy(obj):
             obj.collection_metadata(**metadata)
         elif IModule.providedBy(obj):
             obj.update_metadata(**metadata)
     self.updateRoles(obj, dom)
     obj.reindexObject(idxs=metadata.keys())
 def replaceMetadata(self, obj, dom):
     """ We replace the module metadata with the values from the request.
         We use METADATA_DEFAULTS to reset those values we don't have
         on the request back to what they would be for a new module.
         This effictive 'clears' all metadata fields that were not supplied
         on the request.
         We delete all collaboration requests that are 'pending', but
         don't have equivalent data on the request.
         We add all new roles.
         TODO: Checkout the _reset method in ModuleEditor. It might be
               better to use that than do our own thing here.
     """
     self.update_semantics = 'replace'
     # create a metadata dict that has all the defaults, overridden by the
     # current dom values. This way we will 'clear' the properties not in
     # the dom.
     metadata = copy(METADATA_DEFAULTS)
     metadata.update(self.getMetadata(dom, METADATA_MAPPING))
     if metadata:
         self.validate_metadata(metadata)
         metadata = self.fixEntities(metadata, ATTRIBUTES_TO_FIX)
         if ICollection.providedBy(obj):
             obj.collection_metadata(**metadata)
         elif IModule.providedBy(obj):
             obj.update_metadata(**metadata)
     # we set GoogleAnalyticsTrackingCode explicitly, since the script
     # 'update_metadata' ignores empty strings.
     obj.GoogleAnalyticsTrackingCode = metadata.get('GoogleAnalyticsTrackingCode')
     self.updateRoles(obj, dom)
     obj.reindexObject(idxs=metadata.keys())
 def mergeMetadata(self, obj, dom):
     """        
         Merge the metadata on the obj (module) with whatever is in the dom
         parameter. From the spec. what should be replaced and what we should
         just add to.
         - title (dcterms:title) : Replace
         - abstract/summary (dcterms:abstract) : Replace
         - language (dcterms:language) : Replace
         - keyword (dcterms:subject) : Add
         - subject (dcterms:subject xsi:type="oerdc:Subjects") : Replace
         - contributor roles : Add
         - descriptionOfChanges : Replace
         - analyticsCode : Replace
         For more current info see: 
         - Google doc: SWORD V2 Spec for Publishing Modules in Connexions
     """        
     self.update_semantics = 'merge'
     # create a metadata dict that has all the values from obj, overridden
     # by the current dom values.
     metadata = self.getModuleMetadata(obj, {})
     metadata.update(self.getMetadata(dom, METADATA_MAPPING))
     for oerdc_name, cnx_name in METADATA_MAPPING.items():
         if cnx_name in ['keywords',]:
             old_value = getattr(obj, cnx_name)
             if old_value:
                 current_value = list(metadata.get(cnx_name, []))
                 current_value.extend(old_value)
                 metadata[cnx_name] = current_value
     if metadata:
         self.validate_metadata(metadata)
         metadata = self.fixEntities(metadata, ATTRIBUTES_TO_FIX)
         if ICollection.providedBy(obj):
             obj.collection_metadata(**metadata)
         elif IModule.providedBy(obj):
             obj.update_metadata(**metadata)
     self.updateRoles(obj, dom)
     obj.reindexObject(idxs=metadata.keys())