1 import flask
2 import platform
3 import smtplib
4 from email.mime.text import MIMEText
5 from coprs import app, helpers
6
7
11
12
14 - def __init__(self, copr, applicant, permission_dict):
15 """
16 :param models.Copr copr:
17 :param models.User applicant: object of a user that applies for new permissions (e.g. flask.g.user)
18 :param models.CoprPermission permission: permission object
19 :param dict permission_dict: {"old_builder": int, "old_admin": int, "new_builder": int, "new_admin": int}
20 """
21 self.subject = "[Copr] {0}: {1} is asking permissions".format(copr.name, applicant.name)
22 self.text = ("{0} is asking for these permissions:\n\n"
23 "Builder: {1} -> {2}\n"
24 "Admin: {3} -> {4}\n\n"
25 "Project: {5}\n"
26 "Owner: {6}".format(
27 applicant.name,
28 helpers.PermissionEnum(permission_dict.get("old_builder", 0)),
29 helpers.PermissionEnum(permission_dict.get("new_builder")),
30 helpers.PermissionEnum(permission_dict.get("old_admin", 0)),
31 helpers.PermissionEnum(permission_dict.get("new_admin")),
32 copr.name,
33 copr.owner_name))
34
35
37 - def __init__(self, copr, permission_dict):
38 """
39 :param models.Copr copr:
40 :param dict permission_dict: {"old_builder": int, "old_admin": int, "new_builder": int, "new_admin": int}
41 """
42 self.subject = "[Copr] {0}: Your permissions have changed".format(copr.name)
43 self.text = (
44 "Your permissions have changed:\n\n"
45 "Builder: {0} -> {1}\n"
46 "Admin: {2} -> {3}\n\n"
47 "Project: {4}\n"
48 "Owner: {5}".format(
49 helpers.PermissionEnum(permission_dict["old_builder"]),
50 helpers.PermissionEnum(permission_dict["new_builder"]),
51 helpers.PermissionEnum(permission_dict["old_admin"]),
52 helpers.PermissionEnum(permission_dict["new_admin"]),
53 copr.name, copr.user.name))
54
55
57 - def __init__(self, copr, reporter, reason):
58 """
59 :param models.Copr copr:
60 :param models.User reporter: A person who reported the legal issue (e.g. flask.g.user)
61 :param str reason: What is the legal issue?
62 """
63 self.subject = "Legal flag raised on {0}".format(copr.name)
64 self.text = ("{0}\n"
65 "Navigate to {1}\n"
66 "Contact on owner is: {2} <{3}>\n"
67 "Reported by {4} <{5}>".format(
68 reason,
69 flask.url_for("admin_ns.legal_flag", _external=True),
70 copr.user.username,
71 copr.user.mail,
72 reporter.name,
73 reporter.mail))
74
75
78 """
79 :param models.Copr copr:
80 :param list copr_chroots: list of models.CoprChroot instances
81 """
82 self.subject = "Upcoming deletion of outdated chroots in your projects"
83 self.text = ("You have been notified because you are an admin of projects,"
84 "that have some builds in outdated chroots\n\n"
85
86 "According to the 'Copr outdated chroots removal policy'\n"
87 "https://docs.pagure.org/copr.copr/copr_outdated_chroots_removal_policy.html\n"
88 "data are going to be preserved {0} days after the chroot is EOL"
89 "and then automatically deleted, unless you decide to prolong the expiration period.\n\n"
90
91 "Please, visit the projects settings if you want to extend the time.\n\n"
92 .format(app.config["DELETE_EOL_CHROOTS_AFTER"]))
93
94 if not copr_chroots:
95 raise AttributeError("No outdated chroots to notify about")
96
97 for chroot in copr_chroots:
98 self.text += (
99 "Project: {0}\n"
100 "Chroot: {1}\n"
101 "Remaining: {2} days\n"
102 "{3}\n\n".format(chroot.copr.full_name, chroot.name, chroot.delete_after_days,
103 helpers.copr_url('coprs_ns.copr_repositories', chroot.copr, _external=True)))
104
105
106 -def send_mail(recipient, message, sender=None):
107 """
108 :param str/list recipient: One recipient email as a string or multiple emails in a list
109 :param Message message:
110 :param str sender: Email of a sender
111 :return:
112 """
113 msg = MIMEText(message.text)
114 msg["Subject"] = message.subject
115 msg["From"] = sender or "root@{0}".format(platform.node())
116 msg["To"] = ", ".join(recipient) if type(recipient) == list else recipient
117 s = smtplib.SMTP("localhost")
118 s.sendmail("root@{0}".format(platform.node()), recipient, msg.as_string())
119 s.quit()
120