Changeset View
Changeset View
Standalone View
Standalone View
swh/web/ui/views/browse.py
Show All 32 Lines | Returns: | ||||
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 = {'q': None, | |||||
env = {'search_res': None, | |||||
'search_stats': None, | 'search_stats': None, | ||||
'responses': None, | 'message': []} | ||||
'messages': []} | |||||
search_stats = None | search_stats = {'nbfiles': 0, 'pct': 0} | ||||
responses = [] | search_res = None | ||||
messages = [] | message = '' | ||||
# Get with a single hash request | # Get with a single hash request | ||||
if request.method == 'GET': | if request.method == 'GET': | ||||
data = request.args | data = request.args | ||||
q = data.get('q') | q = data.get('q') | ||||
env['q'] = q | |||||
if q: | if q: | ||||
try: | try: | ||||
search_stats = {'nbfiles': 0, 'pct': 0} | search = api.api_search(q) | ||||
r = service.lookup_hash(q) | search_res = search['search_res'] | ||||
responses.append({'filename': 'User submitted hash', | search_stats = search['search_stats'] | ||||
'sha1': q, | |||||
'found': r.get('found') is not None}) | |||||
search_stats['nbfiles'] = 1 | |||||
search_stats['pct'] = 100 if r.get('found') is not None else 0 | |||||
except BadInputExc as e: | except BadInputExc as e: | ||||
messages.append(str(e)) | message = str(e) | ||||
# POST form submission with many hash requests | # Post form submission with many hash requests | ||||
elif request.method == 'POST': | elif request.method == 'POST': | ||||
data = request.form | try: | ||||
search_stats = {'nbfiles': 0, 'pct': 0} | search = api.api_search(None) | ||||
queries = [] | search_res = search['search_res'] | ||||
# Remove potential inputs with no associated value | search_stats = search['search_stats'] | ||||
for k, v in data.items(): | |||||
if v is not None and v != '': | |||||
queries.append({'filename': k, 'sha1': v}) | |||||
if len(queries) > 0: | |||||
try: | |||||
lookup = service.lookup_multiple_hashes(queries) | |||||
nbfound = len([x for x in lookup if x['found']]) | |||||
responses = lookup | |||||
search_stats['nbfiles'] = len(queries) | |||||
search_stats['pct'] = (nbfound / len(queries))*100 | |||||
except BadInputExc as e: | except BadInputExc as e: | ||||
messages.append(str(e)) | message = str(e) | ||||
env['search_stats'] = search_stats | env['search_stats'] = search_stats | ||||
env['responses'] = responses | env['search_res'] = search_res | ||||
env['messages'] = messages | env['message'] = message | ||||
return render_template('upload_and_search.html', **env) | return render_template('upload_and_search.html', **env) | ||||
@app.route('/browse/content/') | @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. | ||||
▲ Show 20 Lines • Show All 712 Lines • Show Last 20 Lines |