Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import datetime
- import functools
- from flask import Flask, render_template
- from flask_sqlalchemy import SQLAlchemy
- from sqlalchemy import desc
- from flask_bootstrap import Bootstrap
- from htmlmin.minify import html_minify
- app = Flask(__name__)
- app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://bwmodpage:tSCweMPuaNplcQYq@localhost/bwmodpage'
- db = SQLAlchemy(app)
- ### DATABASE MODELS
- class User(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- username = db.Column(db.String(80), unique=True)
- password = db.Column(db.String(32))
- email = db.Column(db.String(120), unique=True)
- posts = db.relationship("Post")
- admin = db.Column(db.Integer)
- comments = db.relationship("Comment")
- def __init__(self, username, password, email, admin=0):
- self.username = username
- self.password = password
- self.email = email
- self.admin = admin
- def __repr__(self):
- return "<User {}".format(self.username)
- class Post(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- author = db.Column(db.Integer, db.ForeignKey("user.id"))
- alias = db.Column(db.String(80), unique=True)
- title = db.Column(db.String(80))
- body = db.Column(db.Text)
- date = db.Column(db.DateTime)
- comments = db.relationship("Comment")
- def __init__(self, author, alias, title, body, date=None):
- self.author = author
- self.alias = alias
- self.title = title
- self.body = body
- if date is None:
- date = datetime.datetime.utcnow()
- self.date = date
- def __repr__(self):
- return "<Post {}".format(self.title)
- class Comment(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- post = db.Column(db.Integer, db.ForeignKey("post.id"))
- author = db.Column(db.Integer, db.ForeignKey("user.id"))
- body = db.Column(db.Text(10000))
- date = db.Column(db.DateTime)
- def __init__(self, post, author, body, date=None):
- self.post = post
- self.author = author
- self.body = body
- if date is None:
- date = datetime.datetime.utcnow()
- self.date = date
- def __repr__(self):
- return "<Comment {}".format(self.title)
- ###HELPERS
- def minify(markup):
- if app.debug:
- return markup
- return html_minify(markup)
- def get_news(amount=3, page=1, per_page=5):
- result = Post.query.order_by(desc(Post.date)).all()
- news = []
- for n in result[per_page*(page-1):per_page*(page-1)+amount]:
- news.append((
- n.id,
- n.alias,
- n.title,
- parse_date_to_page(n.date),
- n.body))
- return news
- def get_comments(amount=5, page=1, per_page=5, post=1):
- result = Comment.query.order_by(desc(Comment.date)).all()
- comments = []
- for c in result[per_page*(page-1):per_page*(page-1)+amount]:
- comments.append((
- c.id,
- c.post,
- c.author,
- c.body,
- c.date))
- return comments;
- def parse_date_to_page(date):
- return date.strftime('%d.%m.%Y %H:%M')
- ### ROUTES
- @app.route('/')
- def index():
- return minify(render_template("index.html", news=get_news(), comments=get_comments()))
- @app.route('/news')
- def news():
- return minify(render_template("news.html", news=get_news(5, 1), comments=get_comments()))
- app.run(debug=True)
- app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement