diff --git a/backend/app/forms.py b/backend/app/forms.py index 4825c86..5d50a5c 100644 --- a/backend/app/forms.py +++ b/backend/app/forms.py @@ -43,6 +43,10 @@ class EditProfileForm(FlaskForm): if user is not None: raise ValidationError('Please use a different username.') +class PostForm(FlaskForm): + post = TextAreaField('Post:', validators=[DataRequired(), Length(min=1, max=140)]) + submit = SubmitField('Submit') + class EmptyForm(FlaskForm): submit = SubmitField('Submit') diff --git a/backend/app/routes.py b/backend/app/routes.py index 921db5a..d2ae9a9 100644 --- a/backend/app/routes.py +++ b/backend/app/routes.py @@ -5,8 +5,8 @@ from datetime import datetime, timezone import sqlalchemy as sa from app import app, db -from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm -from app.models import User +from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm, PostForm +from app.models import User, Post @app.before_request def before_request(): @@ -14,25 +14,37 @@ def before_request(): current_user.last_seen = datetime.now(timezone.utc) db.session.commit() -@app.route('/') -@app.route('/index') +@app.route('/', methods=['GET', 'POST']) +@app.route('/index', methods=['GET', 'POST']) @login_required def index(): app.logger.info('@@@@@@@@@@@@@@@@@ INFO LOG TEST INDEX') + form = PostForm() + #user = {'username': 'aaa', 'email': 'a@a.a'} + if form.validate_on_submit(): + post = Post(body=form.post.data, author=current_user) + db.session.add(post) + db.session.commit() + flash('Your post has been added.') + return redirect(url_for('index')) + #posts = db.session.scalars(current_user.following_posts()).all() + page = request.args.get('page', 1, type=int) + posts = db.paginate(current_user.following_posts(), page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('index', page=posts.next_num) if posts.has_next else None + prev_url = url_for('index', page=posts.prev_num) if posts.has_prev else None - user = {'username': 'Finnaa'} - posts = [ - { - 'author': {'username': 'john'}, - 'body': 'Beautiful day 1' - }, - { - 'author': {'username': 'susie'}, - 'body': 'Movie is good.' - } - ] - #return posts; - return render_template('index.html', title='Home', posts=posts) + return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url) + +@app.route('/explore') +@login_required +def explore(): + query = sa.select(Post).order_by(Post.timestamp.desc()) + page = request.args.get('page', 1, type=int) + posts = db.paginate(query, page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('explore', page=posts.next_num) if posts.has_next else None + prev_url = url_for('explore', page=posts.prev_num) if posts.has_prev else None + + return render_template('index.html', title='Explore', posts=posts.items, next_url=next_url, prev_url=prev_url) @app.route('/login', methods=['GET', 'POST']) def login(): @@ -75,12 +87,14 @@ def register(): @login_required def user(username): user = db.first_or_404(sa.select(User).where(User.username == username)) - posts = [ - {'author': user, 'body': 'Test1'}, - {'author': user, 'body': 'Test2?'} - ] + page = request.args.get('page', 1, type=int) + query = user.posts.select().order_by(Post.timestamp.desc()) + posts = db.paginate(query, page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False) + next_url = url_for('user', username=user.username, page=posts.next_num) if posts.has_next else None + prev_url = url_for('user', username=user.username, page=posts.prev_num) if posts.has_prev else None + form = EmptyForm() - return render_template('user.html', user=user, posts=posts, form=form) + return render_template('user.html', user=user, posts=posts.items, next_url=next_url, prev_url=prev_url, form=form) @app.route('/edit_profile', methods=['GET', 'POST']) @login_required diff --git a/backend/app/templates/_post.html b/backend/app/templates/_post.html index 58f0f6c..dd71e87 100644 --- a/backend/app/templates/_post.html +++ b/backend/app/templates/_post.html @@ -1,6 +1,13 @@
- | {{ post.author.username }} says: {{ post.body }} |
+
+ + | +
+
+ {{ post.author.username }}
+ says: + {{ post.body }} + |
{{ post.author.username }} says: {{ post.body }}