Django Integration
django_slack_tools.slack_messages.models
SlackMention
Bases: TimestampMixin
, Model
People or group in channels receive messages.
Source code in django_slack_tools/slack_messages/models/mention.py
mention
property
Mention string for use in messages, e.g. "<@{USER_ID}>"
.
MentionType
Bases: TextChoices
Possible mention types.
Source code in django_slack_tools/slack_messages/models/mention.py
GROUP = ('G', _('Group'))
class-attribute
instance-attribute
Team mentions. e.g. @backend
.
SPECIAL = ('S', _('Special'))
class-attribute
instance-attribute
Special mentions. e.g. @here
, @channel
, @everyone
.
UNKNOWN = ('?', _('Unknown'))
class-attribute
instance-attribute
Unknown mention type.
USER = ('U', _('User'))
class-attribute
instance-attribute
User mentions. e.g. @lasuillard
.
SlackMessage
Bases: TimestampMixin
, Model
An Slack message.
Source code in django_slack_tools/slack_messages/models/message.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
SlackMessageRecipient
Bases: TimestampMixin
, Model
People or group in channels receive messages.
Source code in django_slack_tools/slack_messages/models/message_recipient.py
SlackMessagingPolicy
Bases: TimestampMixin
, Model
An Slack messaging policy which determines message content and those who receive messages.
Source code in django_slack_tools/slack_messages/models/messaging_policy.py
TemplateType
Bases: TextChoices
Possible template types.
Source code in django_slack_tools/slack_messages/models/messaging_policy.py
DJANGO = ('DJ', _('Django'))
class-attribute
instance-attribute
Django XML-based template.
DJANGO_INLINE = ('DI', _('Django Inline'))
class-attribute
instance-attribute
Django inline template.
PYTHON = ('P', _('Python'))
class-attribute
instance-attribute
Dictionary-based template.
UNKNOWN = ('?', _('Unknown'))
class-attribute
instance-attribute
Unknown template type.
django_slack_tools.slack_messages.middlewares.django
DjangoDatabasePersister
Bases: BaseMiddleware
Persist message history to database. If request is None
, will do nothing.
Source code in django_slack_tools/slack_messages/middlewares/django.py
__init__(*, slack_app=None, get_permalink=False)
Initialize the middleware.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slack_app
|
App | None
|
Slack app instance to use for certain tasks, such as getting permalinks. |
None
|
get_permalink
|
bool
|
If |
False
|
Source code in django_slack_tools/slack_messages/middlewares/django.py
_get_permalink(*, channel, ts)
Get permalink of the message. It returns empty string on error.
Source code in django_slack_tools/slack_messages/middlewares/django.py
DjangoDatabasePolicyHandler
Bases: BaseMiddleware
Middleware to handle Slack messaging policies stored in the database.
Be cautious when using this middleware because it includes functionality to distribute messages to multiple recipients, which could lead to unwanted infinite loop or recursion if used improperly.
This middleware contains a secondary protection against infinite loops by injecting a context key to the message context. If the key is found in the context, the middleware will stop the message from being sent. So be careful when modifying the context.
Source code in django_slack_tools/slack_messages/middlewares/django.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
_RECURSION_DETECTION_CONTEXT_KEY = '__final__'
class-attribute
instance-attribute
Recursion detection key injected to message context for fanned-out messages to provide secondary protection against infinite loops.
messenger
property
Get the messenger instance. If it's a string, will get the messenger from the app settings.
__init__(*, messenger, on_policy_not_exists='error')
Initialize the middleware.
This middleware will load the policy from the database and send the message to all recipients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messenger
|
Messenger | str
|
Messenger instance or name to use for sending messages. The messenger instance should be different from the one used in the policy handler, because this middleware cannot properly handle fanned-out messages modified by this middleware. Also, there are chances of infinite loops if the same messenger is used. |
required |
on_policy_not_exists
|
OnPolicyNotExists
|
Action to take when policy is not found. |
'error'
|
Source code in django_slack_tools/slack_messages/middlewares/django.py
_create_policy(*, code)
Create a policy with the given code.
Policy created is disabled by default, thus no message will be sent. To modify the default policy creation behavior, simply override this method.
Source code in django_slack_tools/slack_messages/middlewares/django.py
_get_default_context(recipient)
Create default context for the recipient.
Source code in django_slack_tools/slack_messages/middlewares/django.py
_get_policy(*, code)
Get the policy for the given code.
Source code in django_slack_tools/slack_messages/middlewares/django.py
django_slack_tools.slack_messages.message_templates.django
DjangoTemplate
Bases: BaseTemplate
Template utilizing Django built-in template engine.
Source code in django_slack_tools/slack_messages/message_templates/django.py
__init__(*, file=None, inline=None, engine=None)
Initialize template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str | None
|
Path to file with template. |
None
|
inline
|
str | None
|
XML inline template. |
None
|
engine
|
BaseEngine | None
|
Template engine to use. Defaults to Django engine. |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
Some of the arguments are missing or multiple are provided. |
Source code in django_slack_tools/slack_messages/message_templates/django.py
_preprocess_xml(xml)
Normalize XML text nodes.
Source code in django_slack_tools/slack_messages/message_templates/django.py
_remove_single_newline(text)
Remove a single newline from repeated newlines. If the are just one newline, replace it with space.
Source code in django_slack_tools/slack_messages/message_templates/django.py
_rename_tag(tag)
_xml_to_dict(xml)
Parse XML string to Python dictionary.
Following transformations are applied by default:
- Normalize text nodes: remove single newlines and dedent text, etc.
- Rename tags for syntactic comfort: block -> blocks, element -> elements
Please check the tests for more detailed examples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xml
|
str
|
XML string. |
required |
Returns:
Type | Description |
---|---|
dict
|
Parsed dictionary. Be aware, the returned value will be the child of |
dict
|
top-level node (e.g. |
Source code in django_slack_tools/slack_messages/message_templates/django.py
django_slack_tools.slack_messages.template_loaders.django
DjangoPolicyTemplateLoader
Bases: BaseTemplateLoader
Django database-backed template loader.
Source code in django_slack_tools/slack_messages/template_loaders/django.py
_get_template_from_policy(policy_or_code)
Get template instance.
Source code in django_slack_tools/slack_messages/template_loaders/django.py
DjangoTemplateLoader
Bases: BaseTemplateLoader
Django filesystem-backed template loader.
Source code in django_slack_tools/slack_messages/template_loaders/django.py
__init__(*, engine=None)
Initialize template loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
BaseEngine | None
|
Template engine to use. Defaults to Django engine. |
None
|