]>
Commit | Line | Data |
---|---|---|
db4f0ba9 MR |
1 | from flask import render_template, flash, redirect, session, url_for, request, g |
2 | from flask_login import login_user, logout_user, current_user, login_required | |
3 | from datetime import datetime | |
4 | from app import app, db, lm, oid | |
163c4d87 MR |
5 | from .forms import LoginForm, EditForm, PostForm |
6 | from .models import User, Post | |
7 | from config import POSTS_PER_PAGE | |
8 | from .emails import follower_notification | |
db4f0ba9 MR |
9 | |
10 | @lm.user_loader | |
11 | def load_user(id): | |
12 | return User.query.get(int(id)) | |
13 | ||
db4f0ba9 MR |
14 | @app.before_request |
15 | def before_request(): | |
16 | g.user = current_user | |
17 | if g.user.is_authenticated: | |
18 | g.user.last_seen = datetime.utcnow() | |
19 | db.session.add(g.user) | |
20 | db.session.commit() | |
21 | ||
22 | ||
23 | @app.errorhandler(404) | |
24 | def not_found_error(error): | |
25 | return render_template('404.html'), 404 | |
26 | ||
27 | ||
28 | @app.errorhandler(500) | |
29 | def internal_error(error): | |
30 | db.session.rollback() | |
31 | return render_template('500.html'), 500 | |
32 | ||
43064c75 | 33 | |
163c4d87 MR |
34 | @app.route('/', methods=['GET', 'POST']) |
35 | @app.route('/index', methods=['GET', 'POST']) | |
36 | @app.route('/index/<int:page>', methods=['GET', 'POST']) | |
db4f0ba9 | 37 | @login_required |
163c4d87 MR |
38 | def index(page=1): |
39 | form = PostForm() | |
40 | if form.validate_on_submit(): | |
41 | post = Post(body=form.post.data, timestamp=datetime.utcnow(), author=g.user) | |
42 | db.session.add(post) | |
43 | db.session.commit() | |
44 | flash('Your post is now live!') | |
45 | app.logger.info("%s: published a post" % g.user) | |
46 | return redirect(url_for('index')) | |
47 | posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False) | |
db4f0ba9 MR |
48 | return render_template('index.html', |
49 | title='Home', | |
163c4d87 | 50 | form=form, |
db4f0ba9 MR |
51 | posts=posts) |
52 | ||
53 | ||
54 | @app.route('/login', methods=['GET', 'POST']) | |
55 | @oid.loginhandler | |
56 | def login(): | |
57 | if g.user is not None and g.user.is_authenticated: | |
58 | return redirect(url_for('index')) | |
59 | form = LoginForm() | |
60 | if form.validate_on_submit(): | |
61 | session['remember_me'] = form.remember_me.data | |
62 | return oid.try_login(form.openid.data, ask_for=['nickname', 'email']) | |
63 | return render_template('login.html', | |
64 | title='Sign In', | |
65 | form=form, | |
66 | providers=app.config['OPENID_PROVIDERS']) | |
67 | ||
68 | ||
69 | @oid.after_login | |
70 | def after_login(resp): | |
71 | if resp.email is None or resp.email == "": | |
72 | flash('Invalid login. Please try again.') | |
73 | return redirect(url_for('login')) | |
74 | user = User.query.filter_by(email=resp.email).first() | |
75 | if user is None: | |
76 | nickname = resp.nickname | |
77 | if nickname is None or nickname == "": | |
78 | nickname = resp.email.split('@')[0] | |
79 | nickname = User.make_unique_nickname(nickname) | |
80 | user = User(nickname=nickname, email=resp.email) | |
163c4d87 | 81 | app.logger.info("Add: %s - %s" % (nickname, respo.email)) |
db4f0ba9 MR |
82 | db.session.add(user) |
83 | db.session.commit() | |
163c4d87 MR |
84 | # make the user follow him/herself |
85 | db.session.add(user.follow(user)) | |
86 | db.session.commit() | |
db4f0ba9 MR |
87 | remember_me = False |
88 | if 'remember_me' in session: | |
89 | remember_me = session['remember_me'] | |
90 | session.pop('remember_me', None) | |
91 | login_user(user, remember=remember_me) | |
92 | return redirect(request.args.get('next') or url_for('index')) | |
93 | ||
94 | ||
95 | @app.route('/logout') | |
96 | def logout(): | |
163c4d87 | 97 | app.logger.info("Logout: %s" % g.user) |
db4f0ba9 MR |
98 | logout_user() |
99 | return redirect(url_for('login')) | |
100 | ||
101 | ||
102 | @app.route('/user/<nickname>') | |
163c4d87 | 103 | @app.route('/user/<nickname>/<int:page>') |
db4f0ba9 | 104 | @login_required |
163c4d87 | 105 | def user(nickname, page=1): |
db4f0ba9 MR |
106 | user = User.query.filter_by(nickname=nickname).first() |
107 | if user is None: | |
108 | flash('User %s not found.' % nickname) | |
109 | return redirect(url_for('index')) | |
163c4d87 MR |
110 | posts = user.sorted_posts().paginate(page, POSTS_PER_PAGE, False) |
111 | app.logger.info("%s: show profile" % user) | |
db4f0ba9 MR |
112 | return render_template('user.html', |
113 | user=user, | |
114 | posts=posts) | |
115 | ||
116 | ||
117 | @app.route('/edit', methods=['GET', 'POST']) | |
118 | @login_required | |
119 | def edit(): | |
120 | form = EditForm(g.user.nickname) | |
121 | if form.validate_on_submit(): | |
122 | g.user.nickname = form.nickname.data | |
123 | g.user.about_me = form.about_me.data | |
124 | db.session.add(g.user) | |
125 | db.session.commit() | |
126 | flash('Your changes have been saved.') | |
163c4d87 | 127 | app.logger.info("%s: updated profile" % g.user) |
db4f0ba9 MR |
128 | return redirect(url_for('edit')) |
129 | elif request.method != "POST": | |
130 | form.nickname.data = g.user.nickname | |
131 | form.about_me.data = g.user.about_me | |
132 | return render_template('edit.html', form=form) | |
133 | ||
163c4d87 MR |
134 | @app.route('/follow/<nickname>') |
135 | @login_required | |
136 | def follow(nickname): | |
137 | user = User.query.filter_by(nickname=nickname).first() | |
138 | if user is None: | |
139 | flash('User %s not found.' % nickname) | |
140 | return redirect(url_for('index')) | |
141 | if user == g.user: | |
142 | flash('You can\'t follow yourself!') | |
143 | return redirect(url_for('user', nickname=nickname)) | |
144 | u = g.user.follow(user) | |
145 | if u is None: | |
146 | flash('Cannot follow ' + nickname + '.') | |
147 | return redirect(url_for('user', nickname=nickname)) | |
148 | db.session.add(u) | |
149 | db.session.commit() | |
150 | flash('You are now following ' + nickname + '!') | |
151 | follower_notification(user, g.user) | |
152 | return redirect(url_for('user', nickname=nickname)) | |
153 | ||
154 | @app.route('/unfollow/<nickname>') | |
155 | @login_required | |
156 | def unfollow(nickname): | |
157 | user = User.query.filter_by(nickname=nickname).first() | |
158 | if user is None: | |
159 | flash('User %s not found.' % nickname) | |
160 | return redirect(url_for('index')) | |
161 | if user == g.user: | |
162 | flash('You can\'t unfollow yourself!') | |
163 | return redirect(url_for('user', nickname=nickname)) | |
164 | u = g.user.unfollow(user) | |
165 | if u is None: | |
166 | flash('Cannot unfollow ' + nickname + '.') | |
167 | return redirect(url_for('user', nickname=nickname)) | |
168 | db.session.add(u) | |
169 | db.session.commit() | |
170 | flash('You have stopped following ' + nickname + '.') | |
171 | return redirect(url_for('user', nickname=nickname)) | |
172 | ||
173 | @app.route('/delete/<int:id>') | |
174 | @login_required | |
175 | def delete(id): | |
176 | post = Post.query.get(id) | |
177 | if post is None: | |
178 | flash('Post not found.') | |
179 | return redirect(url_for('index')) | |
180 | if post.author.id != g.user.id: | |
181 | flash('You cannot delete this post.') | |
182 | return redirect(url_for('index')) | |
183 | db.session.delete(post) | |
184 | db.session.commit() | |
185 | flash('Your post has been deleted.') | |
186 | return redirect(url_for('index')) | |
187 |