Ejemplo n.º 1
0
def copy_coll_data(src_coll, tgt_coll):
    """
    Copy collection data from specified source to target collection.

    returns     list of error messages; an empty list indicates success.
    """
    # @@TESTME: Not tested by test suite
    log.info("Copying collection '%s' to '%s'"%(src_coll.get_id(), tgt_coll.get_id()))
    msgs = []
    entityfinder = EntityFinder(src_coll)
    for e in entityfinder.get_entities():
        entity_id  = e.get_id()
        typeinfo   = EntityTypeInfo(
            tgt_coll, e.get_type_id(), create_typedata=True
            )
        new_entity = typeinfo.create_entity(entity_id, e.get_values())
        if not typeinfo.entity_exists(entity_id):
            msg = (
                "Collection.copy_coll_data: Failed to create entity %s/%s"%
                    (typeinfo.type_id, entity_id)
                )
            log.warning(msg)
            msgs.append(msg)
        msgs += new_entity._copy_entity_files(e)
    return msgs
Ejemplo n.º 2
0
 def check_entity_values(self, type_id, entity_id, check_values=None):
     "Helper function checks content of entity record"
     typeinfo = EntityTypeInfo(self.testcoll, type_id)
     self.assertTrue(typeinfo.entity_exists(entity_id))
     t = typeinfo.get_entity(entity_id)
     self.assertEqual(t.get_id(), entity_id)
     self.assertEqual(t.get_type_id(), type_id)
     self.assertDictionaryMatch(t.get_values(), check_values)
     return t
Ejemplo n.º 3
0
 def check_entity_values(self, type_id, entity_id, check_values=None):
     "Helper function checks content of entity record; returns entity"
     typeinfo = EntityTypeInfo(self.testcoll, type_id)
     self.assertTrue(typeinfo.entity_exists(entity_id))
     e = typeinfo.get_entity(entity_id)
     self.assertEqual(e.get_id(), entity_id)
     self.assertEqual(e.get_type_id(), type_id)
     self.assertDictionaryMatch(e.get_values(), check_values)
     return e
Ejemplo n.º 4
0
def copy_coll_data(src_coll, tgt_coll):
    """
    Copy collection data from specified source to target collection.

    returns     list of error messages; an empty list indicates success.
    """
    log.info("Copying collection '%s' to '%s'" %
             (src_coll.get_id(), tgt_coll.get_id()))
    msgs = []
    entityfinder = EntityFinder(src_coll)
    for e in entityfinder.get_entities():
        entity_id = e.get_id()
        typeinfo = EntityTypeInfo(tgt_coll,
                                  e.get_type_id(),
                                  create_typedata=True)
        new_entity = typeinfo.create_entity(entity_id, e.get_values())
        if not typeinfo.entity_exists(entity_id):
            msg = ("Collection.copy_coll_data: Failed to create entity %s/%s" %
                   (typeinfo.type_id, entity_id))
            log.warning(msg)
            msgs.append(msg)
        msgs += new_entity._copy_entity_files(e)
    return msgs
Ejemplo n.º 5
0
 def check_entity_does_not_exist(self, type_id, entity_id):
     "Helper function checks content of entity record"
     typeinfo = EntityTypeInfo(self.testcoll, type_id)
     self.assertFalse(typeinfo.entity_exists(entity_id))
     return
Ejemplo n.º 6
0
    def save_entity(self, entityvaluemap, form_data, entity_id, entity_type_id,
                    orig_entity_id, orig_entity_type_id, viewinfo,
                    context_extra_values, messages):
        """
        This method contains logic to save entity data modified through a form
        interface.  If an entity is being edited (as opposed to created or copied)
        and the entity id or type have been changed, then new entity data is written 
        and the original entity data is removed.

        Returns None if the save completes successfully, otherwise an 
        HTTP response object that reports the nature of the problem.
        """
        # log.info(
        #     "save_entity: save, action %s, entity_id %s, orig_entity_id %s, entity_type_id %s, orig_entity_type_id %s"
        #     %(form_data['action'], entity_id, orig_entity_id, entity_type_id, orig_entity_type_id)
        #     )
        action = form_data['action']
        typeinfo = viewinfo.entitytypeinfo
        if not action in ["new", "copy", "edit"]:
            log.warning("'Save' operation for action '%s'" % (action))
            # Check "edit" authorization to continue
            if viewinfo.check_authorization("edit"):
                return viewinfo.http_response
        entity_id_changed = ((action == "edit")
                             and ((entity_id != orig_entity_id) or
                                  (entity_type_id != orig_entity_type_id)))

        # Check for valid entity nid and type id
        # @@TODO: factor out repeated re-rendering logic
        if not util.valid_id(entity_id):
            log.warning("save_entity: invalid entity_id (%s)" % (entity_id))
            return self.form_re_render(
                viewinfo,
                entityvaluemap,
                form_data,
                context_extra_values,
                error_head=message.ENTITY_DATA_ID,
                error_message=message.ENTITY_DATA_ID_INVALID)
        if not util.valid_id(entity_type_id):
            log.warning("save_entity: invalid entity_type_id (%s)" %
                        (entity_type_id))
            return self.form_re_render(
                viewinfo,
                entityvaluemap,
                form_data,
                context_extra_values,
                error_head=message.ENTITY_TYPE_ID,
                error_message=message.ENTITY_TYPE_ID_INVALID)

        # Check original parent exists (still)
        #@@ TODO: unless this is a "new" action?
        if not typeinfo.parent_exists():
            log.warning("save_entity: original entity parent does not exist")
            return self.form_re_render(
                viewinfo,
                entityvaluemap,
                form_data,
                context_extra_values,
                error_head=messages['parent_heading'],
                error_message=messages['parent_missing'])

        # Determine type information for saved entity
        if entity_type_id != orig_entity_type_id:
            # log.info("new_typeinfo: entity_type_id %s"%(entity_type_id))
            new_typeinfo = EntityTypeInfo(viewinfo.site,
                                          viewinfo.collection,
                                          entity_type_id,
                                          create_typedata=True)
        else:
            new_typeinfo = typeinfo

        # Check existence of entity to save according to action performed
        if (action in ["new", "copy"]) or entity_id_changed:
            if new_typeinfo.entity_exists(entity_id):
                log.warning("Entity exists: action %s %s/%s, orig %s/%s" %
                            (action, entity_type_id, entity_id,
                             orig_entity_type_id, orig_entity_id))
                return self.form_re_render(
                    viewinfo,
                    entityvaluemap,
                    form_data,
                    context_extra_values,
                    error_head=messages['entity_heading'],
                    error_message=messages['entity_exists'])
        else:
            if not typeinfo.entity_exists(entity_id, use_altparent=True):
                # This shouldn't happen, but just in case...
                log.warning(
                    "Expected %s/%s not found; action %s, entity_id_changed %r"
                    % (entity_type_id, entity_id, action, entity_id_changed))
                return self.form_re_render(
                    viewinfo,
                    entityvaluemap,
                    form_data,
                    context_extra_values,
                    error_head=messages['entity_heading'],
                    error_message=messages['entity_not_exists'])

        # Assemble updated values for storage
        #
        # Note: form data is applied as update to original entity data so that
        # values not in view are preserved.  Use original entity values without
        # field aliases as basis for new value.
        orig_entity = typeinfo.get_entity(entity_id, action)
        orig_values = orig_entity.get_values() if orig_entity else {}
        entity_values = orig_values.copy()
        # log.info("orig entity_values %r"%(entity_values,))
        if action == "copy":
            entity_values.pop(ANNAL.CURIE.uri, None)  # Force new URI on copy
        entity_values.update(entityvaluemap.map_form_data_to_values(form_data))
        entity_values[ANNAL.CURIE.type_id] = entity_type_id
        entity_values[ANNAL.CURIE.type] = new_typeinfo.entityclass._entitytype
        # log.info("save entity_values%r"%(entity_values))

        # Create/update stored data now
        if not entity_id_changed:
            # Normal (non-type) entity create or update, no renaming
            err_vals = self.create_update_entity(new_typeinfo, entity_id,
                                                 entity_values)
        elif "_type" not in [entity_type_id, orig_entity_type_id]:
            # Non-type record rename
            err_vals = self.rename_entity(typeinfo, orig_entity_id,
                                          new_typeinfo, entity_id,
                                          entity_values)
        else:
            err_vals = self.rename_entity_type(viewinfo, typeinfo,
                                               orig_entity_id, new_typeinfo,
                                               entity_id, entity_values)
        if err_vals:
            return self.form_re_render(viewinfo,
                                       entityvaluemap,
                                       form_data,
                                       context_extra_values,
                                       error_head=err_vals[0],
                                       error_message=err_vals[1])

        return None
Ejemplo n.º 7
0
 def check_entity_does_not_exist(self, type_id, entity_id):
     "Helper function checks non-existence of entity record"
     typeinfo = EntityTypeInfo(self.testcoll, type_id)
     self.assertFalse(typeinfo.entity_exists(entity_id))
     return