working, images broken

This commit is contained in:
2025-06-23 04:08:45 -05:00
parent 2bb7a29141
commit 23fee50a77
7 changed files with 135 additions and 79 deletions

View File

@ -9,6 +9,7 @@ import uuid
import zipfile
import tempfile
import difflib
import traceback
from datetime import datetime
# Thirdparty
@ -80,7 +81,7 @@ def upload():
filename = file.filename.lower().strip()
# ── ZIP Import Flow ───────────────────────────────────────────────────
# ── ZIP Import Flow ────────────────────────────────────────────────
if filename.endswith(".zip"):
tmp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
file.save(tmp_zip.name)
@ -127,32 +128,34 @@ def upload():
tmpdir = tempfile.mkdtemp()
z.extractall(tmpdir)
# --- load and validate plants.csv ---
plant_path = os.path.join(tmpdir, "plants.csv")
with open(plant_path, newline="", encoding="utf-8-sig") as pf:
reader = csv.DictReader(pf)
if reader.fieldnames != PLANT_HEADERS:
missing = set(PLANT_HEADERS) - set(reader.fieldnames or [])
extra = set(reader.fieldnames or []) - set(PLANT_HEADERS)
extra = set(reader.fieldnames or []) - set(PLANT_HEADERS)
os.remove(tmp_zip.name)
flash(f"plants.csv header mismatch. Missing: {missing}, Extra: {extra}", "danger")
return redirect(request.url)
plant_rows = list(reader)
# --- load and validate media.csv ---
media_path = os.path.join(tmpdir, "media.csv")
with open(media_path, newline="", encoding="utf-8-sig") as mf:
mreader = csv.DictReader(mf)
if mreader.fieldnames != MEDIA_HEADERS:
missing = set(MEDIA_HEADERS) - set(mreader.fieldnames or [])
extra = set(mreader.fieldnames or []) - set(MEDIA_HEADERS)
extra = set(mreader.fieldnames or []) - set(MEDIA_HEADERS)
os.remove(tmp_zip.name)
flash(f"media.csv header mismatch. Missing: {missing}, Extra: {extra}", "danger")
return redirect(request.url)
media_rows = list(mreader)
# --- import plants ---
neo = get_neo4j_handler()
added_plants = 0
plant_map = {}
for row in plant_rows:
common = PlantCommonName.query.filter_by(name=row["Name"]).first()
if not common:
@ -192,20 +195,23 @@ def upload():
neo.create_plant_node(p.uuid, row["Name"])
if row.get("Mother UUID"):
neo.create_lineage(child_uuid=p.uuid, parent_uuid=row["Mother UUID"])
neo.create_lineage(
child_uuid=p.uuid,
parent_uuid=row["Mother UUID"]
)
added_plants += 1
# ✅ Import media once for the full batch
# --- import media (FIX: now passing plant_id) ---
added_media = 0
for mrow in media_rows:
plant_uuid = mrow["Plant UUID"]
plant_id = plant_map.get(plant_uuid)
plant_id = plant_map.get(plant_uuid)
if not plant_id:
continue
subpath = mrow["Image Path"].split('uploads/', 1)[-1]
src = os.path.join(tmpdir, "images", subpath)
src = os.path.join(tmpdir, "images", subpath)
if not os.path.isfile(src):
continue
@ -220,14 +226,18 @@ def upload():
file=file_storage,
uploader_id=current_user.id,
plugin="plant",
related_id=plant_id
related_id=plant_id,
plant_id=plant_id # ← ensure the FK is set!
)
media.uploaded_at = datetime.fromisoformat(mrow["Uploaded At"])
media.caption = mrow["Source Type"]
media.caption = mrow["Source Type"]
db.session.add(media)
added_media += 1
except Exception as e:
current_app.logger.warning(f"Failed to import media file: {subpath}{e}")
current_app.logger.warning(
f"Failed to import media file: {subpath}{e}"
)
current_app.logger.debug(traceback.format_exc())
db.session.commit()
neo.close()
@ -236,7 +246,7 @@ def upload():
flash(f"Imported {added_plants} plants and {added_media} images.", "success")
return redirect(request.url)
# ── Standalone CSV Review Flow ─────────────────────────────────────
# ── CSV Review Flow ─────────────────────────────────────────────────
if filename.endswith(".csv"):
try:
stream = io.StringIO(file.stream.read().decode("utf-8-sig"))
@ -255,18 +265,14 @@ def upload():
review_list = []
all_common = {c.name.lower(): c for c in PlantCommonName.query.all()}
all_sci = {s.name.lower(): s for s in PlantScientificName.query.all()}
all_sci = {s.name.lower(): s for s in PlantScientificName.query.all()}
for row in reader:
uuid_raw = row.get("uuid", "")
uuid_val = uuid_raw.strip().strip('"')
name_raw = row.get("name", "")
name = name_raw.strip()
sci_raw = row.get("scientific_name", "")
sci_name = sci_raw.strip()
uuid_val = row.get("uuid", "").strip().strip('"')
name = row.get("name", "").strip()
sci_name = row.get("scientific_name", "").strip()
plant_type = row.get("plant_type", "").strip() or "plant"
mother_raw = row.get("mother_uuid", "")
mother_uuid = mother_raw.strip().strip('"')
mother_uuid= row.get("mother_uuid", "").strip().strip('"')
if not (uuid_val and name and plant_type):
continue
@ -274,17 +280,20 @@ def upload():
suggestions = difflib.get_close_matches(
sci_name.lower(),
list(all_sci.keys()),
n=1, cutoff=0.8
n=1,
cutoff=0.8
)
suggested = (
all_sci[suggestions[0]].name
if suggestions and suggestions[0] != sci_name.lower()
else None
)
suggested = (all_sci[suggestions[0]].name
if suggestions and suggestions[0] != sci_name.lower()
else None)
item = {
"uuid": uuid_val,
"name": name,
"sci_name": sci_name,
"suggested": suggested,
"uuid": uuid_val,
"name": name,
"sci_name": sci_name,
"suggested": suggested,
"plant_type": plant_type,
"mother_uuid": mother_uuid
}
@ -294,7 +303,44 @@ def upload():
session["review_list"] = review_list
return redirect(url_for("utility.review"))
flash("Unsupported file type. Please upload a ZIP or CSV.", "danger")
# ── Direct Media Upload Flow ───────────────────────────────────────
plugin = request.form.get("plugin", '')
related_id = request.form.get("related_id", 0)
plant_id = request.form.get("plant_id", None)
growlog_id = request.form.get("growlog_id", None)
caption = request.form.get("caption", None)
now = datetime.utcnow()
unique_id = str(uuid.uuid4()).replace("-", "")
secure_name= secure_filename(file.filename)
storage_path = os.path.join(
current_app.config['UPLOAD_FOLDER'],
str(current_user.id),
now.strftime('%Y/%m/%d')
)
os.makedirs(storage_path, exist_ok=True)
full_file_path = os.path.join(storage_path, f"{unique_id}_{secure_name}")
file.save(full_file_path)
file_url = f"/{current_user.id}/{now.strftime('%Y/%m/%d')}/{unique_id}_{secure_name}"
media = Media(
plugin=plugin,
related_id=related_id,
filename=f"{unique_id}_{secure_name}",
uploaded_at=now,
uploader_id=current_user.id,
caption=caption,
plant_id=plant_id,
growlog_id=growlog_id,
created_at=now,
file_url=file_url
)
db.session.add(media)
db.session.commit()
flash("File uploaded and saved successfully.", "success")
return redirect(request.url)
return render_template("utility/upload.html", csrf_token=generate_csrf())
@ -419,10 +465,12 @@ def export_data():
plants_csv = plant_io.getvalue()
# 2) Gather media
media_records = (Media.query
.filter_by(uploader_id=current_user.id)
.order_by(Media.id)
.all())
media_records = (
Media.query
.filter(Media.uploader_id == current_user.id, Media.plant_id.isnot(None))
.order_by(Media.id)
.all()
)
# Build media.csv
media_io = io.StringIO()
mw = csv.writer(media_io)