ae.django_utils

helpers for django projects

this module is providing helper functions for your django projects.

Functions

ensure_record(model[, defaults])

determine db record matching the passed field values and create it if not found.

generic_language(lang_code)

remove the country specific part - if exists - from the passed language code.

requested_language()

determine the currently requested short translation language code, fallback to english/'en' if unset.

set_url_part(url[, fragment, query])

add or replace fragment and/or query string of url.

ensure_record(model, defaults=None, **field_values)[source]

determine db record matching the passed field values and create it if not found.

Parameters:
  • model (Type[Model]) – the model to get a create a record for.

  • defaults (Optional[dict[str, Any]]) – additional field values.

  • field_values – field values to identify the record to get/create.

Return type:

Model

Returns:

found or created db record (model instance)

generic_language(lang_code)[source]

remove the country specific part - if exists - from the passed language code.

Parameters:

lang_code (str) – lower-case language code with optional hyphen and country specific part.

Return type:

str

Returns:

stripped/short language code (e.g. return “en” if lang_code == “en-gb”)

requested_language()[source]

determine the currently requested short translation language code, fallback to english/’en’ if unset.

Return type:

str

Returns:

short (without country specific part) and lower-case language code

set_url_part(url, fragment='', query='', **set_part_values)[source]

add or replace fragment and/or query string of url.

Parameters:
  • url (str) – url to complete by adding and/or updating parts of it.

  • fragment (str) – id of the fragment to add/replace.

  • query (str) – query string to add/replace.

  • set_part_values – other parts like ‘scheme’, ‘netloc’, ‘path’, ‘params’ (see :func:urllib.parse.urlparse).

Return type:

str

Returns:

url with added/replaced parts.