models

Module: espressodb.notifications.models

Implements notifications similar to logging with optional viewer restrictions

The purpose of this module is having logging like capabilities which can be accessed in web views. Mimicing logging messages, notifications have a content field, a timestamp and a level. To have control over what might be displayed on web views, the notifications come with additional optional features:

Notification.title

(models.CharField) - The title of the notification

Notification.tag

(models.CharField) - A tag for fast searches

Notification.groups

(models.ManyToManyField -> django.contrib.auth.models.Group) - The group of users who are allowed to read this notification

Notification.read_by

(models.ManyToManyField -> django.contrib.auth.models.User) - The users who have read the notification

The notifications view will be rendered on default whenever a user is logged in.


LEVELS

The available notifications levels

Notification(*args, **kwargs)

Model which implements logging like notification interface.

Notifier([tag, groups])

Logger like object which interactions with the Notification model.


LEVELS = ('DEBUG', 'INFO', 'WARNING', 'ERROR')

The available notifications levels

class Notification(*args, **kwargs)[source]

Model which implements logging like notification interface.

The model is ordered according to timestamp in descending order.

exception DoesNotExist
exception MultipleObjectsReturned
add_user_to_read_by(user)[source]

Adds the user to the Notification.read_by list and inserts in the db.

Parameters

user (User) – The user to check.

content

(models.TextField) - The content of the notification

classmethod get_notifications(user, level=None, show_all=False)[source]

Returns all notifications the user is allowed to see.

Parameters
  • user (User) – The user who wants to see notifications

  • level (Optional[str]) – The notification level to specialize. Shows notifications for all levels if not specified.

  • show_all (bool) – If True also shows already read messages

Results are order by timestamp in decreasing order.

Return type

List[Notification]

groups

(models.ManyToManyField -> django.contrib.auth.models.Group) - The group of users who are allowed to read this notification

has_been_read_by(user)[source]

Checks if the user has read the notification

Return type

bool

level

(models.CharField) - The level of the notification mimicing logging levels. See also LEVELS

read_by

(models.ManyToManyField -> django.contrib.auth.models.User) - The users who have read the notification

tag

(models.CharField) - A tag for fast searches

timestamp

(models.DateTimeField) - Creation date of the notification

title

(models.CharField) - The title of the notification

viewable_by(user)[source]

Checks if the user is allowed to read this notification.

Parameters

user (Optional[User]) – The user to check.

Return type

bool

Returns

False if the notification groups are not empty and user is not in the specified groups.

class Notifier(tag=None, groups=None)[source]

Logger like object which interactions with the Notification model.

Example

notifier = Notifier(tag="my_app", groups=["admin"])
notifer.debug("Set up notifier")
notifier.info("Start to invesitgate app")
...
notifier.error("ERROR! read this ...", title="NEED urgent attention!")

Note that tag and groups can be overwriten by the kwargs of the notifier methods, e.g., notifer.debug("Set up notifier", tag="set up")

__init__(tag=None, groups=None)[source]

Init the Notifier class

Parameters
  • tag (Optional[str]) – The tag of the notification. Used for fast searches.

  • groups (Optional[List[str]]) – The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication.

debug(content, title=None, tag=None, groups=None)[source]

Creates notification at debug level.

Parameters
  • content (str) – The content of the notification

  • title (Optional[str]) – The title of the notification

  • tag (Optional[str]) – The tag of the notification. Used for fast searches. Overrides Notifier default tag.

  • groups (Optional[List[str]]) – The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication. Overrides Notifier default groups.

Raises

KeyError – If groups are present but not found.

Return type

Notification

error(content, title=None, tag=None, groups=None)[source]

Creates notification at error level.

Parameters
  • content (str) – The content of the notification

  • title (Optional[str]) – The title of the notification

  • tag (Optional[str]) – The tag of the notification. Used for fast searches. Overrides Notifier default tag.

  • groups (Optional[List[str]]) – The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication. Overrides Notifier default groups.

Raises

KeyError – If groups are present but not found.

Return type

Notification

static get_groups_from_names(group_names)[source]

Parses the group names to Groups.

Parameters

group_names (List[str]) – List of group names which will be converted to a list of espressodb.notifications.models.Notification.

Raises

KeyError – If not all groups are found.

Return type

List[Group]

groups

The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication.

info(content, title=None, tag=None, groups=None)[source]

Creates notification at info level.

Parameters
  • content (str) – The content of the notification

  • title (Optional[str]) – The title of the notification

  • tag (Optional[str]) – The tag of the notification. Used for fast searches. Overrides Notifier default tag.

  • groups (Optional[List[str]]) – The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication. Overrides Notifier default groups.

Raises

KeyError – If groups are present but not found.

Return type

Notification

tag

The tag of the notification. Used for fast searches.

warning(content, title=None, tag=None, groups=None)[source]

Creates notification at warning level.

Parameters
  • content (str) – The content of the notification

  • title (Optional[str]) – The title of the notification

  • tag (Optional[str]) – The tag of the notification. Used for fast searches. Overrides Notifier default tag.

  • groups (Optional[List[str]]) – The user groups which are allowed to view this notfication. No groups means not logged in users are able to view the notfication. Overrides Notifier default groups.

Raises

KeyError – If groups are present but not found.

Return type

Notification