Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancements #32

Merged
merged 9 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions greybook/blueprints/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from flask import Blueprint, current_app, flash, redirect, render_template, request, url_for
from flask import Blueprint, abort, current_app, flash, redirect, render_template, request, url_for
from flask_ckeditor import upload_fail, upload_success
from flask_login import current_user, login_required
from sqlalchemy import select
Expand Down Expand Up @@ -43,10 +43,12 @@ def settings():
@login_required
def manage_post():
page = request.args.get('page', 1, type=int)
per_page = current_app.config['GREYBOOK_MANAGE_POST_PER_PAGE']
stmt = select(Post).order_by(Post.created_at.desc())
pagination = db.paginate(
select(Post).order_by(Post.created_at.desc()),
stmt,
page=page,
per_page=current_app.config['GREYBOOK_MANAGE_POST_PER_PAGE'],
per_page=per_page,
error_out=False,
)
if page > pagination.pages:
Expand Down Expand Up @@ -75,7 +77,7 @@ def new_post():
@login_required
def edit_post(post_id):
form = PostForm()
post = db.get_or_404(Post, post_id)
post = db.session.get(Post, post_id) or abort(404)
if form.validate_on_submit():
post.title = form.title.data
post.body = form.body.data
Expand All @@ -93,7 +95,7 @@ def edit_post(post_id):
@admin_bp.route('/post/<int:post_id>/delete', methods=['POST'])
@login_required
def delete_post(post_id):
post = db.get_or_404(Post, post_id)
post = db.session.get(Post, post_id) or abort(404)
post.delete()
flash('Post deleted.', 'success')
return redirect_back()
Expand All @@ -102,7 +104,7 @@ def delete_post(post_id):
@admin_bp.route('/post/<int:post_id>/set-comment', methods=['POST'])
@login_required
def set_comment(post_id):
post = db.get_or_404(Post, post_id)
post = db.session.get(Post, post_id) or abort(404)
if post.can_comment:
post.can_comment = False
flash('Comment disabled.', 'success')
Expand All @@ -127,8 +129,9 @@ def manage_comment():
else:
filtered_comments = select(Comment)

