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:
( |
|
( |
|
( |
|
( |
The notifications view will be rendered on default whenever a user is logged in.
The available notifications levels |
|
|
Model which implements logging like notification interface. |
|
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 notificationslevel (
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
-
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
-
exception
-
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
andgroups
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 notificationtitle (
Optional
[str
]) – The title of the notificationtag (
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
-
error
(content, title=None, tag=None, groups=None)[source]¶ Creates notification at error level.
- Parameters
content (
str
) – The content of the notificationtitle (
Optional
[str
]) – The title of the notificationtag (
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
-
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 ofespressodb.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 notificationtitle (
Optional
[str
]) – The title of the notificationtag (
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
-
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 notificationtitle (
Optional
[str
]) – The title of the notificationtag (
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
-