Repository: pypi/warehouse

Issue:
Project deletion raises an error
Deleting a project in an organization leads to an error-

- Click on 'Your Project'
- Select a project and click on 'Manage'
- Click on 'Settings'
- Click on 'Delete project'
- Enter the project name and click on 'Delete project'

[screencapture-c7f2-84-64-37-99-eu-ngrok-io-manage-project-tox-parallel-delete-project-2022-08-08-20_21_44.pdf](https://github.com/pypi/warehouse/files/9285717/screencapture-c7f2-84-64-37-99-eu-ngrok-io-manage-project-tox-parallel-delete-project-2022-08-08-20_21_44.pdf)




Discussion:

Resolving patch:
diff --git a/warehouse/manage/views.py b/warehouse/manage/views.py
--- a/warehouse/manage/views.py
+++ b/warehouse/manage/views.py
@@ -133,6 +133,7 @@
 from warehouse.organizations.models import (
     Organization,
     OrganizationInvitationStatus,
+    OrganizationProject,
     OrganizationRole,
     OrganizationRoleType,
     OrganizationType,
@@ -3340,12 +3341,49 @@ def transfer_organization_project(project, request):
 
 
 def get_user_role_in_project(project, user, request):
-    return (
-        request.db.query(Role)
-        .filter(Role.user == user, Role.project == project)
-        .one()
-        .role_name
-    )
+    try:
+        return (
+            request.db.query(Role)
+            .filter(Role.user == user, Role.project == project)
+            .one()
+            .role_name
+        )
+    except NoResultFound:
+        # No project role found so check for Organization roles
+        return get_user_role_in_organization_project(project, user, request)
+
+
+def get_user_role_in_organization_project(project, user, request):
+    try:
+        # If this is an organzation project check to see if user is Org Owner
+        role_name = (
+            request.db.query(OrganizationRole)
+            .join(
+                OrganizationProject,
+                OrganizationProject.organization_id == OrganizationRole.organization_id,
+            )
+            .filter(
+                OrganizationRole.user == user,
+                OrganizationProject.project == project,
+                OrganizationRole.role_name == OrganizationRoleType.Owner,
+            )
+            .one()
+            .role_name
+        )
+    except NoResultFound:
+        # Last but not least check if this is a Team Project and user has a team role
+        role_name = (
+            request.db.query(TeamProjectRole)
+            .join(TeamRole, TeamRole.team_id == TeamProjectRole.team_id)
+            .filter(
+                TeamRole.user == user,
+                TeamProjectRole.project == project,
+            )
+            .one()
+            .role_name
+        )
+
+    return role_name.value
 
 
 @view_config(


Test patch:
diff --git a/tests/unit/manage/test_views.py b/tests/unit/manage/test_views.py
--- a/tests/unit/manage/test_views.py
+++ b/tests/unit/manage/test_views.py
@@ -6676,6 +6676,45 @@ def test_get_user_role_in_project_single_role_maintainer(self, db_request):
         res = views.get_user_role_in_project(project, db_request.user, db_request)
         assert res == "Maintainer"
 
+    def test_get_user_role_in_project_org_owner(self, db_request):
+        organization = OrganizationFactory.create(name="baz")
+        project = ProjectFactory.create(name="foo")
+        OrganizationProjectFactory.create(organization=organization, project=project)
+        db_request.user = UserFactory.create()
+        OrganizationRoleFactory.create(
+            organization=organization, user=db_request.user, role_name="Owner"
+        )
+        db_request.session = pretend.stub(
+            flash=pretend.call_recorder(lambda *a, **kw: None),
+        )
+
+        res = views.get_user_role_in_project(project, db_request.user, db_request)
+        assert res == "Owner"
+
+    def test_get_user_role_in_project_team_project_owner(self, db_request):
+        organization = OrganizationFactory.create(name="baz")
+        team = TeamFactory(organization=organization)
+        project = ProjectFactory.create(name="foo")
+        OrganizationProjectFactory.create(organization=organization, project=project)
+        db_request.user = UserFactory.create()
+        OrganizationRoleFactory.create(
+            organization=organization,
+            user=db_request.user,
+            role_name=OrganizationRoleType.Member,
+        )
+        TeamRoleFactory.create(team=team, user=db_request.user)
+        TeamProjectRoleFactory.create(
+            team=team,
+            project=project,
+            role_name=TeamProjectRoleType.Owner,
+        )
+        db_request.session = pretend.stub(
+            flash=pretend.call_recorder(lambda *a, **kw: None),
+        )
+
+        res = views.get_user_role_in_project(project, db_request.user, db_request)
+        assert res == "Owner"
+
     def test_delete_project(self, monkeypatch, db_request):
         project = ProjectFactory.create(name="foo")