Changeset View
Changeset View
Standalone View
Standalone View
swh/web/ui/views/browse.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 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 | ||||
from encodings.aliases import aliases | from encodings.aliases import aliases | ||||
from flask import render_template, request, url_for, redirect | from flask import render_template, request, url_for, redirect | ||||
from werkzeug.exceptions import BadRequest | |||||
from functools import wraps | |||||
from flask.ext.api.decorators import set_renderers | from flask.ext.api.decorators import set_renderers | ||||
from flask.ext.api.renderers import HTMLRenderer | from flask.ext.api.renderers import HTMLRenderer | ||||
from swh.core.hashutil import ALGORITHMS | from swh.core.hashutil import ALGORITHMS | ||||
from .. import service, utils | from .. import service, utils | ||||
from ..exc import BadInputExc, NotFoundExc | from ..exc import BadInputExc, NotFoundExc | ||||
from ..main import app | from ..main import app | ||||
from . import api | from . import api | ||||
hash_filter_keys = ALGORITHMS | hash_filter_keys = ALGORITHMS | ||||
zack: So where is this used? it is not in this diff, nor it seems to be a special name used by Flask. | |||||
Not Done Inline Actions(no longer applies with this update) zack: (no longer applies with this update) | |||||
class arg_checker(object): | |||||
""" | |||||
Check argument names provided. If they are not present or None, | |||||
then the client sent a request with missing arguments, so we return | |||||
a 400 BAD REQUEST | |||||
""" | |||||
def __init__(self, *name_check): | |||||
self.name_check = name_check | |||||
def __call__(self, func): | |||||
@wraps(func) | |||||
def wrapped_func(*func_args, **func_kwargs): | |||||
print(app) | |||||
for arg in self.name_check: | |||||
if func_kwargs.get(arg) is None: | |||||
return BadRequest() | |||||
return func(*func_args, **func_kwargs) | |||||
return wrapped_func | |||||
@app.route('/search/', methods=['GET', 'POST']) | @app.route('/search/', methods=['GET', 'POST']) | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def search(): | def search(): | ||||
"""Search for hashes in swh-storage. | """Search for hashes in swh-storage. | ||||
One form to submit either: | One form to submit either: | ||||
- hash query to look up in swh storage | - hash query to look up in swh storage | ||||
- file hashes calculated client-side to be queried in swh storage | - file hashes calculated client-side to be queried in swh storage | ||||
- both | - both | ||||
Returns: | Returns: | ||||
dict representing data to look for in swh storage. | dict representing data to look for in swh storage. | ||||
The following keys are returned: | The following keys are returned: | ||||
- search_stats: {'nbfiles': X, 'pct': Y} the number of total | - search_stats: {'nbfiles': X, 'pct': Y} the number of total | ||||
queried files and percentage of files not in storage respectively | queried files and percentage of files not in storage respectively | ||||
- responses: array of {'filename': X, 'sha1': Y, 'found': Z} | - responses: array of {'filename': X, 'sha1': Y, 'found': Z} | ||||
- messages: General messages. | - messages: General messages. | ||||
TODO: | TODO: | ||||
Batch-process with all checksums, not just sha1 | Batch-process with all checksums, not just sha1 | ||||
""" | """ | ||||
env = {'search_res': None, | env = {'search_res': None, | ||||
'search_stats': None, | 'search_stats': None, | ||||
'message': []} | 'message': []} | ||||
search_stats = {'nbfiles': 0, 'pct': 0} | search_stats = {'nbfiles': 0, 'pct': 0} | ||||
search_res = None | search_res = None | ||||
message = '' | message = '' | ||||
Show All 19 Lines | elif request.method == 'POST': | ||||
message = str(e) | message = str(e) | ||||
env['search_stats'] = search_stats | env['search_stats'] = search_stats | ||||
env['search_res'] = search_res | env['search_res'] = search_res | ||||
env['message'] = message | env['message'] = message | ||||
return render_template('search.html', **env) | return render_template('search.html', **env) | ||||
@app.route('/browse/content/') | |||||
@app.route('/browse/content/<string:q>/') | @app.route('/browse/content/<string:q>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_content(q): | def browse_content(q): | ||||
"""Given a hash and a checksum, display the content's meta-data. | """Given a hash and a checksum, display the content's meta-data. | ||||
Args: | Args: | ||||
q is of the form algo_hash:hash with algo_hash in | q is of the form algo_hash:hash with algo_hash in | ||||
(sha1, sha1_git, sha256) | (sha1, sha1_git, sha256) | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | try: | ||||
message = _origin_seen(q, origin) | message = _origin_seen(q, origin) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
message = str(e) | message = str(e) | ||||
env['message'] = message | env['message'] = message | ||||
return render_template('content-with-origin.html', **env) | return render_template('content-with-origin.html', **env) | ||||
@app.route('/browse/directory/') | |||||
@app.route('/browse/directory/<string:sha1_git>/') | @app.route('/browse/directory/<string:sha1_git>/') | ||||
@app.route('/browse/directory/<string:sha1_git>/<path:path>/') | @app.route('/browse/directory/<string:sha1_git>/<path:path>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_directory(sha1_git, path=None): | def browse_directory(sha1_git, path=None): | ||||
"""Show directory information. | """Show directory information. | ||||
Args: | Args: | ||||
- sha1_git: the directory's sha1 git identifier. If path | - sha1_git: the directory's sha1 git identifier. If path | ||||
Show All 30 Lines | try: | ||||
directory_files = api.api_directory(sha1_git) | directory_files = api.api_directory(sha1_git) | ||||
env['files'] = utils.prepare_data_for_view(directory_files) | env['files'] = utils.prepare_data_for_view(directory_files) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('directory.html', **env) | return render_template('directory.html', **env) | ||||
@app.route('/browse/origin/') | |||||
@app.route('/browse/origin/<int:origin_id>/') | @app.route('/browse/origin/<int:origin_id>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_origin(origin_id): | def browse_origin(origin_id): | ||||
"""Browse origin with id id. | """Browse origin with id id. | ||||
""" | """ | ||||
browse_url = url_for('browse_revision_with_origin', origin_id=origin_id) | browse_url = url_for('browse_revision_with_origin', origin_id=origin_id) | ||||
visit_url = url_for('api_origin_visits', origin_id=origin_id) | visit_url = url_for('api_origin_visits', origin_id=origin_id) | ||||
env = {'browse_url': browse_url, | env = {'browse_url': browse_url, | ||||
'visit_url': visit_url, | 'visit_url': visit_url, | ||||
'origin_id': origin_id, | 'origin_id': origin_id, | ||||
'origin': None} | 'origin': None} | ||||
try: | try: | ||||
env['origin'] = api.api_origin(origin_id) | env['origin'] = api.api_origin(origin_id) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('origin.html', **env) | return render_template('origin.html', **env) | ||||
@app.route('/browse/person/') | |||||
@app.route('/browse/person/<int:person_id>/') | @app.route('/browse/person/<int:person_id>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_person(person_id): | def browse_person(person_id): | ||||
"""Browse person with id id. | """Browse person with id id. | ||||
""" | """ | ||||
env = {'person_id': person_id, | env = {'person_id': person_id, | ||||
'person': None, | 'person': None, | ||||
'message': None} | 'message': None} | ||||
try: | try: | ||||
env['person'] = api.api_person(person_id) | env['person'] = api.api_person(person_id) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('person.html', **env) | return render_template('person.html', **env) | ||||
@app.route('/browse/release/') | |||||
@app.route('/browse/release/<string:sha1_git>/') | @app.route('/browse/release/<string:sha1_git>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_release(sha1_git): | def browse_release(sha1_git): | ||||
"""Browse release with sha1_git. | """Browse release with sha1_git. | ||||
""" | """ | ||||
env = {'sha1_git': sha1_git, | env = {'sha1_git': sha1_git, | ||||
'message': None, | 'message': None, | ||||
'release': None} | 'release': None} | ||||
try: | try: | ||||
rel = api.api_release(sha1_git) | rel = api.api_release(sha1_git) | ||||
env['release'] = utils.prepare_data_for_view(rel) | env['release'] = utils.prepare_data_for_view(rel) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('release.html', **env) | return render_template('release.html', **env) | ||||
@app.route('/browse/revision/') | |||||
@app.route('/browse/revision/<string:sha1_git>/') | @app.route('/browse/revision/<string:sha1_git>/') | ||||
@app.route('/browse/revision/<string:sha1_git>/prev/<path:prev_sha1s>/') | @app.route('/browse/revision/<string:sha1_git>/prev/<path:prev_sha1s>/') | ||||
@set_renderers(HTMLRenderer) | @set_renderers(HTMLRenderer) | ||||
def browse_revision(sha1_git, prev_sha1s=None): | def browse_revision(sha1_git, prev_sha1s=None): | ||||
"""Browse the revision with git SHA1 sha1_git_cur, while optionally keeping | """Browse the revision with git SHA1 sha1_git_cur, while optionally keeping | ||||
the context from which we came as a list of previous (i.e. later) | the context from which we came as a list of previous (i.e. later) | ||||
revisions' sha1s. | revisions' sha1s. | ||||
Args: | Args: | ||||
sha1_git: the requested revision's sha1_git. | sha1_git: the requested revision's sha1_git. | ||||
prev_sha1s: an optional string of /-separated sha1s representing our | prev_sha1s: an optional string of /-separated sha1s representing our | ||||
context, ordered by descending revision date. | context, ordered by descending revision date. | ||||
Returns: | Returns: | ||||
Information about revision of git SHA1 sha1_git_cur, with relevant URLS | Information about revision of git SHA1 sha1_git_cur, with relevant URLS | ||||
pointing to the context augmented with sha1_git_cur. | pointing to the context augmented with sha1_git_cur. | ||||
Example: | Example: | ||||
GET /browse/revision/ | GET /browse/revision/ | ||||
""" | """ | ||||
env = {'sha1_git': sha1_git, | env = {'sha1_git': sha1_git, | ||||
'message': None, | 'message': None, | ||||
'revision': None} | 'revision': None} | ||||
try: | try: | ||||
rev = api.api_revision(sha1_git, prev_sha1s) | rev = api.api_revision(sha1_git, prev_sha1s) | ||||
env['revision'] = utils.prepare_data_for_view(rev) | env['revision'] = utils.prepare_data_for_view(rev) | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('revision.html', **env) | return render_template('revision.html', **env) | ||||
@app.route('/browse/revision/<string:sha1_git>/raw/') | @app.route('/browse/revision/<string:sha1_git>/raw/') | ||||
def browse_revision_raw_message(sha1_git): | def browse_revision_raw_message(sha1_git): | ||||
"""Given a sha1_git, display the corresponding revision's raw message. | """Given a sha1_git, display the corresponding revision's raw message. | ||||
""" | """ | ||||
Show All 27 Lines | def browse_revision_log(sha1_git, prev_sha1s=None): | ||||
except (NotFoundExc, BadInputExc) as e: | except (NotFoundExc, BadInputExc) as e: | ||||
env['message'] = str(e) | env['message'] = str(e) | ||||
return render_template('revision-log.html', **env) | return render_template('revision-log.html', **env) | ||||
@app.route('/browse/revision' | @app.route('/browse/revision' | ||||
'/origin/log/') | |||||
@app.route('/browse/revision' | |||||
'/origin/<int:origin_id>/log/') | '/origin/<int:origin_id>/log/') | ||||
@app.route('/browse/revision' | @app.route('/browse/revision' | ||||
'/origin/<int:origin_id>' | '/origin/<int:origin_id>' | ||||
'/branch/<path:branch_name>/log/') | '/branch/<path:branch_name>/log/') | ||||
@app.route('/browse/revision' | @app.route('/browse/revision' | ||||
'/origin/<int:origin_id>' | '/origin/<int:origin_id>' | ||||
'/branch/<path:branch_name>' | '/branch/<path:branch_name>' | ||||
'/ts/<string:ts>/log/') | '/ts/<string:ts>/log/') | ||||
▲ Show 20 Lines • Show All 468 Lines • Show Last 20 Lines |
So where is this used? it is not in this diff, nor it seems to be a special name used by Flask.
What am I missing?