models.py

Includes ModelField definitions.

Class definitions:

class core.models.ManyToManyField(*args, **kwargs)[source]

Bases: django.db.models.fields.related.ManyToManyField

Django modelfield for storing many to many relations updates the form_class

formfield(**kwargs)[source]

Update the formfield

class core.models.ChoiceOtherField(other_field=<class 'django.forms.widgets.TextInput'>, *args, **kwargs)[source]

Bases: django.db.models.fields.CharField

Django modelfield for allowing to choose from a selectbox or specify an other value

formfield(**kwargs)[source]

Update the formfield

class core.models.CheckBoxIntegerField(*args, **kwargs)[source]

Bases: django.db.models.fields.IntegerField

Integerfield with checkbox as widget

formfield(**kwargs)[source]

Update the formfield

class core.models.CheckBoxCharField(*args, **kwargs)[source]

Bases: django.db.models.fields.CharField

Charfield with checkbox as widget

formfield(**kwargs)[source]

Update the formfield

class core.models.DateField(*args, **kwargs)[source]

Bases: django.db.models.fields.DateField

Django modelfield for storing dates

to_python(value)[source]

Remove the time part of the datetime in case unicode or string is given as value

formfield(**kwargs)[source]

Update the formfield

class core.models.ImageField(verbose_name=None, name=None, width_field=None, height_field=None, **kwargs)[source]

Bases: django.db.models.fields.files.ImageField

Imagefield with maximum size validation

formfield(**kwargs)[source]

Update the formfield

class core.models.YesNoChoiceField(*args, **kwargs)[source]

Bases: django.db.models.fields.NullBooleanField

Django modelfield for storing yes/no choices

formfield(**kwargs)[source]

Update the formfield

exception core.models.AuditUserNotDefinedError[source]

Bases: exceptions.Exception

Error class for showing errors of not supported lookups

class core.models.ModelAuditMixin(*args, **kwargs)[source]

Bases: object

A model mixin that tracks model fields’ values and provide some useful functions to determine what fields have been changed.

class core.models.AuditBaseModel(*args, **kwargs)[source]

Bases: django.db.models.base.Model, core.models.ModelAuditMixin

Basemodel which automatically generates audit trails for models.

save(**kwargs)[source]

Override the save method to include the audit functions

exception core.models.NotSupportedLookup(lookup)[source]

Bases: exceptions.Exception

Error class for showing errors of not supported lookups

class core.models.SubfieldBase[source]

Bases: type

A metaclass for custom Field subclasses. This ensures the model’s attribute has the descriptor protocol attached to it.

class core.models.Creator(field)[source]

Bases: object

A placeholder class that provides a way to set the attribute on the model.

core.models.make_contrib(superclass, func=None)[source]

Returns a suitable contribute_to_class() method for the Field subclass.

If ‘func’ is passed in, it is the existing contribute_to_class() method on the subclass and it is called before anything else. It is assumed in this case that the existing contribute_to_class() calls all the necessary superclass methods.

class core.models.HMACField(*args, **kwargs)[source]

Bases: django.db.models.fields.CharField

HMAC field which stores an HMAC version of the “associated_field” Automatically creates an HMAC hash when saving to the database and when looking up values in the database.

create_hmac(value)[source]

Shortcut function for generating a HMAC

Args:
  • value: the value to generate a HMAC from
Returns:
The HMAC value
pre_save(model_instance, add)[source]

Called before saving to the database, automatically creates an HMAC of the value of the “associated_field”

Args:
  • model_instance: the model_instance to use
  • add: newly added True/False
Returns:
The value to store in the database
get_db_prep_lookup_old(lookup_type, value, connection, prepared=False)[source]

Transform the lookup value in an HMAC. Only allow exact lookups.

Args:
  • lookup_type: the name of the lookup
  • value: the lookup argument
  • connection: the database connection
  • prepared: is the value prepared to be used?
Returns:
The value to lookup in HMAC format.
Raises:
NotSupportedLookup if the lookup_type != exact.
class core.models.EncryptBaseField(*args, **kwargs)[source]

Bases: object

Base model field for encrypting the value before sending it to the database and decrypting it before storing it on a model instance.

Provide a ‘encryption_key’ argument in the kwargs which is either a name of a property on the model instance or a function which returns the encryption_key.

is_encrypted(value)[source]

Check if the value is encrypted by comparing the first characters of the value to the list of known ciphers.

Args:
  • value: the value to check (AES256CBC$#encrypted_value#)
Returns:
True if the cipher name could be found in the value else False
encryption_key(model_instance)[source]
Args:
  • obj: the model instance to get the ‘self.encryption_key_func’
    attribute of.
Returns:
The encryption/decryption key to use, either by using a property on a model instance or a function call.
get_db_prep_value(value, connection, prepared)[source]

Override this function so to_python does not get called before saving, which would lead to decrypting the value.

to_python(value, model_instance=None)[source]

Decrypts the value if it is encrypted

Args:
  • value: The value to convert
  • model_instance: the model_instance the function is run for, or None
Returns:
The decrypted value if encrypted, else the value
pre_save(model_instance, add)[source]

Encrypt the value if it is encrypted

Args:
  • model_instance: the model_instance that is saved
  • add: newly added True/False
Returns:
The encrypted value to store.

Note

getattr(model_instance, self.attname) calls get_db_prep_value, which normally would call to_python. But to store an encrypted value, this function is overriden.

class core.models.EncryptLookupBaseField(*args, **kwargs)[source]

Bases: core.models.EncryptBaseField

Base modelfield combining both encryption and HMAC lookup. Automatically generates an hmac_#fieldname# modelfield on the model.

Init the field with an “hmac_key” function (for example: hmac_key=lambda:settings.HMAC_KEY) and the ‘encryption_key’ as used in the EncryptBaseField.

get_db_prep_lookup(lookup_type, value, connection, prepared=False)[source]

Don’t search on encrypted fields!

Raises:
NotSupportedLookup
class core.models.EncryptedHMACLookupCharField(*args, **kwargs)[source]

Bases: core.models.EncryptLookupBaseField, django.db.models.fields.CharField

Encrypted charfield with HMAC lookup

class core.models.EncryptedHMACLookupTextField(*args, **kwargs)[source]

Bases: core.models.EncryptLookupBaseField, django.db.models.fields.TextField

Encrypted textfield with HMAC lookup

class core.models.EncryptedHMACLookupEmailField(*args, **kwargs)[source]

Bases: core.models.EncryptLookupBaseField, django.db.models.fields.EmailField

Encrypted emailfield with HMAC lookup

class core.models.EncryptedCharField(*args, **kwargs)[source]

Bases: core.models.EncryptBaseField, django.db.models.fields.CharField

Encrypted charfield

class core.models.EncryptedTextField(*args, **kwargs)[source]

Bases: core.models.EncryptBaseField, django.db.models.fields.TextField

Encrypted textfield

class core.models.EncryptedEmailField(*args, **kwargs)[source]

Bases: core.models.EncryptBaseField, django.db.models.fields.EmailField

Encrypted emailfield