converted over to a flat settings table, no more key-value pairs of settings
This commit is contained in:
9
files.py
9
files.py
@@ -97,10 +97,9 @@ class FileData():
|
|||||||
# multiple valid paths in import_path) #
|
# multiple valid paths in import_path) #
|
||||||
##############################################################################
|
##############################################################################
|
||||||
def GenerateFileData(self):
|
def GenerateFileData(self):
|
||||||
import_path = Settings.query.filter(Settings.name=="import_path").all()[0].value
|
settings = Settings.query.all()
|
||||||
last_import_date_obj = Settings.query.filter(Settings.name=="last_import_date").all()
|
last_import_date = settings[0].last_import_date
|
||||||
last_import_date = float(last_import_date_obj[0].value)
|
paths = settings[0].import_path.split("#")
|
||||||
paths= import_path.split("#")
|
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
print( "GenerateFileData: Checking {}".format( path ) )
|
print( "GenerateFileData: Checking {}".format( path ) )
|
||||||
@@ -144,7 +143,7 @@ class FileData():
|
|||||||
db.session.add(file_obj)
|
db.session.add(file_obj)
|
||||||
else:
|
else:
|
||||||
print( "{} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ) )
|
print( "{} - {} is OLDER than {}".format( file, stat.st_ctime, last_import_date ) )
|
||||||
last_import_date_obj[0].value = str(time.time())
|
settings[0].last_import_date = time.time()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
self.view_list = Files.query.all()
|
self.view_list = Files.query.all()
|
||||||
return self
|
return self
|
||||||
|
|||||||
50
settings.py
50
settings.py
@@ -1,4 +1,4 @@
|
|||||||
from wtforms import SubmitField, StringField, HiddenField, validators, Form
|
from wtforms import SubmitField, StringField, FloatField, HiddenField, validators, Form
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from flask import request, render_template, redirect
|
from flask import request, render_template, redirect
|
||||||
from main import db, app, ma
|
from main import db, app, ma
|
||||||
@@ -11,13 +11,13 @@ from status import st, Status
|
|||||||
################################################################################
|
################################################################################
|
||||||
class Settings(db.Model):
|
class Settings(db.Model):
|
||||||
id = db.Column(db.Integer, db.Sequence('settings_id_seq'), primary_key=True )
|
id = db.Column(db.Integer, db.Sequence('settings_id_seq'), primary_key=True )
|
||||||
name = db.Column(db.String, unique=True, nullable=True )
|
import_path = db.Column(db.String)
|
||||||
value = db.Column(db.String, unique=True, nullable=True )
|
last_import_date = db.Column(db.Float)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<id: {}, name: {}, value: {}>".format(self.id, self.name, self.value)
|
return "<id: {}, import_path: {}, last_import_date: {}>".format(self.id, self.import_path, self.last_import_date)
|
||||||
|
|
||||||
###############################################################################
|
################################################################################
|
||||||
# Helper class that inherits a .dump() method to turn class Author into json / useful in jinja2
|
# Helper class that inherits a .dump() method to turn class Author into json / useful in jinja2
|
||||||
################################################################################
|
################################################################################
|
||||||
class SettingsSchema(ma.SQLAlchemyAutoSchema):
|
class SettingsSchema(ma.SQLAlchemyAutoSchema):
|
||||||
@@ -25,7 +25,7 @@ class SettingsSchema(ma.SQLAlchemyAutoSchema):
|
|||||||
model = Settings
|
model = Settings
|
||||||
ordered = True
|
ordered = True
|
||||||
|
|
||||||
settings_schema=SettingsSchema()
|
settings_schema = SettingsSchema(many=True)
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Helper class that defines a form for Settings, used to make html <form>
|
# Helper class that defines a form for Settings, used to make html <form>
|
||||||
@@ -33,41 +33,37 @@ settings_schema=SettingsSchema()
|
|||||||
################################################################################
|
################################################################################
|
||||||
class SettingsForm(FlaskForm):
|
class SettingsForm(FlaskForm):
|
||||||
id = HiddenField()
|
id = HiddenField()
|
||||||
name = StringField('Name:', [validators.DataRequired()])
|
import_path = StringField('Path to import from:', [validators.DataRequired()])
|
||||||
value = StringField('value:', [validators.DataRequired()])
|
last_import_date = FloatField('Date of last import:', [validators.DataRequired()])
|
||||||
submit = SubmitField('Save' )
|
submit = SubmitField('Save' )
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# /settings -> show current settings
|
# /settings -> show current settings
|
||||||
################################################################################
|
################################################################################
|
||||||
@app.route("/settings", methods=["GET"])
|
@app.route("/settings", methods=["GET", "POST"])
|
||||||
def settings():
|
def settings():
|
||||||
sets = Settings.query.all()
|
|
||||||
form = SettingsForm()
|
|
||||||
page_title='Show Settings'
|
|
||||||
return render_template("settings.html", objects=sets, form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# /setting/<id> -> show current settings
|
|
||||||
################################################################################
|
|
||||||
@app.route("/setting/<id>", methods=["GET", "POST"])
|
|
||||||
def setting(id):
|
|
||||||
form = SettingsForm(request.form)
|
form = SettingsForm(request.form)
|
||||||
if request.method == 'POST' and form.validate():
|
if request.method == 'POST' and form.validate():
|
||||||
try:
|
try:
|
||||||
|
# HACK, I don't really need an id here, but sqlalchemy get weird
|
||||||
|
# without one, so just grab the id of the only row there, it will
|
||||||
|
# do...
|
||||||
|
id = Settings.query.all()[0].id
|
||||||
s = Settings.query.get(id)
|
s = Settings.query.get(id)
|
||||||
if 'submit' in request.form:
|
if 'submit' in request.form:
|
||||||
st.SetMessage("Successfully Updated Setting (name={})".format(s.name) )
|
st.SetMessage("Successfully Updated Settings" )
|
||||||
s.name = request.form['name']
|
s.import_path = request.form['import_path']
|
||||||
s.value = request.form['value']
|
s.last_import_date = request.form['last_import_date']
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect( '/settings' )
|
return redirect( '/settings' )
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
st.SetAlert( "danger" )
|
st.SetAlert( "danger" )
|
||||||
st.SetMessage( "<b>Failed to modify Setting:</b> {}".format(e.orig) )
|
st.SetMessage( "<b>Failed to modify Setting:</b> {}".format(e.orig) )
|
||||||
return render_template("edit_setting.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
page_title='Show Settings'
|
||||||
|
return render_template("settings.html", form=form, page_title=page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
||||||
else:
|
else:
|
||||||
s = Settings.query.get(id)
|
tmp_sets = Settings.query.all()
|
||||||
page_title='Edit Setting: {}'.format(s.name)
|
sets = settings_schema.dump( tmp_sets )
|
||||||
form = SettingsForm(request.values, obj=s)
|
form = SettingsForm(obj=tmp_sets[0])
|
||||||
return render_template("setting.html", objects=s, form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
page_title='Show Settings'
|
||||||
|
return render_template("settings.html", form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
{% extends "base.html" %} {% block main_content %}
|
|
||||||
<div class="container">
|
|
||||||
<h3 class="offset-lg-2">{{page_title}}</h3>
|
|
||||||
<div class="row">
|
|
||||||
<form class="form form-inline col-xl-12" action="" method="POST">
|
|
||||||
{% for field in form %}
|
|
||||||
{% if field.type == 'HiddenField' or field.type == 'CSRFTokenField' %}
|
|
||||||
{{field}}
|
|
||||||
{% elif field.type != 'SubmitField' %}
|
|
||||||
<div class="form-row col-lg-12">
|
|
||||||
{{ field.label( class="col-lg-2" ) }}
|
|
||||||
{{ field( class="form-control col-lg-4" ) }}
|
|
||||||
</div class="form-row col-lg-12">
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
<div class="row col-lg-12">
|
|
||||||
<br>
|
|
||||||
</div class="row">
|
|
||||||
<div class="form-row col-lg-12">
|
|
||||||
{{ form.submit( class="btn btn-primary offset-lg-2 col-lg-2" )}}
|
|
||||||
</div class="form-row">
|
|
||||||
</form>
|
|
||||||
</div class="row">
|
|
||||||
</div class="container">
|
|
||||||
{% endblock main_content %}
|
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
{% extends "base.html" %} {% block main_content %}
|
{% extends "base.html" %} {% block main_content %}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h3 class="offset-lg-2">{{page_title}}</h3>
|
<h3 class="offset-lg-2">{{page_title}}</h3>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<table id="settings_tbl" class="table table-sm">
|
<form class="form form-inline col-xl-12" action="" method="POST">
|
||||||
<thead><tr class="thead-light"><th>Name</th><th>Value</th></tr></thead><tbody>
|
{% for field in form %}
|
||||||
{% for obj in objects %}
|
{% if field.type == 'HiddenField' or field.type == 'CSRFTokenField' %}
|
||||||
<tr><td><a href="{{url_for('setting', id=obj.id)}}">{{obj.name}}</a></td><td>{{obj.value}}</td></tr>
|
{{field}}<br>
|
||||||
{% endfor %}
|
{% elif field.type != 'SubmitField' %}
|
||||||
</tbody></table>
|
<div class="form-row col-lg-12">
|
||||||
</div class="row">
|
{{ field.label( class="col-lg-2" ) }}
|
||||||
</div class="container">
|
{{ field( class="form-control col-lg-10" ) }}
|
||||||
|
</div class="form-row col-lg-12">
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
<div class="form-row col-lg-12">
|
||||||
|
{{form.submit(class="btn btn-primary offset-lg-2 col-lg-2" )}}
|
||||||
|
</div class="row">
|
||||||
|
</div class="form">
|
||||||
|
</div class="row">
|
||||||
|
</div class="container">
|
||||||
{% endblock main_content %}
|
{% endblock main_content %}
|
||||||
|
|||||||
Reference in New Issue
Block a user