Utils Database
inyoka.utils.database
This module provides some helpers to work with the database.
- copyright:
2007-2024 by the Inyoka Team, see AUTHORS for more details.
- license:
BSD, see LICENSE for more details.
- class inyoka.utils.database.BaseMarkupField(application=None, redis_timeout=None, *args, **kwargs)
Base Class for fields that needs to be rendered.
- contribute_to_class(cls, name)
Register the field with the model class it belongs to.
If private_only is True, create a separate instance of this field for every subclass of cls, even if cls is not an abstract model.
- deconstruct()
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.
- get_redis_key(cls, instance, name)
- exception inyoka.utils.database.CannotResolve
- class inyoka.utils.database.InyokaMarkupField(simplify=False, force_existing=False, *args, **kwargs)
Field to save and render Inyoka markup.
- deconstruct()
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.
- get_content_create_callback(inst_self, field_name)
Returns a callable, that renders the content.
inst_self is an instance of a django model, which has the InyokaMarkupField. field_name is the name of the InyokaMarkupField.
- get_render_method()
Returns a callable that can be bound as staticmethod to the django model.
This callable taks text as first argument and returns the rendered content.
- class inyoka.utils.database.JSONField(*args, db_collation=None, **kwargs)
- contribute_to_class(cls, name, private_only=False)
Register the field with the model class it belongs to.
If private_only is True, create a separate instance of this field for every subclass of cls, even if cls is not an abstract model.
- dumps(obj)
- loads(s)
- pre_save(obj, add)
Return field’s value just before saving.
- class inyoka.utils.database.JabberField(*args, db_collation=None, **kwargs)
ModelField that is a CharField, but uses a different form field with custom validation
- formfield(**kwargs)
Return a django.forms.Field instance for this field.
- class inyoka.utils.database.LockableObject
- _get_lock_key()
- lock(request)
Lock for 15 Minutes
- lock_key_base = None
Must be defined by an inherited model.
- unlock()
- class inyoka.utils.database.PygmentsField(application=None, redis_timeout=None, *args, **kwargs)
- get_content_create_callback(inst_self, field_name)
Returns a callable, that renders the content.
inst_self is an instance of a django model, which has the InyokaMarkupField. field_name is the name of the InyokaMarkupField.
- get_render_method()
- class inyoka.utils.database.SimpleDescriptor(field)
- inyoka.utils.database._strip_ending_nums(string)
- inyoka.utils.database.find_next_increment(model, column, string, **query_opts)
Get the next incremented string based on column and string.
Example:
find_next_increment(Article, 'slug', 'article name')
- inyoka.utils.database.get_simplified_queryset(queryset)
Returns a QuerySet with following modifications:
without Aggregators
unused select fields
.only(‘id’)
no order_by
The resultung QuerySet can be used for efficient .count() queries.
- inyoka.utils.database.model_or_none(pk, reference)