changing options on files_*, search, also next/prev all now use POST->redirect model, so should allow back/forward browser buttons to work - commit so we can test in PROD
This commit is contained in:
95
states.py
95
states.py
@@ -3,7 +3,7 @@ from flask import request, render_template, redirect, url_for
|
||||
from flask_login import login_required, current_user
|
||||
from main import db, app, ma
|
||||
from shared import PA
|
||||
|
||||
from user import PAUser
|
||||
|
||||
################################################################################
|
||||
# PA_UserState: preference data for a given user / path_type combo, so a given user
|
||||
@@ -30,9 +30,10 @@ class PA_UserState(db.Model):
|
||||
orig_ptype = db.Column(db.String, unique=False, nullable=False )
|
||||
# only used if view and orig_ptype was search
|
||||
orig_search_term = db.Column(db.String, unique=False, nullable=False )
|
||||
orig_url = db.Column(db.String, unique=False, nullable=False )
|
||||
|
||||
def __repr__(self):
|
||||
return f"<pa_user_dn: {self.pa_user_dn}, path_type: {self.path_type}, noo: {self.noo}, grouping: {self.grouping}, how_many: {self.how_many}, st_offset: {self.st_offset}, size: {self.size}, folders: {self.folders}, root: {self.root}, cwd: {self.cwd}, view_eid: {self.view_eid}, orig_ptype: {self.orig_ptype}, orig_search_term: {self.orig_search_term}>"
|
||||
return f"<pa_user_dn: {self.pa_user_dn}, path_type: {self.path_type}, noo: {self.noo}, grouping: {self.grouping}, how_many: {self.how_many}, st_offset: {self.st_offset}, size: {self.size}, folders: {self.folders}, root: {self.root}, cwd: {self.cwd}, view_eid: {self.view_eid}, orig_ptype: {self.orig_ptype}, orig_search_term: {self.orig_search_term}, orig_url: {self.orig_url}>"
|
||||
|
||||
|
||||
################################################################################
|
||||
@@ -44,52 +45,89 @@ class States(PA):
|
||||
def __init__(self, request):
|
||||
self.path_type=''
|
||||
self.url = request.path
|
||||
self.view_eid = None
|
||||
|
||||
print( f"States() - path={request.path}, ref={request.referrer}" )
|
||||
|
||||
# this is any next/prev or noo, grouping, etc. change (so use referrer to work out what to do with this)
|
||||
# because this can happen on a view, or files_up, etc. change this FIRST
|
||||
if 'ChangeFileOpts' in request.path:
|
||||
ref=request.referrer
|
||||
base=request.base_url
|
||||
base=base.replace("ChangeFileOpts", "")
|
||||
self.url = "/"+ref.replace(base, "" )
|
||||
print( f"started with ChangeFileOpts, so self.url now is {self.url}, bu={request.base_url}")
|
||||
|
||||
# if viewlist, then we really are a view, and view_eid should be in the form
|
||||
if 'viewlist' in request.path:
|
||||
self.path_type = 'View'
|
||||
self.view_eid = request.form['view_eid']
|
||||
self.url = request.form['orig_url']
|
||||
for el in request.form:
|
||||
print( f"{el}={request.form[el]}" )
|
||||
# this occurs ONLY when a POST to /view/<id> occurs (at this stage orig_url will be from an import, storage, bin or search)
|
||||
if 'orig_url' in request.form:
|
||||
self.path_type='View'
|
||||
elif 'view' in request.path:
|
||||
self.path_type = 'View'
|
||||
self.view_eid = self.url[6:]
|
||||
# use orig url to define defaults/look up states for 'last' import/storage/bin/search
|
||||
url = request.form['orig_url']
|
||||
# get the eid out of the url /view/<id>
|
||||
self.view_eid = request.path[6:]
|
||||
else:
|
||||
url = request.path
|
||||
self.view_eid = None
|
||||
if request.method == "POST":
|
||||
self.url = request.form['orig_url']
|
||||
else:
|
||||
# GET's occur on redirect, and we don't have a form, so get it
|
||||
# from pref
|
||||
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type,PA_UserState.view_eid==self.view_eid).first()
|
||||
self.url = pref.orig_url
|
||||
print( f"view/ so change url to: '{self.url}'" )
|
||||
|
||||
if 'files_ip' in url or 'file_list_ip' in url:
|
||||
print( f"NOW, url={self.url}" )
|
||||
|
||||
if 'files_ip' in self.url or 'file_list_ip' in self.url:
|
||||
if self.path_type == "View":
|
||||
self.orig_ptype = 'Import'
|
||||
self.orig_url = self.url
|
||||
else:
|
||||
self.path_type = 'Import'
|
||||
elif 'files_sp' in url:
|
||||
elif 'files_sp' in self.url:
|
||||
if self.path_type == "View":
|
||||
self.orig_ptype = 'Storage'
|
||||
self.orig_url = self.url
|
||||
else:
|
||||
self.path_type = 'Storage'
|
||||
elif 'files_rbp' in url:
|
||||
elif 'files_rbp' in self.url:
|
||||
if self.path_type == "View":
|
||||
self.orig_ptype = 'Bin'
|
||||
self.orig_url = self.url
|
||||
else:
|
||||
self.path_type = 'Bin'
|
||||
elif 'search' in url:
|
||||
elif 'search' in self.url:
|
||||
# okay if we are a search, but came from a view then get last_search_state form prefs and use it
|
||||
self.orig_search_term = self.url[8:]
|
||||
if self.path_type == "View":
|
||||
last_search_state = PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type=='Search').first()
|
||||
self.orig_search_term = last_search_state.orig_search_term
|
||||
print(f"view, url={self.url}")
|
||||
self.orig_ptype = 'Search'
|
||||
self.orig_url = self.url
|
||||
else:
|
||||
self.orig_search_term = url[8:]
|
||||
self.path_type = 'Search'
|
||||
elif 'view' in url:
|
||||
elif 'view' in self.url:
|
||||
# use url to get eid of viewed entry
|
||||
self.view_eid = self.url[6:]
|
||||
self.path_type="View"
|
||||
self.orig_url=self.url
|
||||
print( f"in view, eid={self.view_eid}, orig_url={self.orig_url}" )
|
||||
elif 'ChangeFileOpts' in self.url:
|
||||
print( f"ChangeFileOpts called, so all good?" )
|
||||
else:
|
||||
print( f"ERROR: DDP messed up, failed to match URL {url} for settings this will fail, redirecting to home" )
|
||||
print( f"referrer={request.referrer}" )
|
||||
print( f"ERROR: DDP messed up, failed to match URL {self.url} for settings this will fail, redirecting to home" )
|
||||
return
|
||||
|
||||
if self.path_type == 'View':
|
||||
print( f"its is a view, find the pref: {self.view_eid}" )
|
||||
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type,PA_UserState.view_eid==self.view_eid).first()
|
||||
if not pref:
|
||||
print( f"no pref" )
|
||||
else:
|
||||
print( f"pref={pref}" )
|
||||
elif self.path_type == 'Search':
|
||||
pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type,PA_UserState.orig_search_term==self.orig_search_term).first()
|
||||
else:
|
||||
@@ -106,6 +144,8 @@ class States(PA):
|
||||
self.cwd=pref.cwd
|
||||
self.orig_ptype=pref.orig_ptype
|
||||
self.orig_search_term=pref.orig_search_term
|
||||
self.orig_url = pref.orig_url
|
||||
self.view_eid = pref.view_eid
|
||||
else:
|
||||
self.folders=False
|
||||
self.noo="Oldest"
|
||||
@@ -122,10 +162,13 @@ class States(PA):
|
||||
self.orig_ptype=None
|
||||
if not hasattr(self, 'orig_search_term'):
|
||||
self.orig_search_term=None
|
||||
|
||||
self.orig_url = self.url
|
||||
|
||||
# the above are defaults, if we are here, then we have current values, use them instead if they are set -- AI: searches dont set them so then we use those in the DB first
|
||||
if request.method=="POST":
|
||||
print("this was a POST, so use form vals to update PREF" )
|
||||
for el in request.form:
|
||||
print( f"{el}={request.form[el]}" )
|
||||
if 'noo' in request.form:
|
||||
self.noo=request.form['noo']
|
||||
if 'how_many' in request.form:
|
||||
@@ -146,6 +189,8 @@ class States(PA):
|
||||
# view with grouping (otherwise we print out group headings for
|
||||
# child content that is not in the CWD)
|
||||
self.grouping=None
|
||||
if 'orig_url' in request.form:
|
||||
self.orig_url = request.form['orig_url']
|
||||
|
||||
# possible to not be set for an AI: search
|
||||
if 'cwd' in request.form:
|
||||
@@ -164,7 +209,8 @@ class States(PA):
|
||||
# now save pref (if this is 'another' search, view, etc. then it will add a row for it with matching search_term, or view_eid, etc.
|
||||
if not pref:
|
||||
pref=PA_UserState( pa_user_dn=current_user.dn, path_type=self.path_type, view_eid=self.view_eid, noo=self.noo, grouping=self.grouping, how_many=self.how_many,
|
||||
st_offset=self.offset, size=self.size, folders=self.folders, root=self.root, cwd=self.cwd, orig_ptype=self.orig_ptype, orig_search_term=self.orig_search_term )
|
||||
st_offset=self.offset, size=self.size, folders=self.folders, root=self.root, cwd=self.cwd,
|
||||
orig_ptype=self.orig_ptype, orig_search_term=self.orig_search_term, orig_url=self.orig_url )
|
||||
else:
|
||||
pref.pa_user_dn=current_user.dn
|
||||
pref.path_type=self.path_type
|
||||
@@ -179,10 +225,13 @@ class States(PA):
|
||||
pref.cwd = self.cwd
|
||||
pref.orig_ptype = self.orig_ptype
|
||||
pref.orig_search_term = self.orig_search_term
|
||||
pref.orig_url = self.orig_url
|
||||
|
||||
db.session.add(pref)
|
||||
db.session.commit()
|
||||
|
||||
print( f"saved pref={pref}" )
|
||||
|
||||
return
|
||||
|
||||
################################################################################
|
||||
@@ -191,6 +240,6 @@ class States(PA):
|
||||
@app.route("/states", methods=["GET"])
|
||||
@login_required
|
||||
def states():
|
||||
user = PAUser.query.filter( PAUser.dn==current_user.dn ).one()
|
||||
states = PA_UserState.query.filter( PA_UserState.pa_user_dn==current_user.dn ).all()
|
||||
return render_template("states.html", states=states )
|
||||
|
||||
return render_template("states.html", user=user, states=states )
|
||||
|
||||
Reference in New Issue
Block a user