From 2dab42d7124e2f999392ecbe596d6f10ba8aba4f Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Thu, 20 Jan 2022 14:14:14 +1100 Subject: [PATCH] renamed pa_pref to better named pa_user_state, changed code to adapt and made new_view and viewlist work with newer Options/pa_user_state - still more work but it should be functional again --- states.py | 165 ++++++++++++++++++++++++++++++++++++++++++ templates/states.html | 34 +++++++++ 2 files changed, 199 insertions(+) create mode 100644 states.py create mode 100644 templates/states.html diff --git a/states.py b/states.py new file mode 100644 index 0000000..d7cd060 --- /dev/null +++ b/states.py @@ -0,0 +1,165 @@ +from settings import Settings, SettingsRBPath, SettingsIPath, SettingsSPath +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 + + +################################################################################ +# PA_UserState: preference data for a given user / path_type combo, so a given user +# and their prefs for say the import path(s) and storage path(s) etc, each +# path_type has different defaults, and keeping those works better +################################################################################ +class PA_UserState(db.Model): + __tablename__ = "pa_user_state" + pa_user_dn = db.Column(db.String, db.ForeignKey('pa_user.dn'), primary_key=True ) + path_type = db.Column(db.String, primary_key=True, unique=False, nullable=False ) + noo = db.Column(db.String, unique=False, nullable=False ) + grouping = db.Column(db.String, unique=False, nullable=False ) + how_many = db.Column(db.Integer, unique=False, nullable=False ) + st_offset = db.Column(db.Integer, unique=False, nullable=False ) + size = db.Column(db.Integer, unique=False, nullable=False ) + folders = db.Column(db.Boolean, unique=False, nullable=False ) + fullscreen = db.Column(db.Boolean, unique=False, nullable=False ) + root = db.Column(db.String, unique=False, nullable=False ) + cwd = db.Column(db.String, unique=False, nullable=False ) + + def __repr__(self): + return f"" + + +################################################################################ +# Options: class to store 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, paths, fullscreen, search, etc. +################################################################################ +class Options(PA): + def __init__(self, request): + if 'orig_url' in request.form: + url = request.form['orig_url'] + else: + url = request.path + self.orig_url=url + if 'files_sp' in url: + self.path_type = 'Storage' + self.paths = SettingsSPath() + pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type).first() + if pref: + self.folders=pref.folders + self.noo=pref.noo + else: + self.folders=True + self.noo="A to Z" + elif 'files_rbp' in url: + self.path_type = 'Bin' + self.paths = [] + self.paths.append(SettingsRBPath()) + pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type).first() + if pref: + self.folders=pref.folders + self.noo=pref.noo + else: + self.folders=True + self.noo="A to Z" + elif 'search' in url: + self.path_type = 'Search' + self.paths = None + pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type).first() + if pref: + self.folders=pref.folders + self.noo=pref.noo + else: + self.folders=False + self.noo="Oldest" + else: + self.path_type = 'Import' + self.paths = SettingsIPath() + pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type).first() + if pref: + self.folders=pref.folders + self.noo=pref.noo + else: + self.folders=False + self.noo="Oldest" + + if pref: + self.grouping=pref.grouping + self.how_many=pref.how_many + self.offset=pref.st_offset + self.size=pref.size + self.root=pref.root + self.cwd=pref.cwd + else: + self.grouping="None" + self.how_many="50" + self.offset="0" + self.size="128" + self.root='static/' + self.path_type + self.cwd=self.root + + + # 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": + if 'noo' in request.form: + self.noo=request.form['noo'] + if 'how_many' in request.form: + self.how_many=request.form['how_many'] + if 'offset' in request.form: + self.offset=int(request.form['offset']) + if 'grouping' in request.form: + self.grouping=request.form['grouping'] + # this can be null if we come from view by details + if 'size' in request.form: + self.size = request.form['size'] + # seems html cant do boolean, but uses strings so convert + if 'folders' not in request.form or request.form['folders'] == "False": + self.folders=False + elif request.form['folders'] == "True": + self.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) + self.grouping=None + + # possible to not be set for an AI: search + if 'cwd' in request.form: + self.cwd = request.form['cwd'] + if 'fullscreen' in request.form: + self.fullscreen=request.form['fullscreen'] + else: + self.fullscreen=False + if 'prev' in request.form: + self.offset -= int(self.how_many) + if self.offset < 0: + self.offset=0 + if 'next' in request.form: + self.offset += int(self.how_many) + + pref=PA_UserState.query.filter(PA_UserState.pa_user_dn==current_user.dn,PA_UserState.path_type==self.path_type).first() + if not pref: + pref=PA_UserState( pa_user_dn=current_user.dn, path_type=self.path_type, 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 ) + else: + pref.noo=self.noo + pref.grouping=self.grouping + pref.how_many=self.how_many + pref.st_offset=self.offset + pref.size=self.size + pref.folders=self.folders + pref.root = self.root + pref.cwd = self.cwd + + db.session.add(pref) + db.session.commit() + + return + +################################################################################ +# /states -> GET only -> prints out list of all prefs (simple for now) +################################################################################ +@app.route("/states", methods=["GET"]) +@login_required +def states(): + states = PA_UserState.query.filter( PA_UserState.pa_user_dn==current_user.dn ).all() + return render_template("states.html", states=states ) + diff --git a/templates/states.html b/templates/states.html new file mode 100644 index 0000000..97b2c77 --- /dev/null +++ b/templates/states.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} + +{% block main_content %} +

PA User state page

+ +
+
+ The following values are based on navigating the application and are not set by hand. This page is for checking/debugging only. +
+
+ + + + + + {% for st in states %} + + + + + + + + + + + + + {% endfor %} + +
PathNew or OldestHow ManyFolders?Group byThumb sizeFullscreenDB retrieve offsetRootcwd
{{st.path_type}}{{st.noo}}{{st.how_many}}{{st.folders}}{{st.grouping}}{{st.size}}{{st.fullscreen}}{{st.st_offset}}{{st.root}}{{st.cwd}}
+
+
+{% endblock main_content %}