Skip to content

Commit

Permalink
Merge pull request #35 from greyli/fixes
Browse files Browse the repository at this point in the history
Improve category edit implementation
  • Loading branch information
greyli authored Sep 17, 2024
2 parents de29357 + dcff8f6 commit 781177e
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
DATABASE_URL=sqlite:////home/greybook/database/data.db
# DATABASE_URL=sqlite:////home/greybook/database/data.db
# MAIL_SERVER=smtp.example.com
# MAIL_USERNAME=example
# MAIL_PASSWORD=example-password
6 changes: 3 additions & 3 deletions greybook/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import select

from greybook.core.extensions import db
from greybook.forms import CategoryForm, LinkForm, PostForm, SettingForm
from greybook.forms import EditCategoryForm, LinkForm, NewCategoryForm, PostForm, SettingForm
from greybook.models import Category, Comment, Link, Post
from greybook.utils import allowed_file, random_filename, redirect_back

Expand Down Expand Up @@ -181,7 +181,7 @@ def manage_category():
@admin_bp.route('/category/new', methods=['GET', 'POST'])
@login_required
def new_category():
form = CategoryForm()
form = NewCategoryForm()
if form.validate_on_submit():
name = form.name.data
category = Category(name=name)
Expand All @@ -195,8 +195,8 @@ def new_category():
@admin_bp.route('/category/<int:category_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_category(category_id):
form = CategoryForm()
category = db.session.get(Category, category_id) or abort(404)
form = EditCategoryForm(current_name=category.name)
if category.id == 1:
flash('You can not edit the default category.', 'warning')
return redirect(url_for('blog.index'))
Expand Down
2 changes: 1 addition & 1 deletion greybook/blueprints/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def show_post(post_id):
form = AdminCommentForm()
form.author.data = current_user.name
form.email.data = current_app.config['GREYBOOK_ADMIN_EMAIL']
form.site.data = url_for('.index')
form.site.data = url_for('.index', _external=True)
from_admin = True
reviewed = True
else:
Expand Down
3 changes: 2 additions & 1 deletion greybook/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from sqlalchemy import select

from greybook.core.extensions import db
from greybook.lorem import fake_admin, fake_categories, fake_comments, fake_links, fake_posts, fake_replies
from greybook.models import Admin, Category


Expand Down Expand Up @@ -66,6 +65,8 @@ 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.lorem import fake_admin, fake_categories, fake_comments, fake_links, fake_posts, fake_replies

db.drop_all()
db.create_all()

Expand Down
3 changes: 2 additions & 1 deletion greybook/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ def internal_server_error(error):

@app.errorhandler(CSRFError)
def handle_csrf_error(error):
return render_template('errors/400.html', description=error.description), 400
description = 'Session expired, return last page and try again.'
return render_template('errors/400.html', description=description), 400
13 changes: 12 additions & 1 deletion greybook/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, *args, **kwargs):
self.category.choices = [(category.id, category.name) for category in categories]


class CategoryForm(FlaskForm):
class NewCategoryForm(FlaskForm):
name = StringField('Name', validators=[DataRequired(), Length(1, 30)])
submit = SubmitField()

Expand All @@ -56,6 +56,17 @@ def validate_name(self, field):
raise ValidationError('Name already in use.')


class EditCategoryForm(NewCategoryForm):
def __init__(self, current_name, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_name = current_name

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


class CommentForm(FlaskForm):
author = StringField('Name', validators=[DataRequired(), Length(1, 30)])
email = StringField('Email', validators=[DataRequired(), Email(), Length(1, 254)])
Expand Down
6 changes: 3 additions & 3 deletions greybook/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
href="{{ url_for('static', filename='css/%s.min.css' % request.cookies.get('theme', 'default')) }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
{% if admin.custom_css %}
<style>{{ admin.custom_css | safe }}</style>
<style>{{ admin.custom_css|safe }}</style>
{% endif %}
{% endblock head %}
</head>
Expand Down Expand Up @@ -82,7 +82,7 @@
<p class="float-start">
<small>
{% if admin.custom_footer %}
{{ admin.custom_footer | safe }}
{{ admin.custom_footer|safe }}
{% else %}
&copy; 2024
<a href="{{ url_for('blog.index') }}">{{ admin.name }}</a> ·
Expand Down Expand Up @@ -110,7 +110,7 @@
<script src="{{ url_for('static', filename='js/dayjs/plugin/localizedFormat.js') }}"></script>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
{% if admin.custom_js %}
<script>{{ admin.custom_js | safe }}</script>
<script>{{ admin.custom_js|safe }}</script>
{% endif %}
{% endblock %}
</body>
Expand Down
2 changes: 1 addition & 1 deletion greybook/templates/errors/400.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>400 Error</h1>
</div>
<div class="row">
<div class="col-sm-8">
<p>{{ description | default('Bad Request') }}</p>
<p>{{ description|default('Bad Request') }}</p>
</div>
<div class="col-sm-4 sidebar">
{% include 'blog/_sidebar.html' %}
Expand Down
2 changes: 1 addition & 1 deletion greybook/templates/errors/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>404 Error</h1>
</div>
<div class="row">
<div class="col-sm-8">
<p>{{ description | default('Page Not Found') }}</p>
<p>{{ description|default('Page Not Found') }}</p>
</div>
<div class="col-sm-4 sidebar">
{% include 'blog/_sidebar.html' %}
Expand Down
2 changes: 1 addition & 1 deletion greybook/templates/errors/500.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>500 Error</h1>
</div>
<div class="row">
<div class="col-sm-8">
<p>{{ description | default('Internal Server Error') }}</p>
<p>{{ description|default('Internal Server Error') }}</p>
</div>
<div class="col-sm-4 sidebar">
{% include 'blog/_sidebar.html' %}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ def test_edit_category(self):
self.assertIn('Category updated.', data)
self.assertIn('Life', data)
self.assertNotIn('Tech', data)
# test name already in use
response = self.client.post('/admin/category/2/edit', data=dict(name='Test Category'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Name already in use.', data)
# test use current name
response = self.client.post('/admin/category/2/edit', data=dict(name='Life'), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Category updated.', data)

def test_delete_category(self):
category = Category(name='Tech')
Expand Down

0 comments on commit 781177e

Please sign in to comment.