partial fix for various BUGs with going past the end of a viewing list... we now keep num_entries (of files) in pa_user_state, and use it to stop going before first or past last entry, even from viewlist, so this fixes many isseus... Final bug(s) are relating to multiple Dirs in a Path and its feeling too complex for no real gain, going to remove the feature, but for now, this version works / can be made to PROD

This commit is contained in:
2022-01-28 15:52:35 +11:00
parent 297b12b2b1
commit 47039ec35c
5 changed files with 133 additions and 62 deletions

110
files.py
View File

@@ -185,6 +185,8 @@ def UpdatePref( pref, OPT ):
pref.first_eid=OPT.first_eid
if OPT.last_eid>0:
pref.last_eid=OPT.last_eid
if OPT.num_entries>0:
pref.num_entries=OPT.num_entries
db.session.add(pref)
db.session.commit()
@@ -193,6 +195,7 @@ def UpdatePref( pref, OPT ):
################################################################################
def GetEntriesInFlatView( OPT, prefix ):
entries=[]
num_entries=0
if OPT.noo == "Oldest":
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(File.year,File.month,File.day,Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
@@ -208,17 +211,21 @@ def GetEntriesInFlatView( OPT, prefix ):
last_entry=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(Entry.name.desc()).limit(1)
if OPT.first_eid == 0 and OPT.offset == 0 and len(entries):
OPT.first_eid = entries[0].id
# FIXME: would prefer to only get this if needed (e.g. when last_eid is 0),
# but with multiple dirs in say import path, then we only set last_eid on
# the first dir... need to to think this through better, pass param maybe?
le=last_entry.all()
if len(le):
OPT.last_eid = le[0].id
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==OPT.path_type).first()
UpdatePref( pref, OPT )
return entries
num_entries=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).count()
print( f"GetEntriesInFlatView - pfx={prefix}, ne={num_entries}" )
for e in entries:
print( f"e={e.id}- {e.name}" )
if OPT.last_eid==0:
le=last_entry.all()
if len(le):
OPT.last_eid = le[0].id
print("1st run, fe={OPT.first_eid}, le={OPT.last_eid}" )
return entries, num_entries
################################################################################
# GetEntriesInFolderView: func. to retrieve DB entries appropriate for folder view
@@ -226,12 +233,13 @@ def GetEntriesInFlatView( OPT, prefix ):
################################################################################
def GetEntriesInFolderView( OPT, prefix ):
entries=[]
num_entries=0
# okay the root cwd is fake, so treat it specially - its Dir can be found by path with dir.rel_path=''
if os.path.dirname(OPT.cwd) == 'static':
dir=Entry.query.join(Dir).join(PathDirLink).join(Path).filter(Dir.rel_path=='').filter(Path.path_prefix==prefix).order_by(Entry.name).first()
# this can occur if the path in settings does not exist as it wont be in # the DB
if not dir:
return entries
return entries, num_entries
# although this is 1 entry, needs to come back via all() to be iterable
entries+= Entry.query.filter(Entry.id==dir.id).all()
else:
@@ -242,7 +250,7 @@ def GetEntriesInFolderView( OPT, prefix ):
dir=Entry.query.join(Dir).join(PathDirLink).join(Path).filter(Dir.rel_path==rp).filter(Path.path_prefix==prefix).order_by(Entry.name).first()
# this can occur if the path in settings does not exist as it wont be in # the DB
if not dir:
return entries
return entries, 0
if OPT.noo == "Z to A" or OPT.noo == "Newest":
entries+= Entry.query.join(EntryDirLink).join(FileType).filter(EntryDirLink.dir_eid==dir.id).filter(FileType.name=='Directory').order_by(Entry.name.desc()).all()
# just do A to Z / Oldest by default or if no valid option
@@ -251,28 +259,32 @@ def GetEntriesInFolderView( OPT, prefix ):
# add any files at the current CWD (based on dir_eid in DB)
if OPT.noo == "Oldest":
entries+=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year,File.month,File.day,Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).limit(1)
file_entries=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year,File.month,File.day,Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name)
elif OPT.noo == "Newest":
entries+=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year,File.month,File.day,Entry.name).limit(1)
file_entries=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(File.year,File.month,File.day,Entry.name)
elif OPT.noo == "Z to A":
entries+=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name.desc()).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name).limit(1)
file_entries=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name.desc()).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name)
# just do A to Z by default or if no valid option
else:
entries+=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name.desc()).limit(1)
if OPT.first_eid == 0 and OPT.offset == 0 and len(entries):
OPT.first_eid = entries[0].id
if OPT.last_eid == 0:
le=last_entry.all()
file_entries=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
last_entry=Entry.query.join(File).join(EntryDirLink).filter(EntryDirLink.dir_eid==dir.id).order_by(Entry.name.desc())
if OPT.offset == 0 and len(file_entries):
OPT.first_eid = file_entries[0].id
if len(file_entries):
num_entries=last_entry.count()
le=last_entry.limit(1).all()
if len(le):
OPT.last_eid = le[0].id
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==OPT.path_type).first()
UpdatePref( pref, OPT )
return entries
entries += file_entries;
print( f"FOLD, fe={OPT.first_eid}, le={OPT.last_eid}, ne={num_entries}" )
for e in entries:
print( f"FOLD e={e.id} - {e.type.name} - {e.name}" )
return entries, num_entries
################################################################################
# /GetEntries -> helper function that Gets Entries for required files to show
@@ -286,6 +298,7 @@ def GetEntries( OPT ):
search_term = search_term.replace('AI:','')
all_entries = Entry.query.join(File).distinct().join(FaceFileLink).join(Face).join(FaceRefimgLink).join(Refimg).join(PersonRefimgLink).join(Person).filter(Person.tag.ilike(f"%{search_term}%")).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
if OPT.last_eid == 0:
OPT.num_entries = Entry.query.join(File).distinct().join(FaceFileLink).join(Face).join(FaceRefimgLink).join(Refimg).join(PersonRefimgLink).join(Person).filter(Person.tag.ilike(f"%{search_term}%")).count()
last_entry=Entry.query.join(File).distinct().join(FaceFileLink).join(Face).join(FaceRefimgLink).join(Refimg).join(PersonRefimgLink).join(Person).filter(Person.tag.ilike(f"%{search_term}%")).order_by(File.year,File.month,File.day,Entry.name.desc()).limit(1).all()
if len(last_entry):
OPT.last_eid = last_entry[0].id
@@ -327,6 +340,12 @@ def GetEntries( OPT ):
order_desc=f"f.year desc, f.month desc, f.day desc, e.name"
order_asc=f"f.year, f.month, f.day, e.name desc"
#num_entries
num_e_sql = f"select count(1) from ( {by_fname} union {by_dirname} union {by_ai} ) as foo"
num_e_result = db.engine.execute( num_e_sql )
for res in num_e_result:
OPT.num_entries=res.count
last_entry_sql= f"{sel_no_order} order by {order_asc} limit 1"
last_entry=db.engine.execute( last_entry_sql )
# can only be 1 due to limit above
@@ -352,14 +371,38 @@ def GetEntries( OPT ):
elif OPT.path_type == 'Bin':
paths.append(SettingsRBPath())
num_paths = len(paths)
num_entries=0
path_cnt=1
# if we have not set last_eid yet, then we need to 'reset' it during the
# path loop below (if we have more than one dir in (say) Import path)
if OPT.last_eid == 0 or OPT.folders:
update_last_eid = True
else:
update_last_eid = False
for path in paths:
if not os.path.exists(path):
continue
prefix = SymlinkName(OPT.path_type,path,path+'/')
if OPT.folders:
entries+=GetEntriesInFolderView( OPT, prefix )
tmp_ents, tmp_num_ents = GetEntriesInFolderView( OPT, prefix )
else:
entries+=GetEntriesInFlatView( OPT, prefix )
tmp_ents, tmp_num_ents = GetEntriesInFlatView( OPT, prefix )
entries += tmp_ents
num_entries += tmp_num_ents
# if we have another path, keep adding num_etnries, and last_eid is the last path, not this one, so reset to 0
if update_last_eid and path_cnt < num_paths:
OPT.last_eid=0
path_cnt += 1
if update_last_eid:
print( f"num_ent={OPT.num_entries} -- need to SAVE this in pa_user_state")
# find pref... via path_type if we are here
OPT.num_entries=num_entries
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==OPT.path_type).first()
UpdatePref( pref, OPT )
return entries
################################################################################
@@ -684,7 +727,14 @@ def view(id):
for face in e.file_details.faces:
face.locn = json.loads(face.locn)
eids=eids.rstrip(",")
return render_template("viewer.html", current=int(id), eids=eids, objs=objs, OPT=OPT )
# jic, sometimes we trip this, and rather than show broken pages / destroy
# face locn data, just warn & redirect
if id not in eids:
print( f"OPT={OPT}, id={id}, eids={eids}")
st.SetMessage("Sorry, viewing data is confused, cannot view this image now", "warning" )
return redirect("/")
else:
return render_template("viewer.html", current=int(id), eids=eids, objs=objs, OPT=OPT )
##################################################################################
# /view/id -> grabs data from DB and views it (POST -> set state, redirect to GET)