Jump to >

djblets.db.fields.base64_field

Field for storing data as Base64-encoded values.

See Base64Field for the main field type and usage instructions.

exception Base64TypeError(value)[source]

Bases: exceptions.TypeError

Error indicating an invalid value type was provided for the field.

__init__(value)[source]

Initialize the error.

Parameters:value (object) – The invalid value that was passed.
class Base64DecodedValue[source]

Bases: str

An identifiable wrapper around byte string values for Base64Field.

This wraps any values coming from Base64Field, helping make a distinction between arbitrary strings and decoded strings from the database. It helps prevent double-encoding or double-decoding of data.

class Base64FieldCreator(field)[source]

Bases: object

Property-like class used to store/retrieve Base64 values.

This works much like a property, and takes care of encoding strings for storage and decoding them on retrieval. It’s set internally by Base64Field in place of the normal field attribute.

__init__(field)[source]

Initialize the creator.

Parameters:field (Base64Field) – The field owning this object.
__set__(obj, value)[source]

Set a new value in the field.

If this is a Base64DecodedValue, or the model is new and hasn’t yet been persisted to the database, the value will be encoded and stored. Otherwise, if it’s a standard string value or the model is not new, it’s assumed that this is encoded data for storage, and will be stored directly.

Parameters:
Raises:

Base64TypeError – The type of value provided could not be set.

__get__(obj, *args, **kwargs)[source]

Return a decoded value from the field.

Parameters:
  • obj (django.db.models.Model) – The model owning the field.
  • *args (tuple) – Unused positional arguments.
  • **kwargs (dict) – Unused keyword arguments.
Returns:

The decoded value from the field. If no value has yet been stored, this will return None instead.

Return type:

Base64DecodedValue

Raises:
  • AttributeError
  • A None value was passed for the object.
class Base64Field(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.TextField

A text field for storing Base64-encoded values.

This is used to store data (such as binary data or encoding-sensitive data) to the database in a Base64 encoding. This is useful if you’re dealing with unknown encodings and must guarantee that no modifications to the text occurs and that you can read/write the data in any database with any encoding.

When accessing this field on an instance of a model, a Base64DecodedValue will be returned consisting of the decoded data. This is a byte string, and can be treated as such. If set back into the field, it will be re-encoded and stored.

When writing to the field, the behavior changes based on the type of value and the state of the model:

  • If the model instance is new (has not yet been saved in the database), any string set will be encoded. This allows the value to be passed during a create() call.
  • If the model is not new, any string that’s set will be assumed to be encoded by the caller.
  • Passing a Base64DecodedValue byte string will always cause the stored data to be encoded.

The field also adds a get_fieldname_base64() method to the class, which returns the raw Base64 encoded content from the database.

contribute_to_class(cls, name)[source]

Set attributes on a new model class.

This is called when constructing a model class making use of this field. It sets the field’s attribute to a Base64FieldCreator and adds a get_fieldname_base64() method to the class.

Parameters:
  • cls (type) – The class to add the arguments to.
  • name (str) – The name of the field.
get_prep_value(value)[source]

Return a value prepared for the field.

Parameters:value (object) – The value to prepare. This is expected to be a string or a Base64DecodedValue. If the latter, it will be encoded.
Returns:The resulting value.
Return type:bytes
Raises:Base64TypeError – The type of value provided could not be prepared for writing.
to_python(value)[source]

Return a Python representation of a value for the field.

This will decode the value (if not already decoded) and return it.

Parameters:value (object) – The value to return a decoded value for.
Returns:The decoded version of the provided value.
Return type:Base64DecodedValue
Raises:Base64TypeError – The type of value provided could not be prepared for writing.
value_to_string(obj)[source]

Return a string representation of the value from a model.

The returned value will be a Base64-encoded string value.

Parameters:obj (django.db.models.Model) – The model instance owning the field and value.
Returns:The Base64-encoded byte string for the stored value.
Return type:bytes