stmt = filtered_comments.order_by(Comment.created_at.desc())
pagination = db.paginate(
filtered_comments.order_by(Comment.created_at.desc()),
stmt,
page=page,
per_page=per_page,
error_out=False,
Expand All @@ -142,7 +145,7 @@ def manage_comment():
@admin_bp.route('/comment/<int:comment_id>/approve', methods=['POST'])
@login_required
def approve_comment(comment_id):
comment = db.get_or_404(Comment, comment_id)
comment = db.session.get(Comment, comment_id) or abort(404)
comment.reviewed = True
db.session.commit()
flash('Comment published.', 'success')
Expand All @@ -152,7 +155,8 @@ def approve_comment(comment_id):
@admin_bp.route('/comments/approve', methods=['POST'])
@login_required
def approve_all_comment():
comments = db.session.execute(select(Comment).filter_by(reviewed=False)).scalars().all()
stmt = select(Comment).filter_by(reviewed=False)
comments = db.session.scalars(stmt).all()
for comment in comments:
comment.reviewed = True
db.session.commit()
Expand All @@ -163,7 +167,7 @@ def approve_all_comment():
@admin_bp.route('/comment/<int:comment_id>/delete', methods=['POST'])
@login_required
def delete_comment(comment_id):
comment = db.get_or_404(Comment, comment_id)
comment = db.session.get(Comment, comment_id) or abort(404)
db.session.delete(comment)
db.session.commit()
flash('Comment deleted.', 'success')
Expand Down Expand Up @@ -194,7 +198,7 @@ def new_category():
@login_required
def edit_category(category_id):
form = CategoryForm()
category = db.get_or_404(Category, category_id)
category = db.session.get(Category, category_id) or abort(404)
if category.id == 1:
flash('You can not edit the default category.', 'warning')
return redirect(url_for('blog.index'))
Expand All @@ -212,7 +216,7 @@ def edit_category(category_id):
@admin_bp.route('/category/<int:category_id>/delete', methods=['POST'])
@login_required
def delete_category(category_id):
category = db.get_or_404(Category, category_id)
category = db.session.get(Category, category_id) or abort(404)
if category.id == 1:
flash('You can not delete the default category.', 'warning')
return redirect(url_for('blog.index'))
Expand Down Expand Up @@ -246,7 +250,7 @@ def new_link():
@login_required
def edit_link(link_id):
form = LinkForm()
link = db.get_or_404(Link, link_id)
link = db.session.get(Link, link_id) or abort(404)
if form.validate_on_submit():
link.name = form.name.data
link.url = form.url.data
Expand All @@ -262,7 +266,7 @@ def edit_link(link_id):
@admin_bp.route('/link/<int:link_id>/delete', methods=['POST'])
@login_required
def delete_link(link_id):
link = db.get_or_404(Link, link_id)
link = db.session.get(Link, link_id) or abort(404)
db.session.delete(link)
db.session.commit()
flash('Link deleted.', 'success')
Expand Down
2 changes: 1 addition & 1 deletion greybook/blueprints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def login():
username = form.username.data
password = form.password.data
remember = form.remember.data
admin = db.session.execute(select(Admin)).scalar()
admin = db.session.scalar(select(Admin))
if admin:
if username == admin.username and admin.validate_password(password):
login_user(admin, remember)
Expand Down
29 changes: 11 additions & 18 deletions greybook/blueprints/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
def index():
page = request.args.get('page', 1, type=int)
per_page = current_app.config['GREYBOOK_POST_PER_PAGE']
pagination = db.paginate(
select(Post).order_by(Post.created_at.desc()),
page=page,
per_page=per_page,
)
stmt = select(Post).order_by(Post.created_at.desc())
pagination = db.paginate(stmt, page=page, per_page=per_page)
posts = pagination.items
return render_template('blog/index.html', pagination=pagination, posts=posts)

Expand All @@ -43,31 +40,27 @@ def about():

@blog_bp.route('/category/<int:category_id>')
def show_category(category_id):
category = db.get_or_404(Category, category_id)
category = db.session.get(Category, category_id) or abort(404)
page = request.args.get('page', 1, type=int)
per_page = current_app.config['GREYBOOK_POST_PER_PAGE']
pagination = db.paginate(
select(Post).filter(with_parent(category, Category.posts)).order_by(Post.created_at.desc()),
page=page,
per_page=per_page,
)
stmt = select(Post).filter(with_parent(category, Category.posts)).order_by(Post.created_at.desc())
pagination = db.paginate(stmt, page=page, per_page=per_page)
posts = pagination.items
return render_template('blog/category.html', category=category, pagination=pagination, posts=posts)


@blog_bp.route('/post/<int:post_id>', methods=['GET', 'POST'])
def show_post(post_id):
post = db.get_or_404(Post, post_id)
post = db.session.get(Post, post_id) or abort(404)
page = request.args.get('page', 1, type=int)
per_page = current_app.config['GREYBOOK_COMMENT_PER_PAGE']
pagination = db.paginate(
stmt = (
select(Comment)
.filter(with_parent(post, Post.comments))
.filter_by(reviewed=True)
.order_by(Comment.created_at.asc()),
page=page,
per_page=per_page,
.order_by(Comment.created_at.asc())
)
pagination = db.paginate(stmt, page=page, per_page=per_page)
comments = pagination.items

if current_user.is_authenticated:
Expand All @@ -92,7 +85,7 @@ def show_post(post_id):
)
replied_id = request.args.get('reply')
if replied_id:
replied_comment = db.get_or_404(Comment, replied_id)
replied_comment = db.session.get(Comment, replied_id) or abort(404)
comment.replied = replied_comment
send_new_reply_email(replied_comment)
db.session.add(comment)
Expand All @@ -108,7 +101,7 @@ def show_post(post_id):

@blog_bp.route('/reply/comment/<int:comment_id>')
def reply_comment(comment_id):
comment = db.get_or_404(Comment, comment_id)
comment = db.session.get(Comment, comment_id) or abort(404)
if not comment.post.can_comment:
flash('Comment is disabled.', 'warning')
return redirect(url_for('.show_post', post_id=comment.post.id))
Expand Down
6 changes: 3 additions & 3 deletions greybook/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def init_blog_command(username, password):
db.create_all()
click.echo('Initialized the database.')

admin = db.session.execute(select(Admin)).scalar()
admin = db.session.scalar(select(Admin))
if admin is not None:
admin.username = username
admin.password = password
Expand All @@ -48,7 +48,7 @@ def init_blog_command(username, password):
db.session.add(admin)
click.echo('Created the administrator account.')

category = db.session.execute(select(Category)).scalar()
category = db.session.scalar(select(Category))
if category is None:
category = Category(name='Default')
db.session.add(category)
Expand All @@ -68,7 +68,7 @@ def init_blog_command(username, password):
@click.option('--reply', default=50, help='Quantity of replies, default is 50.')
def lorem_command(category, post, comment, reply):
"""Generate fake data."""
from greybook.fakes import fake_admin, fake_categories, fake_comments, fake_links, fake_posts, fake_replies
from greybook.lorem import fake_admin, fake_categories, fake_comments, fake_links, fake_posts, fake_replies

db.drop_all()
db.create_all()
Expand Down
2 changes: 1 addition & 1 deletion greybook/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def format(self, record):
mail_handler = SMTPHandler(
mailhost=app.config['MAIL_SERVER'],
fromaddr=app.config['MAIL_USERNAME'],
toaddrs=['ADMIN_EMAIL'],
toaddrs=['GREYBOOK_ADMIN_EMAIL'],
subject=app.config['GREYBOOK_ERROR_EMAIL_SUBJECT'],
credentials=(app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD']),
)
Expand Down
11 changes: 5 additions & 6 deletions greybook/core/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
def register_template_handlers(app):
@app.context_processor
def make_template_context():
admin = db.session.execute(select(Admin)).scalar()
categories = db.session.execute(select(Category).order_by(Category.name)).scalars().all()
links = db.session.execute(select(Link).order_by(Link.name)).scalars().all()
admin = db.session.scalar(select(Admin))
categories = db.session.scalars(select(Category).order_by(Category.name)).all()
links = db.session.scalars(select(Link).order_by(Link.name)).all()

if current_user.is_authenticated:
unread_comments = (
db.session.execute(select(func.count(Comment.id)).filter_by(reviewed=False)).scalars().one()
)
stmt = select(func.count(Comment.id)).filter_by(reviewed=False)
unread_comments = db.session.scalars(stmt).one()
else:
unread_comments = None
return dict(admin=admin, categories=categories, links=links, unread_comments=unread_comments)
4 changes: 2 additions & 2 deletions greybook/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PostForm(FlaskForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
categories = db.session.execute(select(Category).order_by(Category.name)).scalars()
categories = db.session.scalars(select(Category).order_by(Category.name))
self.category.choices = [(category.id, category.name) for category in categories]


Expand All @@ -52,7 +52,7 @@ class CategoryForm(FlaskForm):
submit = SubmitField()

def validate_name(self, field):
if db.session.execute(select(Category).filter_by(name=field.data)).scalar():
if db.session.scalar(select(Category).filter_by(name=field.data)):
raise ValidationError('Name already in use.')


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion greybook/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseConfig:
GREYBOOK_MANAGE_POST_PER_PAGE = 15
GREYBOOK_COMMENT_PER_PAGE = 15
# ('theme name', 'display name')
GREYBOOK_THEMES = {'default': 'Default', 'perfect_blue': 'Perfect Blue'}
GREYBOOK_THEMES = {'default': 'Default', 'bluelog': 'Bluelog'}
GREYBOOK_SLOW_QUERY_THRESHOLD = 1

GREYBOOK_UPLOAD_PATH = os.getenv('GREYBOOK_UPLOAD_PATH', BASE_DIR / 'uploads')
Expand Down
2 changes: 1 addition & 1 deletion greybook/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
{% if admin.custom_footer %}
{{ admin.custom_footer | safe }}
{% else %}
&copy; 2023
&copy; 2024
<a href="{{ url_for('blog.index') }}">{{ admin.name }}</a> ·
Powered by <a href="https://github.com/greyli/greybook">Greybook</a>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion greybook/templates/blog/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1>About</h1>
{{ admin.about|safe }}
</div>
<div class="col-sm-4 sidebar">
{% include "blog/_sidebar.html" %}
{% include 'blog/_sidebar.html' %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion greybook/templates/blog/category.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1>Category: {{ category.name }}</h1>
</div>
</div>
<div class="col-sm-4 sidebar">
{% include "blog/_sidebar.html" %}
{% include 'blog/_sidebar.html' %}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion greybook/templates/blog/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ <h5>Comment disabled.</h5>
{% endif %}
</div>
<div class="col-sm-4 sidebar">
{% include "blog/_sidebar.html" %}
{% include 'blog/_sidebar.html' %}
</div>
</div>
{% endblock %}
19 changes: 19 additions & 0 deletions greybook/templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@
</button>
</form>
{% endmacro %}

{% macro pager(pagination, fragment='') %}
<nav aria-label="Page navigation">
<ul class="pagination">
<li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
<a class="page-link"
href="{{ url_for(request.endpoint, page=pagination. prev_num, **kwargs) + fragment if pagination.has_prev else '#'}}">
<span aria-hidden="true">&larr;</span> Newer
</a>
</li>
<li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
<a class="page-link"
href="{{ url_for(request.endpoint, page=pagination. next_num, **kwargs) + fragment if pagination.has_next else '#'}}">
Older <span aria-hidden="true">&rarr;</span>
</a>
</li>
</ul>
</nav>
{% endmacro %}
6 changes: 3 additions & 3 deletions tests/test_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def test_change_theme(self):
response = self.client.get('/change-theme/default', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('css/default.min.css', data)
self.assertNotIn('css/perfect_blue.min.css', data)
self.assertNotIn('css/bluelog.min.css', data)

response = self.client.get('/change-theme/perfect_blue', follow_redirects=True)
response = self.client.get('/change-theme/bluelog', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('css/perfect_blue.min.css', data)
self.assertIn('css/bluelog.min.css', data)
self.assertNotIn('css/default.min.css', data)

def test_about_page(self):
Expand Down
Loading