AlekSIS
AlekSIS

Core utillity functions

General helper functions in core

aleksis.core.util.core_helpers.copyright_years(years, seperator=', ', joiner='–')[source]

Take a sequence of integegers and produces a string with ranges.

>>> copyright_years([1999, 2000, 2001, 2005, 2007, 2008, 2009])
'1999–2001, 2005, 2007–2009'
Return type

str

aleksis.core.util.core_helpers.custom_information_processor(request)[source]

Provide custom information in all templates.

Return type

dict

aleksis.core.util.core_helpers.dt_show_toolbar(request)[source]

Add a helper to determin if Django debug toolbar should be displayed.

Extends the default behaviour by enabling DJDT for superusers independent of source IP.

Return type

bool

aleksis.core.util.core_helpers.get_allowed_object_ids(request, models)[source]

Get all objects of all given models the user of a given request is allowed to view.

Return type

list

aleksis.core.util.core_helpers.get_app_packages()[source]

Find all registered apps from the setuptools entrypoint.

Return type

Sequence[str]

aleksis.core.util.core_helpers.get_or_create_favicon(title, default, is_favicon=False)[source]

Ensure that there is always a favicon object.

Return type

Favicon

aleksis.core.util.core_helpers.get_site_preferences()[source]

Get the preferences manager of the current site.

aleksis.core.util.core_helpers.has_person(obj)[source]

Check wehether a model object has a person attribute linking it to a Person object.

The passed object can also be a HttpRequest object, in which case its associated User object is unwrapped and tested.

Return type

bool

aleksis.core.util.core_helpers.is_impersonate(request)[source]

Check whether the user was impersonated by an admin.

Return type

bool

aleksis.core.util.core_helpers.lazy_preference(section, name)[source]

Lazily get a config value from dynamic preferences.

Useful to bind preferences to other global settings to make them available to third-party apps that are not aware of dynamic preferences.

Return type

Callable[[str, str], Any]

aleksis.core.util.core_helpers.merge_app_settings(setting, original, deduplicate=False)[source]

Merge app settings.

Get a named settings constant from all apps and merge it into the original. To use this, add a settings.py file to the app, in the same format as Django’s main settings.py.

Note: Only selected names will be imported frm it to minimise impact of potentially malicious apps!

Return type

Union[dict, list]

aleksis.core.util.core_helpers.monkey_patch()[source]

Monkey-patch dependencies for special behaviour.

Return type

None

aleksis.core.util.core_helpers.now_tomorrow()[source]

Return current time tomorrow.

Return type

datetime

aleksis.core.util.core_helpers.objectgetter_optional(model, default=None, default_eval=False)[source]

Get an object by pk, defaulting to None.

Return type

Callable[[HttpRequest, Optional[int]], Model]

aleksis.core.util.core_helpers.queryset_rules_filter(obj, queryset, perm)[source]

Filter queryset by user and permission.

Return type

QuerySet

aleksis.core.util.core_helpers.unread_notifications_badge(request)[source]

Generate badge content with the number of unread notifications.

Return type

int

Predicates for permission systemd

aleksis.core.util.predicates.check_global_permission(user, perm)[source]

Check whether a user has a global permission.

Return type

bool

aleksis.core.util.predicates.check_object_permission(user, perm, obj, checker_obj=None)[source]

Check whether a user has a permission on an object.

You can provide a custom ObjectPermissionChecker for prefetching object permissions by annotating an extensible model with set_object_permission_checker. This can be the provided object (obj) or a special object which is only used to get the checker class (checker_obj).

Return type

bool

aleksis.core.util.predicates.contains_site_preference_value(section, pref, value)[source]

Check if given site preference contains a value.

aleksis.core.util.predicates.has_any_object(perm, klass)[source]

Check if has any object.

Build predicate which checks whether a user has access to objects with the provided permission or rule. Differentiates between object-related permissions and rules.

aleksis.core.util.predicates.has_global_perm(perm)[source]

Build predicate which checks whether a user has a global permission.

aleksis.core.util.predicates.has_object_perm(perm)[source]

Build predicate which checks whether a user has a permission on a object.

aleksis.core.util.predicates.is_site_preference_set(section, pref)[source]

Check the boolean value of a given site preference.

