-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
154 lines (129 loc) · 3.86 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask, render_template, request
from flask.ext.sqlalchemy import SQLAlchemy
from config import DevConfig
from sqlalchemy import func
from flask_wtf import Form
from wtforms import StringField, TextAreaField
from wtforms.validators import DataRequired, Length
app = Flask(__name__)
app.config.from_object(DevConfig)
db = SQLAlchemy(app)
class CommentForm(Form):
name = StringField(
'Name',
validators=[DataRequired(), Length(max=255)]
)
text = TextAreaField(u'Comment', validators=[DataRequired()])
class User(db.Model):
id = db.Column(db.Integer(), primary_key=True)
username = db.Column(db.String(255))
password = db.Column(db.String(255))
posts = db.relationship(
'Post',
backref='user',
lazy='subquery'
)
def __init__(self, username):
self.username = username
def __repr__(self):
return 'User {}'.format(self.username)
tags = db.Table('post_tags',
db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id') )
)
class Post(db.Model):
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.String(255))
text = db.Column(db.Text())
publish_date = db.Column(db.DateTime())
user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
comments = db.relationship(
'Comment',
backref='post',
lazy='dynamic'
)
tags = db.relationship(
'Tag',
secondary=tags,
backref=db.backref('posts', lazy='dynamic')
)
def __init__(self, title):
self.title = title
def __repr__(self):
return "<Post '{}'>".format(self.title)
class Tag(db.Model):
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.String(255))
def __init__(self, title):
self.title = title
def __repr__(self):
return "<Tag '{}'>".format(self.title)
class Comment(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(255))
text = db.Column(db.Text())
date = db.Column(db.DateTime())
post_id = db.Column(db.Integer(), db.ForeignKey('post.id'))
def __repr__(self):
return "<Comment '{}'>".format(self.text[:15])
def sidebar_data():
recnet = Post.query.order_by(
Post.publish_date.desc()
).limit(5).all()
top_tags = db.session.query(
Tag, func.count(tags.c.post_id).label('total')
).join(
tags
).group_by(Tag).order_by('total DESC').limit(5).all()
return recnet, top_tags
@app.route('/')
@app.route('/<int:page>')
def home(page=1):
posts = Post.query.order_by(
Post.publish_date.desc()
).paginate(page, 10)
recent, top_tags = sidebar_data()
return render_template(
'home.html',
posts=posts,
recent=recent,
top_tags=top_tags,
)
@app.route('/posts/<int:post_id>', methods=('GET', 'POST'))
def posts(post_id):
form = CommentForm()
if request.method == 'GET':
post = Post.query.get(post_id)
recent, top_tags = sidebar_data()
return render_template(
'posts.html',
post=post,
recent=recent,
top_tags=top_tags,
form=form
)
elif request.method == 'POST':
return "<h1>hello world!</h1>"
@app.route('/tag/<int:tag_id>')
def tag(tag_id):
tag = Tag.query.get(tag_id)
recent, top_tags = sidebar_data()
return render_template(
'tags.html',
recent=recent,
posts=tag.posts,
top_tags=top_tags,
tag_id=tag.id
)
@app.route('/user/<int:user_id>')
def user(user_id):
user = User.query.get(user_id)
recent, top_tags = sidebar_data()
return render_template(
'user.html',
recent=recent,
top_tags=top_tags,
posts=user.posts
)
if __name__ == '__main__':
app.run()