models.py¶
Includes ModelField definitions.
Class definitions:
-
class
core.models.ManyToManyField(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.related.ManyToManyFieldDjango modelfield for storing many to many relations updates the form_class
-
class
core.models.ChoiceOtherField(other_field=<class 'django.forms.widgets.TextInput'>, *args, **kwargs)[source]¶ Bases:
django.db.models.fields.CharFieldDjango modelfield for allowing to choose from a selectbox or specify an other value
-
class
core.models.CheckBoxIntegerField(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.IntegerFieldIntegerfield with checkbox as widget
-
class
core.models.CheckBoxCharField(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.CharFieldCharfield with checkbox as widget
-
class
core.models.DateField(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.DateFieldDjango modelfield for storing dates
-
class
core.models.ImageField(verbose_name=None, name=None, width_field=None, height_field=None, **kwargs)[source]¶ Bases:
django.db.models.fields.files.ImageFieldImagefield with maximum size validation
-
class
core.models.YesNoChoiceField(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.NullBooleanFieldDjango modelfield for storing yes/no choices
-
exception
core.models.AuditUserNotDefinedError[source]¶ Bases:
exceptions.ExceptionError class for showing errors of not supported lookups
-
class
core.models.ModelAuditMixin(*args, **kwargs)[source]¶ Bases:
objectA 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.ModelAuditMixinBasemodel which automatically generates audit trails for models.
-
exception
core.models.NotSupportedLookup(lookup)[source]¶ Bases:
exceptions.ExceptionError class for showing errors of not supported lookups
-
class
core.models.SubfieldBase[source]¶ Bases:
typeA 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:
objectA 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.CharFieldHMAC 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:
objectBase 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.EncryptBaseFieldBase 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.
-
class
core.models.EncryptedHMACLookupCharField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptLookupBaseField,django.db.models.fields.CharFieldEncrypted charfield with HMAC lookup
-
class
core.models.EncryptedHMACLookupTextField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptLookupBaseField,django.db.models.fields.TextFieldEncrypted textfield with HMAC lookup
-
class
core.models.EncryptedHMACLookupEmailField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptLookupBaseField,django.db.models.fields.EmailFieldEncrypted emailfield with HMAC lookup
-
class
core.models.EncryptedCharField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptBaseField,django.db.models.fields.CharFieldEncrypted charfield
-
class
core.models.EncryptedTextField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptBaseField,django.db.models.fields.TextFieldEncrypted textfield
-
class
core.models.EncryptedEmailField(*args, **kwargs)[source]¶ Bases:
core.models.EncryptBaseField,django.db.models.fields.EmailFieldEncrypted emailfield