aleksis.core.util.predicates.permission_validator(request, perm)[source]

Check whether the request user has a permission.

Return type

bool

Messages

aleksis.core.util.messages.add_message(request, level, message, **kwargs)[source]

Add a message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to DEBUG level.

Return type

Optional[Any]

aleksis.core.util.messages.debug(request, message, **kwargs)[source]

Add a debug message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to DEBUG level.

Return type

Optional[Any]

aleksis.core.util.messages.error(request, message, **kwargs)[source]

Add an error message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to ERROR level.

Return type

Optional[Any]

aleksis.core.util.messages.info(request, message, **kwargs)[source]

Add a info message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to INFO level.

Return type

Optional[Any]

aleksis.core.util.messages.success(request, message, **kwargs)[source]

Add a success message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to SUCCESS level.

Return type

Optional[Any]

aleksis.core.util.messages.warning(request, message, **kwargs)[source]

Add a warning message.

Add a message to either Django’s message framework, if called from a web request, or to the default logger.

Default to WARNING level.

Return type

Optional[Any]

General helper functions for models

Helper functions for SASS

Helpers for SASS/SCSS compilation.

aleksis.core.util.sass_helpers.clean_scss(*args, **kwargs)[source]

Unlink compiled CSS (i.e. cache invalidation).

Return type

None

aleksis.core.util.sass_helpers.get_colour(html_colour)[source]

Get a SASS colour object from an HTML colour string.

Return type

SassColor

aleksis.core.util.sass_helpers.get_preference(section, name)[source]

Get a preference from dynamic-preferences.

Return type

str

Utillity function for AlekSIS app container

class aleksis.core.util.apps.AppConfig(app_name, app_module)[source]

An extended version of DJango’s AppConfig container.

get_all_scopes()[source]

Return all OAuth scopes and their descriptions for this app.

Return type

dict[str, str]

get_available_scopes(application=None, request=None, *args, **kwargs)[source]

Return a list of all OAuth scopes available to the request and application.

Return type

list[str]

Get copyright information tuples for application package.

Return type

Sequence[tuple[str, str, str]]

get_default_scopes(application=None, request=None, *args, **kwargs)[source]

Return a list of all OAuth scopes to always include for this request and application.

Return type

list[str]

get_distribution()[source]

Get distribution of application package.

get_distribution_name()[source]

Get distribution name of application package.

classmethod get_licence()[source]

Get tuple of licence information of application package.

Return type

tuple

get_name()[source]

Get name of application package.

classmethod get_urls()[source]

Get list of URLs for this application package.

get_version()[source]

Get version of application package.

post_migrate(app_config, verbosity, interactive, using, **kwargs)[source]

Call on every app instance after its models have been migrated.

By default, asks all models to do maintenance on their default data.

Return type

None

pre_migrate(app_config, verbosity, interactive, using, plan, apps, **kwargs)[source]

Call on every app instance before its models are migrated.

By default, it does nothing.

Return type

None

preference_updated(sender, section=None, name=None, old_value=None, new_value=None, **kwargs)[source]

Call on every app instance if a dynamic preference changes, and once on startup.

By default, it does nothing.

Return type

None

ready()[source]

Override this method in subclasses to run code when Django starts.

user_logged_in(sender, request, user, **kwargs)[source]

Call after a user logged in.

By default, it does nothing.

Return type

None

user_logged_out(sender, request, user, **kwargs)[source]

Call after a user logged out.

By default, it does nothing.

Return type

None

AlekSIS core middlewares

class aleksis.core.util.middlewares.EnsurePersonMiddleware(get_response)[source]

Middleware that ensures that the logged-in user is linked to a person.

It is needed to inject a dummy person to a superuser that would otherwise not have an associated person, in order they can get their account set up without external help.

In addition, if configured in preferences, it auto-creates or links persons to regular users if they match.

__init__(get_response)[source]

Initialize self. See help(type(self)) for accurate signature.

Search utillity

class aleksis.core.util.search.SearchIndex[source]

Base class for search indexes on AlekSIS models.

It provides a default document field caleld text and exects the related model in the model attribute.

get_model()[source]

Should return the Model class (not an instance) that the rest of the SearchIndex should use.

This method is required & you must override it to return the correct class.