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

Add cursor to updates RSS feed. #9155

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api-reference/feeds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Available at https://pypi.org/rss/updates.xml, this feed provides the latest
newly created releases for individual projects on PyPI, including the project
name and description, release version, and a link to the release page.

Accepts an optional ``after`` integer cursor, which limits results to the
releases after the given seconds since the UTC epoch (e.g., the
``timestamp`` method to a ``datetime.datetime`` object). Note that this changes
the ordering of the results from newest->oldest to oldest->newest.


Project Releases Feed
---------------------
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/rss/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def test_rss_updates(db_request):
}
assert db_request.response.content_type == "text/xml"

after_cursor = datetime.datetime(
release1.created.year, release1.created.month, release1.created.day
)
db_request.params["after"] = after_cursor.timestamp()
assert rss.rss_updates(db_request) == {
"latest_releases": tuple(zip((release2, release3), ("[email protected]", None)))
}
assert db_request.response.content_type == "text/xml"


def test_rss_packages(db_request):
db_request.find_service = pretend.call_recorder(
Expand Down
33 changes: 26 additions & 7 deletions warehouse/rss/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
from email.utils import getaddresses

from pyramid.httpexceptions import HTTPBadRequest
from pyramid.view import view_config
from sqlalchemy.orm import joinedload

Expand Down Expand Up @@ -59,17 +61,34 @@ def _format_author(release):
],
)
def rss_updates(request):

after_timestamp = request.params.get("after")
if after_timestamp:
try:
after_timestamp = datetime.utcfromtimestamp(int(after_timestamp))
except ValueError:
raise HTTPBadRequest("'after' must be an integer") from None
latest_releases = (
request.db.query(Release)
.options(joinedload(Release.project))
.filter(Release.created > after_timestamp)
.order_by(Release.created.asc())
.limit(100)
.all()
)
else:
latest_releases = (
request.db.query(Release)
.options(joinedload(Release.project))
.order_by(Release.created.desc())
.limit(100)
.all()
)

request.response.content_type = "text/xml"

request.find_service(name="csp").merge(XML_CSP)

latest_releases = (
request.db.query(Release)
.options(joinedload(Release.project))
.order_by(Release.created.desc())
.limit(40)
.all()
)
release_authors = [_format_author(release) for release in latest_releases]

return {"latest_releases": tuple(zip(latest_releases, release_authors))}
Expand Down