diff --git a/.idea/mvl.iml b/.idea/mvl.iml index 237f81c..75fcac9 100644 --- a/.idea/mvl.iml +++ b/.idea/mvl.iml @@ -1,5 +1,8 @@ + + diff --git a/app.py b/app.py index df31f78..3e102d5 100644 --- a/app.py +++ b/app.py @@ -1,22 +1,28 @@ import os -from flask import Flask, render_template, jsonify, abort +from flask import Flask, render_template, jsonify, abort, send_file, request, redirect from sqlalchemy import func +from base64 import standard_b64decode, standard_b64encode +from io import BytesIO -from model import Post, Category, db - -app = Flask(__name__) +from model import db, Post, Category, ImageBase64 # Check for environment variable -if not os.getenv("DATABASE_URL"): - raise RuntimeError("DATABASE_URL is not set") +env_vars = ["DATABASE_URL", "PASSWORD"] +for env_var in env_vars: + if not os.getenv(env_var): + raise RuntimeError(f"{env_var} is not set") + +app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DATABASE_URL") app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['JSON_SORT_KEYS'] = False +# Bind db to application db.init_app(app) + @app.route("/") def index(): posts = Post.get_posts() @@ -25,7 +31,6 @@ def index(): @app.route('/') def category(category_name): - category_item = Category.query.filter(func.lower(Category.name) == category_name.replace('_', ' ')).first() if not category_item: return abort(404) @@ -35,6 +40,11 @@ def category(category_name): return render_template("main/index.html", posts=posts) +@app.route('/contact') +def contact(): + return render_template("main/contact.html") + + @app.route("/api/post/", methods=["POST"]) def get_post(post_id): post = Post.query.get(post_id) @@ -44,8 +54,47 @@ def get_post(post_id): return jsonify({"success": False}) -@app.route("/login", methods=["GET"]) -def login(): - return render_template("adm/login.html") +@app.route('/images/') +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) +@app.route('/adm/uploadfile', methods=['POST', 'GET']) +def file_uploaded(): + if request.method == 'POST': + if not request.form.get('password') == os.getenv('PASSWORD'): + abort(401) + # check if the post request has the file part + if 'file' not in request.files: + # flash('No file part') + return abort(400) + # return redirect(request.url) + file = request.files['file'] + # if user does not select file, browser also + # submit an empty part without filename + if file.filename == '': + # flash('No selected file') + return redirect(request.url) + + if file: + data = standard_b64encode(file.read()).decode() + print(file.filename, file.mimetype) + print(data) + database_object = ImageBase64(filename=file.filename, mimetype=file.mimetype, data=data) + db.session.add(database_object) + db.session.commit() + # if file and allowed_file(file.filename): + # filename = secure_filename(file.filename) + # file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + # return redirect(url_for('uploaded_file', + # filename=filename)) + return render_template("adm/uploadfile.html") + + +if __name__ == '__main__': + app.app_context().push() + db.create_all() diff --git a/model.py b/model.py index 375201a..d328b66 100644 --- a/model.py +++ b/model.py @@ -26,7 +26,7 @@ class Post(db.Model): "title": self.title, "intro": self.intro, "description": self.description, - "images": self.images} + "images": [image.uri for image in self.images]} @staticmethod def get_posts(): @@ -44,6 +44,14 @@ class Image(db.Model): post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False) +class ImageBase64(db.Model): + __tablename__ = "image_store" + id = db.Column(db.Integer, primary_key=True) + filename = db.Column(db.String(30), unique=True, nullable=False) + mimetype = db.Column(db.String(127), nullable=False) + data = db.Column(db.Text, nullable=False) + + class Category(db.Model): __tablename__ = "category" id = db.Column(db.Integer, primary_key=True) diff --git a/static/style.css b/static/style.css index b2ad9b0..37cdc4a 100644 --- a/static/style.css +++ b/static/style.css @@ -1,6 +1,10 @@ /* * Start of Custom CSS */ +.nav-link, .navbar-brand { + font-family: 'Amatic SC', cursive; + font-size: 40px; } + .card-img-overlay { opacity: 0; } @@ -17,8 +21,11 @@ .card .card-img-overlay .card-title-bg .card-text { opacity: 1; } +.social-icon { + margin: 10px; } + body { - padding-top: 4.5rem; } + padding-top: 6.5rem; } footer { margin-top: 10px; diff --git a/static/style.scss b/static/style.scss index 6d506f1..6c2e846 100644 --- a/static/style.scss +++ b/static/style.scss @@ -5,6 +5,15 @@ $margin: 20px; $half-margin: $margin / 2; +// Nav +.nav-link, .navbar-brand { + font-family: 'Amatic SC', cursive; + font-size: 40px; +} + +.navbar-brand { + +} // Overlay is hidden .card-img-overlay { @@ -36,8 +45,13 @@ $half-margin: $margin / 2; } } +// Contact page +.social-icon { + margin: $half-margin; +} + body { - padding-top: 4.5rem; + padding-top: 6.5rem; } diff --git a/templates/adm/uploadfile.html b/templates/adm/uploadfile.html new file mode 100644 index 0000000..00f1c77 --- /dev/null +++ b/templates/adm/uploadfile.html @@ -0,0 +1,21 @@ +{% extends 'adm/layout.html' %} +{% block main %} +
+

Upload new Photo

+
+
+ + +
+
+
+ + +
+
+
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 4be5f45..9e5e54c 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -5,8 +5,13 @@ + + + {% block title %}{% endblock %} @@ -22,6 +27,7 @@ + {% block nav %}{% endblock %} diff --git a/templates/main/contact.html b/templates/main/contact.html new file mode 100644 index 0000000..e712173 --- /dev/null +++ b/templates/main/contact.html @@ -0,0 +1,22 @@ +{% extends "main/layout.html" %} + +{% block main %} +
+
+
+

Hoi!

+

Na het behalen van mijn diploma 'Product Design' van de Hogeschool van Amsterdam in februari 2018 ben + ik gaan werken bij een grafisch ontwerpbureau in Haarlem. + Daarnaast ben ik steeds meer bezig met eigen opdrachten, zoals te zien is in dit portfolio.

+
+ + + +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/main/index.html b/templates/main/index.html index 22341aa..82c50af 100644 --- a/templates/main/index.html +++ b/templates/main/index.html @@ -12,7 +12,7 @@ data-post_id="{{ post.id }}" >
- random image + random image
diff --git a/templates/main/modal.html b/templates/main/modal.html index 464f25f..f4a5144 100644 --- a/templates/main/modal.html +++ b/templates/main/modal.html @@ -1,6 +1,6 @@