-
Notifications
You must be signed in to change notification settings - Fork 29
/
app.py
158 lines (133 loc) · 4.84 KB
/
app.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
155
156
157
158
#!/usr/bin/env python
import bottle
import redis
import settings
#ugly hack
settings.r = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB)
from bottle_session import Session
from domain import User,Post,Timeline
reserved_usernames = 'follow mentions home signup login logout post'
def authenticate(handler):
def _check_auth(*args,**kwargs):
sess = Session(bottle.request,bottle.response)
if not sess.is_new():
user = User.find_by_id(sess['id'])
if user:
return handler(user,*args,**kwargs)
bottle.redirect('/login')
return _check_auth
def logged_in_user():
sess = Session(bottle.request,bottle.response)
if not sess.is_new():
return User.find_by_id(sess['id'])
return None
def user_is_logged():
if logged_in_user():
return True
return False
@bottle.route('/')
def index():
if user_is_logged():
bottle.redirect('/home')
return bottle.template('home_not_logged',logged=False)
@bottle.route('/home')
@authenticate
def home(user):
bottle.TEMPLATES.clear()
counts = user.followees_count,user.followers_count,user.tweet_count
if len(user.posts()) >0:
last_tweet = user.posts()[0]
else:
last_tweet = None
return bottle.template('timeline',timeline=user.timeline(),page='timeline',username=user.username,
counts=counts,last_tweet=last_tweet,logged=True)
@bottle.route('/mentions')
@authenticate
def mentions(user):
counts = user.followees_count,user.followers_count,user.tweet_count
return bottle.template('mentions',mentions=user.mentions(),page='mentions',username=user.username,
counts=counts,posts=user.posts()[:1],logged=True)
@bottle.route('/:name')
def user_page(name):
is_following,is_logged = False,user_is_logged()
user = User.find_by_username(name)
if user:
counts = user.followees_count,user.followers_count,user.tweet_count
logged_user = logged_in_user()
himself = logged_user.username == name
if logged_user:
is_following = logged_user.following(user)
return bottle.template('user',posts=user.posts(),counts=counts,page='user',
username=user.username,logged=is_logged,is_following=is_following,himself=himself)
else:
return bottle.HTTPError(code=404)
@bottle.route('/:name/statuses/:id')
@bottle.validate(id=int)
def status(name,id):
post = Post.find_by_id(id)
if post:
if post.user.username == name:
return bottle.template('single',username=post.user.username,tweet=post,page='single',
logged=user_is_logged())
return bottle.HTTPError(code=404,message='tweet not found')
@bottle.route('/post',method='POST')
@authenticate
def post(user):
content = bottle.request.POST['content']
Post.create(user, content)
bottle.redirect('/home')
@bottle.route('/follow/:name',method='POST')
@authenticate
def post(user,name):
user_to_follow = User.find_by_username(name)
if user_to_follow:
user.follow(user_to_follow)
bottle.redirect('/%s' % name)
@bottle.route('/unfollow/:name',method='POST')
@authenticate
def post(user,name):
user_to_unfollow = User.find_by_username(name)
if user_to_unfollow:
user.stop_following(user_to_unfollow)
bottle.redirect('/%s' % name)
@bottle.route('/signup')
@bottle.route('/login')
def login():
bottle.TEMPLATES.clear()
if user_is_logged():
bottle.redirect('/home')
return bottle.template('login',page='login',error_login=False,error_signup=False,logged=False)
@bottle.route('/login', method='POST')
def login():
if 'name' in bottle.request.POST and 'password' in bottle.request.POST:
name = bottle.request.POST['name']
password = bottle.request.POST['password']
user = User.find_by_username(name)
if user and user.password == settings.SALT + password:
sess=Session(bottle.request,bottle.response)
sess['id'] = user.id
sess.save()
bottle.redirect('/home')
return bottle.template('login',page='login',error_login=True,error_signup=False,logged=False)
@bottle.route('/logout')
def logout():
sess = Session(bottle.request,bottle.response)
sess.invalidate()
bottle.redirect('/')
@bottle.route('/signup', method='POST')
def sign_up():
if 'name' in bottle.request.POST and 'password' in bottle.request.POST:
name = bottle.request.POST['name']
if name not in reserved_usernames.split():
password = bottle.request.POST['password']
user = User.create(name,password)
if user:
sess=Session(bottle.request,bottle.response)
sess['id'] = user.id
sess.save()
bottle.redirect('/home')
return bottle.template('login',page='login',error_login=False,error_signup=True,logged=False)
@bottle.route('/static/:filename')
def static_file(filename):
bottle.send_file(filename, root='static/')
bottle.run(host='localhost', port=8080,reloader=True)