Skip to content

Commit f82fdec

Browse files
diMVrachev
authored andcommitted
Add email notication on package/release removal
Until now, where there are multiple contributors on a single the project, if one of them deletes a release or the whole project the other contributors don't get any notification, which is problematic. Connected with issue #5714. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 90111e4 commit f82fdec

File tree

8 files changed

+283
-0
lines changed

8 files changed

+283
-0
lines changed

warehouse/email/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,40 @@ def send_added_as_collaborator_email(request, user, *, submitter, project_name,
201201
return {"project": project_name, "submitter": submitter.username, "role": role}
202202

203203

204+
@_email("removed-package")
205+
def send_removed_package_email(
206+
request, user, *, project_name, submitter_name, submitter_role, recipient_role
207+
):
208+
recipient_role_descr = "an owner"
209+
if recipient_role == "Maintainer":
210+
recipient_role_descr = "a maintainer"
211+
212+
return {
213+
"project": project_name,
214+
"submitter": submitter_name,
215+
"submitter_role": submitter_role,
216+
"recipient_role_descr": recipient_role_descr,
217+
}
218+
219+
220+
@_email("removed-package-release")
221+
def send_removed_package_release_email(
222+
request, user, *, project_name, release, release_date, submitter_name, submitter_role, recipient_role
223+
):
224+
recipient_role_descr = "an owner"
225+
if recipient_role == "Maintainer":
226+
recipient_role_descr = "a maintainer"
227+
228+
return {
229+
"project": project_name,
230+
"release": release,
231+
"release_date": release_date,
232+
"submitter": submitter_name,
233+
"submitter_role": submitter_role,
234+
"recipient_role_descr": recipient_role_descr,
235+
}
236+
237+
204238
def includeme(config):
205239
email_sending_class = config.maybe_dotted(config.registry.settings["mail.backend"])
206240
config.register_service_factory(email_sending_class.create_service, IEmailSender)

warehouse/manage/views.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
send_email_verification_email,
3939
send_password_change_email,
4040
send_primary_email_change_email,
41+
send_removed_package_email,
42+
send_removed_package_release_email,
4143
)
4244
from warehouse.i18n import localize as _
4345
from warehouse.macaroons.interfaces import IMacaroonService
@@ -799,6 +801,40 @@ def manage_project_settings(project, request):
799801
return {"project": project}
800802

801803

804+
def get_project_contributors(project, request):
805+
query_res = (
806+
request.db.query(Project)
807+
.join(User, Project.users)
808+
.filter(Project.name == project.name)
809+
.one()
810+
)
811+
return query_res.users
812+
813+
814+
def get_user_role_in_project(project, request):
815+
query_res = (
816+
request.db.query(Project)
817+
.join(User, Project.users)
818+
.filter(User.username == request.user.username, Project.name == project.name)
819+
.with_entities(Role.role_name)
820+
.distinct(Role.role_name)
821+
.all()
822+
)
823+
824+
user_role = ""
825+
# This check is needed because of
826+
# issue https://github.com/pypa/warehouse/issues/2745
827+
# which is not yet resolved and a user could be an owner
828+
# and a maintainer at the same time
829+
if len(query_res) == 2 and (
830+
query_res[0].role_name == "Owner" or query_res[1].role_name == "Owner"
831+
):
832+
user_role = "Owner"
833+
else:
834+
user_role = "Maintainer"
835+
return user_role
836+
837+
802838
@view_config(
803839
route_name="manage.project.delete_project",
804840
context=Project,
@@ -823,6 +859,21 @@ def delete_project(project, request):
823859
confirm_project(project, request, fail_route="manage.project.settings")
824860
remove_project(project, request)
825861

862+
submitter_role = get_user_role_in_project(project, request)
863+
contributors = get_project_contributors(project, request)
864+
865+
for contributor in contributors:
866+
contributor_role = get_user_role_in_project(project, request)
867+
868+
send_removed_package_email(
869+
request,
870+
contributor,
871+
project_name=project.name,
872+
submitter_name=request.user.username,
873+
submitter_role=submitter_role,
874+
recipient_role=contributor_role,
875+
)
876+
826877
return HTTPSeeOther(request.route_path("manage.projects"))
827878

828879

@@ -978,6 +1029,23 @@ def delete_project_release(self):
9781029
f"Deleted release {self.release.version!r}", queue="success"
9791030
)
9801031

