Fields for Models and Forms

This module contains custom field types for Django models and forms.

Custom Django field and formfield types (for models and forms.

openquake.engine.db.fields.ARRAY_RE = <_sre.SRE_Pattern object>

regex for splitting string lists on whitespace and/or commas

class openquake.engine.db.fields.CharArrayField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

This field models a postgres varchar array.

db_type(_connection=None)[source]
formfield(**kwargs)[source]

Specify a custom form field type so forms know how to handle fields of this type.

get_prep_value(value)[source]

Return data in a format that has been prepared for use as a parameter in a query.

Parameters:value (list or tuple) – sequence of string values to be saved in a varchar[] field
>>> caf = CharArrayField()
>>> caf.get_prep_value(['foo', 'bar', 'baz123'])
'{"foo", "bar", "baz123"}'
to_python(value)[source]

Split strings on whitespace or commas and return the list.

If the input value is not a string, just return the value (for example, if value is already a list).

class openquake.engine.db.fields.DictField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: openquake.engine.db.fields.PickleField

Field for storing Python dict objects (or a JSON text representation.

contribute_to_class(cls, name, **kwargs)
to_python(value)[source]

The value of a DictField can obviously be a dict. The value can also be specified as a JSON string. If it is, convert it to a dict.

class openquake.engine.db.fields.FloatArrayField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

This field models a postgres float array.

db_type(connection)[source]
formfield(**kwargs)[source]

Specify a custom form field type so forms know how to handle fields of this type.

get_prep_value(value)[source]
class openquake.engine.db.fields.FloatArrayFormField(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=[], localize=False, label_suffix=None)[source]

Bases: openquake.engine.db.fields.StringArrayFormField

Form field for properly handling float arrays/lists.

cast

alias of float

value_type = 'float'
class openquake.engine.db.fields.GzippedField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

Automatically stores gzipped text as a bytearray

contribute_to_class(cls, name, **kwargs)
db_type(_connection)[source]
get_prep_value(value)[source]

Compress the value

to_python(value)[source]

Decompress the value

class openquake.engine.db.fields.IntArrayField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

This field models a postgresql int array

db_type(connection)[source]
get_prep_value(value)[source]
class openquake.engine.db.fields.LiteralField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

Convert from Postgres TEXT to Python objects and viceversa by using ast.literal_eval and repr.

contribute_to_class(cls, name, **kwargs)
db_type(_connection=None)[source]
get_prep_value(value)[source]
to_python(value)[source]
class openquake.engine.db.fields.NullCharField(max_length=None, min_length=None, *args, **kwargs)[source]

Bases: django.forms.fields.CharField

to_python(value)[source]

Returns a Unicode object.

class openquake.engine.db.fields.NullFloatField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.FloatField

A nullable float field that handles blank input values properly.

get_prep_value(value)[source]
class openquake.engine.db.fields.NullTextField(**kwargs)[source]

Bases: django.db.models.fields.TextField

formfield(**kwargs)[source]

Specify a custom form field type so forms know how to handle fields of this type.

class openquake.engine.db.fields.NumpyListField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: openquake.engine.db.fields.PickleField

Field for storing numpy arrays as pickled blobs. The actual blob stored in the database is simply a pickled list. When the field is instantiated, the value is converted back to a numpy array.

contribute_to_class(cls, name, **kwargs)
get_prep_value(value)[source]

Convert the value to the pickled representation of a list. If value is a numpy.ndarray, it will be converted to a list of the same size and shape before being pickled.

Parameters:value – A list, tuple, or numpy.ndarray.
to_python(value)[source]

Try to reconstruct a numpy.ndarray from the given value.

Parameters:value – The pickled representation of an object which can be reconstituted using pickle.loads.
class openquake.engine.db.fields.OqNullBooleanField(*args, **kwargs)[source]

Bases: django.db.models.fields.NullBooleanField

A NullBooleanField that can convert meaningful strings to boolean values (in the case of config file parameters).

to_python(value)[source]

If value is a str, try to extract some boolean value from it.

class openquake.engine.db.fields.PickleField(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class django.db.models.fields.NOT_PROVIDED>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text=u'', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None)[source]

Bases: django.db.models.fields.Field

Field for transparent pickling and unpickling of python objects.

SUPPORTED_BACKENDS = set(['django.db.backends.postgresql_psycopg2', 'django.contrib.gis.db.backends.postgis'])
contribute_to_class(cls, name, **kwargs)
db_type(connection)[source]

Return “bytea” as postgres’ column type.

formfield(**kwargs)[source]

Specify a custom form field type so forms don’t treat this as a default type (such as a string). Any Python object is valid for this field type.

get_prep_value(value)[source]

Pickle the value.

to_python(value)[source]

Unpickle the value.

class openquake.engine.db.fields.PickleFormField(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=[], localize=False, label_suffix=None)[source]

Bases: django.forms.fields.Field

Form field for Python objects which are pickle and saved to the database.

clean(value)[source]

We assume that the Python value specified for this field is exactly what we want to pickle and save to the database.

The value will not modified.

class openquake.engine.db.fields.StringArrayFormField(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=[], localize=False, label_suffix=None)[source]

Bases: django.forms.fields.Field

Base class containing general functionality for handling list-like parameters.

cast

alias of str

clean(value)[source]

Try to coerce either a string list of values (separated by whitespace and/or commas or a list/tuple of values to a list of floats. If unsuccessful, raise a django.forms.ValidationError

value_type = 'str'