Flask-FedoraCommons

Flask-FedoraCommons is an extension to Flask that provides an interface to the open-source Fedora Commons digital repository. Certain modules from the Eulfedora project were forked and included as libraries in this extension.

Installation

Install the extension with one of the following commands:

$ easy_install Flask-FedoraCommons

or alternatively if you have pip installed:

$ pip install Flask-FedoraCommons

Configuration

The fedora object itself can be used to configure the following required settings (and example values) for this extension:

FEDORA_ROOT http://fedora.host.name:8080/fedora/
FEDORA_USER ‘user’
FEDORA_PASSWORD ‘password’
FEDORA_PIDSPACE ‘changeme’
FEDORA_TEST_ROOT http://fedora.host.name:8180/fedora/
FEDORA_TEST_PIDSPACE ‘testme’

Using in a Flask Application

To get started with Flask-FedoraCommons, you need to instantiate a FedoraCommons object after configuring the application:

from flask import Flask
from flask_fedora_commons import FedoraCommons

app = Flask(__name__)
app.config.from_pyfile('mysettings.cfg')
fedora = FedoraCommons(app)

API Access to Fedora

You can access both M-API and A-API REST web services available in the digital repository through the FedoraCommons API interface.

Classes and Methods

class flask_fedora_commons.FedoraCommons(app=None)[source]

Class provides an interface to a Fedora Commons digital repository.

connect()[source]

Returns a Repository object initializes with default configuration values

create_stubs(mods_xml, title, parent_pid, num_objects, content_model)

Method creates 1-n number of basic Fedora Objects in a repository

Parameters:
  • mods_xml – MODS XML used for all stub MODS datastreams
  • title – Title of Fedora Object
  • parent_pid – PID of Parent collection
  • num_objects – Number of stub records to create in the parent collection
  • content_model – Content model for the stub records, defaults to adr:adrBasicObject
Rtype list:

List of PIDS

init_app(app)[source]

Initializes a Flask app object for the extension.

Parameters:app – Flask app
move(source_pid, collection_pid)

Method takes a source_pid and collection_pid, retrives source_pid RELS-EXT and updates fedora:isMemberOfCollection value with new collection_pid

Parameters:
  • source_pid – Source Fedora Object PID
  • collection_pid – Collection Fedora Object PID
Rtype boolean:

True if move was a success

repository None

Property method function returns or creates a class property for a Repository instance.

teardown(exception)[source]

Teardown and closes the connection to a Repository instance

Parameters:exception – Exception to catch during teardown
class flask_fedora_commons.Repository(root=None, username=None, password=None, request=None)

Pythonic interface to a single Fedora Commons repository instance. Based on Apache2 licensed source code from Emory University’s Eulfedora project

best_subtype_for_object(obj)

Given a DigitalObject, examine the object to select the most appropriate subclass to instantiate. This generic implementation examines the object’s content models and compares them against the defined subclasses of DigitalObject to pick the best match. Projects that have a more nuanced understanding of their particular objects should override this method in a Repository subclass. This method is intended primarily for use by infer_object_subtype().

Parameters:obj – a DigitalObject to inspect
Return type:a subclass of DigitalObject
default_object_type = None

Default type to use for methods that return fedora objects - DigitalObject

find_objects(terms=None, type=None, chunksize=None, **kwargs)

Find objects in Fedora. Find query should be generated via keyword args, based on the fields in Fedora documentation. By default, the query uses a contains (~) search for all search terms. Calls ApiFacade.findObjects(). Results seem to return consistently in ascending PID order.

Example usage - search for all objects where the owner contains ‘jdoe’:

repository.find_objects(ownerId='jdoe')

Supports all search operators provided by Fedora findObjects query (exact, gt, gte, lt, lte, and contains). To specify the type of query for a particular search term, call find_objects like this:

repository.find_objects(ownerId__exact='lskywalker')
repository.find_objects(date__gt='20010302')
Parameters:
  • type – type of objects to return; defaults to DigitalObject
  • chunksize – number of objects to return at a time
Return type:

generator for list of objects

get_next_pid(namespace=None, count=None)

Request next available pid or pids from Fedora, optionally in a specified namespace. Calls ApiFacade.getNextPID().

Deprecated since version 0.14: Mint pids for new objects with eulfedora.models.DigitalObject.get_default_pid() instead, or call ApiFacade.getNextPID() directly.

Parameters:
  • namespace – (optional) get the next pid in the specified pid namespace; otherwise, Fedora will return the next pid in the configured default namespace.
  • count – (optional) get the specified number of pids; by default, returns 1 pid
Return type:

string or list of strings

get_object(pid=None, type=None, create=None)

Initialize a single object from Fedora, or create a new one, with the same Fedora configuration and credentials.

Parameters:
  • pid – pid of the object to request, or a function that can be called to get one. if not specified, get_next_pid() will be called if a pid is needed
  • type – type of object to return; defaults to DigitalObject
Return type:

single object of the type specified

Create:

boolean: create a new object? (if not specified, defaults to False when pid is specified, and True when it is not)

get_objects_with_cmodel(cmodel_uri, type=None)

Find objects in Fedora with the specified content model.

Parameters:
  • cmodel_uri – content model URI (should be full URI in info:fedora/pid:### format)
  • type – type of object to return (e.g., class:DigitalObject)
Return type:

list of objects

infer_object_subtype(api, pid=None, create=False, default_pidspace=None)

Construct a DigitalObject or appropriate subclass, inferring the appropriate subtype using best_subtype_for_object(). Note that this method signature has been selected to match the DigitalObject constructor so that this method might be passed directly to get_object() as a type:

>>> obj = repo.get_object(pid, type=repo.infer_object_subtype)

See also: TypeInferringRepository

ingest(text, log_message=None)

Ingest a new object into Fedora. Returns the pid of the new object on success. Calls ApiFacade.ingest().

Parameters:
  • text – full text content of the object to be ingested
  • log_message – optional log message
Return type:

string

purge_object(pid, log_message=None)

Purge an object from Fedora. Calls ApiFacade.purgeObject().

Parameters:
  • pid – pid of the object to be purged
  • log_message – optional log message
Return type:

boolean

risearch None

instance of eulfedora.api.ResourceIndex, with the same root url and credentials

search_fields = ['pid', 'label', 'state', 'ownerId', 'cDate', 'mDate', 'dcmDate', 'title', 'creator', 'subject', 'description', 'publisher', 'contributor', 'date', 'type', 'format', 'identifier', 'source', 'language', 'relation', 'coverage', 'rights']

fields that can be searched against in find_objects()

search_fields_aliases = {'owner': 'ownerId', 'dc_modified': 'dcmDate', 'modified': 'mDate', 'created': 'cDate'}

human-readable aliases for oddly-named fedora search fields

Indices and tables