Skip to content

Commit ce2c8cf

Browse files
committed
Added script to send notifications to open dialogue event
1 parent a13520b commit ce2c8cf

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os, sys
2+
3+
proj_path = "/home/webuser/webapps/tigaserver/"
4+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tigaserver_project.settings")
5+
sys.path.append(proj_path)
6+
7+
os.chdir(proj_path + "util_scripts/")
8+
9+
from django.core.wsgi import get_wsgi_application
10+
11+
application = get_wsgi_application()
12+
13+
### CODE STARTS HERE
14+
15+
from tigaserver_app.models import *
16+
import pandas as pd
17+
18+
titles = {
19+
"en": "Inspire other users this November!",
20+
"es": "Inspira a otros usuarios este Noviembre!",
21+
"ca": "Inspira a altres usuaris aquest Novembre!",
22+
}
23+
titles['gl'] = titles['es']
24+
25+
translations = {
26+
"en": {
27+
"title": "Top contributors like you inspire us!",
28+
"line1": "You are one of the <strong>top 100 users</strong> with the most activity on our platform.",
29+
"line2": "This November, we’re hosting an <strong>Open Dialogue on citizen science</strong>, and we’d love for you to share your story in a short 5-min talk.",
30+
"line3": 'If you’re interested, please email <a href="mailto:[email protected]?subject=Open%20Dialogue%20Lightning%20Talk">[email protected]</a>.'
31+
},
32+
"es": {
33+
"title": "¡Los contribuidores como tú nos inspiran!",
34+
"line1": "Eres uno de los <strong>top 100 usuarios</strong> con más actividad en nuestra plataforma.",
35+
"line2": "Este noviembre organizamos un <strong>Diálogo Abierto sobre ciencia ciudadana</strong>, y nos encantaría que compartieras tu historia en una charla corta de 5 minutos.",
36+
"line3": 'Si estás interesado, por favor escribe a <a href="mailto:[email protected]?subject=Open%20Dialogue%20Lightning%20Talk">[email protected]</a>.'
37+
},
38+
"ca": {
39+
"title": "Els contribuïdors com tu ens inspiren!",
40+
"line1": "Ets un dels <strong>top 100 usuaris</strong> amb més activitat a la nostra plataforma.",
41+
"line2": "Aquest novembre organitzem un <strong>Diàleg Obert sobre ciència ciutadana</strong>, i ens encantaria que compartissis la teva història en una xerrada curta de 5 minuts.",
42+
"line3": 'Si hi estàs interessat, si us plau escriu a <a href="mailto:[email protected]?subject=Open%20Dialogue%20Lightning%20Talk">[email protected]</a>.'
43+
}
44+
}
45+
translations['gl'] = translations['es']
46+
47+
html_template = """
48+
<!DOCTYPE html>
49+
<html>
50+
<head>
51+
<meta charset="utf-8" />
52+
</head>
53+
<body>
54+
<div>
55+
<h4>{title}</h4>
56+
<p>{line1}</p>
57+
<p>{line2}</p>
58+
<p>{line3}</p>
59+
</div>
60+
</body>
61+
</html>
62+
"""
63+
64+
def generate_email(language="en"):
65+
text = translations.get(language, translations["en"]) # fallback to English
66+
return html_template.format(**text)
67+
68+
df = pd.DataFrame(
69+
TigaUser.objects.annotate(
70+
last_report=models.Subquery(Report.objects.filter(user=models.OuterRef('pk')).order_by('-server_upload_time').values('server_upload_time')[:1]),
71+
num_reports=models.Count('user_reports', filter=models.Q(user_reports__type='adult'))
72+
).filter(
73+
models.Q(num_reports__gte=100, locale__in=('ca', 'es', 'gl'))
74+
| models.Q(num_reports__gte=20, locale='en')
75+
).filter(last_report__year__gte=2023).values('pk','locale')
76+
)
77+
78+
for locale in df.locale.unique():
79+
notification_content = NotificationContent.objects.create(
80+
title_en=titles.get("en"),
81+
body_html_en=generate_email(language="en"),
82+
title_es=titles.get("es"),
83+
body_html_es=generate_email(language="es"),
84+
title_ca=titles.get("ca"),
85+
body_html_ca=generate_email(language="ca"),
86+
title_native=titles.get(locale),
87+
body_html_native=generate_email(language=locale),
88+
native_locale=locale,
89+
)
90+
for user in TigaUser.objects.filter(pk__in=df[df['locale'] == locale]['pk']):
91+
notification = Notification.objects.create(
92+
user=user,
93+
notification_content=notification_content
94+
)
95+
notification.send_to_user(user=user)

0 commit comments

Comments
 (0)