Utils (feincms3.utils
)¶
Note
The utils module is meant purely for feincms3’s internal use. Utilities may be added and removed without prior warning and without a deprecation period.
If you depend on some functionality from this module copy the code into your project (according to the very permissive license of course).
- class feincms3.utils.ChoicesCharField(*args, **kwargs)[source]¶
models.CharField
with choices, which makes the migration framework always ignore changes tochoices
, ever.- deconstruct()[source]¶
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.
- feincms3.utils.is_first_party_link(url, *, first_party_hosts=None)[source]¶
Return whether an URL is a first-party link or not.
First parties are defined by
ALLOWED_HOSTS
and can be overridden by passing an alternative list of hosts. The wildcard["*"]
isn’t recognized.NOTE!
first_party_hosts
should not contain port numbers even if using a non-standard port, the same is true for Django’sALLOWED_HOSTS
setting.One template tag is available to help with ensuring off-site links open in a new window (if you need this…). The template tag does not allow specifying the list of first party hosts (it always uses
ALLOWED_HOSTS
):{% load feincms3 %} <a href="{{ url }}" {% maybe_target_blank url %}>text</a>
- feincms3.utils.upload_to(instance, filename)[source]¶
Standard
upload_to
callable for feincms3 file fieldsThe generated path consists of:
The instance’s lowercased model label
A part of the proleptic Gregorian ordinal of the date
The original filename
The ordinal changes each day which means that filename collisions only happen when uploading to the same model (or one with the same name in a different app) and on the same day. This is much better than the previous default of using
images/%Y/%m
where you had to wait up to a full month to avoid collisions.
- feincms3.utils.validation_error(error, *, field, exclude, **kwargs)[source]¶
Return a validation error that is associated with a particular field if it isn’t excluded from validation.
See https://github.com/django/django/commit/e8c056c31 for some background.