made settings work lamely, but functional for now, also removed test route
This commit is contained in:
58
settings.py
58
settings.py
@@ -17,7 +17,57 @@ class Settings(db.Model):
|
||||
def __repr__(self):
|
||||
return "<id: {}, name: {}, value: {}>".format(self.id, self.name, self.value)
|
||||
|
||||
### modify the below to add a import path, assumption will be there is only 1 ever import_path, it will have : separated values
|
||||
#s = Settings( name="import_path", value="/home/ddp/src/photoassistant/images_to_process" )
|
||||
#db.session.add(s)
|
||||
#db.session.commit()
|
||||
###############################################################################
|
||||
# Helper class that inherits a .dump() method to turn class Author into json / useful in jinja2
|
||||
################################################################################
|
||||
class SettingsSchema(ma.SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Settings
|
||||
ordered = True
|
||||
|
||||
settings_schema=SettingsSchema()
|
||||
|
||||
################################################################################
|
||||
# Helper class that defines a form for Settings, used to make html <form>
|
||||
# with field validation (via wtforms)
|
||||
################################################################################
|
||||
class SettingsForm(FlaskForm):
|
||||
id = HiddenField()
|
||||
name = StringField('Name:', [validators.DataRequired()])
|
||||
value = StringField('value:', [validators.DataRequired()])
|
||||
submit = SubmitField('Save' )
|
||||
|
||||
################################################################################
|
||||
# /settings -> show current settings
|
||||
################################################################################
|
||||
@app.route("/settings", methods=["GET"])
|
||||
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)
|
||||
if request.method == 'POST' and form.validate():
|
||||
try:
|
||||
s = Settings.query.get(id)
|
||||
if 'submit' in request.form:
|
||||
st.SetMessage("Successfully Updated Setting (name={})".format(s.name) )
|
||||
s.name = request.form['name']
|
||||
s.value = request.form['value']
|
||||
db.session.commit()
|
||||
return redirect( '/settings' )
|
||||
except SQLAlchemyError as e:
|
||||
st.SetAlert( "danger" )
|
||||
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() )
|
||||
else:
|
||||
s = Settings.query.get(id)
|
||||
page_title='Edit Setting: {}'.format(s.name)
|
||||
form = SettingsForm(request.values, obj=s)
|
||||
return render_template("setting.html", objects=s, form=form, page_title = page_title, alert=st.GetAlert(), message=st.GetMessage() )
|
||||
|
||||
Reference in New Issue
Block a user