Django Integration

Contents

Views

Generic, re-usable views for use with Fedora-based Django projects. Intended to be analogous to Django’s generic views .

Using these views (in the simpler cases) should be as easy as:

from django.conf.urls import *
from eulfedora.views import raw_datastream, raw_audit_trail

urlpatterns = patterns('',
    url(r'^(?P<pid>[^/]+)/(?P<dsid>(MODS|RELS-EXT|DC))/$', raw_datastream),
    url(r'^(?P<pid>[^/]+)/AUDIT/$', raw_audit_trail),
)
views.raw_datastream(request, *args, **kwargs)

Access raw datastream content from a Fedora object. Returns HttpResponse for HEAD requests, StreamingHttpResponse for GET requests. The headers and status code from Fedora response are set on the django response; any headers specified in the parameters will override Fedora headers. If an HTTP_RANGE header is present on the request, it is passed through to Fedora.

This view method is wrapped with ETag and last modified conditionals.

Parameters:
  • request – HttpRequest
  • pid – Fedora object PID
  • dsid – datastream ID
  • repoRepository instance to use, in case your application requires custom repository initialization (optional)
  • headers – dictionary of additional headers to include in the response
  • as_of_date – access a historical version of the datastream
class eulfedora.views.HttpResponseRangeNotSatisfiable(content='', *args, **kwargs)

Custom version of HttpResponseBadRequest to return a 416 response when a requested range cannot be satisfied.

class eulfedora.views.RawDatastreamView(**kwargs)

Class-based view for serving out datastream content from Fedora. (View-based wrapper for raw_datastream().)

as_of_date_url_kwarg = u'date'

url kwarg term for retrieving date time, if used (default: date)

datastream_id = u''

datastream id

classmethod etag(request, *args, **kwargs)

Class method to generate an ETag for use with conditional processing; calls datastream_etag() with class configuration.

get_headers()

Return headers to be included when generating the datastream content response. Default implementation is to return headers.

get_repository()

Initialize and return the configured repository class, passing in the current request.

headers = {}

extra http headers to include

classmethod last_modified(request, *args, **kwargs)

Class method to generate last-modified header for use with conditional processing; calls datastream_lastmodified() with class configuration.

pid_url_kwarg = u'pid'

url kwarg term for retrieving object pid (default: pid)

repository_class

Repository class to use, if needed

alias of Repository

class eulfedora.views.RawDatastreamViewOld(**kwargs)

Class-based view for serving out datastream content from Fedora. (View-based wrapper for raw_datastream_old().)

accept_range_request = False

Enable range requests (default: False)

as_of_date_url_kwarg = u'date'

url kwarg term for retrieving date time, if used (default: date)

datastream_id = u''

datastream id

classmethod etag(request, *args, **kwargs)

Class method to generate an ETag for use with conditional processing; calls datastream_etag() with class configuration.

get_headers()

Return headers to be included when generating the datastream content response. Default implementation is to return headers.

get_repository()

Initialize and return the configured repository class, passing in the current request.

headers = {}

extra http headers to include

classmethod last_modified(request, *args, **kwargs)

Class method to generate last-modified header for use with conditional processing; calls datastream_lastmodified() with class configuration.

object_type = None

subclass of DigitalObject, if needed

pid_url_kwarg = u'pid'

url kwarg term for retrieving object pid (default: pid)

repository_class

Repository class to use, if needed

alias of Repository

streaming = False

streaming response option (default: False)

eulfedora.views.datastream_etag(request, pid, dsid, repo=None, as_of_date=None, **kwargs)

Method suitable for use as an etag function with django.views.decorators.http.condition. Takes the same arguments as raw_datastream().

eulfedora.views.datastream_lastmodified(request, pid, dsid, repo=None, as_of_date=None, *args, **kwargs)

Method suitable for use as a a last-modified function with django.views.decorators.http.condition. Takes basically the same arguments as raw_datastream().

eulfedora.views.get_range_content(ds, start, end)

Generator for range-requested datastream content. Iterates over datastream content in chunks, and yields the chunks (or partial chunks) that are part of the requested range.

eulfedora.views.login_and_store_credentials_in_session(request, *args, **kwargs)

Custom login view. Calls the standard Django authentication, but on successful login, stores encrypted user credentials in order to allow accessing the Fedora repository with the credentials of the currently-logged in user (e.g., when the application and Fedora share a common authentication system, such as LDAP).

In order for Repository to pick up user credentials, you must pass the request object in (so it will have access to the session). Example:

from eulcore.django.fedora.server import Repository

def my_view(rqst):
    repo = Repository(request=rqst)

Any arguments supported by django.contrib.auth.views.login() can be specified and they will be passed along for the standard login functionality.

This is not a terribly secure. Do NOT use this method unless you need the functionality.

eulfedora.views.raw_audit_trail(request, *args, **kwargs)

View to display the raw xml audit trail for a Fedora Object. Returns an HttpResponse with the response content populated with the content of the audit trial.

If the object is not found or does not have an audit trail, raises an Http404 . For any other errors (e.g., permission denied by Fedora), the exception is not caught and should be handled elsewhere.

Parameters:
  • request – HttpRequest
  • pid – Fedora object PID
  • repoRepository instance to use, in case your application requires custom repository initialization (optional)

Note

Fedora does not make checksums, size, or other attributes available for the audit trail (since it is internal and not a true datastream), so the additional headers included in raw_datastream() cannot be added here.

eulfedora.views.raw_datastream(request, *args, **kwargs)

