Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/origin.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 json | |||||
from distutils.util import strtobool | |||||
from django.http import HttpResponse | |||||
from django.shortcuts import render, redirect | from django.shortcuts import render, redirect | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.origin_visits import get_origin_visits | from swh.web.common.origin_visits import get_origin_visits | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, format_utc_iso_date, parse_timestamp | reverse, format_utc_iso_date, parse_timestamp | ||||
) | ) | ||||
from swh.web.common.exc import handle_view_exception | from swh.web.common.exc import handle_view_exception | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | return render(request, 'browse/origin-visits.html', | ||||
'swh_object_metadata': origin_info, | 'swh_object_metadata': origin_info, | ||||
'origin_visits': origin_visits, | 'origin_visits': origin_visits, | ||||
'origin_info': origin_info, | 'origin_info': origin_info, | ||||
'snapshot_context': snapshot_context, | 'snapshot_context': snapshot_context, | ||||
'vault_cooking': None, | 'vault_cooking': None, | ||||
'show_actions_menu': False}) | 'show_actions_menu': False}) | ||||
@browse_route(r'origin/search/(?P<url_pattern>.+)/', | |||||
view_name='browse-origin-search') | |||||
def _origin_search(request, url_pattern): | |||||
"""Internal browse endpoint to search for origins whose urls contain | |||||
a provided string pattern or match a provided regular expression. | |||||
The search is performed in a case insensitive way. | |||||
""" | |||||
offset = int(request.GET.get('offset', '0')) | |||||
limit = min(int(request.GET.get('limit', '50')), 1000) | |||||
regexp = request.GET.get('regexp', 'false') | |||||
with_visit = request.GET.get('with_visit', 'false') | |||||
try: | |||||
results = service.search_origin(url_pattern, offset, limit, | |||||
bool(strtobool(regexp)), | |||||
bool(strtobool(with_visit))) | |||||
results = json.dumps(list(results), sort_keys=True, indent=4, | |||||
separators=(',', ': ')) | |||||
except Exception as exc: | |||||
return handle_view_exception(request, exc, html_response=False) | |||||
return HttpResponse(results, content_type='application/json') | |||||
@browse_route(r'origin/(?P<origin_url>.+)/', | @browse_route(r'origin/(?P<origin_url>.+)/', | ||||
view_name='browse-origin') | view_name='browse-origin') | ||||
def origin_browse(request, origin_url): | def origin_browse(request, origin_url): | ||||
"""Django view that redirects to the display of the latest archived | """Django view that redirects to the display of the latest archived | ||||
snapshot for a given software origin. | snapshot for a given software origin. | ||||
""" | """ | ||||
last_snapshot_url = reverse('browse-origin-directory', | last_snapshot_url = reverse('browse-origin-directory', | ||||
url_args={'origin_url': origin_url}) | url_args={'origin_url': origin_url}) | ||||
return redirect(last_snapshot_url) | return redirect(last_snapshot_url) |