Package coprs :: Module mail
[hide private]
[frames] | no frames]

Source Code for Module coprs.mail

  1  import flask 
  2  import platform 
  3  from smtplib import SMTP 
  4  from email.mime.text import MIMEText 
  5  from coprs import app, helpers 
  6   
  7   
8 -class Message(object):
9 subject = None 10 text = None 11
12 - def __str__(self):
13 return self.subject + "\n\n" + self.text
14 15
16 -class PermissionRequestMessage(Message):
17 - def __init__(self, copr, applicant, permission_dict):
18 """ 19 :param models.Copr copr: 20 :param models.User applicant: object of a user that applies for new permissions (e.g. flask.g.user) 21 :param models.CoprPermission permission: permission object 22 :param dict permission_dict: {"old_builder": int, "old_admin": int, "new_builder": int, "new_admin": int} 23 """ 24 self.subject = "[Copr] {0}: {1} is requesting permissions change".format(copr.full_name, applicant.name) 25 26 self.text = "{0} asked for these changes:\n\n".format(applicant.name) 27 28 for perm in ['Builder', 'Admin']: 29 old = permission_dict.get("old_"+perm.lower()) 30 new = permission_dict.get("new_"+perm.lower()) 31 32 if old != new: 33 if old is None: 34 old = 0 # previously unset 35 self.text += "{0}: {1} -> {2}\n".format( 36 perm, 37 helpers.PermissionEnum(old), 38 helpers.PermissionEnum(new), 39 ) 40 41 self.text += "\nProject: {0}".format(copr.full_name)
42 43
44 -class PermissionChangeMessage(Message):
45 - def __init__(self, copr, permission_dict):
46 """ 47 :param models.Copr copr: 48 :param dict permission_dict: {"old_builder": int, "old_admin": int, "new_builder": int, "new_admin": int} 49 """ 50 self.subject = "[Copr] {0}: Your permissions have changed".format(copr.full_name) 51 self.text = "Your permissions have changed:\n\n" 52 53 for perm in ['Builder', 'Admin']: 54 old = permission_dict.get("old_"+perm.lower()) 55 new = permission_dict.get("new_"+perm.lower()) 56 if old != new: 57 if old is None: 58 old = 0 # previously unset 59 self.text += "{0}: {1} -> {2}\n".format( 60 perm, helpers.PermissionEnum(old), 61 helpers.PermissionEnum(new)) 62 63 self.text += "\nProject: {0}".format(copr.full_name)
64 65
66 -class LegalFlagMessage(Message):
67 - def __init__(self, copr, reporter, reason):
68 """ 69 :param models.Copr copr: 70 :param models.User reporter: A person who reported the legal issue (e.g. flask.g.user) 71 :param str reason: What is the legal issue? 72 """ 73 self.subject = "Legal flag raised on {0}".format(copr.name) 74 self.text = ("{0}\n" 75 "Navigate to {1}\n" 76 "Contact on owner is: {2} <{3}>\n" 77 "Reported by {4} <{5}>".format( 78 reason, 79 flask.url_for("admin_ns.legal_flag", _external=True), 80 copr.user.username, 81 copr.user.mail, 82 reporter.name, 83 reporter.mail))
84 85
86 -class OutdatedChrootMessage(Message):
87 - def __init__(self, copr_chroots):
88 """ 89 :param models.Copr copr: 90 :param list copr_chroots: list of models.CoprChroot instances 91 """ 92 self.subject = "[Copr] upcoming deletion of outdated chroots in your projects" 93 self.text = ("You have been notified because you are an admin of projects, " 94 "that have some builds in outdated chroots\n\n" 95 96 "According to the 'Copr outdated chroots removal policy'\n" 97 "https://docs.pagure.org/copr.copr/copr_outdated_chroots_removal_policy.html\n" 98 "data are going to be preserved {0} days after the chroot is EOL " 99 "and then automatically deleted, unless you decide to prolong the expiration period.\n\n" 100 101 "Please, visit the projects settings if you want to extend the time.\n\n" 102 .format(app.config["DELETE_EOL_CHROOTS_AFTER"])) 103 104 if not copr_chroots: 105 raise AttributeError("No outdated chroots to notify about") 106 107 for chroot in copr_chroots: 108 url = helpers.fix_protocol_for_frontend( 109 helpers.copr_url('coprs_ns.copr_repositories', chroot.copr, _external=True)) 110 self.text += ( 111 "Project: {0}\n" 112 "Chroot: {1}\n" 113 "Remaining: {2} days\n" 114 "{3}\n\n".format(chroot.copr.full_name, chroot.name, chroot.delete_after_days, url))
115 116
117 -def filter_whitelisted_recipients(recipients):
118 """ 119 Filters e-mail recipients if the white list of recipients in conf is not empty. 120 :param recipients: list of recipients 121 :return: list of recipients who should receive an e-mail 122 """ 123 if not app.config["WHITELIST_EMAILS"]: 124 return recipients 125 126 return [r for r in recipients if r in app.config["WHITELIST_EMAILS"]]
127 128
129 -def send_mail(recipients, message, sender=None, reply_to=None):
130 """ 131 :param list recipients: List of email recipients 132 :param Message message: 133 :param str sender: Email of a sender 134 :return: 135 """ 136 msg = MIMEText(message.text) 137 msg["Subject"] = message.subject 138 msg["From"] = sender or "root@{0}".format(platform.node()) 139 msg["To"] = ", ".join(recipients) 140 msg.add_header("reply-to", reply_to or app.config["REPLY_TO"]) 141 recipients = filter_whitelisted_recipients(recipients) 142 if not recipients: 143 return 144 with SMTP("localhost") as smtp: 145 smtp.sendmail("root@{0}".format(platform.node()), recipients, msg.as_string())
146