Varia library working group XPPL.
https://gitea.xpub.nl/XPUB/XPPL
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.6 KiB
159 lines
4.6 KiB
7 years ago
|
"""
|
||
|
Flask Documentation: http://flask.pocoo.org/docs/
|
||
|
Jinja2 Documentation: http://jinja.pocoo.org/2/documentation/
|
||
|
Werkzeug Documentation: http://werkzeug.pocoo.org/documentation/
|
||
|
This file creates your application.
|
||
|
"""
|
||
|
|
||
|
from app import app, db
|
||
|
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify, abort
|
||
|
|
||
|
from app.forms import UserForm
|
||
|
from app.models import Book, BookSchema
|
||
|
|
||
|
import os
|
||
|
from werkzeug.utils import secure_filename
|
||
|
# import sqlite3
|
||
|
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
|
||
|
|
||
|
book_schema = BookSchema()
|
||
|
books_schema = BookSchema(many=True)
|
||
|
|
||
|
def allowed_file(filename):
|
||
|
return '.' in filename and \
|
||
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||
|
###
|
||
|
# Routing for your application.
|
||
|
###
|
||
|
|
||
|
@app.route('/')
|
||
|
def home():
|
||
|
"""Render website's home page."""
|
||
|
return render_template('home.html')
|
||
|
|
||
|
|
||
|
@app.route('/about/')
|
||
|
def about():
|
||
|
"""Render the website's about page."""
|
||
|
return render_template('about.html', name="Mary Jane")
|
||
|
|
||
|
@app.route('/uploads/<filename>')
|
||
|
def uploaded_file(filename):
|
||
|
return send_from_directory(app.config['UPLOAD_FOLDER'],
|
||
|
filename)
|
||
|
|
||
|
@app.route('/books')
|
||
|
def show_books():
|
||
|
books = db.session.query(Book).all() # or you could have used User.query.all()
|
||
|
|
||
|
return render_template('show_books.html', books=books)
|
||
|
|
||
|
@app.route('/books/<int:id>')
|
||
|
def show_book_by_id(id):
|
||
|
book = Book.query.get(id)
|
||
|
if not book:
|
||
|
abort(404)
|
||
|
else:
|
||
|
return render_template('show_book_detail.html', book=book)
|
||
|
|
||
|
|
||
|
@app.route('/add-book', methods=['POST', 'GET'])
|
||
|
def add_book():
|
||
|
user_form = UserForm()
|
||
|
|
||
|
if request.method == 'POST':
|
||
|
|
||
|
if user_form.validate_on_submit():
|
||
|
# check if the post request has the file part
|
||
|
if 'file' not in request.files:
|
||
|
flash('No file part')
|
||
|
return redirect(request.url)
|
||
|
file = request.files['file']
|
||
|
# if user does not select file, browser also
|
||
|
# submit a empty part without filename
|
||
|
if file.filename == '':
|
||
|
flash('No selected file')
|
||
|
return redirect(request.url)
|
||
|
if file and allowed_file(file.filename):
|
||
|
filename = secure_filename(file.filename)
|
||
|
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||
|
|
||
|
file.save(fullpath)
|
||
|
|
||
|
title = user_form.title.data # You could also have used request.form['name']
|
||
|
author = user_form.author.data # You could also have used request.form['email']
|
||
|
# save user to database
|
||
|
book = Book(title, author, filename)
|
||
|
db.session.add(book)
|
||
|
db.session.commit()
|
||
|
flash("%s added to the library" % (title))
|
||
|
return redirect(url_for('show_books'))
|
||
|
else:
|
||
|
flash('allowed file formats: %s' % ALLOWED_EXTENSIONS)
|
||
|
|
||
|
flash_errors(user_form)
|
||
|
return render_template('add_book.html', form=user_form)
|
||
|
|
||
|
# Flash errors from the form if validation fails
|
||
|
def flash_errors(form):
|
||
|
for field, errors in form.errors.items():
|
||
|
for error in errors:
|
||
|
flash(u"Error in the %s field - %s" % (
|
||
|
getattr(form, field).label.text,
|
||
|
error
|
||
|
))
|
||
|
|
||
|
|
||
|
###
|
||
|
# The API
|
||
|
###
|
||
|
|
||
|
@app.route('/api/books', methods=['GET'])
|
||
|
def get_books():
|
||
|
books = Book.query.all()
|
||
|
data, errors = books_schema.dump(books)
|
||
|
return jsonify({'books': data})
|
||
|
|
||
|
@app.route('/api/books/<int:id>', methods=['GET'])
|
||
|
def get_book_by_id(id):
|
||
|
book = Book.query.get(id)
|
||
|
book_result, error = book_schema.dump(book)
|
||
|
if not book_result:
|
||
|
return jsonify({"message": "Book could not be found."}), 400
|
||
|
else:
|
||
|
return jsonify({'book': book_result})
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
###
|
||
|
# The functions below should be applicable to all Flask apps.
|
||
|
###
|
||
|
|
||
|
@app.route('/<file_name>.txt')
|
||
|
def send_text_file(file_name):
|
||
|
"""Send your static text file."""
|
||
|
file_dot_text = file_name + '.txt'
|
||
|
return app.send_static_file(file_dot_text)
|
||
|
|
||
|
|
||
|
@app.after_request
|
||
|
def add_header(response):
|
||
|
"""
|
||
|
Add headers to both force latest IE rendering engine or Chrome Frame,
|
||
|
and also to cache the rendered page for 10 minutes.
|
||
|
"""
|
||
|
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
|
||
|
response.headers['Cache-Control'] = 'public, max-age=600'
|
||
|
return response
|
||
|
|
||
|
|
||
|
@app.errorhandler(404)
|
||
|
def page_not_found(error):
|
||
|
"""Custom 404 page."""
|
||
|
return render_template('404.html'), 404
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(debug=True,host="0.0.0.0",port="8080")
|