Skip to content

Commit

Permalink
api: DELETE support for REST API
Browse files Browse the repository at this point in the history
* NEW Adds DELETE support to the records REST API.

Signed-off-by: Nicolas Harraudeau <[email protected]>
  • Loading branch information
Nicolas Harraudeau committed Jan 11, 2016
1 parent a3005a5 commit 8c70ec8
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ charset = utf-8
indent_size = 4
# isort plugin configuration
known_first_party = invenio_records_rest
known_third_party = invenio_records, invenio_rest
known_third_party = invenio_records, invenio_rest, invenio_pidstore
multi_line_output = 2
default_section = THIRDPARTY

Expand Down
43 changes: 37 additions & 6 deletions invenio_records_rest/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand All @@ -27,27 +27,27 @@
from __future__ import absolute_import, print_function

import uuid
from functools import wraps
from functools import partial, wraps

from flask import Blueprint, abort, current_app, jsonify, make_response, \
request, url_for
from invenio_db import db
from invenio_pidstore import current_pidstore
from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError, \
PIDMissingObjectError, PIDRedirectedError, PIDUnregistered
from invenio_pidstore.models import PersistentIdentifier
from invenio_pidstore.resolver import Resolver
from invenio_records.api import Record
from invenio_rest import ContentNegotiatedMethodView
from invenio_rest.decorators import require_content_types
from jsonpatch import JsonPatchException, JsonPointerException
from sqlalchemy.exc import SQLAlchemyError
from werkzeug.routing import BuildError
from werkzeug.local import LocalProxy
from werkzeug.routing import BuildError
from werkzeug.utils import import_string

from .serializers import record_to_json_serializer


current_records_rest = LocalProxy(
lambda: current_app.extensions['invenio-records-rest'])

Expand Down Expand Up @@ -108,7 +108,7 @@ def create_url_rules(endpoint, list_route=None, item_route=None,
if delete_permission_factory_imp else None

resolver = Resolver(pid_type=pid_type, object_type='rec',
getter=Record.get_record)
getter=partial(Record.get_record, with_deleted=True))

serializers = {'application/json': record_to_json_serializer, }

Expand Down Expand Up @@ -273,12 +273,43 @@ def __init__(self, resolver=None, read_permission_factory=None,
:param resolver: Persistent identifier resolver instance.
"""
super(RecordResource, self).__init__(**kwargs)
super(RecordResource, self).__init__(method_serializers={
'DELETE': {
'*/*': lambda *args: make_response(*args),
},
}, **kwargs)
self.resolver = resolver
self.read_permission_factory = read_permission_factory
self.update_permission_factory = update_permission_factory
self.delete_permission_factory = delete_permission_factory

@pass_record
@need_record_permission('delete_permission_factory')
def delete(self, pid, record, **kwargs):
"""Delete a record.
:param pid: Persistent identifier for record.
:param record: Record object.
"""
self.check_etag(str(record.model.version_id))

try:
record.delete()
# mark all PIDs as DELETED
all_pids = PersistentIdentifier.query \
.filter(PersistentIdentifier.object_type == pid.object_type) \
.filter(PersistentIdentifier.object_uuid == pid.object_uuid) \
.all()
for rec_pid in all_pids:
if not rec_pid.is_deleted():
rec_pid.delete()
db.session.commit()
except Exception:
db.session.rollback()
current_app.logger.exception("Failed to delete record.")
abort(500)
return '', 204

@pass_record
@need_record_permission('read_permission_factory')
def get(self, pid, record, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -64,7 +64,7 @@
install_requires = [
'Flask-CLI>=0.2.1',
'six>=1.10',
'invenio-rest>=1.0.0a3',
'invenio-rest>=1.0.0a4',
'invenio-records>=1.0.0a7',
'invenio-pidstore>=1.0.0a2',
]
Expand Down
Loading

0 comments on commit 8c70ec8

Please sign in to comment.