Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profile: add archived projects section #17571

Merged
merged 10 commits into from
Feb 12, 2025
22 changes: 22 additions & 0 deletions tests/functional/test_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from http import HTTPStatus

from tests.common.db.accounts import UserFactory
from tests.common.db.packaging import ProjectFactory, ReleaseFactory, RoleFactory
from warehouse.packaging.models import LifecycleStatus


def test_user_profile(webtest):
Expand All @@ -30,3 +32,23 @@ def test_user_profile(webtest):
# ...and verify that the user's profile page exists
resp = webtest.get(f"/user/{user.username}/")
assert resp.status_code == HTTPStatus.OK


def test_user_profile_project_states(webtest):
user = UserFactory.create()

# Create some live projects
projects = ProjectFactory.create_batch(3)
for project in projects:
RoleFactory.create(user=user, project=project)
ReleaseFactory.create(project=project)

# Create an archived project
archived_project = ProjectFactory.create(lifecycle_status=LifecycleStatus.Archived)
RoleFactory.create(user=user, project=archived_project)
ReleaseFactory.create(project=archived_project)

resp = webtest.get(f"/user/{user.username}/")

assert resp.status_code == HTTPStatus.OK
assert "4 projects" in resp.html.h2.text
30 changes: 24 additions & 6 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ def test_user_redirects_username(self, db_request):

def test_returns_user(self, db_request):
user = UserFactory.create()
assert views.profile(user, db_request) == {"user": user, "projects": []}
assert views.profile(user, db_request) == {
"user": user,
"live_projects": [],
"archived_projects": [],
}

def test_user_profile_queries_once_for_all_projects(
self, db_request, query_recorder
Expand Down Expand Up @@ -179,10 +183,27 @@ def test_user_profile_queries_once_for_all_projects(
response = views.profile(user, db_request)

assert response["user"] == user
assert len(response["projects"]) == 3
assert len(response["live_projects"]) == 3
# Two queries, one for the user (via context), one for their projects
assert len(query_recorder.queries) == 2

def test_returns_archived_projects(self, db_request):
user = UserFactory.create()

projects = ProjectFactory.create_batch(3)
for project in projects:
RoleFactory.create(user=user, project=project)
ReleaseFactory.create(project=project)

archived_project = ProjectFactory.create(lifecycle_status="archived")
RoleFactory.create(user=user, project=archived_project)
ReleaseFactory.create(project=archived_project)

resp = views.profile(user, db_request)

assert len(resp["live_projects"]) == 3
assert len(resp["archived_projects"]) == 1


class TestAccountsSearch:
def test_unauthenticated_raises_401(self):
Expand Down Expand Up @@ -4039,10 +4060,7 @@ def test_add_pending_github_oidc_publisher_too_many_already(
]
assert db_request.session.flash.calls == [
pretend.call(
(
"You can't register more than 3 pending trusted "
"publishers at once."
),
"You can't register more than 3 pending trusted publishers at once.",
queue="error",
)
]
Expand Down
26 changes: 19 additions & 7 deletions warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
from warehouse.packaging.interfaces import IProjectService
from warehouse.packaging.models import (
JournalEntry,
LifecycleStatus,
Project,
Release,
Role,
Expand Down Expand Up @@ -185,6 +186,7 @@ def profile(user, request):
select(
Project.name,
Project.normalized_name,
Project.lifecycle_status,
Release.created,
Release.summary,
)
Expand All @@ -206,17 +208,28 @@ def profile(user, request):
)

# Construct the list of projects with their latest releases from query results
projects = [
{
archived_projects = []
live_projects = []

for row in request.db.execute(stmt):
project = {
"name": row.name,
"normalized_name": row.normalized_name,
"lifecycle_status": row.lifecycle_status,
"created": row.created,
"summary": row.summary,
}
for row in request.db.execute(stmt)
]

return {"user": user, "projects": projects}
if row.lifecycle_status == LifecycleStatus.Archived:
archived_projects.append(project)
miketheman marked this conversation as resolved.
Show resolved Hide resolved
else:
live_projects.append(project)

return {
"user": user,
"live_projects": live_projects,
"archived_projects": archived_projects,
}


@view_config(
Expand Down Expand Up @@ -1640,8 +1653,7 @@ def _add_pending_oidc_publisher(
if len(self.request.user.pending_oidc_publishers) >= 3:
self.request.session.flash(
self.request._(
"You can't register more than 3 pending trusted "
"publishers at once."
"You can't register more than 3 pending trusted publishers at once."
),
queue="error",
)
Expand Down
Loading