diff --git a/app/forms.py b/app/forms.py index d8bf857..42b6b11 100644 --- a/app/forms.py +++ b/app/forms.py @@ -25,4 +25,6 @@ class EditForm(Form): return False return True - +class PostForm(Form): + post = TextField('post', validators = [Required()]) + diff --git a/app/models.py b/app/models.py index 89da1af..f11e92e 100644 --- a/app/models.py +++ b/app/models.py @@ -77,4 +77,4 @@ class Post(db.Model): user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): - return '' % (self.body) \ No newline at end of file + return '' % (self.text) \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index 80d4533..ed1f23f 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,10 +2,29 @@ {% extends "base.html" %} {% block content %} -

Hi, {{user.nickname}}!

-{% for post in posts %} -

- {{post.author.nickname}} says: {{post.body}} -

+

Hi, {{g.user.nickname}}!

+
+ {{form.hidden_tag()}} + + + + + + + + + + + +
Say something:{{ form.post(size = 30, maxlength = 140) }} + {% for error in form.errors.post %} + [{{error}}]
+ {% endfor %} +
+
+{% for post in posts.items %} + {% include 'post.html' %} {% endfor %} +{% if posts.has_prev %}<< Newer posts{% else %}<< Newer posts{% endif %} | +{% if posts.has_next %}Older posts >>{% else %}Older posts >>{% endif %} {% endblock %} \ No newline at end of file diff --git a/app/templates/user.html b/app/templates/user.html index 170cfa4..607d873 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -22,7 +22,9 @@
-{% for post in posts %} +{% for post in posts.items %} {% include 'post.html' %} {% endfor %} +{% if posts.has_prev %}<< Newer posts{% else %}<< Newer posts{% endif %} | +{% if posts.has_next %}Older posts >>{% else %}Older posts >>{% endif %} {% endblock %} \ No newline at end of file diff --git a/app/views.py b/app/views.py index 287d94a..12fdeb9 100644 --- a/app/views.py +++ b/app/views.py @@ -1,9 +1,10 @@ from flask import render_template, flash, redirect, session, url_for, request, g from flask.ext.login import login_user, logout_user, current_user, login_required from app import app, db, lm, oid -from forms import LoginForm, EditForm -from models import User, ROLE_USER, ROLE_ADMIN +from forms import LoginForm, EditForm, PostForm +from models import User, ROLE_USER, ROLE_ADMIN, Post from datetime import datetime +from config import POSTS_PER_PAGE @lm.user_loader def load_user(id): @@ -26,24 +27,22 @@ def internal_error(error): db.session.rollback() return render_template('500.html'), 500 -@app.route('/') -@app.route('/index') +@app.route('/', methods = ['GET', 'POST']) +@app.route('/index', methods = ['GET', 'POST']) +@app.route('/index/', methods = ['GET', 'POST']) @login_required -def index(): - user = g.user - posts = [ - { - 'author': { 'nickname': 'John' }, - 'body': 'Beautiful day in Portland!' - }, - { - 'author': { 'nickname': 'Susan' }, - 'body': 'The Avengers movie was so cool!' - } - ] +def index(page = 1): + form = PostForm() + if form.validate_on_submit(): + post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user) + db.session.add(post) + db.session.commit() + flash('Your post is now live!') + return redirect(url_for('index')) + posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False) return render_template('index.html', title = 'Home', - user = user, + form = form, posts = posts) @app.route('/login', methods = ['GET', 'POST']) @@ -89,16 +88,14 @@ def logout(): return redirect(url_for('index')) @app.route('/user/') +@app.route('/user//') @login_required -def user(nickname): +def user(nickname, page = 1): user = User.query.filter_by(nickname = nickname).first() if user == None: flash('User ' + nickname + ' not found.') return redirect(url_for('index')) - posts = [ - { 'author': user, 'body': 'Test post #1' }, - { 'author': user, 'body': 'Test post #2' } - ] + posts = user.posts.paginate(page, POSTS_PER_PAGE, False) return render_template('user.html', user = user, posts = posts) @@ -126,6 +123,9 @@ def follow(nickname): if user == None: flash('User ' + nickname + ' not found.') return redirect(url_for('index')) + if user == g.user: + flash('You can\'t follow yourself!') + return redirect(url_for('user', nickname = nickname)) u = g.user.follow(user) if u is None: flash('Cannot follow ' + nickname + '.') @@ -141,6 +141,9 @@ def unfollow(nickname): if user == None: flash('User ' + nickname + ' not found.') return redirect(url_for('index')) + if user == g.user: + flash('You can\'t unfollow yourself!') + return redirect(url_for('user', nickname = nickname)) u = g.user.unfollow(user) if u is None: flash('Cannot unfollow ' + nickname + '.') diff --git a/config.py b/config.py index 63e2380..6de81f1 100644 --- a/config.py +++ b/config.py @@ -22,3 +22,7 @@ MAIL_PASSWORD = None # administrator list ADMINS = ['you@example.com'] + +# pagination +POSTS_PER_PAGE = 3 +