added upload new version of file into edit form

geo
nberting 7 years ago
parent 7938ec3591
commit 68c38ae198

@ -25,6 +25,7 @@ class EditForm(FlaskForm):
author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1)
category = StringField('category', validators=[InputRequired()])
year_published = StringField('year published', [validators.Length(max=4)],default=None)
file = FileField()
class ChatForm(FlaskForm):
message = StringField('message', validators=[InputRequired()])

@ -7,7 +7,7 @@
<form method="POST" action="{{ url_for('edit_book_by_id', id=book.id )}}">
<form method="POST" action="{{ url_for('edit_book_by_id', id=book.id )}}" enctype=multipart/form-data>
{{ form.csrf_token }}
<div class="form-group"><h1 class="header">{{ form.title.label }} {{ form.title(size=20, class="form-control") }}</h1></div>
@ -32,10 +32,19 @@
</table>
</div><br>
<div class="form-group">
{{ form.category.label }} {{ form.category(size=20, class="form-control") }}
{{ form.category.label }} {{ form.category(size=20,
class="form-control") }}
</div><br>
<div class="form-group">
{{ form.year_published.label }} {{ form.year_published(size=4, class="form-control") }}
</div>
<div class="form-group">
Current file: {{ book.file }}
</div>
<div class="form-group">
Upload new file: {{form.file}}
</div>
<br>
<button type="submit" class="btn btn-primary">Update</button>

@ -134,16 +134,13 @@ def edit_book_by_id(id):
if request.method == 'POST':
if user_form.validate_on_submit():
# check if the post request has the file part
title = user_form.title.data # You could also have used request.form['name']
input_authors = user_form.author.data # You could also have used request.form['email']
# on submit, check fields
title = user_form.title.data
input_authors = user_form.author.data
category = user_form.category.data
year_published = user_form.year_published.data
if year_published=="":
year_published = None
# save user to database
#book = Book(title, author, filename, cover, file_extension)
book = Book.query.filter_by(id=id).first()
book.title = title
book.category = category
@ -159,6 +156,26 @@ def edit_book_by_id(id):
a = Author(author_name=author_name)
db.session.add(a)
book.authors.append(a)
# editing / uploading new file
if user_form.file.data:
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
allbooks = db.session.query(Book).all()
id = book.id
new_filename = str(id) +"_"+ filename
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], new_filename)
name, file_extension = os.path.splitext(new_filename)
file.save(fullpath)
book.cover = get_cover(fullpath, name)
book.file = new_filename
else:
flash('allowed file formats: %s' % ALLOWED_EXTENSIONS)
db.session.commit()
flash("%s updated" % (title))
return redirect(url_for('show_book_by_id', id=id))

Binary file not shown.
Loading…
Cancel
Save