[gsoc-admin] EventConfig: Add arbitrary mail data sources implementation



commit ca7838d9562fc0ee7f45e2821927a44232e6e6a4
Author: Lasse Schuirmann <lasse schuirmann gmail com>
Date:   Thu Aug 13 21:58:10 2015 +0200

    EventConfig: Add arbitrary mail data sources implementation

 data/config.cfg   |    2 +-
 maillib/config.py |   15 ++++++++++++++-
 maillib/email.py  |   44 +++++++++++++++++++++++++-------------------
 test_main.py      |    6 +++++-
 4 files changed, 45 insertions(+), 22 deletions(-)
---
diff --git a/data/config.cfg b/data/config.cfg
index d62ef84..de2b1ec 100644
--- a/data/config.cfg
+++ b/data/config.cfg
@@ -36,7 +36,7 @@ recipients_name_column = name
 # holding all needed keys for this template. Method should accept one param
 # which is an EventConfig object holding all data referenced above in this
 # file.
-data_source = data/templates/welcome_interns_data.py
+data_source = data.templates.welcome_interns_data
 
 [FOO]
 type = mail_template
diff --git a/maillib/config.py b/maillib/config.py
index e6e5eb2..fb17417 100644
--- a/maillib/config.py
+++ b/maillib/config.py
@@ -7,6 +7,7 @@ import re
 from ics import Calendar
 from urllib import request
 from datetime import timedelta
+from importlib import __import__
 
 from maillib.email import Contact, EmailTemplate
 
@@ -37,6 +38,11 @@ def get_main_data_from_section(section):
             return file.read()
 
 
+def get_mail_data_source(path, event_config):
+    get_data = __import__(path, fromlist=('get_data',)).get_data
+    return get_data(event_config)
+
+
 class EventConfig:
     def __init__(self):
         self.ics_sources = {}
@@ -94,8 +100,15 @@ class EventConfig:
         related_to = self.dates[section['related_to']].begin
         related_to -= timedelta(days=days_before)
 
+        data_source = section.get('data_source')
+        if data_source is not None:
+            data_dict = get_mail_data_source(data_source, self)
+        else:
+            data_dict = {}
+
         self.mail_templates.append(
-            EmailTemplate(text, sender, recipients, cc, bcc, related_to))
+            EmailTemplate(text, sender, recipients, data_dict,
+                          cc, bcc, related_to))
 
     def append_ics_from_section(self, section):
         ics_data = get_main_data_from_section(section)
diff --git a/maillib/email.py b/maillib/email.py
index c3b421b..8434f4c 100755
--- a/maillib/email.py
+++ b/maillib/email.py
@@ -31,46 +31,52 @@ class Contact:
 
 
 class EmailTemplate:
-    def __init__(self, text, sender, recipients, cc="", bcc="", date=None):
+    def __init__(self,
+                 text,
+                 sender,
+                 recipients,
+                 data_dict,
+                 cc="",
+                 bcc="",
+                 date=None):
         """
         Creates a new mail template.
 
         :param text:       The text with data fields marked like {key}.
         :param sender:     The mail adress of the sender (string).
         :param recipients: A list of Contact objects.
+        :param data_dict:  A dictionary to fill the text.
         :param cc:         The mail adress(es) to CC (string).
         :param bcc:        The mail adress(es) to BCC (string).
         :param date:       An Arrow object indicating when this template is to
                            be sent.
         """
+        now = datetime.datetime.now()
+        data_dict.update({
+            'year': now.year,
+            'month': now.month,
+            'day': now.day,
+            'hour': now.hour,
+            'minute': now.minute})
+
         self.text = text
         self.sender = sender
         self.recipients = recipients
+        self.data_dict = data_dict
         self.cc = cc
         self.bcc = bcc
         self.date = date
 
-    def get_specific(self, data_dict):
+    def get_specific(self):
         """
         Applies the data to the template.
-
-        :param data_dict: Will be modified (mutable)! Data about the current
-                          time will be added.
         """
-        now = datetime.datetime.now()
-        data_dict.update({
-            'year': now.year,
-            'month': now.month,
-            'day': now.day,
-            'hour': now.hour,
-            'minute': now.minute})
-        return self.text.format(**data_dict)
+        return self.text.format(**self.data_dict)
 
-    def send(self, data_dict):
-        data_dict = data_dict.copy()
+    def send(self):
         for recipient in self.recipients:
-            data_dict['recipient_name'] = recipient.name
-            send_mail(self.get_specific(data_dict),
+            self.data_dict['recipient_name'] = recipient.name
+            send_mail(self.get_specific(),
                       self.sender,
                       recipient.pretty_email_name(),
                       self.cc,
@@ -80,6 +86,6 @@ class EmailTemplate:
 if __name__ == '__main__':
     template_file = open('../data/templates/foo')
     template_text = template_file.read()
-    template = EmailTemplate(template_text, None, None)
     format_dict = {'student_name': 'Rupert Monkey'}
-    print(template.get_specific(format_dict))
+    template = EmailTemplate(template_text, None, None, format_dict)
+    print(template.get_specific())
diff --git a/test_main.py b/test_main.py
index 2301ad7..2198134 100755
--- a/test_main.py
+++ b/test_main.py
@@ -3,4 +3,8 @@
 from maillib.config import parse_event_config
 
 if __name__ == '__main__':
-    parse_event_config("data/config.cfg")
+    c = parse_event_config("data/config.cfg")
+    print("TEMPLATE")
+    print(c.mail_templates[0].text)
+    print("SPECIFIC")
+    print(c.mail_templates[0].get_specific())


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]