Access raw datastream content from a Fedora object. Returns HttpResponse for HEAD requests, StreamingHttpResponse for GET requests. The headers and status code from Fedora response are set on the django response; any headers specified in the parameters will override Fedora headers. If an HTTP_RANGE header is present on the request, it is passed through to Fedora.

This view method is wrapped with ETag and last modified conditionals.

Parameters:
  • request – HttpRequest
  • pid – Fedora object PID
  • dsid – datastream ID
  • repoRepository instance to use, in case your application requires custom repository initialization (optional)
  • headers – dictionary of additional headers to include in the response
  • as_of_date – access a historical version of the datastream
eulfedora.views.raw_datastream_old(request, *args, **kwargs)

Note

This version of raw_datastream() is deprecated, and you should update to the new raw_datastream(). This version is still available if you are using a version of Fedora prior to 3.7 and need the additional functionality.

View to display a raw datastream that belongs to a Fedora Object. Returns an HttpResponse with the response content populated with the content of the datastream. The following HTTP headers may be included in all the responses:

  • Content-Type: mimetype of the datastream in Fedora
  • ETag: datastream checksum, as long as the checksum type is not ‘DISABLED’

The following HTTP headers may be set if the appropriate content is included in the datastream metadata:

  • Content-MD5: MD5 checksum of the datastream in Fedora, if available
  • Content-Length: size of the datastream in Fedora

If either the datastream or object are not found, raises an Http404 . For any other errors (e.g., permission denied by Fedora), the exception is re-raised and should be handled elsewhere.

Parameters:
  • request – HttpRequest
  • pid – Fedora object PID
  • dsid – datastream ID to be returned
  • type – custom object type (should extend DigitalObject) (optional)
  • repoRepository instance to use, in case your application requires custom repository initialization (optional)
  • headers – dictionary of additional headers to include in the response
  • accept_range_request – enable HTTP Range requests (disabled by default)
  • as_of_date – access a historical version of the datastream
  • streaming – if True, response will be returned as an instance of django.http.StreamingHttpResponse instead of django.http.HttpResponse; intended for use with large datastreams, defaults to False.

Indexing

Generic, re-usable views for use with Fedora-based Django projects. These views expose data intended for use with eulindexer. These views currently return data in JSON form.

Projects that use this module should include the following settings in their settings.py:

# Index server url. In this example case, we are wish to push data to a Solr instance.
SOLR_SERVER_URL = "http://localhost:8983/solr/"
# IPs that will be allowed to access the indexdata views
EUL_INDEXER_ALLOWED_IPS = "ANY" #Or put in a list such as ("127.0.0.1", "127.0.0.2")

# OPTIONAL SETTING: A list of lists of content models you want this application to index.
# If this setting is missing, the code will automatically detect all content
# models the application is using. In this example, it will index items with BOTH
# content-model_1 and content-model_2 as well as those that have just content-model_3.
EUL_INDEXER_CONTENT_MODELS = "[['content-model_1', 'content-model_2'], ['content-model_3']]"

To use these views in your eulfedora -based application, make sure that eulfedora is included in INSTALLED_APPS in your settings.py:

INSTALLED_APPS = (
    'eulfedora'
    # Additional installed applications here,
)

And then bind the indexdata views to a url in your application urls.py:

from django.conf.urls import *

urlpatterns = patterns('',
    url(r'^indexdata/', include('eulfedora.indexdata.urls', namespace='indexdata')),
    # Additional url patterns here,
)

An example Solr schema with fields defined for all the index values exposed in the default index_data() method is included with eulfedora.indexdata to be used as a starting point for applications.


eulfedora.indexdata.views.index_config(request)

This view returns the index configuration of the current application as JSON. Currently, this consists of a Solr index url and the Fedora content models that this application expects to index.

Note

By default, Fedora system content models (such as fedora-system:ContentModel-3.0) are excluded. Any application that actually wants to index such objects will need to customize this view to include them.

eulfedora.indexdata.views.index_data(request, id, repo=None)

Return the fields and values to be indexed for a single object as JSON. Index content is generated via eulfedora.models.DigitalObject.index_data().

Parameters:id – id of the object to be indexed; in this case a Fedora pid

Management commands

The following management commands will be available when you include eulfedora in your django INSTALLED_APPS and rely on the existdb settings described above.

For more details on these commands, use manage.py <command> help

  • syncrepo - load simple content models and fixture object to the configured fedora repository

Template tags

eulfedora adds custom template tags for use in templates.

fedora_access

Catch fedora failures and permission errors encountered during template rendering:

{% load fedora %}

{% fedora_access %}
   <p>Try to access data on fedora objects which could be
     <span class='{{ obj.inaccessible_ds.content.field }}'>inaccessible</span>
     or when fedora is
     <span class='{{ obj.regular_ds.content.other_field }}'>down</span>.</p>
{% permission_denied %}
   <p>Fall back to this content if the main body results in a permission
     error while trying to access fedora data.</p>
{% fedora_failed %}
   <p>Fall back to this content if the main body runs into another error
     while trying to access fedora data.</p>
{% end_fedora_access %}

The permission_denied and fedora_failed sections are optional. If only permission_denied is present then non-permission errors will result in the entire block rendering empty. If only fedora_failed is present then that section will be used for all errors whether permission-related or not. If neither is present then all errors will result in the entire block rendering empty.

Note that when Django’s TEMPLATE_DEBUG setting is on, it precludes all error handling and displays the Django exception screen for all errors, including fedora errors, even if you use this template tag. To disable this Django internal functionality and see the effects of the fedora_access tag, add the following to your Django settings:

TEMPLATE_DEBUG = False