Generator¶
You can define a generator by createing a mail. This generator can be generated your context for a specific email type. For example you can use your own generator to create specific links in a “Password Forgotten” Email.
If you define your own generator you can create your own method wich add a new value to the context, update available context values.
The Generator was called by the MailFactory class. Die get_context methode in der MailFactory Klasse ruft methoden im generator auf die den selben namen haben wie die Variablen zum MailTemplate oder zur Mail
D.h.
Ausgangsituation¶
MailTemplate:
- name: DefaultTemplate
MailTemplateVariable:
- name: headerdefault: Hi
- name: contentdefault: Wie gehts? Alles fit?
Mail:
- subject: Welcometemplate: DefaultTemplategenerator: notification.generator.generic.BasicGeneratorreason: for_every_thing
The notification.generator.generic.BasicGenerator call all methods with the same name like the MailTemplateVariable and overwrites the result of the function with the result of method of the MailVariable. Like the following Example code:
from notification.generator.generic import BasicGenerator
from mailsystem.models import Mail
class BasicGenerator:
def <NameOfEveryVariable>(self):
result = self.get_content_of_mail_<NameOfEveryVariable>()
if result is None:
result = self.get_content_of_mail_template_<NameOfEveryVariable>()
return result
context = {}
gen = BasicGenerator()
context["header"] = gen.<NameOfEveryVariable>()
The context that BasicGenerator creates was
{
"header": "Hi"
"content": "Wie gehts? Alles fit?"
}
if the following setting was added to the Mail Configuration
MailVariable:
- mail_template_variable: headercontent: Dear Sir or Madam
- mail_template_variable: contentcontent: Welcome to……
the result of BasicGenerator was
{
"header": "Dear Sir or Madam",
"content": "Welcome to......"
}
If nessesary you can create your own method to create adidtional context. Your own Method must be start with generator_ and gets the context as parameter
def generate_registration_link(self, ctx):
# ctx["reference"] contains an user object
# do magic returns for example a string
link = ctx["reference"].do_magic()
return {"registration_link": link}
Your own generator should be Inherith from BasicGenerator