Ejemplo n.º 1
0
    def POST(self, name):
        """Create a work"""
        logger.debug("Data: %s" % (web.data()))

        data = json.loads(web.data())
        wtype = data.get('type')
        title = data.get('title')
        uri = data.get('URI') or data.get('uri')
        parent = data.get('parent')
        child = data.get('child')

        try:
            titles = strtolist(title)
            uris = strtolist(uri)
            assert wtype and titles and uris
        except AssertionError as error:
            logger.debug(error)
            raise Error(BADPARAMS,
                        msg="You must provide a (work) type" +
                        ", a title, and at least one URI")

        try:
            assert WorkType(wtype).exists()
        except:
            raise Error(BADPARAMS, msg="Unknown work type '%s'" % (wtype))

        for i in uris:
            # attempt to get scheme from URI
            try:
                ident = i['URI'] or i['uri']
                scheme, value = Identifier.split_uri(ident)
                try:
                    i['canonical'] = i['canonical'] in (True, "true", "True")
                except:
                    i['canonical'] = False
            except:
                raise Error(BADPARAMS, msg="Invalid URI '%s'" % (ident))

            # check whether the URI scheme exists in the database
            try:
                assert UriScheme(scheme).exists()
            except:
                raise Error(BADPARAMS,
                            msg="Unknown URI scheme '%s'" % (scheme))

        uuid = Work.generate_uuid()
        work = Work(uuid, wtype, titles, uris)

        if parent:
            parents = strtolist(parent)
            for p in parents:
                try:
                    assert Work.is_uuid(p)
                    assert Work.uuid_exists(p)
                except AssertionError as error:
                    logger.debug(error)
                    raise Error(BADPARAMS, msg="Invalid parent UUID provided.")
            work.set_parents(parents)

        if child:
            children = strtolist(child)
            for c in children:
                try:
                    assert Work.is_uuid(c)
                    assert Work.uuid_exists(c)
                except AssertionError as error:
                    logger.debug(error)
                    raise Error(BADPARAMS, msg="Invalid child UUID provided.")
            work.set_children(children)

        work.save()

        return [work.__dict__]