Documents¶
invenio.modules.documents.api¶
Documents API
Following example shows how to handle documents metadata:
>>> from flask import g
>>> from invenio.base.factory import create_app
>>> app = create_app()
>>> ctx = app.test_request_context()
>>> ctx.push()
>>> from invenio.modules.documents import api
>>> from invenio.modules.jsonalchemy.jsonext.engines import memory
>>> app.config['DOCUMENTS_ENGINE'] = "invenio.modules.jsonalchemy.jsonext.engines.memory:MemoryStorage"
>>> d = api.Document.create({'title': 'Title 1'})
>>> d['title']
'Title 1'
>>> d['creator']
0
>>> d['title'] = 'New Title 1'
>>> d = d.update()
>>> api.Document.get_document(d['_id'])['title']
'New Title 1'
>>> ctx.pop()
-
class
invenio.modules.documents.api.
Document
(json=None, set_default_values=False, process_model_info=False, **kwargs)¶ -
delete
(force=False)¶ Deletes the instance of document.
Parameters: force – If it is True then the document is deleted including attached files and metadata.
-
classmethod
get_document
(uuid, include_deleted=False)¶ Returns document instance identified by UUID.
Find existing document:
>>> from flask import g >>> from invenio.base.factory import create_app >>> app = create_app() >>> ctx = app.test_request_context() >>> ctx.push() >>> from invenio.modules.documents import api >>> from invenio.modules.jsonalchemy.jsonext.engines import memory >>> app.config['DOCUMENTS_ENGINE'] = "invenio.modules.jsonalchemy.jsonext.engines.memory:MemoryStorage" >>> d = api.Document.create({'title': 'Title 1'}) >>> e = api.Document.get_document(d['_id'])
If you try to find deleted document you will get an exception:
>>> e.delete() >>> api.Document.get_document(d['_id']) Traceback (most recent call last): ... DeletedDocument
and also if you try to find not existing document:
>>> import uuid >>> api.Document.get_document(str(uuid.uuid4())) Traceback (most recent call last): ... DocumentNotFound >>> ctx.pop()
Returns: a Document
instance.Raises: DocumentNotFound
orDeletedDocument
-
open
(mode='r', **kwargs)¶ Open a the ‘uri’ as a file-like object.
-
setcontents
(source, name, chunk_size=65536)¶ A convenience method to create a new file from a string or file-like object.
Note: All paths has to be absolute or specified in full URI format.
Parameters: - data –
.
- name – File URI or filename generator taking self as argument.
- data –
-
update
()¶ Update document object.
-
invenio.modules.documents.errors¶
Defines exceptions raised by Document API.
-
exception
invenio.modules.documents.errors.
DeletedDocument
¶ Document has not been found because it has been previously deleted.
-
exception
invenio.modules.documents.errors.
DocumentError
¶ General document exception.
-
exception
invenio.modules.documents.errors.
DocumentNotFound
¶ Document has not been found in database.