diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2014-08-12 16:45:26 -0300 |
---|---|---|
committer | Duda Dornelles <ddornell@thoughtworks.com> | 2014-08-12 18:50:20 -0300 |
commit | bf7b6aa193188c95c50cbc9d24f4be56afa6dc6f (patch) | |
tree | a7774f148defd799f2e24547fca0e22b63db681c /py-fake-service/app | |
parent | 27e07e05d97b577ef6b62b74ca7daebafd41db98 (diff) |
sending mails in py-fake-service
Diffstat (limited to 'py-fake-service/app')
-rw-r--r-- | py-fake-service/app/adapter/mail.py | 38 | ||||
-rw-r--r-- | py-fake-service/app/adapter/mail_service.py | 16 | ||||
-rw-r--r-- | py-fake-service/app/adapter/mailset.py | 10 | ||||
-rw-r--r-- | py-fake-service/app/pixelated_user_agent.py | 15 |
4 files changed, 65 insertions, 14 deletions
diff --git a/py-fake-service/app/adapter/mail.py b/py-fake-service/app/adapter/mail.py index 380d1e2c..242501c8 100644 --- a/py-fake-service/app/adapter/mail.py +++ b/py-fake-service/app/adapter/mail.py @@ -1,15 +1,31 @@ from datetime import datetime import random +import calendar class Mail: - def __init__(self, mbox_mail, ident): - self.header = self._get_headers(mbox_mail) - self.ident = ident - self.body = mbox_mail.get_payload() - self.tags = self._get_tags(mbox_mail) - self.security_casing = {} - self.status = self._get_status() + NOW = calendar.timegm(datetime.strptime(datetime.now().isoformat(), "%Y-%m-%dT%H:%M:%S.%f").timetuple()) + @staticmethod + def from_json(mail_json): + mail = Mail() + mail.header = mail_json['header'] + mail.header['date'] = datetime.now().isoformat() + mail.ident = mail_json['ident'] or 0 + mail.body = mail_json['body'] + mail.tags = mail_json['tags'] + mail.security_casing = {} + mail.status = [] + return mail + + + def __init__(self, mbox_mail=None, ident=None): + if mbox_mail: + self.header = self._get_headers(mbox_mail) + self.ident = ident + self.body = mbox_mail.get_payload() + self.tags = self._get_tags(mbox_mail) + self.security_casing = {} + self.status = self._get_status() def _get_status(self): status = [] @@ -23,7 +39,7 @@ class Mail: headers['from'] = mbox_mail.get_from() headers['to'] = [mbox_mail.get('To')] headers['subject'] = mbox_mail.get('Subject') - headers['date'] = datetime.fromtimestamp(random.randrange(1222222222, 1444444444)).isoformat() + headers['date'] = datetime.fromtimestamp(random.randrange(1222222222, self.NOW)).isoformat() return headers def _get_tags(self, mbox_mail): @@ -32,4 +48,8 @@ class Mail: @property def subject(self): - return self.headers['subject'] + return self.header['subject'] + + @property + def date(self): + return self.header['date'] diff --git a/py-fake-service/app/adapter/mail_service.py b/py-fake-service/app/adapter/mail_service.py index c6bd3ab4..3d12feb5 100644 --- a/py-fake-service/app/adapter/mail_service.py +++ b/py-fake-service/app/adapter/mail_service.py @@ -4,6 +4,7 @@ import mailbox from tagsset import TagsSet from mailset import MailSet from contacts import Contacts +from mail import Mail class MailService: MAILSET_PATH = os.path.join(os.environ['HOME'], 'mailsets', 'mediumtagged') @@ -28,7 +29,7 @@ class MailService: def mails(self, query, page, window_size): mails = self.mailset.values() mails = [mail for mail in mails if query.test(mail)] - return mails + return sorted(mails, key=lambda mail: mail.date, reverse=True) def mail(self, mail_id): return self.mailset.get(mail_id) @@ -60,7 +61,18 @@ class MailService: mail.tags = new_tags - + def send(self, mail): + mail = Mail.from_json(mail) + self.mailset.update(mail) + self.tagsset.increment_tag_total_count('sent') + self.tagsset.decrement_tag_total_count('drafts') + return mail.ident + + def save_draft(self, mail): + mail = self.mailset.add_draft(Mail.from_json(mail)) + return mail.ident + + diff --git a/py-fake-service/app/adapter/mailset.py b/py-fake-service/app/adapter/mailset.py index 5a6f27a4..b59959a7 100644 --- a/py-fake-service/app/adapter/mailset.py +++ b/py-fake-service/app/adapter/mailset.py @@ -1,6 +1,5 @@ from mail import Mail - class MailSet: def __init__(self): @@ -31,4 +30,13 @@ class MailSet: mail.tags.append('trash') return False + def update(self, mail): + self.mails[mail.ident] = mail + + def add_draft(self, mail): + mail.ident = self.ident + self.mails[mail.ident] = mail + self.ident += 1 + return mail + diff --git a/py-fake-service/app/pixelated_user_agent.py b/py-fake-service/app/pixelated_user_agent.py index 41188b01..e0312114 100644 --- a/py-fake-service/app/pixelated_user_agent.py +++ b/py-fake-service/app/pixelated_user_agent.py @@ -10,6 +10,7 @@ app = Flask(__name__, static_url_path='', static_folder='../../web-ui/app') client = None converter = None account = None +loaded = False mail_service = MailService() def respond_json(entity): @@ -24,7 +25,13 @@ def disabled_features(): @app.route('/mails', methods=['POST']) def save_draft_or_send(): - return respond_json({'ident': 123}) + mail = request.json + if mail['ident']: + ident = mail_service.send(mail) + else: + ident = mail_service.save_draft(mail) + + return respond_json({'ident': ident}) @app.route('/mails', methods=['PUT']) @@ -115,9 +122,13 @@ def load_mailset(mailset): return respond_json(None) - @app.route('/') def index(): + global loaded + if not loaded: + load_mailset('mediumtagged') + loaded = True + return app.send_static_file('index.html') |