Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/content.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
import difflib | import difflib | ||||
import json | import json | ||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
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 | |||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.web.common import query, service, highlightjs | from swh.web.common import query, service, highlightjs | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, gen_path_info, swh_object_icons | reverse, gen_path_info, swh_object_icons | ||||
) | ) | ||||
from swh.web.common.exc import NotFoundExc, handle_view_exception | from swh.web.common.exc import NotFoundExc, handle_view_exception | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | else: | ||||
content_to_lines = content_to['raw_data'].decode('utf-8')\ | content_to_lines = content_to['raw_data'].decode('utf-8')\ | ||||
.splitlines(True) | .splitlines(True) | ||||
if content_to_lines and content_to_lines[-1][-1] != '\n': | if content_to_lines and content_to_lines[-1][-1] != '\n': | ||||
content_to_lines[-1] += '[swh-no-nl-marker]\n' | content_to_lines[-1] += '[swh-no-nl-marker]\n' | ||||
diff_lines = difflib.unified_diff(content_from_lines, | diff_lines = difflib.unified_diff(content_from_lines, | ||||
content_to_lines) | content_to_lines) | ||||
diff_str = ''.join(list(diff_lines)[2:]) | diff_str = ''.join(list(diff_lines)[2:]) | ||||
except Exception as e: | except Exception as exc: | ||||
diff_str = str(e) | sentry_sdk.capture_exception(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=(',', ': ')) | diff_data_json = json.dumps(diff_data, separators=(',', ': ')) | ||||
return HttpResponse(diff_data_json, content_type='application/json') | return HttpResponse(diff_data_json, content_type='application/json') | ||||
@browse_route(r'content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/', | @browse_route(r'content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/', | ||||
Show All 15 Lines | try: | ||||
selected_language = request.GET.get('language', None) | selected_language = request.GET.get('language', None) | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get('origin', None) | origin_url = request.GET.get('origin', None) | ||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url: | if origin_url: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context(origin_url=origin_url) | snapshot_context = get_snapshot_context(origin_url=origin_url) | ||||
except Exception: | except NotFoundExc: | ||||
raw_cnt_url = reverse('browse-content', | raw_cnt_url = reverse('browse-content', | ||||
url_args={'query_string': query_string}) | url_args={'query_string': query_string}) | ||||
error_message = \ | error_message = \ | ||||
('The Software Heritage archive has a content ' | ('The Software Heritage archive has a content ' | ||||
'with the hash you provided but the origin ' | 'with the hash you provided but the origin ' | ||||
'mentioned in your request appears broken: %s. ' | 'mentioned in your request appears broken: %s. ' | ||||
'Please check the URL and try again.\n\n' | 'Please check the URL and try again.\n\n' | ||||
'Nevertheless, you can still browse the content ' | 'Nevertheless, you can still browse the content ' | ||||
▲ Show 20 Lines • Show All 133 Lines • Show Last 20 Lines |