Changeset View
Standalone View
swh/web/browse/views/snapshot.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from django.shortcuts import redirect | from django.shortcuts import redirect | ||||
from swh.web.browse.browseurls import browse_route | from swh.web.browse.browseurls import browse_route | ||||
from swh.web.browse.snapshot_context import ( | from swh.web.browse.snapshot_context import ( | ||||
browse_snapshot_branches, | browse_snapshot_branches, | ||||
browse_snapshot_content, | browse_snapshot_content, | ||||
browse_snapshot_directory, | browse_snapshot_directory, | ||||
browse_snapshot_log, | browse_snapshot_log, | ||||
browse_snapshot_releases, | browse_snapshot_releases, | ||||
get_snapshot_context, | |||||
) | ) | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
def get_snapshot_from_request(request): | |||||
anlambert: `request.GET` is already a [[ https://docs.djangoproject.com/fr/3.2/ref/request… | |||||
args = {key: value for key, value in request.GET.items()} | |||||
snapshot = args.get("snapshot") or args.get("snapshot_id") | |||||
if snapshot: | |||||
return snapshot | |||||
Done Inline ActionsIf origin_url is not provided, get_snapshot_context will fail so you should raise a BadInputExc in that case to get a 400 error page. anlambert: If `origin_url` is not provided, `get_snapshot_context` will fail so you should raise a… | |||||
return get_snapshot_context(**args)["snapshot_id"] | |||||
Done Inline ActionsWe need a test for this. anlambert: We need a test for this. | |||||
Done Inline ActionsThis call will fail if the request.GET dict contains parameters unexpected by the get_snapshot_context function. anlambert: This call will fail if the `request.GET` dict contains parameters unexpected by the… | |||||
@browse_route( | @browse_route( | ||||
r"snapshot/(?P<snapshot_id>[0-9a-f]+)/", | r"snapshot/(?P<snapshot_id>[0-9a-f]+)/", | ||||
view_name="browse-snapshot", | view_name="browse-snapshot", | ||||
checksum_args=["snapshot_id"], | checksum_args=["snapshot_id"], | ||||
) | ) | ||||
def snapshot_browse(request, snapshot_id): | def snapshot_browse(request, snapshot_id): | ||||
"""Django view for browsing the content of a snapshot. | """Django view for browsing the content of a snapshot. | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | return browse_snapshot_content( | ||||
request, | request, | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
path=path, | path=path, | ||||
selected_language=request.GET.get("language"), | selected_language=request.GET.get("language"), | ||||
) | ) | ||||
@browse_route( | @browse_route( | ||||
r"snapshot/(?P<snapshot_id>[0-9a-f]+)/log/", | r"snapshot/(?P<snapshot_id>[0-9a-f]+)/log/", | ||||
Done Inline ActionsPlease use r"snapshot/log/ instead. anlambert: Please use `r"snapshot/log/` instead. | |||||
r"snapshot-log/", | |||||
view_name="browse-snapshot-log", | view_name="browse-snapshot-log", | ||||
checksum_args=["snapshot_id"], | checksum_args=["snapshot_id"], | ||||
) | ) | ||||
def snapshot_log_browse(request, snapshot_id): | def snapshot_log_browse(request, snapshot_id=None): | ||||
"""Django view that produces an HTML display of revisions history (aka | """Django view that produces an HTML display of revisions history (aka | ||||
the commit log) collected in a snapshot. | the commit log) collected in a snapshot. | ||||
The url that points to it is | The url that points to it is | ||||
Done Inline ActionsThe urls that point to it are :http:get:`/browse/snapshot/log` and
anlambert: %%%The urls that point to it are :http:get:`/browse/snapshot/log` and
%%% | |||||
:http:get:`/browse/snapshot/(snapshot_id)/log/` | :http:get:`/browse/snapshot/(snapshot_id)/log/` | ||||
""" | """ | ||||
Done Inline Actionsif snasphot_id is None anlambert: `if snasphot_id is None` | |||||
if not snapshot_id: | |||||
# This case happens when redirected from /origin/log | |||||
snapshot_id = get_snapshot_from_request(request) | |||||
return browse_snapshot_log(request, snapshot_id=snapshot_id) | return browse_snapshot_log(request, snapshot_id=snapshot_id) | ||||
Done Inline Actionsorigin_url and timestamp query parameters must be passed to browse_snapshot_log to anlambert: `origin_url` and `timestamp` query parameters must be passed to `browse_snapshot_log` to
ensure… | |||||
Done Inline ActionsCould you do a redirection to the same view with the snapshot argument instead ? anlambert: Could you do a redirection to the same view with the snapshot argument instead ?
This way, the… | |||||
Done Inline ActionsThat view is not covered by tests, we must add some in a new tests/browse/views/test_snapshot.py file. anlambert: That view is not covered by tests, we must add some in a new `tests/browse/views/test_snapshot. | |||||
@browse_route( | @browse_route( | ||||
r"snapshot/(?P<snapshot_id>[0-9a-f]+)/branches/", | r"snapshot/(?P<snapshot_id>[0-9a-f]+)/branches/", | ||||
r"snapshot-branches/", | |||||
view_name="browse-snapshot-branches", | view_name="browse-snapshot-branches", | ||||
checksum_args=["snapshot_id"], | checksum_args=["snapshot_id"], | ||||
) | ) | ||||
def snapshot_branches_browse(request, snapshot_id): | def snapshot_branches_browse(request, snapshot_id=None): | ||||
"""Django view that produces an HTML display of the list of releases | """Django view that produces an HTML display of the list of releases | ||||
collected in a snapshot. | collected in a snapshot. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/snapshot/(snapshot_id)/branches/` | :http:get:`/browse/snapshot/(snapshot_id)/branches/` | ||||
""" | """ | ||||
if not snapshot_id: | |||||
# This case happens when redirected from /origin/branches | |||||
snapshot_id = get_snapshot_from_request(request) | |||||
return browse_snapshot_branches(request, snapshot_id=snapshot_id) | return browse_snapshot_branches(request, snapshot_id=snapshot_id) | ||||
@browse_route( | @browse_route( | ||||
r"snapshot/(?P<snapshot_id>[0-9a-f]+)/releases/", | r"snapshot/(?P<snapshot_id>[0-9a-f]+)/releases/", | ||||
view_name="browse-snapshot-releases", | view_name="browse-snapshot-releases", | ||||
checksum_args=["snapshot_id"], | checksum_args=["snapshot_id"], | ||||
) | ) | ||||
def snapshot_releases_browse(request, snapshot_id): | def snapshot_releases_browse(request, snapshot_id): | ||||
"""Django view that produces an HTML display of the list of releases | """Django view that produces an HTML display of the list of releases | ||||
collected in a snapshot. | collected in a snapshot. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/snapshot/(snapshot_id)/releases/` | :http:get:`/browse/snapshot/(snapshot_id)/releases/` | ||||
""" | """ | ||||
return browse_snapshot_releases(request, snapshot_id=snapshot_id) | return browse_snapshot_releases(request, snapshot_id=snapshot_id) |
request.GET is already a dictionary so that instruction is not needed.pt