diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml
index 033f30b..6e3fb31 100644
--- a/.idea/jsLibraryMappings.xml
+++ b/.idea/jsLibraryMappings.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/.idea/mvl.iml b/.idea/mvl.iml
index b97aa12..237f81c 100644
--- a/.idea/mvl.iml
+++ b/.idea/mvl.iml
@@ -4,8 +4,8 @@
-
+
diff --git a/app.py b/app.py
index f8f8b46..df31f78 100644
--- a/app.py
+++ b/app.py
@@ -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('/')
+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/", 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)
-
diff --git a/model.py b/model.py
new file mode 100644
index 0000000..375201a
--- /dev/null
+++ b/model.py
@@ -0,0 +1,50 @@
+from flask_sqlalchemy import SQLAlchemy
+
+db = SQLAlchemy()
+
+
+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}
+
+ @staticmethod
+ def get_posts():
+ return Post.query.order_by(Post.priority).all()
+
+ @staticmethod
+ def get_posts_with_category(category_id):
+ return Post.query.filter(Post.category_id == category_id).order_by(Post.priority).all()
+
+
+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)
diff --git a/requirements.txt b/requirements.txt
deleted file mode 100644
index 806562f..0000000
--- a/requirements.txt
+++ /dev/null
@@ -1,11 +0,0 @@
--i https://pypi.org/simple
-click==7.0
-flask-sqlalchemy==2.3.2
-flask==1.0.2
-gunicorn==19.9.0
-itsdangerous==1.1.0
-jinja2==2.10
-markupsafe==1.1.1
-psycopg2-binary==2.7.7
-sqlalchemy==1.3.1
-werkzeug==0.14.1
diff --git a/templates/layout.html b/templates/layout.html
index 92f0612..4be5f45 100644
--- a/templates/layout.html
+++ b/templates/layout.html
@@ -12,8 +12,9 @@
{% block title %}{% endblock %}
-
\ No newline at end of file