diff --git a/swh/web/api/templates/layout.html b/swh/web/api/templates/layout.html index 71593a94a..f99e4c184 100644 --- a/swh/web/api/templates/layout.html +++ b/swh/web/api/templates/layout.html @@ -1,75 +1,75 @@ {% load static %} {% block title %}{% endblock %}

{{ self.title }}

{% block content %}{% endblock %}
back to top
diff --git a/swh/web/api/views/__init__.py b/swh/web/api/views/__init__.py index 10e897e5d..65b678165 100644 --- a/swh/web/api/views/__init__.py +++ b/swh/web/api/views/__init__.py @@ -1,92 +1,92 @@ # Copyright (C) 2015-2017 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information from django.conf.urls import url from rest_framework.response import Response from rest_framework.decorators import api_view from types import GeneratorType from swh.web.api.exc import NotFoundExc from swh.web.api.apiurls import APIUrls, api_route # canned doc string snippets that are used in several doc strings _doc_arg_content_id = """A "[hash_type:]hash" content identifier, where hash_type is one of "sha1" (the default), "sha1_git", "sha256", and hash is a checksum obtained with the hash_type hashing algorithm.""" _doc_arg_last_elt = 'element to start listing from, for pagination purposes' _doc_arg_per_page = 'number of elements to list, for pagination purposes' _doc_exc_bad_id = 'syntax error in the given identifier(s)' _doc_exc_id_not_found = 'no object matching the given criteria could be found' _doc_ret_revision_meta = 'metadata of the revision identified by sha1_git' _doc_ret_revision_log = """list of dictionaries representing the metadata of each revision found in the commit log heading to revision sha1_git. For each commit at least the following information are returned: author/committer, authoring/commit timestamps, revision id, commit message, parent (i.e., immediately preceding) commits, "root" directory id.""" _doc_header_link = """indicates that a subsequent result page is available, pointing to it""" def _api_lookup(lookup_fn, *args, notfound_msg='Object not found', enrich_fn=lambda x: x): """Capture a redundant behavior of: - looking up the backend with a criteria (be it an identifier or checksum) passed to the function lookup_fn - if nothing is found, raise an NotFoundExc exception with error message notfound_msg. - Otherwise if something is returned: - either as list, map or generator, map the enrich_fn function to it and return the resulting data structure as list. - either as dict and pass to enrich_fn and return the dict enriched. Args: - criteria: discriminating criteria to lookup - lookup_fn: function expects one criteria and optional supplementary *args. - notfound_msg: if nothing matching the criteria is found, raise NotFoundExc with this error message. - enrich_fn: Function to use to enrich the result returned by lookup_fn. Default to the identity function if not provided. - *args: supplementary arguments to pass to lookup_fn. Raises: NotFoundExp or whatever `lookup_fn` raises. """ res = lookup_fn(*args) if not res: raise NotFoundExc(notfound_msg) if isinstance(res, (map, list, GeneratorType)): return [enrich_fn(x) for x in res] return enrich_fn(res) @api_view(['GET', 'HEAD']) def api_home(request): return Response({}, template_name='api.html') -APIUrls.urlpatterns.append(url(r'^$', api_home, name='homepage')) +APIUrls.urlpatterns.append(url(r'^$', api_home, name='api_homepage')) @api_route(r'/', 'endpoints') def api_endpoints(request): """Display the list of opened api endpoints. """ routes = APIUrls.get_app_endpoints().copy() for route, doc in routes.items(): doc['doc_intro'] = doc['docstring'].split('\n\n')[0] # Return a list of routes with consistent ordering env = { 'doc_routes': sorted(routes.items()) } return Response(env, template_name="api-endpoints.html") diff --git a/swh/web/urls.py b/swh/web/urls.py index 499e9c628..5a93b8d2e 100644 --- a/swh/web/urls.py +++ b/swh/web/urls.py @@ -1,36 +1,44 @@ # 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 """swhweb URL Configuration The :data:`urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: - Function views: 1. Add an import: ``from my_app import views`` 2. Add a URL to urlpatterns: ``url(r'^$', views.home, name='home')`` - Class-based views: 1. Add an import: ``from other_app.views import Home`` 2. Add a URL to urlpatterns: ``url(r'^$', Home.as_view(), name='home')`` - Including another URLconf: 1. Import the include function: ``from django.conf.urls import url, include`` 2. Add a URL to urlpatterns: ``url(r'^blog/', include('blog.urls'))`` """ from django.conf.urls import url, include from django.contrib.staticfiles.urls import staticfiles_urlpatterns +from django.shortcuts import redirect + + +def default_view(request): + return redirect('api_homepage') + + urlpatterns = [ url(r'^api/', include('swh.web.api.urls')), + url(r'^$', default_view), ] urlpatterns += staticfiles_urlpatterns()