fairdm.contrib.import_export.views#

Views for importing and exporting dataset records, publishing, and package downloads.

Module Contents#

Classes#

BaseImportExportView

Base view for importing and exporting a dataset’s records with django-import-export.

DataImportView

Import records from an uploaded spreadsheet into the current dataset.

DatasetPublishConfirm

Ask the user to confirm publishing a dataset.

DataExportView

Download the current dataset’s records, or an empty template, in the chosen format.

DatasetPackageDownloadView

Download a dataset and its metadata as a ZIP package.

MetadataDownloadView

Download a dataset’s DataCite metadata as XML.

UploadForm

Form with a single field for an uploaded file.

API#

class BaseImportExportView(**kwargs)#

Bases: django.views.generic.FormView

Base view for importing and exporting a dataset’s records with django-import-export.

Attributes: from_encoding: Encoding used to read uploaded text files. model: The model whose records are imported and exported. form_class: The form class, set by subclasses. success_url: Where to send the user after success, set by subclasses. import_kwargs: Default keyword arguments for the import.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

from_encoding#

‘utf-8-sig’

model#

None

form_class#

None

success_url#

None

import_kwargs#

None

get_context_data(**kwargs)#

Add the formats, the data type, the import result and which error details to show.

property export_formats#

Map each export format’s title to its format class.

property import_formats#

Map each import format’s title to its format class.

dispatch(request, *args, **kwargs)#

Resolve the model being imported or exported before handling the request.

get_resource()#

Return the resource for the resolved model, bound to the current dataset.

Returns: An instance of the model’s registered resource class.

get_resource_model()#

Resolve the model class named by the type query parameter.

Without a type the first registered sample model is used.

Returns: The model class for the requested type.

Raises: ValueError: No models are registered, or type matches none.

get_resource_qs()#

Return the current dataset’s records of the resolved model.

Returns: A queryset filtered to the current dataset.

get_dataset_format(file)#

Choose the import format from the uploaded file’s extension.

Args: file: The uploaded file.

Returns: An instance of the matching import format.

Raises: ValueError: The extension matches no import format.

result_has_errors(result, form)#

Re-render the form with the import errors listed.

Args: result: The failed import result. form: The submitted form.

Returns: The rendered response.

form_invalid(form)#

Re-render the invalid form.

get_import_kwargs()#

Return the keyword arguments passed to the resource’s import, for subclasses to customise.

Returns: A copy of import_kwargs.

class DataImportView(**kwargs)#

Bases: fairdm.contrib.import_export.views.BaseImportExportView

Import records from an uploaded spreadsheet into the current dataset.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

name#

‘import’

title#

‘_(…)’

heading_config#

None

sections#

None

form_class#

None

template_name#

‘import_export/import.html’

import_kwargs#

None

static check(request, instance, **kwargs)#

Allow users holding the dataset’s import permission.

form_valid(form)#

Import the uploaded file and report success or re-render with the errors.

handle_import(file)#

Import the rows of an uploaded file into the current dataset.

Args: file: The uploaded file.

Returns: The import result, or None when the file’s format cannot be resolved.

get_success_url()#

Send the user back to the dataset.

class DatasetPublishConfirm(**kwargs)#

Bases: fairdm.views.FairDMModelFormMixin, django.views.generic.FormView

Ask the user to confirm publishing a dataset.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

name#

‘get-published’

title#

‘_(…)’

heading_config#

None

sections#

None

form_class#

None

form_config#

None

static check(request, instance, **kwargs)#

Allow users holding the dataset’s publish permission.

get_context_data(**kwargs)#

Add an empty confirmation form.

class DataExportView(**kwargs)#

Bases: django_downloadview.VirtualDownloadView, fairdm.contrib.import_export.views.BaseImportExportView

Download the current dataset’s records, or an empty template, in the chosen format.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

form_class#

None

get_file()#

Export the records, or only the column headings when template is posted.

post(request, *args, **kwargs)#

Stream the export file in the submitted format.

Args: request: The POST request carrying the format. *args: Unused. **kwargs: Unused.

Returns: The file response.

Raises: ValueError: The submitted format is not valid.

get_basename()#

Name the file after the model and the chosen format.

get_format()#

Return an instance of the chosen export format.

Returns: The export format for the submitted format title.

class DatasetPackageDownloadView(**kwargs)#

Bases: django.views.generic.detail.SingleObjectMixin, django_downloadview.VirtualDownloadView

Download a dataset and its metadata as a ZIP package.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

model#

None

get_file()#

Build the ZIP package for the dataset.

get_basename()#

Name the file after the dataset.

get_content_type(file)#

Return the ZIP MIME type.

class MetadataDownloadView(**kwargs)#

Bases: django_downloadview.VirtualDownloadView

Download a dataset’s DataCite metadata as XML.

Initialization

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

template_name#

‘publishing/datacite44.xml’

get_file()#

Render the dataset’s metadata XML.

get_basename()#

Name the file after the dataset.

class UploadForm(data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList, label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None, bound_field_class=None)#

Bases: django.forms.Form

Form with a single field for an uploaded file.

Initialization

docfile#

‘FileField(…)’