Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import os
  2. import sys
  3. import datetime
  4. import functools
  5.  
  6. from flask import Flask, render_template
  7. from flask_sqlalchemy import SQLAlchemy
  8. from sqlalchemy import desc
  9. from flask_bootstrap import Bootstrap
  10. from htmlmin.minify import html_minify
  11.  
  12. app = Flask(__name__)
  13. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://bwmodpage:tSCweMPuaNplcQYq@localhost/bwmodpage'
  14. db = SQLAlchemy(app)
  15.  
  16. ### DATABASE MODELS
  17.  
  18. class User(db.Model):
  19. id = db.Column(db.Integer, primary_key=True)
  20. username = db.Column(db.String(80), unique=True)
  21. password = db.Column(db.String(32))
  22. email = db.Column(db.String(120), unique=True)
  23. posts = db.relationship("Post")
  24. admin = db.Column(db.Integer)
  25. comments = db.relationship("Comment")
  26.  
  27. def __init__(self, username, password, email, admin=0):
  28. self.username = username
  29. self.password = password
  30. self.email = email
  31. self.admin = admin
  32.  
  33. def __repr__(self):
  34. return "<User {}".format(self.username)
  35.  
  36.  
  37.  
  38. class Post(db.Model):
  39. id = db.Column(db.Integer, primary_key=True)
  40. author = db.Column(db.Integer, db.ForeignKey("user.id"))
  41. alias = db.Column(db.String(80), unique=True)
  42. title = db.Column(db.String(80))
  43. body = db.Column(db.Text)
  44. date = db.Column(db.DateTime)
  45. comments = db.relationship("Comment")
  46.  
  47. def __init__(self, author, alias, title, body, date=None):
  48. self.author = author
  49. self.alias = alias
  50. self.title = title
  51. self.body = body
  52. if date is None:
  53. date = datetime.datetime.utcnow()
  54. self.date = date
  55.  
  56. def __repr__(self):
  57. return "<Post {}".format(self.title)
  58.  
  59.  
  60.  
  61. class Comment(db.Model):
  62. id = db.Column(db.Integer, primary_key=True)
  63. post = db.Column(db.Integer, db.ForeignKey("post.id"))
  64. author = db.Column(db.Integer, db.ForeignKey("user.id"))
  65. body = db.Column(db.Text(10000))
  66. date = db.Column(db.DateTime)
  67.  
  68. def __init__(self, post, author, body, date=None):
  69. self.post = post
  70. self.author = author
  71. self.body = body
  72. if date is None:
  73. date = datetime.datetime.utcnow()
  74. self.date = date
  75.  
  76. def __repr__(self):
  77. return "<Comment {}".format(self.title)
  78.  
  79.  
  80.  
  81. ###HELPERS
  82.  
  83. def minify(markup):
  84. if app.debug:
  85. return markup
  86. return html_minify(markup)
  87.  
  88. def get_news(amount=3, page=1, per_page=5):
  89. result = Post.query.order_by(desc(Post.date)).all()
  90. news = []
  91. for n in result[per_page*(page-1):per_page*(page-1)+amount]:
  92. news.append((
  93. n.id,
  94. n.alias,
  95. n.title,
  96. parse_date_to_page(n.date),
  97. n.body))
  98. return news
  99.  
  100. def get_comments(amount=5, page=1, per_page=5, post=1):
  101. result = Comment.query.order_by(desc(Comment.date)).all()
  102. comments = []
  103. for c in result[per_page*(page-1):per_page*(page-1)+amount]:
  104. comments.append((
  105. c.id,
  106. c.post,
  107. c.author,
  108. c.body,
  109. c.date))
  110. return comments;
  111.  
  112. def parse_date_to_page(date):
  113. return date.strftime('%d.%m.%Y %H:%M')
  114.  
  115.  
  116.  
  117. ### ROUTES
  118.  
  119. @app.route('/')
  120. def index():
  121. return minify(render_template("index.html", news=get_news(), comments=get_comments()))
  122.  
  123. @app.route('/news')
  124. def news():
  125. return minify(render_template("news.html", news=get_news(5, 1), comments=get_comments()))
  126.  
  127. app.run(debug=True)
  128. app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement