convert OPT from a dict to a class

This commit is contained in:
2021-08-29 20:13:26 +10:00
parent f9bd558727
commit 10fcda0d7c
7 changed files with 347 additions and 177 deletions

146
files.py
View File

@@ -18,6 +18,7 @@ import time
import re
import json
from flask_login import login_required, current_user
from options import Options
################################################################################
# Local Class imports
@@ -170,85 +171,20 @@ def ClearJM_Message(id):
db.session.commit()
return
################################################################################
# SetViewingOptions: defines set of default values for viewing (order/size, etc.)
# and if a request object (from a POST) is passed in, it returns those instead
# it also handles the cwd appropriately
################################################################################
def SetViewingOptions( request ):
OPT={}
OPT['noo']="Oldest"
OPT['grouping']="None"
OPT['how_many']="50"
OPT['offset']="0"
OPT['size']="128"
settings=Settings.query.first()
if 'orig_url' in request.form:
print("okay, this has come from a viewer.html and needs next/prev how_many, recreate orig critiera...")
url = request.form['orig_url']
else:
url = request.path
if 'files_sp' in url:
OPT['noo']="A to Z"
OPT['folders']=True
OPT['path_type'] = 'Storage'
OPT['cwd']='static/Storage'
OPT['paths'] = settings.storage_path.split("#")
elif 'files_rbp' in url:
OPT['folders']=True
OPT['path_type'] = 'Bin'
OPT['cwd']='static/Bin'
OPT['paths'] = settings.recycle_bin_path.split("#")
else:
OPT['folders']=False
OPT['path_type'] = 'Import'
OPT['cwd']='static/Import'
OPT['paths'] = settings.import_path.split("#")
OPT['root']=OPT['cwd']
# the above are defaults, if we are here, then we have current values, use them instead
if request.method=="POST":
OPT['noo']=request.form['noo']
OPT['how_many']=request.form['how_many']
OPT['offset']=int(request.form['offset'])
OPT['grouping']=request.form['grouping']
OPT['size'] = request.form['size']
# seems html cant do boolean, but uses strings so convert
if request.form['folders'] == "False":
OPT['folders']=False
if request.form['folders'] == "True":
OPT['folders']=True
# have to force grouping to None if we flick to folders from a flat
# view with grouping (otherwise we print out group headings for
# child content that is not in the CWD)
OPT['grouping']=None
OPT['cwd'] = request.form['cwd']
if 'fullscreen' in request.form:
OPT['fullscreen']=request.form['fullscreen']
if 'prev' in request.form:
OPT['offset'] -= int(OPT['how_many'])
if OPT['offset'] < 0:
OPT['offset']=0
if 'next' in request.form:
OPT['offset'] += int(OPT['how_many'])
return OPT
################################################################################
# GetEntriesInFlatView: func. to retrieve DB entries appropriate for flat view
################################################################################
def GetEntriesInFlatView( OPT, prefix ):
entries=[]
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()
elif OPT['noo'] == "Newest":
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).offset(OPT['offset']).limit(OPT['how_many']).all()
elif OPT['noo'] == "Z to A":
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(Entry.name.desc()).offset(OPT['offset']).limit(OPT['how_many']).all()
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()
elif OPT.noo == "Newest":
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(File.year.desc(),File.month.desc(),File.day.desc(),Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
elif OPT.noo == "Z to A":
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(Entry.name.desc()).offset(OPT.offset).limit(OPT.how_many).all()
else:
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(Entry.name).offset(OPT['offset']).limit(OPT['how_many']).all()
entries+=Entry.query.join(File).join(EntryDirLink).join(Dir).join(PathDirLink).join(Path).filter(Path.path_prefix==prefix).order_by(Entry.name).offset(OPT.offset).limit(OPT.how_many).all()
return entries
################################################################################
@@ -258,7 +194,7 @@ def GetEntriesInFlatView( OPT, prefix ):
def GetEntriesInFolderView( OPT, prefix ):
entries=[]
# 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':
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:
@@ -266,7 +202,7 @@ def GetEntriesInFolderView( OPT, prefix ):
# although this is 1 entry, needs to come back via all() to be iterable
entries+= Entry.query.filter(Entry.id==dir.id).all()
else:
rp = OPT['cwd'].replace( prefix, '' )
rp = OPT.cwd.replace( prefix, '' )
# when in subdirs, replacing prefix will leave the first char as /, get rid of it
if len(rp) and rp[0] == '/':
rp=rp[1:]
@@ -274,22 +210,22 @@ def GetEntriesInFolderView( OPT, prefix ):
# this can occur if the path in settings does not exist as it wont be in # the DB
if not dir:
return entries
if OPT['noo'] == "Z to A" or "Newest":
if OPT.noo == "Z to A" or "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
else:
entries+= Entry.query.join(EntryDirLink).join(FileType).filter(EntryDirLink.dir_eid==dir.id).filter(FileType.name=='Directory').order_by(Entry.name).all()
# 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()
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()
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()
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()
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()
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()
# 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()
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()
return entries
@@ -303,19 +239,19 @@ def GetEntries( OPT ):
search_term=request.form['search_term']
if 'AI:' in search_term:
search_term = search_term.replace('AI:','')
all_entries = Entry.query.join(File).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()
all_entries = Entry.query.join(File).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()
else:
file_data=Entry.query.join(File).filter(Entry.name.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()
dir_data=Entry.query.join(File).join(EntryDirLink).join(Dir).filter(Dir.rel_path.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()
ai_data=Entry.query.join(File).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()
file_data=Entry.query.join(File).filter(Entry.name.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()
dir_data=Entry.query.join(File).join(EntryDirLink).join(Dir).filter(Dir.rel_path.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()
ai_data=Entry.query.join(File).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()
all_entries = file_data + dir_data + ai_data
return all_entries
for path in OPT['paths']:
for path in OPT.paths:
if not os.path.exists(path):
continue
prefix = SymlinkName(OPT['path_type'],path,path+'/')
if OPT['folders']:
prefix = SymlinkName(OPT.path_type,path,path+'/')
if OPT.folders:
entries+=GetEntriesInFolderView( OPT, prefix )
else:
entries+=GetEntriesInFlatView( OPT, prefix )
@@ -327,7 +263,7 @@ def GetEntries( OPT ):
@app.route("/file_list_ip", methods=["GET","POST"])
@login_required
def file_list_ip():
OPT=SetViewingOptions( request )
OPT=Options( request )
entries=GetEntries( OPT )
return render_template("file_list.html", page_title='View File Details (Import Path)', entry_data=entries, OPT=OPT )
@@ -337,10 +273,10 @@ def file_list_ip():
@app.route("/files_ip", methods=["GET", "POST"])
@login_required
def files_ip():
OPT=SetViewingOptions( request )
OPT=Options( request )
entries=GetEntries( OPT )
people = Person.query.all()
return render_template("files.html", page_title=f"View Files ({OPT['path_type']} Path)", entry_data=entries, OPT=OPT, people=people )
return render_template("files.html", page_title=f"View Files ({OPT.path_type} Path)", entry_data=entries, OPT=OPT, people=people )
################################################################################
# /files -> show thumbnail view of files from storage_path
@@ -348,10 +284,10 @@ def files_ip():
@app.route("/files_sp", methods=["GET", "POST"])
@login_required
def files_sp():
OPT=SetViewingOptions( request )
OPT=Options( request )
entries=GetEntries( OPT )
people = Person.query.all()
return render_template("files.html", page_title=f"View Files ({OPT['path_type']} Path)", entry_data=entries, OPT=OPT, people=people )
return render_template("files.html", page_title=f"View Files ({OPT.path_type} Path)", entry_data=entries, OPT=OPT, people=people )
################################################################################
@@ -360,10 +296,10 @@ def files_sp():
@app.route("/files_rbp", methods=["GET", "POST"])
@login_required
def files_rbp():
OPT=SetViewingOptions( request )
OPT=Options( request )
entries=GetEntries( OPT )
people = Person.query.all()
return render_template("files.html", page_title=f"View Files ({OPT['path_type']} Path)", entry_data=entries, OPT=OPT )
return render_template("files.html", page_title=f"View Files ({OPT.path_type} Path)", entry_data=entries, OPT=OPT )
################################################################################
@@ -372,9 +308,9 @@ def files_rbp():
@app.route("/search", methods=["GET","POST"])
@login_required
def search():
OPT=SetViewingOptions( request )
OPT=Options( request )
# always show flat results for search to start with
OPT['folders']=False
OPT.folders=False
entries=GetEntries( OPT )
return render_template("files.html", page_title='View Files', search_term=request.form['search_term'], entry_data=entries, OPT=OPT )
@@ -522,7 +458,7 @@ def move_files():
@app.route("/viewlist", methods=["POST"])
@login_required
def viewlist():
OPT=SetViewingOptions( request )
OPT=Options( request )
# Get next/prev set of data - e.g. if next set, then it will use orig_url
# to go forward how_many from offset and then use viewer.html to show that
# first obj of the new list of entries
@@ -531,10 +467,10 @@ def viewlist():
# it) and it just happened to also be the last in the DB...
if not entries:
# undo the skip by how_many and getentries again
OPT['offset'] -= int(OPT['how_many'])
OPT.offset -= int(OPT.how_many)
entries=GetEntries( OPT )
# now flag we are at the last in db, to reset current below
OPT['last_entry_in_db']=1
OPT.last_entry_in_db=1
objs = {}
eids=""
for e in entries:
@@ -550,10 +486,10 @@ def viewlist():
current = int(lst[0])
if 'prev' in request.form:
current = int(lst[-1])
if 'last_entry_in_db' in OPT:
if hasattr( OPT, 'last_entry_in_db' ):
# force this back to the last image of the last page - its the last in the DB, so set OPT for it
current = int(lst[-1])
OPT['last_entry_in_db']=current
OPT.last_entry_in_db=current
return render_template("viewer.html", current=current, eids=eids, objs=objs, OPT=OPT )
@@ -563,7 +499,7 @@ def viewlist():
@app.route("/view/<id>", methods=["POST"])
@login_required
def view_img(id):
OPT=SetViewingOptions( request )
OPT=Options( request )
eids=request.form['eids'].rstrip(',')
objs = {}
lst = eids.split(',')
@@ -638,7 +574,7 @@ def custom_static(filename):
###############################################################################
# This func creates a new filter in jinja2 to test to see if the Dir being
# checked, is a top-level folder of 'OPT['cwd']'
# checked, is a top-level folder of 'OPT.cwd'
################################################################################
@app.template_filter('TopLevelFolderOf')
def _jinja2_filter_toplevelfolderof(path, cwd):