Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
2f49ce1b
Verified
Commit
2f49ce1b
authored
5 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Refactor notification logic out of model
parent
94f56d9e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!137
Generalise notifications and implement SMS notifications
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/core/models.py
+2
-15
2 additions, 15 deletions
aleksis/core/models.py
aleksis/core/util/notifications.py
+53
-8
53 additions, 8 deletions
aleksis/core/util/notifications.py
with
55 additions
and
23 deletions
aleksis/core/models.py
+
2
−
15
View file @
2f49ce1b
...
...
@@ -7,9 +7,8 @@ from django.utils.translation import ugettext_lazy as _
from
image_cropping
import
ImageCropField
,
ImageRatioField
from
phonenumber_field.modelfields
import
PhoneNumberField
from
.mailer
import
send_mail_with_template
from
templated_email
import
send_templated_mail
from
.mixins
import
ExtensibleModel
from
.util.notifications
import
send_notification
from
constance
import
config
...
...
@@ -221,20 +220,8 @@ class Notification(models.Model):
return
self
.
title
def
save
(
self
,
**
kwargs
):
if
not
self
.
sent
:
context
=
{
"
notification
"
:
self
,
"
notification_user
"
:
self
.
person
.
adressing_name
,
}
send_templated_mail
(
template_name
=
'
notification
'
,
from_email
=
config
.
MAIL_OUT
,
recipient_list
=
[
self
.
person
.
email
],
context
=
context
,
)
self
.
sent
=
True
super
().
save
(
**
kwargs
)
send_notification
(
self
)
class
Meta
:
verbose_name
=
_
(
"
Notification
"
)
...
...
This diff is collapsed.
Click to expand it.
aleksis/core/util/notifications.py
+
53
−
8
View file @
2f49ce1b
...
...
@@ -4,9 +4,58 @@ from django.conf import settings
from
django.utils.translation
import
gettext_lazy
as
_
from
constance
import
config
from
templated_email
import
send_templated_mail
from
..models
import
Notification
from
.core_helpers
import
celery_optional
def
get_notification_choices
():
def
_send_notification_email
(
notification
:
Notification
,
template
:
str
=
"
notification
"
)
->
None
:
context
=
{
"
notification
"
:
notification
,
"
notification_user
"
:
notification
.
person
.
adressing_name
,
}
send_templated_mail
(
template_name
=
template
,
from_email
=
config
.
MAIL_OUT
,
recipient_list
=
[
notification
.
person
.
email
],
context
=
context
,
)
# Mapping of channel id to name and two functions:
# - Check for availability
# - Send notification through it
_CHANNELS_MAP
=
{
"
email
"
:
(
_
(
"
E-Mail
"
),
lambda
:
config
.
get
(
"
MAIL_OUT
"
,
None
),
_send_notification_email
),
"
sms
"
:
(
_
(
"
SMS
"
),
lambda
:
settings
.
get
(
"
TWILIO_SID
"
,
None
),
_send_notification_sms
),
}
@celery_optional
def
send_notification
(
notification
:
Union
[
int
,
Notification
],
resend
:
bool
=
False
)
->
None
:
"""
Send a notification through enabled channels.
If resend is passed as True, the notification is sent even if it was
previously marked as sent.
"""
channels
=
config
.
get
(
"
NOTIFICATION_CHANNELS
"
,
[])
if
isinstance
(
notification
,
int
):
notification
=
Notification
.
objects
.
get
(
pk
=
notification
)
if
resend
or
not
notification
.
sent
:
for
channel
in
channels
:
name
,
check
,
send
=
_CHANNEL_MAPS
[
channel
]
if
check
():
send
(
notification
)
notification
.
sent
=
True
notification
.
save
()
def
get_notification_choices
()
->
list
:
"""
Return all available channels for notifications.
This gathers the channels that are technically available as per the
...
...
@@ -15,13 +64,9 @@ def get_notification_choices():
"""
choices
=
[]
if
config
.
get
(
"
MAIL_OUT
"
,
None
):
choices
+=
(
"
email
"
,
_
(
"
E-Mail
"
))
if
settings
.
get
(
"
TWILIO_SID
"
,
None
):
choices
+=
(
"
sms
"
,
_
(
"
SMS
"
))
for
channel
,
(
name
,
check
,
send
)
in
_CHANNEL_MAPS
:
if
check
():
choices
+=
(
channel
,
name
)
return
choices
get_notification_choices_lazy
=
lazy
(
get_notification_choices
,
tuple
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment