Made is more presentable, added a README and a simple cleanup.
This commit is contained in:
2021-03-23 10:51:17 +01:00
parent e4abd2e3cf
commit cb6c84069e
11 changed files with 78 additions and 81 deletions

10
app.py
View File

@@ -9,6 +9,7 @@ from model import db, Post, Category, ImageBase64
# Check for environment variable
env_vars = ["DATABASE_URL", "PASSWORD", 'SECRET_KEY']
for env_var in env_vars:
if not os.getenv(env_var):
raise RuntimeError(f"{env_var} is not set")
@@ -28,12 +29,14 @@ db.init_app(app)
def index():
posts = Post.get_posts()
print(posts[0].title)
return render_template("main/index.html", posts=posts)
@app.route('/<string:category_name>')
def category(category_name):
category_item = Category.query.filter(func.lower(Category.name) == category_name.replace('_', ' ')).first()
if not category_item:
return abort(404)
@@ -50,6 +53,7 @@ def contact():
@app.route("/api/post/<int:post_id>", methods=["POST"])
def get_post(post_id):
post = Post.query.get(post_id)
if post:
return jsonify({"success": True, "post": post.serialize})
else:
@@ -59,9 +63,12 @@ def get_post(post_id):
@app.route('/images/<string:filename>')
def get_image(filename):
image_b64 = ImageBase64.query.filter(ImageBase64.filename == filename).first()
if not image_b64:
return abort(404)
image = standard_b64decode(image_b64.data)
return send_file(BytesIO(image), mimetype=image_b64.mimetype, attachment_filename=filename)
@@ -71,10 +78,13 @@ def file_uploaded():
if not request.form.get('password') == os.getenv('PASSWORD'):
flash("Wrong Password")
return redirect("/adm/uploadfile")
# check if the post request has the file part
if 'files' not in request.files:
return redirect("/adm/uploadfile")
files = request.files.getlist('files')
# if user does not select file, browser also
# submit an empty part without filename
for file in files: