forked from PrivateCoffee/wikimore
fix: improve search input validation and error handling
Added validation to ensure both language and project are selected before proceeding with the search. Redirects to the homepage if the query is empty. Enhanced error handling in index redirect function to catch invalid project/language combinations and inform users appropriately. Resolves issues with user experience and input handling.
This commit is contained in:
parent
a4a9cbbdb3
commit
f1b5b81050
2 changed files with 22 additions and 2 deletions
|
@ -171,6 +171,17 @@ def search() -> Union[Text, Response]:
|
|||
query = request.form["query"]
|
||||
lang = request.form["lang"]
|
||||
project = request.form["project"]
|
||||
|
||||
if not lang or not project:
|
||||
return render_template(
|
||||
"article.html",
|
||||
title="Error",
|
||||
content="Please select a language and a project.",
|
||||
)
|
||||
|
||||
if not query:
|
||||
return redirect(url_for("index_php_redirect", project=project, lang=lang))
|
||||
|
||||
return redirect(
|
||||
url_for("search_results", project=project, lang=lang, query=query)
|
||||
)
|
||||
|
@ -400,7 +411,16 @@ def index_php_redirect(project, lang) -> Response:
|
|||
"""
|
||||
# TODO: Handle query string
|
||||
|
||||
url = f"{app.languages[lang]['projects'][project]}/w/api.php?action=query&format=json&meta=siteinfo&siprop=general"
|
||||
try:
|
||||
url = f"{app.languages[lang]['projects'][project]}/w/api.php?action=query&format=json&meta=siteinfo&siprop=general"
|
||||
except KeyError:
|
||||
return (
|
||||
render_template(
|
||||
"article.html",
|
||||
title="Project does not exist",
|
||||
content=f"Sorry, the project {project} does not exist in the {lang} language.",
|
||||
),
|
||||
)
|
||||
with urllib.request.urlopen(url) as response:
|
||||
data = json.loads(response.read().decode())
|
||||
main_page = data["query"]["general"]["mainpage"]
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<option {% if key == "en" %}selected="selected"{% endif %} value="{{ key }}">{{ value["name"] }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="text" name="query" id="query" placeholder="Search Wikipedia" required>
|
||||
<input type="text" name="query" id="query" placeholder="Search Wikipedia">
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue