fix images not displaying when the database is cleared

This commit is contained in:
Ugric committed 2026-03-12 18:22:20 +00:00
1 parent fdfd653873
commit ca221771ba
29 files changed
+59 -40

No files matched your search

+2 -2
View File
@@ -97,7 +97,7 @@ def get_all_species():
def get_species_by_name(species_english):
with get_db() as conn:
row = conn.execute(
"SELECT * FROM species WHERE species_english = ?",
"SELECT * FROM species WHERE species_english = ? LIMIT 1",
(species_english,)
).fetchone()
return dict(row) if row else None
@@ -105,7 +105,7 @@ def get_species_by_name(species_english):
def get_species_by_id(species_id):
with get_db() as conn:
result = conn.execute(
"SELECT * FROM species WHERE species_id = ?",
"SELECT * FROM species WHERE species_id = ? LIMIT 1",
(species_id,)
).fetchone()
if result:
+3 -2
View File
@@ -122,14 +122,15 @@ def seed_species():
continue
conn.execute("""
INSERT INTO species
(species_english, species_latin, body_text, category, extinction_risk, created_at)
VALUES (?, ?, ?, ?, ?, ?)
(species_english, species_latin, body_text, category, extinction_risk, photoid, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
sp["species_english"],
sp["species_latin"],
sp["body_text"],
sp["category"],
sp["extinction_risk"],
sp["photoid"],
datetime.now(),
))
print(f" ADD {sp['species_english']}")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+25 -3
View File
@@ -1,8 +1,10 @@
from flask import Flask, request, g
from flask import Flask, request, g, render_template
import os
import importlib.util
from database.db_init import init_database
from database.db_commands import get_user_from_token
from werkzeug.exceptions import HTTPException
def import_pages(route, app):
for root, _, files in os.walk(route):
@@ -31,19 +33,39 @@ def import_pages(route, app):
app.register_blueprint(module.page)
app = Flask(__name__, static_folder='static', static_url_path='')
app = Flask(__name__, static_folder="static", static_url_path="")
@app.before_request
def load_user():
token = request.cookies.get("token")
g.user = token and get_user_from_token(token)
@app.context_processor
def inject_user():
return {"user": g.user}
@app.errorhandler(404)
def page_not_found(e):
return render_template(
"error.jinja",
extra_information=e,
), 404
@app.errorhandler(Exception)
def handle_bad_request(e):
print(e)
if isinstance(e, HTTPException):
return e
return render_template(
"error.jinja",
extra_information=e,
), 500
init_database()
import_pages("routes", app)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=3000, debug=True)
app.run(host="0.0.0.0", port=3000, debug=True)
+19 -21
View File
@@ -1,5 +1,5 @@
from flask import render_template, Blueprint
from database.db_commands import get_species_by_name
from flask import render_template, Blueprint, send_from_directory
from database.db_commands import get_species_by_name, get_species_by_id
import sqlite3
page = Blueprint("species_information", __name__)
@@ -15,22 +15,20 @@ page = Blueprint("species_information", __name__)
@page.route("/species/<species_english>")
def data(species_english):
try:
database_entry = get_species_by_name(species_english)
if database_entry is None:
raise TypeError(f"No species named '{species_english}' found in the database.")
return render_template(
"species_information/species_information.jinja",
english_name =(database_entry["species_english"]).capitalize(),
latin_name =database_entry["species_latin"].capitalize(),
main_text =database_entry["body_text"],
category =database_entry["category"].capitalize(),
extinction_risk =database_entry["extinction_risk"],
photoid =database_entry["photoid"],
species_id =str(database_entry["species_id"])
)
except TypeError as error_information:
return render_template(
"error.jinja",
extra_information=error_information,
)
database_entry = get_species_by_name(species_english)
if database_entry is None:
raise TypeError(f"No species named '{species_english}' found in the database.")
return render_template(
"species_information/species_information.jinja",
english_name =(database_entry["species_english"]).capitalize(),
latin_name =database_entry["species_latin"].capitalize(),
main_text =database_entry["body_text"],
category =database_entry["category"].capitalize(),
extinction_risk =database_entry["extinction_risk"],
photoid =database_entry["photoid"],
species_id =str(database_entry["species_id"])
)
@page.route("/species/image/<photoid>")
def image(photoid):
return send_from_directory("database/images", photoid)
+1 -1
View File
@@ -29,7 +29,7 @@ def species_update(species_id):
if species_image and species_image.filename:
image_id = str(uuid.uuid4())
species_image.save(f"static/images/species_database/{image_id}.jpg")
species_image.save(f"database/images/{image_id}")
update_species(species_id, eng_name, latin_name, main_text, category, extinction_risk)
+1 -1
View File
@@ -23,7 +23,7 @@ def add_species_to_database():
image_id = ""
if species_image:
image_id = str(uuid.uuid4())
species_image.save(f"static/images/species_database/{image_id}.jpg")
species_image.save(f"database/images/{image_id}")
print(category, extinction_risk)
add_species(eng_name, latin_name, main_text, category, extinction_risk, image_id)
Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

+3 -6
View File
@@ -57,7 +57,7 @@ h6 {
.content {
max-width: 2000px;
margin: 0 auto;
margin: 2rem auto;
padding: 0 1rem 1rem;
}
@@ -66,7 +66,7 @@ h6 {
position: sticky;
top: 0;
background-color: var(--transparent);
backdrop-filter: blur(50px);
backdrop-filter: blur(15px);
height: var(--navbar-height);
min-height: 40px;
padding: 0.5rem 1rem;
@@ -75,6 +75,7 @@ h6 {
justify-content: space-between;
align-items: center;
z-index: 998;
max-height: 50px;
}
.navbar .link_button {
@@ -174,10 +175,6 @@ h6 {
text-decoration: none;
}
.logo::before {
content: "The Sovereign Bank of Libreland";
}
.welcome {
display: flex;
background-image: url("/1706110852106.jpg");
+1 -1
View File
@@ -6,7 +6,7 @@
<a href="/species/{{ item['species_english'] }}" class="container-lite">
<h3 class="plain-white-text">{{ item['species_english'] }}</h3>
<div
style="background-image: url('{{ url_for('static', filename='images/species_database/' ~ item['photoid'] ~ '.jpg') }}');" class="div-image">
style="background-image: url('{{ '/species/image/' ~ item['photoid'] }}');" class="div-image">
</div>
<p style="color: var(--text)">
Main category: {{ item['category'] }}
+1 -1
View File
@@ -11,7 +11,7 @@
{%for species in featured_species%}
<a href="/species/{{ species['species_english'] }}" class="container-lite">
<h3 class="plain-white-text">{{ species['species_english'] }}</h3>
<div style="background-image: url('images/species_database/{{ species['photoid'] }}.jpg');" class="div-image">
<div style="background-image: url('/species/image/{{ species['photoid'] }}');" class="div-image">
</div>
</a>
+1
View File
@@ -26,6 +26,7 @@
<a href="/admin">Admin</a>
{% endif %}
<a href="/dashboard">Dashboard</a>
<a href="/dashboard">Settings</a>
<a href="/logout">Logout</a>
{% else %}
{{ button(url='/login', button_text='Login/Register', extra_classes='') }}
@@ -9,7 +9,7 @@
<h1>{{ english_name }}</h1>
<h2><em>{{ latin_name }}</em></h2>
<p>Category: {{category}}</p>
<img src="/images/species_database/{{ photoid }}.jpg" class="species-photo" style="max-width: 100%; height: auto; border-radius: inherit;">
<img src="/species/image/{{ photoid }}" class="species-photo" style="max-width: 100%; height: auto; border-radius: inherit;">
<p>This species is currently classed as: {{extinction_risk}}</p>
{% if extinction_risk == "Not Evaluated" %}
<img src="/images/extinction_risk/notevaluated.png" class="extinction-icon">
@@ -8,7 +8,7 @@
<div style="display:flex;flex-direction: row;">
<div class="container" style="height: fit-content; width:fit-content; text-align:center; max-width: 40%; padding:20px">
<p> Current photo: </p>
<img src="/images/species_database/{{ sp['photoid'] }}.jpg" style="width: 100%; height: auto; margin: auto">
<img src="/species/image/{{ sp['photoid'] }}" style="width: 100%; height: auto; margin: auto">
</div>
<section id="hero" class="container">
<form action="{{ url_for('species_editor.species_update', species_id=species_id) }}" class="form" method="POST"