remove db.session.execute and use simple ORM statements - less efficient, but only really deleting a small amount anyway
This commit is contained in:
24
person.py
24
person.py
@@ -3,7 +3,7 @@ from flask_wtf import FlaskForm
|
||||
from flask import request, render_template, redirect, url_for, make_response, jsonify
|
||||
from main import db, app, ma
|
||||
from settings import Settings, AIModel
|
||||
from sqlalchemy import Sequence
|
||||
from sqlalchemy import Sequence, func
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from flask_login import login_required, current_user
|
||||
from werkzeug.utils import secure_filename
|
||||
@@ -143,15 +143,14 @@ def TempRefimgFile( data, tag ):
|
||||
@login_required
|
||||
def persons():
|
||||
persons = Person.query.order_by(Person.tag).all()
|
||||
# get the num matches for each person too, and allow link for it
|
||||
stats = list( db.session.execute( "select p.tag, count(f.id) from person p, face f, face_file_link ffl, face_refimg_link frl, person_refimg_link prl where p.id = prl.person_id and prl.refimg_id = frl.refimg_id and frl.face_id = ffl.face_id and ffl.face_id = f.id group by p.tag" ) )
|
||||
for p in persons:
|
||||
if not p.refimg:
|
||||
continue
|
||||
for s in stats:
|
||||
if p.tag == s[0]:
|
||||
p.num_matches = s[1];
|
||||
break
|
||||
# okay see how many faces match this person
|
||||
stat=Person.query.filter(Person.id==p.id).join(PersonRefimgLink).join(Refimg).join(FaceRefimgLink).with_entities( func.count( Person.tag ) ).group_by( Person.tag ).first()
|
||||
if stat:
|
||||
p.num_matches=stat[0]
|
||||
|
||||
return render_template("persons.html", persons=persons)
|
||||
|
||||
|
||||
@@ -201,10 +200,13 @@ def person(id):
|
||||
person = Person.query.get(id)
|
||||
if 'delete' in request.form:
|
||||
SetFELog( f"Successfully deleted Person: ({person.tag})" )
|
||||
# do linkages by hand, or one day replace with delete cascade in the DB defintions
|
||||
db.session.execute( f"delete from face_refimg_link frl where refimg_id in ( select refimg_id from person_refimg_link where person_id = {id} )" )
|
||||
db.session.execute( f"delete from person_refimg_link where person_id = {id}" )
|
||||
db.session.execute( f"delete from refimg where id not in ( select refimg_id from person_refimg_link )" )
|
||||
|
||||
# delete refimgs that are associated with this person (one-by-one), not super efficient
|
||||
# but simple/clearer than cascades for now
|
||||
for ref in person.refimg:
|
||||
FaceRefimgLink.query.filter(FaceRefimgLink.refimg_id==ref.id).delete()
|
||||
PersonRefimgLink.query.filter(PersonRefimgLink.refimg_id==ref.id).delete()
|
||||
Refimg.query.filter(Refimg.id==ref.id).delete()
|
||||
|
||||
# now can delete the person entry with no foreign key data left
|
||||
Person.query.filter(Person.id==id).delete()
|
||||
|
||||
Reference in New Issue
Block a user