Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/utils/snapshot_context.py
# Copyright (C) 2018 The Software Heritage developers | # Copyright (C) 2018 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 | ||||
# Utility module implementing Django views for browsing the archive | # Utility module implementing Django views for browsing the archive | ||||
# in a snapshot context. | # in a snapshot context. | ||||
# Its purpose is to factorize code for the views reachable from the | # Its purpose is to factorize code for the views reachable from the | ||||
# /origin/.* and /snapshot/.* endpoints. | # /origin/.* and /snapshot/.* endpoints. | ||||
from django.shortcuts import render, redirect | from django.shortcuts import render | ||||
from django.template.defaultfilters import filesizeformat | from django.template.defaultfilters import filesizeformat | ||||
from swh.model.identifiers import snapshot_identifier | from swh.model.identifiers import snapshot_identifier | ||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
get_snapshot_context, get_directory_entries, gen_directory_link, | get_snapshot_context, get_directory_entries, gen_directory_link, | ||||
gen_revision_link, request_content, gen_content_link, | gen_revision_link, request_content, gen_content_link, | ||||
prepare_content_for_display, content_display_max_size, | prepare_content_for_display, content_display_max_size, | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | try: | ||||
origin_type, origin_url, | origin_type, origin_url, | ||||
timestamp, path, | timestamp, path, | ||||
browse_context='directory') # noqa | browse_context='directory') # noqa | ||||
root_sha1_git = snapshot_context['root_sha1_git'] | root_sha1_git = snapshot_context['root_sha1_git'] | ||||
sha1_git = root_sha1_git | sha1_git = root_sha1_git | ||||
if root_sha1_git and path: | if root_sha1_git and path: | ||||
dir_info = service.lookup_directory_with_path(root_sha1_git, path) | dir_info = service.lookup_directory_with_path(root_sha1_git, path) | ||||
# some readme files can reference assets reachable from the | |||||
# browsed directory, handle that special case in order to | |||||
# correctly displayed them | |||||
if dir_info and dir_info['type'] == 'file': | |||||
file_raw_url = reverse( | |||||
'browse-content-raw', | |||||
url_args={'query_string': dir_info['checksums']['sha1']}) | |||||
return redirect(file_raw_url) | |||||
sha1_git = dir_info['target'] | sha1_git = dir_info['target'] | ||||
dirs = [] | dirs = [] | ||||
files = [] | files = [] | ||||
if sha1_git: | if sha1_git: | ||||
dirs, files = get_directory_entries(sha1_git) | dirs, files = get_directory_entries(sha1_git) | ||||
except Exception as exc: | except Exception as exc: | ||||
▲ Show 20 Lines • Show All 661 Lines • Show Last 20 Lines |