-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (51 loc) · 2.09 KB
/
Copy pathutils.py
File metadata and controls
64 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
import logging
import swapper
from django.core.mail import send_mail
from django.db.models import Q
from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import render_to_string
from django.utils.text import slugify
def render_verb_template_or_default(verb, context, template_type, extension):
try:
return render_to_string(
f"emails/notifications/{verb}-{template_type}.{extension}.j2",
context=context,
)
except TemplateDoesNotExist:
logging.warning(
f"Template emails/notifications/{verb}-{template_type}.{extension}.j2 does not exist, falling back to default template"
)
return render_to_string(
f"emails/notification-{template_type}.{extension}.j2",
context=context,
)
def send_email_notification(to, context):
verb = slugify(context["verb"])
subject = render_verb_template_or_default(verb, context, "subject", "txt")
subject = " ".join(subject.strip().split())
text_message = render_verb_template_or_default(verb, context, "body", "txt")
html_message = render_verb_template_or_default(verb, context, "body", "html")
send_mail(
subject, text_message, html_message=html_message, from_email=None, recipient_list=[to]
)
def can_user_receive_notification(user_id, verb, channel):
NotificationSetting = swapper.load_model("baseapp_notifications", "NotificationSetting")
user_setting = (
NotificationSetting.objects.filter(
Q(channel=channel) | Q(channel=NotificationSetting.NotificationChannelTypes.ALL),
Q(verb=get_setting_from_verb(verb)) | Q(verb="_ALL_"),
user_id=user_id,
)
.order_by("is_active")
.first()
)
if user_setting:
return user_setting.is_active
return True
def get_setting_from_verb(verb):
""" "
Returns the setting group from the verb if available, otherwise returns the verb itself
'CHATS.SEND_MESSAGE' -> 'CHATS'
'SEND_MESSAGE' -> 'SEND_MESSAGE'
"""
return verb.split(".")[0] if "." in verb else verb