]>
git.datanom.net - flask-test.git/blob - app/views.py
7017bd59d9e4e1a83e780b4d19d8318e6e9cb894
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
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
12 return User
.query
.get(int(id))
17 if g
.user
.is_authenticated
:
18 g
.user
.last_seen
= datetime
.utcnow()
19 db
.session
.add(g
.user
)
23 @app.errorhandler(404)
24 def not_found_error(error
):
25 return render_template('404.html'), 404
28 @app.errorhandler(500)
29 def internal_error(error
):
31 return render_template('500.html'), 500
34 @app.route('/', methods
=['GET', 'POST'])
35 @app.route('/index', methods
=['GET', 'POST'])
36 @app.route('/index/<int:page>', methods
=['GET', 'POST'])
40 if form
.validate_on_submit():
41 post
= Post(body
=form
.post
.data
, timestamp
=datetime
.utcnow(), author
=g
.user
)
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)
48 return render_template('index.html',
54 @app.route('/login', methods
=['GET', 'POST'])
57 if g
.user
is not None and g
.user
.is_authenticated
:
58 return redirect(url_for('index'))
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',
66 providers
=app
.config
['OPENID_PROVIDERS'])
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()
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
)
81 app
.logger
.info("Add: %s - %s" % (nickname
, respo
.email
))
84 # make the user follow him/herself
85 db
.session
.add(user
.follow(user
))
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'))
97 app
.logger
.info("Logout: %s" % g
.user
)
99 return redirect(url_for('login'))
102 @app.route('/user/<nickname>')
103 @app.route('/user/<nickname>/<int:page>')
105 def user(nickname
, page
=1):
106 user
= User
.query
.filter_by(nickname
=nickname
).first()
108 flash('User %s not found.' % nickname
)
109 return redirect(url_for('index'))
110 posts
= user
.sorted_posts().paginate(page
, POSTS_PER_PAGE
, False)
111 app
.logger
.info("%s: show profile" % user
)
112 return render_template('user.html',
117 @app.route('/edit', methods
=['GET', 'POST'])
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
)
126 flash('Your changes have been saved.')
127 app
.logger
.info("%s: updated profile" % g
.user
)
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
)
134 @app.route('/follow/<nickname>')
136 def follow(nickname
):
137 user
= User
.query
.filter_by(nickname
=nickname
).first()
139 flash('User %s not found.' % nickname
)
140 return redirect(url_for('index'))
142 flash('You can\'t follow yourself!')
143 return redirect(url_for('user', nickname
=nickname
))
144 u
= g
.user
.follow(user
)
146 flash('Cannot follow ' + nickname
+ '.')
147 return redirect(url_for('user', nickname
=nickname
))
150 flash('You are now following ' + nickname
+ '!')
151 follower_notification(user
, g
.user
)
152 return redirect(url_for('user', nickname
=nickname
))
154 @app.route('/unfollow/<nickname>')
156 def unfollow(nickname
):
157 user
= User
.query
.filter_by(nickname
=nickname
).first()
159 flash('User %s not found.' % nickname
)
160 return redirect(url_for('index'))
162 flash('You can\'t unfollow yourself!')
163 return redirect(url_for('user', nickname
=nickname
))
164 u
= g
.user
.unfollow(user
)
166 flash('Cannot unfollow ' + nickname
+ '.')
167 return redirect(url_for('user', nickname
=nickname
))
170 flash('You have stopped following ' + nickname
+ '.')
171 return redirect(url_for('user', nickname
=nickname
))
173 @app.route('/delete/<int:id>')
176 post
= Post
.query
.get(id)
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
)
185 flash('Your post has been deleted.')
186 return redirect(url_for('index'))
This page took 0.134277 seconds and 6 git commands to generate.