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