Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9337369
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Subscribers
None
View Options
diff --git a/swh/web/ui/api.py b/swh/web/ui/api.py
index da9814ae5..b43969df7 100755
--- a/swh/web/ui/api.py
+++ b/swh/web/ui/api.py
@@ -1,89 +1,91 @@
# Copyright (C) 2015 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
import logging
from flask import redirect, render_template, url_for, flash, request
from swh.web.ui.main import app
from swh.web.ui.model import content
from swh.web.ui import query
from swh.web.ui.controller import service
@app.route('/')
def main():
"""Main application view.
At the moment, redirect to the content search view.
"""
return redirect(url_for('info'))
@app.route('/info')
def info():
"""A simple api to define what the server is all about.
"""
logging.info('Dev SWH UI')
return 'Dev SWH UI'
@app.route('/public')
def public():
"""Main application view.
At the moment, redirect to the content search view.
+
"""
return redirect(url_for('search'))
@app.route('/public/search')
def search():
+ """Search for hashes in swh-storage.
+
+ """
q = request.args.get('q', '')
if q:
flash('Search hash %s posted!' % q)
hashes = query.group_by_checksums(query.parse(q))
api_backend = app.config['conf']['api_backend']
- resp_result = service.search(api_backend, hashes)
- else:
- q = ''
- resp_result = []
-
+ present = service.search(api_backend, hashes)
+ return render_template('search.html',
+ searched_hash=q,
+ present='Found!' if present else 'Not found!')
return render_template('search.html',
- searched_hash=q,
- entries=resp_result)
-
+ searched_hash='',
+ present='')
def run(conf):
"""Run the api's server.
Args:
conf is a dictionary of keywords:
- 'db_url' the db url's access (through psycopg2 format)
- 'content_storage_dir' revisions/directories/contents storage on disk
- 'host' to override the default 127.0.0.1 to open or not the server
to the world
- 'port' to override the default of 5000 (from the underlying layer:
flask)
- 'debug' activate the verbose logs
Returns:
Never
Raises:
?
"""
print("""SWH Web UI run
host: %s
port: %s
debug: %s""" % (conf['host'], conf.get('port', None), conf['debug']))
app.config.update({'conf': conf})
app.run(host=conf['host'],
port=conf.get('port', None),
debug=conf['debug'])
diff --git a/swh/web/ui/back/http.py b/swh/web/ui/back/http.py
index 1d12398a2..f65295432 100644
--- a/swh/web/ui/back/http.py
+++ b/swh/web/ui/back/http.py
@@ -1,79 +1,76 @@
# Copyright (C) 2015 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
import requests
import json
from swh.web.ui.main import app
if 'conf' in app.config:
base_url = app.config['conf']['api_backend']
session_swh = requests.Session()
MIMETYPE = "application/json"
def compute_simple_url(base_url, api_url):
"""Compute the full api url from base_url and api_url.
"""
return '%s%s' % (base_url, api_url)
query_dispatch = {
'post': session_swh.post,
'put': session_swh.put,
'get': session_swh.get
}
def execute(base_url,
query,
result_fn=lambda result: result.ok):
"""Execute a query to the backend.
Args:
base_url: is the backend url to discuss with
query: is the computed query to execute
result_fn: is the function to execute on the query's response result
Returns:
The result of the query on which the result_fn has been applied
Raises:
None
"""
- print(query)
method_fn = query_dispatch[query['method']]
- print(method_fn)
res = method_fn(compute_simple_url(base_url, query['url']),
data=query.get('data'),
headers=query.get('headers'))
return result_fn(res)
-
def create_request(method, api_url, data=None):
"""Create a request model without executing it.
Args:
- method (post, put, get)
- base_url, the server's base url to ask for data
- obj_type: the nature of data we are dealing with
- data: the data to json serialize and send to server as body (could be None)
"""
query = {'method': method,
'url': api_url}
if data:
query.update({'data': json.dumps(data) if data else '',
'headers': {'Content-Type': MIMETYPE}})
return query
diff --git a/swh/web/ui/controller/service.py b/swh/web/ui/controller/service.py
index f6abfab1d..6fb666020 100755
--- a/swh/web/ui/controller/service.py
+++ b/swh/web/ui/controller/service.py
@@ -1,31 +1,37 @@
# Copyright (C) 2015 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
from swh.web.ui.back import http, api_query
+import json
def search(base_url, hashes):
"""Search a content with given hashes.
Args:
hashes, dictionary of hash indexed by key, sha1, sha256, etc...
Returns:
None if no content is found.
An enriched content if the content is found.
Raises:
OSError (no route to host), etc... Network issues in general
"""
def deal_with_result(res):
- print("result:", res)
- return res.data
+ if res.ok:
+ output = res.content.decode('utf-8')
+ if output:
+ h_res = json.loads(output)
+ return h_res['found']
+ return False
+ return False
#return []
#return [{'title': 'some title', 'text': 'some text'}]
- q = api_query.api_storage_content_present(hashes)
+ q = api_query.api_storage_content_present({'content': [hashes]})
return http.execute(base_url, q, result_fn=deal_with_result)
diff --git a/swh/web/ui/templates/search.html b/swh/web/ui/templates/search.html
index 1215465b1..73395f348 100644
--- a/swh/web/ui/templates/search.html
+++ b/swh/web/ui/templates/search.html
@@ -1,19 +1,13 @@
{% extends "layout.html" %}
{% block body %}
<form action="{{ url_for('search') }}" method=get class=search>
<dl>
<dt>Hashes (colon separated values):
<dd><input type=text size=32 name=q value={{ searched_hash }}>
<dd><input type=submit value=Search>
</dl>
</form>
- {% if searched_hash %}
- <ul class=entries>
- {% for entry in entries %}
- <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}
- {% else %}
- <li><em>This content has not yet been seen.</em>
- {% endfor %}
- </ul>
- {% endif %}
+ <ul class=entries>
+ <li><h2>{{ present | safe }}</h2>
+ </ul>
{% endblock %}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Jul 4 2025, 8:02 AM (10 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3452466
Attached To
R65 Staging repository
Event Timeline
Log In to Comment