Beispiel #1
0
    def DELETE(self, name):
        """Delete a work"""
        logger.debug("Data: %s" % (web.input()))

        work_id = web.input().get('UUID') or web.input().get('uuid')

        require_params_or_fail([work_id], 'a (work) UUID')

        work = Work.find_or_fail(work_id)
        work.delete()
        return []
Beispiel #2
0
    def DELETE(self, name):
        """Delete a title"""
        work_id = web.input().get('UUID') or web.input().get('uuid')
        title = web.input().get('title')

        require_params_or_fail([title, work_id], "(work) UUID and title")

        work = Work.find_or_fail(work_id, titles=[title])
        work.delete_titles()
        work.load_titles()
        work.load_identifiers()

        return [work.__dict__]
Beispiel #3
0
    def POST(self, name):
        """Add titles to an existing work"""
        data = json.loads(web.data().decode('utf-8'))
        title = data.get('title')
        work_id = data.get('UUID') or data.get('uuid')

        titles = strtolist(title)
        require_params_or_fail([work_id], "a (work) UUID")
        require_params_or_fail([titles], "at least a title")

        work = Work.find_or_fail(work_id, titles=titles)
        work.save()
        work.load_titles()
        work.load_identifiers()

        return [work.__dict__]
Beispiel #4
0
    def GET(self, name):
        """ Get a matching ID in the desired format for a given object ID.

         1. get uri or title from request
            - if uri: get scheme and path lowecased
                - if isbn: remove hyphens
         2. get parameters from filters
         3. query by URI or title or both
         4. iterate through results and check them
           - if single: output
           - if multiple && strict: attempt to choose fittest result
           - if multiple && !strict: output all
        """
        uri     = web.input().get('uri') or web.input().get('URI')
        title   = web.input().get('title')
        filters = web.input().get('filter')
        strict  = web.input().get('strict') in ("true", "True")

        try:
            if uri:
                scheme, value = Identifier.split_uri(uri)
                require_params_or_fail([scheme, value], 'a valid URI')
            if title:
                title = urllib.parse.unquote(title.strip())
                require_params_or_fail([title], 'a valid title')
            if not uri and not title:
                raise Error
        except BaseException:
            raise Error(BADPARAMS, msg="Invalid URI or title provided")

        clause, params = build_parms(filters)

        if uri and not title:
            results = Identifier.get_from_uri(scheme, value, clause, params)
        elif title and not uri:
            results = Identifier.get_from_title(title, clause, params)
        else:
            results = Identifier.get_from_title(title, clause, params,
                                                scheme, value)

        if not results:
            raise Error(NORESULT)

        return self.process_results(list(results), strict)
Beispiel #5
0
    def DELETE(self, name):
        """Delete an identifier"""
        work_id = web.input().get('UUID') or web.input().get('uuid')
        uri = web.input().get('URI') or web.input().get('uri')

        require_params_or_fail([uri, work_id], "a (work) UUID and a URI")

        try:
            scheme, value = Identifier.split_uri(uri)
            uris = [{'URI': uri}]
        except Exception:
            raise Error(BADPARAMS, msg="Invalid URI '%s'" % (uri))

        work = Work.find_or_fail(work_id, uris)

        work.delete_uris()
        work.load_identifiers()

        return [work.__dict__]
Beispiel #6
0
    def POST(self, name):
        """Create a work relation"""
        data = json.loads(web.data().decode('utf-8'))
        parent_uuid = data.get('parent_UUID') or data.get('parent_uuid')
        child_uuid = data.get('child_UUID') or data.get('child_uuid')

        require_params_or_fail([parent_uuid, child_uuid],
                               'a parent and a child UUID')

        parent = Work.find_or_fail(parent_uuid)
        child = Work.find_or_fail(child_uuid)

        parent.set_children([child.UUID])
        parent.save()

        parent.load_titles()
        parent.load_identifiers()
        parent.load_children()
        parent.load_parents()

        return [parent.__dict__]
Beispiel #7
0
    def POST(self, name):
        """Add identifiers to an existing work"""
        data = json.loads(web.data().decode('utf-8'))
        uri = data.get('URI') or data.get('uri')
        canonical = data.get('canonical') in (True, "true", "True")
        work_id = data.get('UUID') or data.get('uuid')

        require_params_or_fail([uri, work_id], "a (work) UUID and a URI")

        try:
            scheme, value = Identifier.split_uri(uri)
            uris = [{'URI': uri, 'canonical': canonical}]
        except Exception:
            raise Error(BADPARAMS, msg="Invalid URI '%s'" % (uri))

        UriScheme.find_or_fail(scheme)

        work = Work.find_or_fail(work_id, uris=uris)
        work.save()
        work.load_identifiers()

        return [work.__dict__]
Beispiel #8
0
    def POST(self, name):
        """Create a work"""
        data = json.loads(web.data().decode('utf-8'))
        wtype = data.get('type', '')
        title = data.get('title')
        uri = data.get('URI') or data.get('uri')
        parent = data.get('parent')
        child = data.get('child')

        titles = strtolist(title)
        uris = strtolist(uri)
        require_params_or_fail([wtype], 'a (work) type')
        require_params_or_fail(titles, 'at least one title')
        require_params_or_fail(uris, 'at least one URI')
        WorkType.find_or_fail(wtype)

        for i in uris:
            # attempt to get scheme from URI
            try:
                ident = i.get('URI') or i.get('uri')
                scheme, value = Identifier.split_uri(ident)
                try:
                    i['canonical'] = i['canonical'] in (True, "true", "True")
                except Exception:
                    i['canonical'] = False
            except Exception:
                identifier = ident if ident else ''
                raise Error(BADPARAMS, msg="Invalid URI '%s'" % (identifier))
            # check whether the URI scheme exists in the database
            UriScheme.find_or_fail(scheme)

        # instantiate a new work with the input data
        uuid = generate_uuid()
        work = Work(uuid, wtype, titles, uris)

        # check relatives and associate them with the work
        work.check_and_set_relatives(parent, child)
        work.save()

        return [work.__dict__]