mgt c9 complete

This commit is contained in:
finn 2024-08-04 13:14:58 -07:00
parent 94434d4f8e
commit 61c8cadb87
7 changed files with 75 additions and 29 deletions

View File

@ -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')

View File

@ -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

View File

@ -1,6 +1,13 @@
<table>
<tr valign="top">
<td style="vertical-align: middle;"><img style="vertical-align: middle; width: 40px;" src="data:image/png;base64,{{ user.gen_avatar(write_png=False) }}"></td>
<td>{{ post.author.username }} says:<br>{{ post.body }}</td>
<tr style="vertical-align: top;">
<td style="vertical-align: middle; width: 60px;">
<img style="vertical-align: middle; width: 40px;" src="data:image/png;base64,{{ post.author.gen_avatar(write_png=False) }}">
</td>
<td>
<a href="{{ url_for('user', username=post.author.username) }}">
{{ post.author.username }}
</a> says:<br>
{{ post.body }}
</td>
</tr>
</table>

View File

@ -3,19 +3,20 @@
<head>
<link rel="stylesheet" href="{{ url_for('static', filename='simple.css') }}">
{% if title %}
<title>{{ title }} - blog</title>
<title>{{ title }} - blogpage</title>
{% else %}
<title>Welcome to blog.</title>
{% endif %}
</head>
<body>
<div>
blgo:
blog:
<a href="{{ url_for('index') }}">home</a>
<a href="{{ url_for('explore') }}">explore</a>
{% if current_user.is_anonymous %}
<a href="{{ url_for('login') }}">login</a>
{% else %}
<a href="{{ url_for('user', username=current_user.username) }}">Profile</a>
<a href="{{ url_for('user', username=current_user.username) }}">profile</a>
<a href="{{ url_for('logout') }}">logout</a>
{% endif %}

View File

@ -2,9 +2,25 @@
{% block content %}
<h1>Hello, {{ current_user.username }}!</h1>
{% if form %}
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.post.label }}
{{ form.post(cols=32, rows=4) }}
{% for error in form.post.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endif %}
{% for post in posts %}
<div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}<a href="{{ prev_url }}">Newer Posts</a>{% endif %}
{% if next_url %}<a href="{{ next_url }}">Older Posts</a>{% endif %}
{% endblock %}

View File

@ -38,5 +38,8 @@
{% for post in posts %}
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}<a href="{{ prev_url }}">Newer Posts</a>{% endif %}
{% if next_url %}<a href="{{ next_url }}">Older Posts</a>{% endif %}
{% endblock %}

View File

@ -18,5 +18,6 @@ class Config:
FROM_ADDRESS = os.environ.get('FROM_ADDRESS')
DC_LOGGING = True
POSTS_PER_PAGE=5