Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/content.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 | ||||
import difflib | import difflib | ||||
import json | |||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
from django.http import HttpResponse | from django.http import HttpResponse, JsonResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.template.defaultfilters import filesizeformat | from django.template.defaultfilters import filesizeformat | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.web.browse.browseurls import browse_route | from swh.web.browse.browseurls import browse_route | ||||
Show All 18 Lines | @browse_route( | ||||
checksum_args=["query_string"], | checksum_args=["query_string"], | ||||
) | ) | ||||
def content_raw(request, query_string): | def content_raw(request, query_string): | ||||
"""Django view that produces a raw display of a content identified | """Django view that produces a raw display of a content identified | ||||
by its hash value. | by its hash value. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/content/[(algo_hash):](hash)/raw/` | :http:get:`/browse/content/[(algo_hash):](hash)/raw/` | ||||
""" | """ | ||||
try: | try: | ||||
re_encode = bool(strtobool(request.GET.get("re_encode", "false"))) | re_encode = bool(strtobool(request.GET.get("re_encode", "false"))) | ||||
algo, checksum = query.parse_hash(query_string) | algo, checksum = query.parse_hash(query_string) | ||||
checksum = hash_to_hex(checksum) | checksum = hash_to_hex(checksum) | ||||
content_data = request_content(query_string, max_size=None, re_encode=re_encode) | content_data = request_content(query_string, max_size=None, re_encode=re_encode) | ||||
except Exception as exc: | except Exception as exc: | ||||
return handle_view_exception(request, exc) | return handle_view_exception(request, exc) | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | else: | ||||
diff_lines = difflib.unified_diff(content_from_lines, content_to_lines) | diff_lines = difflib.unified_diff(content_from_lines, content_to_lines) | ||||
diff_str = "".join(list(diff_lines)[2:]) | diff_str = "".join(list(diff_lines)[2:]) | ||||
except Exception as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
diff_str = str(exc) | diff_str = str(exc) | ||||
diff_data["diff_str"] = diff_str | diff_data["diff_str"] = diff_str | ||||
diff_data["language"] = language | diff_data["language"] = language | ||||
diff_data_json = json.dumps(diff_data, separators=(",", ": ")) | return JsonResponse(diff_data) | ||||
return HttpResponse(diff_data_json, content_type="application/json") | |||||
@browse_route( | @browse_route( | ||||
r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/", | r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/", | ||||
view_name="browse-content", | view_name="browse-content", | ||||
checksum_args=["query_string"], | checksum_args=["query_string"], | ||||
) | ) | ||||
def content_display(request, query_string): | def content_display(request, query_string): | ||||
▲ Show 20 Lines • Show All 225 Lines • Show Last 20 Lines |