Common functionality¶
models¶
- class geniza.common.models.DisplayLabelMixin[source]¶
Mixin for models with translatable display labels that may differ from names, in order to override fallback behavior when a label for the current language is not defined. Used for search response handling and display on the public frontend.
Example: DocumentType with name ‘Legal’ has a display label in English, ‘Legal document’. In Hebrew, it only has a name ‘מסמך משפטי’ and no display label. In English, we want to show DocumentType.display_label_en. In Hebrew, we want to show DocumentType.name_he because display_label_he is not defined. We also need to ensure that the document type מסמך משפטי can be looked up by display_label_en, as that is what gets indexed in solr.
- class geniza.common.models.TrackChangesModel(*args, **kwargs)[source]¶
Model
mixin that keeps a copy of initial data in order to check if fields have been changed. Change detection only works on the current instance of an object.
- class geniza.common.models.UserProfile(id, user, github_coauthor, creator)[source]¶
- exception DoesNotExist¶
- exception MultipleObjectsReturned¶
- geniza.common.models.cached_class_property(f)[source]¶
Reusable decorator to cache a class property, as opposed to an instance property. from https://stackoverflow.com/a/71887897
fields¶
- class geniza.common.fields.NaturalSortField(for_field, **kwargs)[source]¶
Natural sort field for Django models. Takes a field name, and uses
natsort
to generate a natural sort value based on the value of that field.- deconstruct()[source]¶
Return enough information to recreate the field as a 4-tuple:
The name of the field on the model, if contribute_to_class() has been run.
The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.
A list of positional arguments.
A dict of keyword arguments.
Note that the positional or keyword arguments must contain values of the following types (including inner values of collection types):
None, bool, str, int, float, complex, set, frozenset, list, tuple, dict
UUID
datetime.datetime (naive), datetime.date
top-level classes, top-level functions - will be referenced by their full import path
Storage instances - these have their own deconstruct() method
This is because the values here must be serialized into a text format (possibly new Python code, possibly JSON) and these are the only types with encoding handlers defined.
There’s no need to return the exact way the field was instantiated this time, just ensure that the resulting field is the same - prefer keyword arguments over positional ones, and omit parameters with their default values.
- class geniza.common.fields.RangeField(*args, **kwargs)[source]¶
Date range field, for two numeric inputs. Compresses to a tuple of two values for the start and end of the range; tuple values set to None for no input.
- set_min_max(min_val, max_val)[source]¶
Set a min and max value for
RangeWidget
attributes and placeholders.
- widget¶
alias of
RangeWidget
- class geniza.common.fields.RangeForm(data=None, *args, **kwargs)[source]¶
Form mixin to initialize min/max values for range fields.
- property media¶
Return all media required to render the widgets on this form.
- class geniza.common.fields.RangeWidget(*args, **kwargs)[source]¶
date range widget, for two numeric inputs
- decompress(value)[source]¶
Return a list of decompressed values for the given compressed value. The given value can be assumed to be valid, but not necessarily non-empty.
- property media¶
Media for a multiwidget is the combination of all media of the subwidgets.
middleware¶
metadata export¶
- class geniza.common.metadata_export.Exporter(queryset=None, progress=False)[source]¶
Base class for data exports. See DocumentExporter geniza/corpus/metadata_export.py for an example of a subclass implementation of Exporter.
For initializing:
- Parameters:
queryset (QuerySet, optional) – Limit this export to a given queryset?, defaults to None
progress (bool, optional) – Use a progress bar?, defaults to False
- csv_filename()[source]¶
Generate the appropriate CSV filename for model and time
- Returns:
Filename string
- Return type:
- get_export_data_dict(obj)[source]¶
A given Exporter class (DocumentExporter, FootnoteExporter, etc) must implement this function. It ought to return a dictionary of exported information for a given object.
- Parameters:
obj (object) – Model object (document, footnote, etc)
- Raises:
NotImplementedError – This method must be implemented by subclasses
- get_queryset()[source]¶
Get the queryset in use. If not set at init, this will be all objects from the given model.
- Returns:
QuerySet of documents to export
- Return type:
QuerySet
- http_export_data_csv(fn=None)[source]¶
Download CSV of exportable data to file.
- Parameters:
fn (str, optional) – Filename to download CSV as, defaults to None
- Returns:
Django implementation of StreamingHttpResponse which can be downloaded via web client or programmatically.
- Return type:
StreamingHttpResponse
- iter_csv(fn=None, pseudo_buffer=False, **kwargs)[source]¶
Iterate over the string lines of a CSV file as it’s being written, either to file or a string buffer.
- iter_dicts(desc='Iterating rows')[source]¶
Iterate over the exportable data, one dictionary per row
- Yield:
Dictionary of information for each object
- Return type:
Generator[dict]
- serialize_dict(data)[source]¶
Return a new dictionary whose keys and values are safe, serialized string versions of the keys and values in input dictionary data.
- class geniza.common.metadata_export.LogEntryExporter(queryset=None, progress=False)[source]¶
- action_label = {1: 'addition', 2: 'change', 3: 'deletion'}¶
map log entry action flags to text labels
- get_export_data_dict(log)[source]¶
A given Exporter class (DocumentExporter, FootnoteExporter, etc) must implement this function. It ought to return a dictionary of exported information for a given object.
- Parameters:
obj (object) – Model object (document, footnote, etc)
- Raises:
NotImplementedError – This method must be implemented by subclasses