Commit 8cf8755a authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Add multi languages for email sending

parent cd35a8d2
......@@ -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,29 +792,36 @@ 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}}")
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']
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}))
# 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))
self.send_emails(bcc, message, subject, email_batch_size)
return None
class ProfessorMessageRead(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment