tried to add a reference_images directory in git, that contains actual uploaded images and the modify/remove also deals with filesystem issues

This commit is contained in:
2021-01-13 13:52:56 +11:00
parent 991b654d4c
commit 2a7151dc66
3 changed files with 27 additions and 45 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ photos/
images_to_process/
new_img_dir/
static/*
reference_images/*

View File

@@ -5,6 +5,7 @@ from main import db, app, ma
from sqlalchemy import Sequence
from sqlalchemy.exc import SQLAlchemyError
from status import st, Status
import os
################################################################################
# Class describing Refimg in the database, and via sqlalchemy, connected to the DB as well
@@ -29,8 +30,8 @@ class RefimgSchema(ma.SQLAlchemyAutoSchema):
################################################################################
class RefimgForm(FlaskForm):
id = HiddenField()
# fname = StringField('File name:', [validators.DataRequired()])
fname = FileField('File name:')
fname = StringField('File name:', [validators.DataRequired()])
refimg_file = FileField('File name:')
submit = SubmitField('Save' )
delete = SubmitField('Delete' )
@@ -44,7 +45,6 @@ def refimgs():
refimgs = Refimg.query.all()
return render_template("refimgs.html", refimgs=refimgs, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################
# /refimg -> GET/POST -> creates a new refimg type and when created, takes you back to /refimgs
################################################################################
@@ -52,14 +52,18 @@ def refimgs():
def new_refimg():
form = RefimgForm(request.form)
page_title='Create new Reference Image'
if 'fname' not in request.form:
if request.method == 'GET':
return render_template("refimg.html", form=form, page_title=page_title )
else:
# save the actual uploaded image to reference_images/
f=request.files['refimg_file']
f.save(os.path.join("reference_images/", request.form["fname"]))
# now save into the DB
refimg = Refimg( fname=request.form["fname"] )
try:
db.session.add(refimg)
db.session.commit()
print(refimg)
st.SetMessage( "Created new Refimg ({})".format(refimg.fname) )
return redirect( '/refimgs' )
except SQLAlchemyError as e:
@@ -78,6 +82,7 @@ def refimg(id):
if request.method == 'POST':
try:
refimg = Refimg.query.get(id)
os.remove("reference_images/{}".format(refimg.fname) )
if 'delete' in request.form:
st.SetMessage("Successfully deleted Refimg: ({})".format( refimg.fname ) )
refimg = Refimg.query.filter(Refimg.id==id).delete()
@@ -85,6 +90,9 @@ def refimg(id):
st.SetMessage("Successfully Updated Refimg: (From: {})".format(refimg.fname))
refimg.fname = request.form['fname']
st.AppendMessage(" To: ({})".format(refimg.fname) )
# save the actual uploaded image to reference_images/
f=request.files['refimg_file']
f.save(os.path.join("reference_images/", request.form["fname"]))
db.session.commit()
return redirect( '/refimgs' )
except SQLAlchemyError as e:

View File

@@ -3,43 +3,18 @@
<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}}<br>
{% elif field.type != 'SubmitField' %}
<div class="form-row col-lg-12">
{% if 'Edit' in page_title %}
{{ field.label( class="col-lg-2" ) }}
<div class="input-group col-lg-8">
<div class="input-group-prepend">
<input id="fname_id" type="hidden" name="fname" value="{{field.data}}">
</div>
<div class="input-group-append">
<span id="fname_span" name="fname" class="form-control">{{field.data}}</span>
<label class="btn btn-outline-primary">
Change File
<input type="file" onChange="DoMagic()" style="display: none;" id="new_file_chooser">
</label>
</div class="input-group-append">
{% else %}
{{ field.label( class="col-lg-2" ) }}
<input id="fname_id" type="hidden" name="fname" value="{{field.data}}">
<div class="input-group col-lg-8">
<div class="input-group-prepend">
<span id="fname_span" name="fname" class="form-control">{{field.data}}</span>
</div>
<div class="input-group-append">
<label class="btn btn-outline-primary">
Choose File
<input type="file" onChange="DoMagic()" style="display:none;" id="new_file_chooser">
</label>
</div class="input-group-append">
</div>
{% endif %}
</div class="form-row col-lg-12">
{% endif %}
{% endfor %}
<form class="form form-inline col-xl-12" action="" method="POST" enctype="multipart/form-data">
{{form.id}}
{{form.csrf_token}}
<div class="form-row col-lg-12">
{{ form.fname.label( class="col-lg-2" ) }}
{{ form.fname( id="fname", class="form-control col-lg-4" ) }}
&nbsp;
<label class="btn btn-outline-primary col-lg-2">
Choose File
<input name="refimg_file" type="file" onChange="DoMagic()" style="display:none;" id="new_file_chooser">
</label>
</div class="form-row col-lg-12">
<div class="row col-lg-12">
<br>
</div class="row">
@@ -61,9 +36,7 @@
console.log(str)
str=str.replace('C:\\fakepath\\', '' )
console.log(str)
$("#fname_span").html(str)
$("#fname_id").val(str)
$("#new_file_chooser").val('')
$("#fname").val(str)
}
</script>
{% endblock script_content %}