forked from Ugric/Komodo-Hub
add search
This commit is contained in:
1 parent
7d97832072
commit
401fd737e0
4 files changed
+31
-5
No files matched your search
@@ -11,7 +11,7 @@ def add_category_column():
|
||||
cursor = conn.cursor()
|
||||
category = input("What category do you want to add to the species table? ").lower()
|
||||
if not column_exists(cursor, "species", category):
|
||||
cursor.execute(f"ALTER TABLE species ADD COLUMN {category} TEXT")
|
||||
cursor.execute(f"ALTER TABLE species ADD COLUMN ? TEXT", (category,))
|
||||
print(f"Column '{category}' added.")
|
||||
else:
|
||||
print("Column already exists.")
|
||||
|
||||
@@ -23,6 +23,30 @@ def get_all_species():
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
def get_species_by_name(species_english):
|
||||
with get_db() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT * FROM species WHERE species_english = ?",
|
||||
(species_english,)
|
||||
).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
def search_species(query):
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""SELECT *
|
||||
FROM species
|
||||
WHERE
|
||||
instr(LOWER(COALESCE(CAST(species_english AS TEXT), '')), ?1) > 0
|
||||
OR instr(LOWER(COALESCE(CAST(species_latin AS TEXT), '')), ?1) > 0
|
||||
OR instr(LOWER(COALESCE(CAST(body_text AS TEXT), '')), ?1) > 0
|
||||
OR instr(LOWER(COALESCE(CAST(category AS TEXT), '')), ?1) > 0
|
||||
OR instr(LOWER(COALESCE(CAST(extinction_risk AS TEXT), '')), ?1) > 0;""",
|
||||
(query.lower(),)
|
||||
).fetchmany()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
def get_species_by_name(species_english):
|
||||
with get_db() as conn:
|
||||
row = conn.execute(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from flask import render_template, Blueprint
|
||||
from flask import render_template, Blueprint, request
|
||||
import configparser
|
||||
|
||||
from database.db_commands import get_all_species
|
||||
from database.db_commands import get_all_species, search_species
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
@@ -29,5 +29,7 @@ def all_species():
|
||||
@page.route("/search")
|
||||
def search():
|
||||
query = request.args.get("search-query")
|
||||
|
||||
results = search_species(query)
|
||||
|
||||
return render_template("/species/species_search.jinja", results=results)
|
||||
return render_template("/species/species_search.jinja", results=results, query=query)
|
||||
@@ -11,7 +11,7 @@ Species Database
|
||||
{# All Species #}
|
||||
<div>
|
||||
<form action="/species/search" method="GET" class="search">
|
||||
<input type="text" name="search-query" placeholder="Search all species..." class="bar">
|
||||
<input type="text" name="search-query" placeholder="Search all species..." value="{{query}}" class="bar">
|
||||
<input type="submit" value="Search" class="button">
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user