1 import flask
2 import platform
3 import smtplib
4 from email.mime.text import MIMEText
5 from coprs import app, helpers
6
7
14
15
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
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
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
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
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
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 send_mail(recipient, message, sender=None, reply_to=None):
118 """
119 :param str/list recipient: One recipient email as a string or multiple emails in a list
120 :param Message message:
121 :param str sender: Email of a sender
122 :return:
123 """
124 msg = MIMEText(message.text)
125 msg["Subject"] = message.subject
126 msg["From"] = sender or "root@{0}".format(platform.node())
127 msg["To"] = ", ".join(recipient) if type(recipient) == list else recipient
128 msg.add_header("reply-to", reply_to or app.config["REPLY_TO"])
129 s = smtplib.SMTP("localhost")
130 s.sendmail("root@{0}".format(platform.node()), recipient, msg.as_string())
131 s.quit()
132