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.

natural_key()[source]

Natural key, name

classmethod objects_by_label()[source]

A dict of object instances keyed on English display label, used for search form and search results, which should be based on Solr facet and query responses (indexed in English).

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.

has_changed(field)[source]

check if a field has been changed

initial_value(field)[source]

return the initial value for a field

save(*args, **kwargs)[source]

Saves data and reset copy of initial data.

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.

format_val(val)[source]

Format values that can occur in a natural sort key tuple.

pre_save(model_instance, add)[source]

Set the value of the field from for_field value before it is saved. Generates a string based on the output of natsort.natsort_key().

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.

compress(data_list)[source]

Compress into a single value; returns a two-tuple of range end, start.

set_min_max(min_val, max_val)[source]

Set a min and max value for RangeWidget attributes and placeholders.

Parameters:
  • min_value (int) – minimum value to set on widget

  • max_value (int) – maximum value to set on widget

Return type:

None

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.

set_range_minmax(range_minmax)[source]

Set the min, max, and placeholder values for all RangeField instances.

Parameters:

range_minmax (dict) – a dictionary with form fields as key names and tuples of min and max integers as values.

Return type:

None

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

class geniza.common.middleware.LocaleMiddleware(get_response=None)[source]

“Customize django’s default locale middleware to exempt some urls from redirects

process_response(request, response)[source]

exempt untranslated paths from locale redirects

redirect_exempt_paths = ['admin', 'annotations', 'accounts']

base paths for urls to exempt from locale redirects

class geniza.common.middleware.PublicLocaleMiddleware(get_response)[source]

Middleware to redirect anonymous users attempting to access locales that are not in the list of public site languages.

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:

str

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.

Parameters:
  • fn (str, optional) – Filename to save CSV to (if pseudo_buffer is False), defaults to None

  • pseudo_buffer (bool, optional) – Save to string buffer instead of file?, defaults to False

Yield:

String of current line in CSV

Return type:

Generator[str]

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.

Parameters:

data (dict) – Dictionary of keys and values

Returns:

Dictionary with keys and values safely serialized as strings

Return type:

dict

serialize_value(value)[source]

A quick serialize method to transform a value into a CSV-friendly string.

Parameters:

value (object) – Any value

Returns:

Stringified value

Return type:

str

write_export_data_csv(fn=None)[source]

Save CSV of exportable data to file.

Parameters:

fn (str, optional) – Filename to save CSV to, defaults to None

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

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

model

alias of LogEntry