diff --git a/README.md b/README.md index d7a1adf..f85486e 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ The following methods are allowed: | `GET` | `/translate` | Takes a `uri` as parameter and returns all identifiers associated with it. | | `GET` | `/works` | Return information about stored publications. | | `POST` | `/works` | Store a publication and associated URIs in the database. | -| `DELETE` | `/works` | Delete a publicatin and associated URIs from the database. | +| `DELETE` | `/works` | Delete a publication from the database. | | `POST` | `/titles` | Add a new title to an existing publication. | | `DELETE` | `/titles` | Remove a title from its publication. | | `POST` | `/uris` | Add a new URI to an existing publication. | @@ -239,6 +239,13 @@ In this example we use a fictional parent UUID, which could be one of a book ser } ``` +#### `DELETE /works` data + +| Attribute | Type | Description | +| --------- | ------ | ---------------------------------- | +| UUID | string | The work_id of the work to delete. | + + ### More Check some more [example queries][6]. diff --git a/src/models.py b/src/models.py index 7ad7852..7b4f71f 100644 --- a/src/models.py +++ b/src/models.py @@ -19,13 +19,13 @@ def get_type(self): options = dict(uuid=self.UUID) result = db.select('work', options, what="work_type", where="work_id = $uuid") - return result.first()["work_type"] + return result.first()["work_type"] if result else None def get_titles(self): options = dict(uuid=self.UUID) titles = db.select('work_title', options, what="title", where="work_id = $uuid") - return [(x["title"]) for x in titles] + return [(x["title"]) for x in titles] if titles else [] def get_identifiers(self): options = dict(uuid=self.UUID) @@ -144,6 +144,10 @@ def delete_titles(self): except BaseException: pass + def delete(self): + q = '''DELETE FROM work WHERE work_id = $work_id''' + db.query(q, dict(work_id=self.UUID)) + @staticmethod def generate_uuid(): return str(uuid.uuid4()) diff --git a/src/worksctrl.py b/src/worksctrl.py index 772c2ff..05b4a8b 100644 --- a/src/worksctrl.py +++ b/src/worksctrl.py @@ -142,7 +142,26 @@ def PUT(self, name): @check_token def DELETE(self, name): """Delete a work""" - raise Error(NOTALLOWED) + logger.debug("Data: %s" % (web.input())) + + work_id = web.input().get('UUID') or web.input().get('uuid') + + try: + if not work_id: + raise AssertionError + except AssertionError as error: + logger.debug(error) + raise Error(BADPARAMS, msg="You must provide a (work) UUID") + + try: + work = Work(work_id) + if not work.exists(): + raise AssertionError + except AssertionError: + raise Error(BADPARAMS, msg="Unknown work '%s'" % (work_id)) + + work.delete() + return [] @json_response def OPTIONS(self, name):