1032+
submitter_role = get_user_role_in_project(self.release.project, self.request)
1033+
contributors = get_project_contributors(self.release.project, self.request)
1034+
1035+
for contributor in contributors:
1036+
contributor_role = get_user_role_in_project(self.release.project, self.request)
1037+
1038+
send_removed_package_release_email(
1039+
self.request,
1040+
contributor,
1041+
project_name=self.release.project.name,
1042+
release=self.release.version,
1043+
release_date=self.release.created.strftime("%b %d %Y"),
1044+
submitter_name=self.request.user.username,
1045+
submitter_role=submitter_role,
1046+
recipient_role=contributor_role,
1047+
)
1048+
9811049
return HTTPSeeOther(
9821050
self.request.route_path(
9831051
"manage.project.releases", project_name=self.release.project.name
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
{% extends "email/_base/body.html" %}
15+
16+
{% block extra_style %}
17+
ul.collaborator-details {
18+
list-style-type: none;
19+
}
20+
{% endblock %}
21+
22+
{% block content %}
23+
<p>
24+
<ul class="removed-package-release">
25+
<li>{% trans project=project, release=release, date=release_date %}The {{ project }} release {{ release }} released on {{ date }} has been deleted.{% endtrans %}</li>
26+
<li>{% trans submitter=submitter, role=submitter_role %}<strong>Deleted by:</strong> {{ submitter }} with a role:
27+
{{ role }}.{% endtrans %}
28+
</li>
29+
</ul>
30+
</p>
31+
32+
<p>{% trans href='mailto:[email protected]', email_address='[email protected]' %}If this was a mistake, you can email <a
33+
href="{{ href }}">{{ email_address }}</a> to communicate with the PyPI administrators.{% endtrans %}</p>
34+
{% endblock %}
35+
36+
{% block reason %}
37+
38+
{% trans %}
39+
You are receiving this because you are {{ recipient_role_descr }} of this package.
40+
{% endtrans %}
41+
42+
{% endblock %}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
15+
{% extends "email/_base/body.txt" %}
16+
17+
{% block content %}
18+
{% trans project=project, release=release, date=release_date %}The {{ project }} release {{ release }} released on {{ date }} has been deleted.{% endtrans %}
19+
20+
{% trans submitter=submitter, role=submitter_role %}Deleted by: {{ submitter }} with a role: {{ role }}.{% endtrans %}
21+
22+
{% trans email_address='[email protected]' %}If this was a mistake, you can email {{ email_address }} to communicate with the PyPI administrators.{% endtrans %}
23+
{% endblock %}
24+
25+
{% block reason %}
26+
{% trans %}
27+
You are receiving this because you are {{ recipient_role_descr }} of this package.
28+
{% endtrans %}
29+
{% endblock %}
30+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
15+
{% extends "email/_base/subject.txt" %}
16+
17+
{% block content %}
18+
{% trans project=project, release=release %}The {{ project }} release {{ release }} has been deleted.{% endtrans %}{% endblock %}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
{% extends "email/_base/body.html" %}
15+
16+
{% block extra_style %}
17+
ul.collaborator-details {
18+
list-style-type: none;
19+
}
20+
{% endblock %}
21+
22+
{% block content %}
23+
<p>
24+
<ul class="removed-package-details">
25+
<li>{% trans project=project %}The package {{ project }} has been deleted.{% endtrans %}</li>
26+
<li>{% trans submitter=submitter, role=submitter_role %}<strong>Deleted by:</strong> {{ submitter }} with a role:
27+
{{ role }}.{% endtrans %}
28+
</li>
29+
</ul>
30+
</p>
31+
32+
<p>{% trans href='mailto:[email protected]', email_address='[email protected]' %}If this was a mistake, you can email <a
33+
href="{{ href }}">{{ email_address }}</a> to communicate with the PyPI administrators.{% endtrans %}</p>
34+
{% endblock %}
35+
36+
{% block reason %}
37+
38+
{% trans %}
39+
You are receiving this because you are {{ recipient_role_descr }} of this package.
40+
{% endtrans %}
41+
42+
{% endblock %}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
15+
{% extends "email/_base/body.txt" %}
16+
17+
{% block content %}
18+
{% trans project=project %}The package {{ project }} has been deleted.{% endtrans %}
19+
20+
{% trans submitter=submitter, role=submitter_role %}Deleted by: {{ submitter }} with a role: {{ role }}.{% endtrans %}
21+
22+
{% trans email_address='[email protected]' %}If this was a mistake, you can email {{ email_address }} to communicate with the PyPI administrators.{% endtrans %}
23+
{% endblock %}
24+
25+
{% block reason %}
26+
{% trans %}
27+
You are receiving this because you are {{ recipient_role_descr }} of this package.
28+
{% endtrans %}
29+
{% endblock %}
30+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
-#}
14+
15+
{% extends "email/_base/subject.txt" %}
16+
17+
{% block content %}
18+
{% trans project=project %}The package {{ project }} has been deleted.{% endtrans %}
19+
{% endblock %}

0 commit comments

Comments
 (0)