mirror of
https://github.com/partitioncloud/partitioncloud-server.git
synced 2025-01-23 09:16:25 +01:00
Add MAX_ONLINE_QUERIES parameter
This commit is contained in:
parent
7b0566fbff
commit
b6a965dd87
@ -6,4 +6,5 @@
|
||||
SECRET_KEY="dev"
|
||||
|
||||
# Port to run on
|
||||
PORT="5000"
|
||||
PORT="5000"
|
||||
MAX_ONLINE_QUERIES=3
|
@ -7,7 +7,7 @@ import shutil
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import (Blueprint, abort, flash, redirect, render_template, request,
|
||||
send_file, session)
|
||||
send_file, session, current_app)
|
||||
|
||||
from .auth import login_required
|
||||
from .db import get_db
|
||||
@ -23,7 +23,12 @@ def index():
|
||||
user = User(session.get("user_id"))
|
||||
albums = user.get_albums()
|
||||
|
||||
return render_template("albums/index.html", albums=albums)
|
||||
if user.access_level == 1:
|
||||
max_queries = 10
|
||||
else:
|
||||
max_queries = current_app.config["MAX_ONLINE_QUERIES"]
|
||||
|
||||
return render_template("albums/index.html", albums=albums, MAX_QUERIES=max_queries)
|
||||
|
||||
|
||||
@bp.route("/search", methods=["POST"])
|
||||
@ -34,13 +39,20 @@ def search_page():
|
||||
return redirect("/albums")
|
||||
|
||||
query = request.form["query"]
|
||||
nb_queries = abs(int(request.form["nb-queries"]))
|
||||
search.flush_cache()
|
||||
partitions_local = search.local_search(query, get_all_partitions())
|
||||
if "online-search" in request.form:
|
||||
google_results = search.online_search(query)
|
||||
|
||||
user = User(session.get("user_id"))
|
||||
|
||||
if nb_queries > 0:
|
||||
if user.access_level != 1:
|
||||
nb_queries = min(current_app.config["MAX_ONLINE_QUERIES"], nb_queries)
|
||||
else:
|
||||
nb_queries = min(10, nb_queries) # Query limit is 10 for an admin
|
||||
google_results = search.online_search(query, nb_queries)
|
||||
else:
|
||||
google_results = []
|
||||
user = User(session.get("user_id"))
|
||||
|
||||
return render_template(
|
||||
"albums/search.html",
|
||||
|
@ -40,7 +40,7 @@ def local_search(query, partitions):
|
||||
return sorted_partitions[:min(5,len(sorted_partitions))]
|
||||
|
||||
|
||||
def online_search(query):
|
||||
def online_search(query, num_queries):
|
||||
"""
|
||||
Renvoie les 3 résultats les plus pertinents depuis google
|
||||
"""
|
||||
@ -49,8 +49,8 @@ def online_search(query):
|
||||
partitions = []
|
||||
results = googlesearch.search(
|
||||
query,
|
||||
num=3,
|
||||
stop=3,
|
||||
num=num_queries,
|
||||
stop=num_queries,
|
||||
pause=0.2
|
||||
)
|
||||
for element in results:
|
||||
@ -83,8 +83,8 @@ def online_search(query):
|
||||
break
|
||||
except db.IntegrityError:
|
||||
pass
|
||||
except urllib.error.HTTPError as e:
|
||||
print(e)
|
||||
except (urllib.error.HTTPError, urllib.error.URLError) as e:
|
||||
print(e, element)
|
||||
db.execute(
|
||||
"""
|
||||
DELETE FROM search_results
|
||||
|
@ -205,7 +205,7 @@ input[type=submit] {
|
||||
width: stretch;
|
||||
}
|
||||
|
||||
#online-search-label {
|
||||
#nb-queries-label {
|
||||
font-size: .7rem;
|
||||
font-weight: lighter;
|
||||
}
|
||||
|
@ -14,8 +14,12 @@
|
||||
<form action="/albums/search" method="post" id="search-form">
|
||||
<input id="search-bar" type="search" name="query" placeholder="Rechercher" required>
|
||||
<br/>
|
||||
<input type="checkbox" id="online-search" name="online-search" value="online-search" checked="on">
|
||||
<label for="online-search" id="online-search-label">Recherche en ligne</label>
|
||||
<select id="nb-queries" name="nb-queries">
|
||||
{% for i in range(0, MAX_QUERIES+1) %}
|
||||
<option value="{{ i }}">{{ i }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<label for="nb-queries" id="nb-queries-label">Recherches en ligne</label>
|
||||
</form>
|
||||
<section id="albums">
|
||||
{% if albums|length != 0 %}
|
||||
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
||||
flask
|
||||
sqlite3
|
||||
urllib
|
||||
googlesearch
|
Loading…
Reference in New Issue
Block a user