diff --git a/docs/uri-scheme.rst b/docs/uri-scheme.rst index cc67eadbe..f06574688 100644 --- a/docs/uri-scheme.rst +++ b/docs/uri-scheme.rst @@ -1,487 +1,475 @@ URI scheme for SWH Web applications =================================== SWH Browse Urls --------------- This web application aims to provide HTML views to easily navigate in the SWH archive, thus it needs to be reached from a web browser. If you intend to query the SWH archive programmatically through any HTTP client, please refer to the `SWH Web API URLs`_ instead. Content ^^^^^^^ -.. _browse_content: - .. http:get:: /browse/content/[(algo_hash):](hash)/ HTML view that displays a SWH content identified by its hash value. If the content to display is textual, it will be highlighted client-side if possible using highlightjs_. In order for that operation to be performed, a programming language must first be associated to the content. The following procedure is used in order to find the language: 1) First try to find a language from the content filename (provided as query parameter when navigating from a directory view). 2) If no language has been found from the filename, try to find one from the content mime type. The mime type is retrieved from the content metadata stored in the SWH archive or is computed server-side using Python magic module. When that view is called in the context of a navigation coming from a directory view, a breadcrumb will be displayed on top of the rendered content in order to easily navigate up to the associated root directory. In that case, the path query parameter will be used and filled with the path of the file relative to the root directory. :param algo_hash: optionnal parameter to indicate the algorithm used to compute the content checksum (default to *sha1*) :type algo_hash: a string identifying the hashing algorithm (either *sha1*, *sha1_git*, *sha256* or *blake2s256*) :param hash: the checksum from which to retrieve the associated content in the SWH archive :type hash: hexadecimal representation of the hash value :query path: optionnal parameter used to describe the path of the content relative to a root directory (used to add context aware navigation links when navigating from a directory view) :type path: string :statuscode 200: no error :statuscode 400: an invalid query string has been provided :statuscode 404: requested content can not be found in the SWH archive -.. _browse_content_raw: - .. http:get:: /browse/content/[(algo_hash):](hash)/raw/ HTML view that produces a raw display of a SWH content identified by its hash value. The behaviour of that view depends on the mime type of the requested content. If the mime type is from the text family, the view will return a response whose content type is 'text/plain' that will be rendered by the browser. Otherwise, the view will return a response whose content type is 'application/octet-stream' and the browser will then offer to download the file. In the context of a navigation coming from a directory view, the filename query parameter will be used in order to provide the real name of the file when one wants to save it locally. :param algo_hash: optionnal parameter to indicate the algorithm used to compute the content checksum (default to *sha1*) :type algo_hash: a string identifying the hashing algorithm (either *sha1*, *sha1_git*, *sha256* or *blake2s256*) :param hash: the checksum from which to retrieve the associated content in the SWH archive :type hash: hexadecimal representation of the hash value :query filename: optionnal parameter used to indicate the name of the file holding the requested content (used when one wants to save the content to a local file) :type path: string :statuscode 200: no error :statuscode 400: an invalid query string has been provided :statuscode 404: requested content can not be found in the SWH archive Directory ^^^^^^^^^ -.. _browse_directory: - .. http:get:: /browse/directory/(sha1_git)/ HTML view for browsing the content of a SWH directory identified by its `sha1_git` value. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the provided root directory to directories reachable from it in a recursive way. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. :param sha1_git: the `sha1_git` identifier of the directory to browse :type sha1_git: hexadecimal representation of that hash value :statuscode 200: no error :statuscode 400: an invalid `sha1_git` value has been provided :statuscode 404: requested directory can not be found in the SWH archive -.. http:get:: /browse/directory/(sha1_git)/(path) +.. http:get:: /browse/directory/(sha1_git)/(path)/ HTML view for browsing the content of a SWH directory reachable from the provided root one identified by its `sha1_git` value. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the requested directory to directories reachable from it in a recursive way but also up to the root directory. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. :param sha1_git: the `sha1_git` identifier of the directory to browse :type sha1_git: hexadecimal representation of that hash value :param path: path of a directory reachable from the provided root one :type path: string :statuscode 200: no error :statuscode 400: an invalid `sha1_git` value has been provided :statuscode 404: requested directory can not be found in the SWH archive Origin ^^^^^^ Origin metadata """"""""""""""" -.. _browse_origin: - .. http:get:: /browse/origin/(origin_id)/ HTML view that displays a SWH origin identified by its id. The view displays the origin metadata and contains links for browsing its directories and contents for each SWH visit. :param origin_id: the id of a SWH origin :type origin_id: int :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive .. http:get:: /browse/origin/(origin_type)/url/(origin_url)/ HTML view that displays a SWH origin identified by its type and url. The view displays the origin metadata and contains links for browsing its directories and contents for each SWH visit. :param origin_type: the type of the SWH origin (*git*, *svn*, ...) :type origin_type: string :param origin_url: the url of the origin (e.g. https://github.com///) :type origin_url: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive Origin directory """""""""""""""" -.. _browse_origin_directory: - .. http:get:: /browse/origin/(origin_id)/directory/ HTML view for browsing the content of the root directory associated to the latest visit of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the origin root directory to directories reachable from it in a recursive way. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive .. http:get:: /browse/origin/(origin_id)/directory/(path)/ HTML view for browsing the content of a directory reachable from the root directory associated to the latest visit of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the requested directory to directories reachable from it in a recursive way but also up to the origin root directory. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param path: path of a directory reachable from the origin root one :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive or the provided path does not exist from the origin root directory .. http:get:: /browse/origin/(origin_id)/visit/(visit_id)/directory/ HTML view for browsing the content of the root directory associated to a specific visit (identified by its id) of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the origin root directory to directories reachable from it in a recursive way. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param visit_id: the id of the origin visit :type visit_id: int :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive or requested visit id does not exist .. http:get:: /browse/origin/(origin_id)/visit/(visit_id)/directory/(path)/ HTML view for browsing the content of a directory reachable from the root directory associated to a specific visit (identified by its id) of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the requested directory to directories reachable from it in a recursive way but also up to the origin root directory. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param visit_id: the id of the origin visit :type visit_id: int :param path: path of a directory reachable from the origin root one :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive, requested visit id does not exist or the provided path does not exist from the origin root directory .. http:get:: /browse/origin/(origin_id)/ts/(ts)/directory/ HTML view for browsing the content of the root directory associated to a specific visit (identified by its timestamp) of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the origin root directory to directories reachable from it in a recursive way. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param ts: the timestamp of the origin visit :type ts: Unix timestamp :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive or requested visit timestamp does not exist .. http:get:: /browse/origin/(origin_id)/ts/(ts)/directory/(path)/ HTML view for browsing the content of a directory reachable from the root directory associated to a specific visit (identified by its timestamp) of a SWH origin. The content of the directory is first sorted in lexicographical order and the sub-directories are displayed before the regular files. The view enables to navigate from the requested directory to directories reachable from it in a recursive way but also up to the origin root directory. A breadcrumb located in the top part of the view allows to keep track of the paths navigated so far. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the directory content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param ts: the timestamp of the origin visit :type ts: Unix timestamp :param path: path of a directory reachable from the origin root one :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the directory :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive, requested visit timestamp does not exist or the provided path does not exist from the origin root directory Origin content """""""""""""" -.. _browse_origin_content: - .. http:get:: /browse/origin/(origin_id)/content/(path)/ HTML view that produces an HTML display of a SWH content associated to the latest visit of a SWH origin. If the content to display is textual, it will be highlighted client-side if possible using highlightjs_. In order for that operation to be performed, a programming language must first be associated to the content. The following procedure is used in order to find the language: 1) First try to find a language from the content filename 2) If no language has been found from the filename, try to find one from the content mime type. The mime type is retrieved from the content metadata stored in the SWH archive or is computed server-side using Python magic module. The view displays a breadcrumb on top of the rendered content in order to easily navigate up to the origin root directory. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param path: path of a content reachable from the origin root directory :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the content :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive, or the provided content path does not exist from the origin root directory .. http:get:: /browse/origin/(origin_id)/visit/(visit_id)/content/(path)/ HTML view that produces an HTML display of a SWH content associated to a specific visit (identified by its id) of a SWH origin. If the content to display is textual, it will be highlighted client-side if possible using highlightjs_. In order for that operation to be performed, a programming language must first be associated to the content. The following procedure is used in order to find the language: 1) First try to find a language from the content filename 2) If no language has been found from the filename, try to find one from the content mime type. The mime type is retrieved from the content metadata stored in the SWH archive or is computed server-side using Python magic module. The view displays a breadcrumb on top of the rendered content in order to easily navigate up to the origin root directory. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param visit_id: the id of the origin visit :type visit_id: int :param path: path of a content reachable from the origin root directory :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the content :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive, requested visit id does not exist or the provided content path does not exist from the origin root directory .. http:get:: /browse/origin/(origin_id)/ts/(ts)/content/(path)/ HTML view that produces an HTML display of a SWH content associated to a specific visit (identified by its timestamp) of a SWH origin. If the content to display is textual, it will be highlighted client-side if possible using highlightjs_. In order for that operation to be performed, a programming language must first be associated to the content. The following procedure is used in order to find the language: 1) First try to find a language from the content filename 2) If no language has been found from the filename, try to find one from the content mime type. The mime type is retrieved from the content metadata stored in the SWH archive or is computed server-side using Python magic module. The view displays a breadcrumb on top of the rendered content in order to easily navigate up to the origin root directory. The view also enables to easily switch between the origin branches through a dropdown menu. The origin branch (default to master) from which to retrieve the content can also be specified by using the branch query parameter. :param origin_id: the id of a SWH origin :type origin_id: int :param ts: the timestamp of the origin visit :type ts: Unix timestamp :param path: path of a content reachable from the origin root directory :type path: string :query branch: optional query parameter to specify the origin branch from which to retrieve the content :type branch: string :statuscode 200: no error :statuscode 404: requested origin can not be found in the SWH archive, requested visit timestamp does not exist or the provided content path does not exist from the origin root directory SWH Web API URLs ---------------- .. _highlightjs: https://highlightjs.org/ \ No newline at end of file diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py index d02dda56b..14d4603a9 100644 --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -1,131 +1,131 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from django.http import HttpResponse from django.shortcuts import render from swh.model.hashutil import hash_to_hex from swh.web.common import query from swh.web.common.utils import reverse from swh.web.common.exc import handle_view_exception from swh.web.browse.utils import ( gen_path_info, request_content, prepare_content_for_display ) def content_display(request, query_string): """Django view that produces an HTML display of a SWH content identified by its hash value. - See :ref:`Content browsing URI scheme ` for more details. + The url that points to it is :http:get:`/browse/content/[(algo_hash):](hash)/` Args: request: input django http request query_string: a string of the form "[ALGO_HASH:]HASH" where optional ALGO_HASH can be either *sha1*, *sha1_git*, *sha256*, or *blake2s256* (default to *sha1*) and HASH the hexadecimal representation of the hash value Returns: The HTML rendering of the requested content. - """ + """ # noqa try: algo, checksum = query.parse_hash(query_string) checksum = hash_to_hex(checksum) content_data, mime_type = request_content(query_string) except Exception as exc: return handle_view_exception(exc) path = request.GET.get('path', None) content_display_data = prepare_content_for_display(content_data, mime_type, path) root_dir = None filename = None path_info = None breadcrumbs = [] if path: split_path = path.split('/') root_dir = split_path[0] filename = split_path[-1] path = path.replace(root_dir + '/', '') path = path.replace(filename, '') path_info = gen_path_info(path) breadcrumbs.append({'name': root_dir[:7], 'url': reverse('browse-directory', kwargs={'sha1_git': root_dir})}) for pi in path_info: breadcrumbs.append({'name': pi['name'], 'url': reverse('browse-directory', kwargs={'sha1_git': root_dir, 'path': pi['path']})}) breadcrumbs.append({'name': filename, 'url': None}) query_params = None if filename: query_params = {'filename': filename} content_raw_url = reverse('browse-content-raw', kwargs={'query_string': query_string}, query_params=query_params) return render(request, 'content.html', {'content_hash_algo': algo, 'content_checksum': checksum, 'content': content_display_data['content_data'], 'content_raw_url': content_raw_url, 'mime_type': mime_type, 'language': content_display_data['language'], 'breadcrumbs': breadcrumbs, 'branches': None, 'branch': None}) def content_raw(request, query_string): """Django view that produces a raw display of a SWH content identified by its hash value. - See :ref:`Raw content browsing URI scheme ` for more details. + The url that points to it is :http:get:`/browse/content/[(algo_hash):](hash)/raw/` Args: request: input django http request query_string: a string of the form "[ALGO_HASH:]HASH" where optional ALGO_HASH can be either *sha1*, *sha1_git*, *sha256*, or *blake2s256* (default to *sha1*) and HASH the hexadecimal representation of the hash value Returns: The raw bytes of the content. """ # noqa try: algo, checksum = query.parse_hash(query_string) checksum = hash_to_hex(checksum) content_data, mime_type = request_content(query_string) except Exception as exc: return handle_view_exception(exc) filename = request.GET.get('filename', None) if not filename: filename = '%s_%s' % (algo, checksum) if mime_type.startswith('text/'): response = HttpResponse(content_data, content_type="text/plain") response['Content-disposition'] = 'filename=%s' % filename else: response = HttpResponse(content_data, content_type='application/octet-stream') response['Content-disposition'] = 'attachment; filename=%s' % filename return response diff --git a/swh/web/browse/views/directory.py b/swh/web/browse/views/directory.py index 6cac9d965..d58753612 100644 --- a/swh/web/browse/views/directory.py +++ b/swh/web/browse/views/directory.py @@ -1,75 +1,77 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from django.shortcuts import render from swh.web.common import service from swh.web.common.utils import reverse from swh.web.common.exc import handle_view_exception from swh.web.browse.utils import ( gen_path_info, get_directory_entries ) def directory_browse(request, sha1_git, path=None): """Django view for browsing the content of a SWH directory identified by its sha1_git value. - See :ref:`Directory browsing URI scheme ` for - more details. + The url scheme that points to it is the following: + + * :http:get:`/browse/directory/(sha1_git)/` + * :http:get:`/browse/directory/(sha1_git)/(path)/` Args: request: input django http request sha1_git: swh sha1_git identifer of the directory to browse path: optionnal path parameter used to navigate in directories reachable from the provided root one Returns: The HTML rendering for the content of the provided directory. """ root_sha1_git = sha1_git try: if path: dir_info = service.lookup_directory_with_path(sha1_git, path) sha1_git = dir_info['target'] dirs, files = get_directory_entries(sha1_git) except Exception as exc: return handle_view_exception(exc) path_info = gen_path_info(path) breadcrumbs = [] breadcrumbs.append({'name': root_sha1_git[:7], 'url': reverse('browse-directory', kwargs={'sha1_git': root_sha1_git})}) for pi in path_info: breadcrumbs.append({'name': pi['name'], 'url': reverse('browse-directory', kwargs={'sha1_git': root_sha1_git, 'path': pi['path']})}) path = '' if path is None else (path + '/') for d in dirs: d['url'] = reverse('browse-directory', kwargs={'sha1_git': root_sha1_git, 'path': path + d['name']}) for f in files: query_string = 'sha1_git:' + f['target'] f['url'] = reverse('browse-content', kwargs={'query_string': query_string}, query_params={'path': root_sha1_git + '/' + path + f['name']}) return render(request, 'directory.html', {'dir_sha1_git': sha1_git, 'dirs': dirs, 'files': files, 'breadcrumbs': breadcrumbs, 'branches': None, 'branch': None}) diff --git a/swh/web/browse/views/origin.py b/swh/web/browse/views/origin.py index 9cb4760c0..01184f7c3 100644 --- a/swh/web/browse/views/origin.py +++ b/swh/web/browse/views/origin.py @@ -1,301 +1,313 @@ # Copyright (C) 2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import dateutil from django.shortcuts import render from swh.web.common import service from swh.web.common.utils import reverse from swh.web.common.exc import NotFoundExc, handle_view_exception from swh.web.browse.utils import ( get_origin_visits, get_origin_visit_branches, gen_path_info, get_directory_entries, request_content, prepare_content_for_display ) def origin_browse(request, origin_id=None, origin_type=None, origin_url=None): """Django view that produces an HTML display of a swh origin identified by its id or its url. - See :ref:`Origin browsing URI scheme ` for more details. + The url scheme that points to it is the following: + + * :http:get:`/browse/origin/(origin_id)/` + * :http:get:`/browse/origin/(origin_id)/directory/` Args: request: input django http request origin_id: a swh origin id origin_type: type of origin (git, svn, ...) origin_url: url of the origin (e.g. https://github.com//) Returns: The HMTL rendering for the metadata of the provided origin. """ try: if origin_id: origin_request_params = { 'id': origin_id, } else: origin_request_params = { 'type': origin_type, 'url': origin_url } origin_info = service.lookup_origin(origin_request_params) origin_id = origin_info['id'] origin_visits = get_origin_visits(origin_id) except Exception as exc: return handle_view_exception(exc) origin_info['last swh visit browse url'] = \ reverse('browse-origin-directory', kwargs={'origin_id': origin_id}) origin_visits_data = [] for visit in origin_visits: visit_date = dateutil.parser.parse(visit['date']) visit['date'] = visit_date.strftime('%d %B %Y, %H:%M UTC') visit['browse_url'] = reverse('browse-origin-directory', kwargs={'origin_id': origin_id, 'visit_id': visit['visit']}) origin_visits_data.append( {'date': visit_date.timestamp()}) return render(request, 'origin.html', {'origin': origin_info, 'origin_visits_data': origin_visits_data, 'visits': origin_visits, 'browse_url_base': '/browse/revision/origin/%s/' % origin_id}) def origin_directory_browse(request, origin_id, visit_id=None, ts=None, path=None): """Django view for browsing the content of a swh directory associated to an origin for a given visit. - See :ref:`Origin's directory browsing URI scheme ` - for more details. + The url scheme that points to it is the following: + + * :http:get:`/browse/origin/(origin_id)/directory/` + * :http:get:`/browse/origin/(origin_id)/directory/(path)/` + * :http:get:`/browse/origin/(origin_id)/visit/(visit_id)/directory/` + * :http:get:`/browse/origin/(origin_id)/visit/(visit_id)/directory/(path)/` + * :http:get:`/browse/origin/(origin_id)/ts/(ts)/directory/` + * :http:get:`/browse/origin/(origin_id)/ts/(ts)/directory/(path)/` Args: request: input django http request origin_id: a swh origin id visit_id: optionnal visit id parameter (the last one will be used by default) ts: optionnal visit timestamp parameter (the last one will be used by default) path: optionnal path parameter used to navigate in directories reachable from the origin root one Returns: The HTML rendering for the content of the directory associated to the provided origin and visit. """ # noqa try: if not visit_id and not ts: origin_visits = get_origin_visits(origin_id) return origin_directory_browse(request, origin_id, origin_visits[-1]['visit'], path=path) if not visit_id and ts: branches = get_origin_visit_branches(origin_id, visit_ts=ts) url_args = {'origin_id': origin_id, 'ts': ts} else: branches = get_origin_visit_branches(origin_id, visit_id) url_args = {'origin_id': origin_id, 'visit_id': visit_id} branch = request.GET.get('branch', 'master') filtered_branches = [b for b in branches if branch in b['name']] if len(filtered_branches) > 0: root_sha1_git = filtered_branches[0]['directory'] branch = filtered_branches[0]['name'] else: if visit_id: raise NotFoundExc('Branch %s associated to visit with' ' id %s for origin with id %s' ' not found!' % (branch, visit_id, origin_id)) else: raise NotFoundExc('Branch %s associated to visit with' ' timestamp %s for origin with id %s' ' not found!' % (branch, ts, origin_id)) sha1_git = root_sha1_git if path: dir_info = service.lookup_directory_with_path(root_sha1_git, path) sha1_git = dir_info['target'] dirs, files = get_directory_entries(sha1_git) except Exception as exc: return handle_view_exception(exc) for b in branches: branch_url_args = dict(url_args) if path: b['path'] = path branch_url_args['path'] = path b['url'] = reverse('browse-origin-directory', kwargs=branch_url_args, query_params={'branch': b['name']}) path_info = gen_path_info(path) breadcrumbs = [] breadcrumbs.append({'name': root_sha1_git[:7], 'url': reverse('browse-origin-directory', kwargs=url_args, query_params={'branch': branch})}) for pi in path_info: bc_url_args = dict(url_args) bc_url_args['path'] = pi['path'] breadcrumbs.append({'name': pi['name'], 'url': reverse('browse-origin-directory', kwargs=bc_url_args, query_params={'branch': branch})}) path = '' if path is None else (path + '/') for d in dirs: bc_url_args = dict(url_args) bc_url_args['path'] = path + d['name'] d['url'] = reverse('browse-origin-directory', kwargs=bc_url_args, query_params={'branch': branch}) for f in files: bc_url_args = dict(url_args) bc_url_args['path'] = path + f['name'] f['url'] = reverse('browse-origin-content', kwargs=bc_url_args, query_params={'branch': branch}) return render(request, 'directory.html', {'dir_sha1_git': sha1_git, 'dirs': dirs, 'files': files, 'breadcrumbs': breadcrumbs, 'branches': branches, 'branch': branch}) def origin_content_display(request, origin_id, path, visit_id=None, ts=None): """Django view that produces an HTML display of a swh content associated to an origin for a given visit. - See :ref:`Origin content browsing URI scheme ` - for more details. + The url scheme that points to it is the following: + + * :http:get:`/browse/origin/(origin_id)/content/(path)/` + * :http:get:`/browse/origin/(origin_id)/visit/(visit_id)/content/(path)/` + * :http:get:`/browse/origin/(origin_id)/ts/(ts)/content/(path)/` Args: request: input django http request origin_id: id of a swh origin path: path of the content relative to the origin root directory visit_id: optionnal visit id parameter (the last one will be used by default) ts: optionnal visit timestamp parameter (the last one will be used by default) branch: optionnal query parameter that specifies the origin branch from which to retrieve the content Returns: The HTML rendering of the requested content associated to the provided origin and visit. """ # noqa try: if not visit_id and not ts: origin_visits = get_origin_visits(origin_id) return origin_content_display(request, origin_id, path, origin_visits[-1]['visit']) if not visit_id and ts: branches = get_origin_visit_branches(origin_id, visit_ts=ts) kwargs = {'origin_id': origin_id, 'ts': ts} else: branches = get_origin_visit_branches(origin_id, visit_id) kwargs = {'origin_id': origin_id, 'visit_id': visit_id} for b in branches: bc_kwargs = dict(kwargs) bc_kwargs['path'] = path b['url'] = reverse('browse-origin-content', kwargs=bc_kwargs, query_params={'branch': b['name']}) branch = request.GET.get('branch', 'master') filtered_branches = [b for b in branches if branch in b['name']] if len(filtered_branches) > 0: root_sha1_git = filtered_branches[0]['directory'] branch = filtered_branches[0]['name'] else: if visit_id: raise NotFoundExc('Branch %s associated to visit with' ' id %s for origin with id %s' ' not found!' % (branch, visit_id, origin_id)) else: raise NotFoundExc('Branch %s associated to visit with' ' timestamp %s for origin with id %s' ' not found!' % (branch, ts, origin_id)) content_info = service.lookup_directory_with_path(root_sha1_git, path) sha1_git = content_info['target'] query_string = 'sha1_git:' + sha1_git content_data, mime_type = request_content(query_string) except Exception as exc: return handle_view_exception(exc) content_display_data = prepare_content_for_display(content_data, mime_type, path) filename = None path_info = None breadcrumbs = [] split_path = path.split('/') filename = split_path[-1] path = path.replace(filename, '') path_info = gen_path_info(path) breadcrumbs.append({'name': root_sha1_git[:7], 'url': reverse('browse-origin-directory', kwargs=kwargs, query_params={'branch': branch})}) for pi in path_info: bc_kwargs = dict(kwargs) bc_kwargs['path'] = pi['path'] breadcrumbs.append({'name': pi['name'], 'url': reverse('browse-origin-directory', kwargs=bc_kwargs, query_params={'branch': branch})}) breadcrumbs.append({'name': filename, 'url': None}) content_raw_url = reverse('browse-content-raw', kwargs={'query_string': query_string}, query_params={'filename': filename}) return render(request, 'content.html', {'content_hash_algo': 'sha1_git', 'content_checksum': sha1_git, 'content': content_display_data['content_data'], 'content_raw_url': content_raw_url, 'mime_type': mime_type, 'language': content_display_data['language'], 'breadcrumbs': breadcrumbs, 'branches': branches, 'branch': branch})