bunch of changes

This commit is contained in:
2025-06-04 23:24:16 -05:00
parent 98c868113c
commit 6cf2fdec61
41 changed files with 1580 additions and 286 deletions

View File

@ -1,16 +1,25 @@
# plugins/importer/routes.py
import csv
import io
import difflib
from flask import Blueprint, request, render_template, redirect, flash, session, url_for
from flask_login import login_required, current_user
from app.neo4j_utils import get_neo4j_handler
from plugins.plant.models import db, Plant, PlantCommon, PlantScientific
from flask_wtf.csrf import generate_csrf
bp = Blueprint("importer", __name__, template_folder="templates")
from app.neo4j_utils import get_neo4j_handler
from plugins.plant.models import (
db,
Plant, PlantCommonName, PlantScientificName, PlantOwnershipLog
)
bp = Blueprint("importer", __name__, template_folder="templates", url_prefix="/import")
REQUIRED_HEADERS = {"uuid", "plant_type", "name"}
@bp.route("/import/", methods=["GET", "POST"])
@bp.route("/", methods=["GET", "POST"])
@login_required
def upload():
if request.method == "POST":
@ -24,48 +33,58 @@ def upload():
stream = io.StringIO(decoded)
reader = csv.DictReader(stream)
headers = set(reader.fieldnames)
if not REQUIRED_HEADERS.issubset(headers):
flash(f"Missing required CSV headers: {REQUIRED_HEADERS - headers}", "error")
headers = set(reader.fieldnames or [])
missing = REQUIRED_HEADERS - headers
if missing:
flash(f"Missing required CSV headers: {missing}", "error")
return redirect(request.url)
session["pending_rows"] = []
review_list = []
all_common = {c.name.lower(): c for c in PlantCommon.query.all()}
all_scientific = {s.name.lower(): s for s in PlantScientific.query.all()}
# Preload existing common/scientific names
all_common = {c.name.lower(): c for c in PlantCommonName.query.all()}
all_scientific = {s.name.lower(): s for s in PlantScientificName.query.all()}
for row in reader:
uuid = row.get("uuid")
name = row.get("name", "").strip()
sci_name = row.get("scientific_name", "").strip()
plant_type = row.get("plant_type", "plant")
mother_uuid = row.get("mother_uuid", "").strip()
uuid_raw = row.get("uuid", "")
uuid = uuid_raw.strip().strip('"')
if not all([uuid, name, plant_type]):
name_raw = row.get("name", "")
name = name_raw.strip()
sci_raw = row.get("scientific_name", "")
sci_name = sci_raw.strip()
plant_type = row.get("plant_type", "").strip() or "plant"
mother_raw = row.get("mother_uuid", "")
mother_uuid = mother_raw.strip().strip('"')
# If any required field is missing, skip
if not (uuid and name and plant_type):
continue
name_lc = name.lower()
sci_lc = sci_name.lower()
# Try fuzzymatching scientific names if needed
suggested_match = None
original_input = sci_name
original_sci = sci_name
name_lc = name.lower()
sci_lc = sci_name.lower()
# Fuzzy match scientific name
if sci_lc and sci_lc not in all_scientific:
close = difflib.get_close_matches(sci_lc, all_scientific.keys(), n=1, cutoff=0.85)
if close:
suggested_match = all_scientific[close[0]].name
# Infer from common name
if not sci_lc and name_lc in all_common:
sci_obj = PlantScientific.query.filter_by(common_id=all_common[name_lc].id).first()
sci_obj = PlantScientificName.query.filter_by(common_id=all_common[name_lc].id).first()
if sci_obj:
sci_name = sci_obj.name
elif not sci_lc:
close_common = difflib.get_close_matches(name_lc, all_common.keys(), n=1, cutoff=0.85)
if close_common:
match_name = close_common[0]
sci_obj = PlantScientific.query.filter_by(common_id=all_common[match_name].id).first()
sci_obj = PlantScientificName.query.filter_by(common_id=all_common[match_name].id).first()
if sci_obj:
suggested_match = sci_obj.name
sci_name = sci_obj.name
@ -74,17 +93,17 @@ def upload():
"uuid": uuid,
"name": name,
"sci_name": sci_name,
"original_sci_name": original_input,
"original_sci_name": original_sci,
"plant_type": plant_type,
"mother_uuid": mother_uuid,
"suggested_scientific_name": suggested_match,
})
if suggested_match and suggested_match != original_input:
if suggested_match and suggested_match != original_sci:
review_list.append({
"uuid": uuid,
"common_name": name,
"user_input": original_input or "(blank)",
"user_input": original_sci or "(blank)",
"suggested_name": suggested_match
})
@ -92,44 +111,60 @@ def upload():
return redirect(url_for("importer.review"))
except Exception as e:
flash(f"Import failed: {str(e)}", "error")
flash(f"Import failed: {e}", "error")
return redirect(request.url)
return render_template("importer/upload.html")
return render_template("importer/upload.html", csrf_token=generate_csrf())
@bp.route("/import/review", methods=["GET", "POST"])
@bp.route("/review", methods=["GET", "POST"])
@login_required
def review():
rows = session.get("pending_rows", [])
rows = session.get("pending_rows", [])
review_list = session.get("review_list", [])
if request.method == "POST":
neo = get_neo4j_handler()
neo = get_neo4j_handler()
added = 0
# —————————————————————————————————————————————
# (1) CREATE MySQL records & MERGE every Neo4j node
# —————————————————————————————————————————————
for row in rows:
uuid = row["uuid"]
name = row["name"]
sci_name = row["sci_name"]
user_input = row["original_sci_name"]
plant_type = row["plant_type"]
mother_uuid = row["mother_uuid"]
suggested = row.get("suggested_scientific_name")
uuid_raw = row["uuid"]
uuid = uuid_raw.strip().strip('"')
common = PlantCommon.query.filter_by(name=name).first()
name_raw = row["name"]
name = name_raw.strip()
sci_raw = row["sci_name"]
sci_name = sci_raw.strip()
plant_type = row["plant_type"].strip()
mother_raw = row["mother_uuid"]
mother_uuid = mother_raw.strip().strip('"')
suggested = row.get("suggested_scientific_name")
# ——— MySQL: PlantCommonName ———
common = PlantCommonName.query.filter_by(name=name).first()
if not common:
common = PlantCommon(name=name)
common = PlantCommonName(name=name)
db.session.add(common)
db.session.flush()
accepted = request.form.get(f"confirm_{uuid}")
sci_name_to_use = suggested if (suggested and accepted) else sci_name
# ——— MySQL: PlantScientificName ———
accepted = request.form.get(f"confirm_{uuid}")
sci_to_use = suggested if (suggested and accepted) else sci_name
scientific = PlantScientific.query.filter_by(name=sci_name_to_use).first()
scientific = PlantScientificName.query.filter_by(name=sci_to_use).first()
if not scientific:
scientific = PlantScientific(name=sci_name_to_use, common_id=common.id)
scientific = PlantScientificName(name=sci_to_use, common_id=common.id)
db.session.add(scientific)
db.session.flush()
# ——— MySQL: Plant row ———
plant = Plant.query.filter_by(uuid=uuid).first()
if not plant:
plant = Plant(
@ -141,18 +176,60 @@ def review():
is_verified=bool(accepted)
)
db.session.add(plant)
db.session.flush() # so plant.id is available immediately
added += 1
neo.create_plant_node(uuid, name)
if mother_uuid:
neo.create_plant_node(mother_uuid, "Parent")
neo.create_lineage(uuid, mother_uuid)
# ——— MySQL: Create initial ownership log entry ———
log = PlantOwnershipLog(
plant_id = plant.id,
user_id = current_user.id,
date_acquired = datetime.utcnow(),
transferred = False,
is_verified = bool(accepted)
)
db.session.add(log)
# ——— Neo4j: ensure a node exists for this plant UUID ———
neo.create_plant_node(uuid, name)
# Commit MySQL so that all Plant/OwnershipLog rows exist
db.session.commit()
# —————————————————————————————————————————————
# (2) CREATE Neo4j LINEAGE relationships (child → parent). (Unchanged)
# —————————————————————————————————————————————
for row in rows:
child_raw = row.get("uuid", "")
child_uuid = child_raw.strip().strip('"')
mother_raw = row.get("mother_uuid", "")
mother_uuid = mother_raw.strip().strip('"')
print(
f"[DEBUG] row → child_raw={child_raw!r}, child_uuid={child_uuid!r}; "
f"mother_raw={mother_raw!r}, mother_uuid={mother_uuid!r}"
)
if mother_uuid:
neo.create_plant_node(mother_uuid, name="Unknown")
neo.create_lineage(child_uuid, mother_uuid)
else:
print(f"[DEBUG] Skipping LINEAGE creation for child {child_uuid!r} (no mother_uuid)")
# (Optional) Check two known UUIDs
neo.debug_check_node("8b1059c8-8dd3-487a-af19-1eb548788e87")
neo.debug_check_node("2ee2e0e7-69de-4d8f-abfe-4ed973c3d760")
neo.close()
flash(f"{added} plants added.", "success")
flash(f"{added} plants added (MySQL) + Neo4j nodes/relations created.", "success")
session.pop("pending_rows", None)
session.pop("review_list", None)
return redirect(url_for("importer.upload"))
return render_template("importer/review.html", review_list=review_list)
return render_template(
"importer/review.html",
review_list=review_list,
csrf_token=generate_csrf()
)

