1
0
forked from finn/site

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: if user is not None:
raise ValidationError('Please use a different username.') 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): class EmptyForm(FlaskForm):
submit = SubmitField('Submit') submit = SubmitField('Submit')

View File

@ -5,8 +5,8 @@ from datetime import datetime, timezone
import sqlalchemy as sa import sqlalchemy as sa
from app import app, db from app import app, db
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm, PostForm
from app.models import User from app.models import User, Post
@app.before_request @app.before_request
def before_request(): def before_request():
@ -14,25 +14,37 @@ def before_request():
current_user.last_seen = datetime.now(timezone.utc) current_user.last_seen = datetime.now(timezone.utc)
db.session.commit() db.session.commit()
@app.route('/') @app.route('/', methods=['GET', 'POST'])
@app.route('/index') @app.route('/index', methods=['GET', 'POST'])
@login_required @login_required
def index(): def index():
app.logger.info('@@@@@@@@@@@@@@@@@ INFO LOG TEST 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'} return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
posts = [
{ @app.route('/explore')
'author': {'username': 'john'}, @login_required
'body': 'Beautiful day 1' def explore():
}, query = sa.select(Post).order_by(Post.timestamp.desc())
{ page = request.args.get('page', 1, type=int)
'author': {'username': 'susie'}, posts = db.paginate(query, page=page, per_page=app.config['POSTS_PER_PAGE'], error_out=False)
'body': 'Movie is good.' 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 posts; return render_template('index.html', title='Explore', posts=posts.items, next_url=next_url, prev_url=prev_url)
return render_template('index.html', title='Home', posts=posts)
@app.route('/login', methods=['GET', 'POST']) @app.route('/login', methods=['GET', 'POST'])
def login(): def login():
@ -75,12 +87,14 @@ def register():
@login_required @login_required
def user(username): def user(username):
user = db.first_or_404(sa.select(User).where(User.username == username)) user = db.first_or_404(sa.select(User).where(User.username == username))
posts = [ page = request.args.get('page', 1, type=int)
{'author': user, 'body': 'Test1'}, query = user.posts.select().order_by(Post.timestamp.desc())
{'author': user, 'body': 'Test2?'} 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() 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']) @app.route('/edit_profile', methods=['GET', 'POST'])
@login_required @login_required

View File

@ -1,6 +1,13 @@
<table> <table>
<tr valign="top"> <tr style="vertical-align: 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 style="vertical-align: middle; width: 60px;">
<td>{{ post.author.username }} says:<br>{{ post.body }}</td> <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> </tr>
</table> </table>

View File

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

View File

@ -2,9 +2,25 @@
{% block content %} {% block content %}
<h1>Hello, {{ current_user.username }}!</h1> <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 %} {% for post in posts %}
<div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div> {% include '_post.html' %}
{% endfor %} {% endfor %}
{% if prev_url %}<a href="{{ prev_url }}">Newer Posts</a>{% endif %}
{% if next_url %}<a href="{{ next_url }}">Older Posts</a>{% endif %}
{% endblock %} {% endblock %}

View File

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

View File

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