This commit is contained in:
Marijn Jansen
2019-03-19 13:24:52 +01:00
parent 107b523b4a
commit b2c078af1f
8 changed files with 99 additions and 75 deletions

64
app.py
View File

@@ -1,7 +1,9 @@
import os
from flask import Flask, render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, jsonify, abort
from sqlalchemy import func
from model import Post, Category, db
app = Flask(__name__)
@@ -13,14 +15,24 @@ app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DATABASE_URL")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JSON_SORT_KEYS'] = False
db = SQLAlchemy(app)
db.init_app(app)
@app.route("/")
def index():
result = Post.query.order_by(Post.priority).all()
print(result)
return render_template("main/index.html", count=12)
posts = Post.get_posts()
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)
posts = Post.get_posts_with_category(category_item.id)
return render_template("main/index.html", posts=posts)
@app.route("/api/post/<int:post_id>", methods=["POST"])
@@ -37,41 +49,3 @@ def login():
return render_template("adm/login.html")
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
priority = db.Column(db.Integer, unique=True, nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey("category.id"), nullable=True)
category = db.relationship("Category", lazy=True)
title = db.Column(db.String(80), nullable=False)
intro = db.Column(db.Text, nullable=False)
description = db.Column(db.Text, nullable=True)
images = db.relationship("Image", backref="post", lazy=True)
@property
def serialize(self):
return {"id": self.id,
"priority": self.priority,
"category_id": self.category_id,
"category": self.category.name,
"title": self.title,
"intro": self.intro,
"description": self.description,
"images": self.images}
class Image(db.Model):
__tablename__ = "images"
id = db.Column(db.Integer, primary_key=True)
uri = db.Column(db.String(80), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False)
class Category(db.Model):
__tablename__ = "category"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)