View File

@ -1,43 +1,38 @@
{% extends "core_ui/base.html" %}
{% block title %}Review Scientific Names{% endblock %}
{% block title %}Review Matches{% endblock %}
{% block content %}
<div class="container py-4">
<h2 class="mb-4">🔍 Review Suggested Matches</h2>
<form method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
{% if review_list %}
<div class="table-responsive">
<table class="table table-bordered align-middle">
<thead>
<p class="text-muted mb-3">Confirm the suggested scientific name replacements below. Only confirmed matches will override user input.</p>
<table class="table table-bordered table-sm align-middle">
<thead>
<tr>
<th>Common Name</th>
<th>User Input</th>
<th>Suggested Match</th>
<th>Confirm</th>
</tr>
</thead>
<tbody>
{% for row in review_list %}
<tr>
<th>Common Name</th>
<th>Suggested Scientific Name</th>
<th>Confirm?</th>
<td>{{ row.common_name }}</td>
<td><code>{{ row.user_input }}</code></td>
<td><code>{{ row.suggested_name }}</code></td>
<td>
<input type="checkbox" name="confirm_{{ row.uuid }}" value="1">
</td>
</tr>
</thead>
<tbody>
{% for row in review_list %}
<tr>
<td>{{ row.common_name }}</td>
<td>{{ row.suggested_name }}</td>
<td>
<input class="form-check-input" type="checkbox" name="confirm_{{ row.uuid }}" value="1" checked>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No suggestions were made. You can safely continue.</p>
<p>No matches found that need confirmation.</p>
{% endif %}
<div class="mt-3">
<button type="submit" class="btn btn-primary">Confirm and Import</button>
</div>
<button type="submit" class="btn btn-primary mt-3">Finalize Import</button>
</form>
</div>
{% endblock %}

View File

@ -1,10 +1,8 @@
{% extends "core_ui/base.html" %}
{% block title %}CSV Import{% endblock %}
{% block content %}
<div class="container py-4">
<h2 class="mb-4">📤 Import Plant Data</h2>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
@ -15,14 +13,13 @@
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<div class="mb-3">
<label for="file" class="form-label">Choose CSV File</label>
<input type="file" class="form-control" id="file" name="file" required>
<div class="form-text">
Must include: <code>uuid</code>, <code>plant_type</code>, <code>name</code><br>
Required: <code>uuid</code>, <code>plant_type</code>, <code>name</code><br>
Optional: <code>scientific_name</code>, <code>mother_uuid</code>
</div>
</div>