From cd831d1dfc42c2a0d292fe5c1b7f497b2ca393eb Mon Sep 17 00:00:00 2001 From: Giovane Date: Thu, 14 Jan 2016 18:34:00 -0200 Subject: Removes InputMail.FROM_EMAIL_ADDRESS constant #578 - Created the replier component to generate the reply dict for the email. This was needed to decouple the InputMail from the need to know who is the logged user. --- service/pixelated/support/replier.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 service/pixelated/support/replier.py (limited to 'service/pixelated/support') diff --git a/service/pixelated/support/replier.py b/service/pixelated/support/replier.py new file mode 100644 index 00000000..0bcb1a27 --- /dev/null +++ b/service/pixelated/support/replier.py @@ -0,0 +1,32 @@ +from email.utils import parseaddr + + +def generate_recipients(sender, to, ccs, current_user): + result = {'single': None, 'all': {'to-field': [], 'cc-field': []}} + + to.append(sender) + to = remove_duplicates(to) + ccs = remove_duplicates(ccs) + + result['single'] = swap_recipient_if_needed(sender, remove_address(to, current_user), current_user) + result['all']['to-field'] = remove_address(to, current_user) if len(to) > 1 else to + result['all']['cc-field'] = remove_address(ccs, current_user) if len(ccs) > 1 else ccs + return result + + +def remove_duplicates(recipients): + return list(set(recipients)) + + +def remove_address(recipients, current_user): + return [recipient for recipient in recipients if not parsed_mail_matches(recipient, current_user)] + + +def parsed_mail_matches(to_parse, expected): + return parseaddr(to_parse)[1] == expected + + +def swap_recipient_if_needed(sender, recipients, current_user): + if len(recipients) == 1 and parsed_mail_matches(sender, current_user): + return recipients[0] + return sender -- cgit v1.2.3