Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
MOOC packages by hacklab
django-courses
Commits
8cf8755a
Commit
8cf8755a
authored
Jun 02, 2021
by
Matheus Miranda
Browse files
Add multi languages for email sending
parent
cd35a8d2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
15 deletions
+35
-15
courses/models.py
courses/models.py
+35
-15
No files found.
courses/models.py
View file @
8cf8755a
...
...
@@ -768,6 +768,19 @@ class ProfessorMessage(models.Model):
def
__str__
(
self
):
return
self
.
subject
+
" - "
+
str
(
self
.
date
)
def
send_emails
(
self
,
bcc
,
message
,
subject
,
email_batch_size
):
# Iterate over the bcc list to send emails in chunks
# Maximum chunk size is email_batch_size
start
=
0
end
=
min
(
email_batch_size
,
len
(
bcc
))
while
start
<
len
(
bcc
):
email
=
EmailMessage
(
subject
,
message
,
settings
.
DEFAULT_FROM_EMAIL
,
[
settings
.
DEFAULT_FROM_EMAIL
,
],
bcc
[
start
:
end
])
email
.
content_subtype
=
"html"
email
.
send
()
start
=
end
end
=
min
(
end
+
email_batch_size
,
len
(
bcc
))
def
send
(
self
):
# bcc = list with all destinataries
# batch = bcc.split( constant )
...
...
@@ -779,28 +792,35 @@ class ProfessorMessage(models.Model):
# check if everything is sent
email_batch_size
=
settings
.
PROFESSOR_MESSAGE_CHUNK_SIZE
bcc
=
[
u
.
email
for
u
in
self
.
users
.
all
()
if
u
.
is_active
and
re
.
match
(
r
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
,
u
.
email
)]
try
:
et
=
EmailTemplate
.
objects
.
get
(
name
=
'professor-message'
)
except
EmailTemplate
.
DoesNotExist
:
et
=
EmailTemplate
(
name
=
"professor-message"
,
subject
=
"{{subject}}"
,
template
=
"{{message|safe}}"
)
subject
=
Template
(
et
.
subject
).
render
(
Context
({
'subject'
:
self
.
subject
}))
message
=
Template
(
et
.
template
).
render
(
Context
({
'message'
:
self
.
message
}))
# Iterate over the bcc list to send emails in chunks
# Maximum chunk size is email_batch_size
start
=
0
end
=
min
(
email_batch_size
,
len
(
bcc
))
while
start
<
len
(
bcc
):
email
=
EmailMessage
(
subject
,
message
,
settings
.
DEFAULT_FROM_EMAIL
,
[
settings
.
DEFAULT_FROM_EMAIL
,
],
bcc
[
start
:
end
])
email
.
content_subtype
=
"html"
email
.
send
()
start
=
end
end
=
min
(
end
+
email_batch_size
,
len
(
bcc
))
if
settings
.
MULTILINGUAL_EMAIL_SEND
:
bcc_en
=
[
u
.
email
for
u
in
self
.
users
.
all
()
if
u
.
is_active
and
re
.
match
(
r
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
,
u
.
email
)
and
u
.
preferred_language
==
'en'
]
bcc_es
=
[
u
.
email
for
u
in
self
.
users
.
all
()
if
u
.
is_active
and
re
.
match
(
r
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
,
u
.
email
)
and
u
.
preferred_language
==
'es'
]
bcc_pt_br
=
[
u
.
email
for
u
in
self
.
users
.
all
()
if
u
.
is_active
and
re
.
match
(
r
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
,
u
.
email
)
and
u
.
preferred_language
==
'pt-br'
]
return
None
subject_en
=
Template
(
et
.
subject
).
render
(
Context
({
'subject'
:
self
.
subject_en
}))
message_en
=
Template
(
et
.
template
).
render
(
Context
({
'message'
:
self
.
message_en
}))
self
.
send_emails
(
bcc_en
,
message_en
,
subject_en
,
email_batch_size
)
subject_es
=
Template
(
et
.
subject
).
render
(
Context
({
'subject'
:
self
.
subject_es
}))
message_es
=
Template
(
et
.
template
).
render
(
Context
({
'message'
:
self
.
message_es
}))
self
.
send_emails
(
bcc_es
,
message_es
,
subject_es
,
email_batch_size
)
subject_pt_br
=
Template
(
et
.
subject
).
render
(
Context
({
'subject'
:
self
.
subject_pt_br
}))
message_pt_br
=
Template
(
et
.
template
).
render
(
Context
({
'message'
:
self
.
message_pt_br
}))
self
.
send_emails
(
bcc_pt_br
,
message_pt_br
,
subject_pt_br
,
email_batch_size
)
else
:
bcc
=
[
u
.
email
for
u
in
self
.
users
.
all
()
if
u
.
is_active
and
re
.
match
(
r
"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
,
u
.
email
)]
subject
=
Template
(
et
.
subject
).
render
(
Context
({
'subject'
:
self
.
subject
}))
message
=
Template
(
et
.
template
).
render
(
Context
({
'message'
:
self
.
message
}))
self
.
send_emails
(
bcc
,
message
,
subject
,
email_batch_size
)
return
None
class
ProfessorMessageRead
(
models
.
Model
):
user
=
models
.
ForeignKey
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment