Skip to content

Commit 9e9bab6

Browse files
committed
Use a SQLAlchemy event
1 parent 07fcf1d commit 9e9bab6

File tree

7 files changed

+58
-52
lines changed

7 files changed

+58
-52
lines changed

tests/unit/packaging/test_views.py

+37-39
Original file line numberDiff line numberDiff line change
@@ -191,53 +191,51 @@ def test_normalizing_version_redirects(self, db_request):
191191
def test_detail_rendered(self, db_request):
192192
users = [UserFactory.create(), UserFactory.create(), UserFactory.create()]
193193
project = ProjectFactory.create()
194-
releases = (
195-
[
196-
ReleaseFactory.create(
197-
project=project,
198-
version=v,
199-
description=DescriptionFactory.create(
200-
raw="unrendered description",
201-
html="rendered description",
202-
content_type="text/html",
203-
),
204-
)
205-
for v in ["1.0", "2.0", "3.0", "4.0.dev0"]
206-
]
207-
+ [
208-
ReleaseFactory.create(
209-
project=project,
210-
version="5.0",
211-
description=DescriptionFactory.create(
212-
raw="plaintext description",
213-
html="",
214-
content_type="text/plain",
215-
),
216-
yanked=True,
217-
yanked_reason="plaintext yanked reason",
218-
)
219-
]
220-
+ [
221-
ReleaseFactory.create(
222-
project=project,
223-
version="5.1",
224-
description=DescriptionFactory.create(
225-
raw="unrendered description",
226-
html="rendered description",
227-
content_type="text/html",
228-
),
229-
published=False,
230-
)
231-
]
194+
releases = [
195+
ReleaseFactory.create(
196+
project=project,
197+
version=v,
198+
description=DescriptionFactory.create(
199+
raw="unrendered description",
200+
html="rendered description",
201+
content_type="text/html",
202+
),
203+
)
204+
for v in ["1.0", "2.0", "3.0", "4.0.dev0"]
205+
] + [
206+
ReleaseFactory.create(
207+
project=project,
208+
version="5.0",
209+
description=DescriptionFactory.create(
210+
raw="plaintext description",
211+
html="",
212+
content_type="text/plain",
213+
),
214+
yanked=True,
215+
yanked_reason="plaintext yanked reason",
216+
)
217+
]
218+
219+
# Add an unpublished version
220+
staged_release = ReleaseFactory.create(
221+
project=project,
222+
version="5.1",
223+
description=DescriptionFactory.create(
224+
raw="unrendered description",
225+
html="rendered description",
226+
content_type="text/html",
227+
),
228+
published=False,
232229
)
230+
233231
files = [
234232
FileFactory.create(
235233
release=r,
236234
filename=f"{project.name}-{r.version}.tar.gz",
237235
python_version="source",
238236
packagetype="sdist",
239237
)
240-
for r in releases
238+
for r in releases + [staged_release]
241239
]
242240

243241
# Create a role for each user

warehouse/legacy/api/json.py

-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def _json_data(request, project, release, *, all_releases):
6464
.outerjoin(File)
6565
.filter(
6666
Release.project == project,
67-
Release.published.is_(True),
6867
)
6968
)
7069

@@ -210,7 +209,6 @@ def latest_release_factory(request):
210209
Project.lifecycle_status.is_distinct_from(
211210
LifecycleStatus.QuarantineEnter
212211
),
213-
Release.published.is_(True),
214212
)
215213
.order_by(
216214
Release.yanked.asc(),

warehouse/locale/messages.pot

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ msgstr ""
776776
msgid "Provide an Inspector link to specific lines of code."
777777
msgstr ""
778778

779-
#: warehouse/packaging/views.py:355
779+
#: warehouse/packaging/views.py:352
780780
msgid "Your report has been recorded. Thank you for your help."
781781
msgstr ""
782782

warehouse/packaging/models.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252
from sqlalchemy.ext.hybrid import hybrid_property
5353
from sqlalchemy.orm import (
5454
Mapped,
55+
ORMExecuteState,
5556
attribute_keyed_dict,
5657
declared_attr,
5758
mapped_column,
5859
validates,
60+
with_loader_criteria,
5961
)
6062
from urllib3.exceptions import LocationParseError
6163
from urllib3.util import parse_url
@@ -469,7 +471,6 @@ def latest_version(self):
469471
.filter(
470472
Release.project == self,
471473
Release.yanked.is_(False),
472-
Release.published.is_(True),
473474
)
474475
.order_by(Release.is_prerelease.nullslast(), Release._pypi_ordering.desc())
475476
.first()
@@ -628,7 +629,7 @@ def __table_args__(cls): # noqa
628629
_pypi_ordering: Mapped[int | None]
629630
requires_python: Mapped[str | None] = mapped_column(Text)
630631
created: Mapped[datetime_now] = mapped_column()
631-
published: Mapped[bool_true]
632+
published: Mapped[bool_true] = mapped_column()
632633

633634
description_id: Mapped[UUID] = mapped_column(
634635
ForeignKey("release_descriptions.id", onupdate="CASCADE", ondelete="CASCADE"),
@@ -1046,6 +1047,21 @@ def ensure_monotonic_journals(config, session, flush_context, instances):
10461047
return
10471048

10481049

1050+
@db.listens_for(db.Session, "do_orm_execute")
1051+
def filter_staged_release(config, state: ORMExecuteState):
1052+
if (
1053+
state.is_select
1054+
and not state.is_column_load
1055+
and not state.is_relationship_load
1056+
and not state.statement.get_execution_options().get("include_staged", False)
1057+
):
1058+
state.statement = state.statement.options(
1059+
with_loader_criteria(
1060+
Release, lambda cls: cls.published, include_aliases=True
1061+
)
1062+
)
1063+
1064+
10491065
class ProhibitedProjectName(db.Model):
10501066
__tablename__ = "prohibited_project_names"
10511067
__table_args__ = (

warehouse/packaging/utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def _simple_detail(project, request):
5757
.join(Project)
5858
.filter(
5959
Project.lifecycle_status.is_distinct_from(LifecycleStatus.QuarantineEnter),
60-
Release.published.is_(True),
6160
)
6261
.all(),
6362
key=lambda f: (packaging_legacy.version.parse(f.release.version), f.filename),

warehouse/packaging/views.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ def project_detail(project, request):
179179
try:
180180
release = (
181181
request.db.query(Release)
182-
.filter(
183-
Release.project == project,
184-
Release.published.is_(True),
185-
)
182+
.filter(Release.project == project)
186183
.order_by(
187184
Release.yanked,
188185
Release.is_prerelease.nullslast(),

warehouse/search/tasks.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
def _project_docs(db, project_name=None):
4343
releases_list = (
4444
select(Release.id)
45-
.filter(
46-
Release.yanked.is_(False), Release.published.is_(True), Release.files.any()
47-
)
45+
.filter(Release.yanked.is_(False), Release.files.any())
4846
.order_by(
4947
Release.project_id,
5048
Release.is_prerelease.nullslast(),

0 commit comments

Comments
 (0)