diff --git a/.env.example b/.env.example index ce65e25..39b591c 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/greybook/blueprints/admin.py b/greybook/blueprints/admin.py index b0df347..0693c75 100644 --- a/greybook/blueprints/admin.py +++ b/greybook/blueprints/admin.py @@ -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 @@ -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) @@ -195,8 +195,8 @@ def new_category(): @admin_bp.route('/category//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')) diff --git a/greybook/blueprints/blog.py b/greybook/blueprints/blog.py index c1ab504..e7e6fac 100644 --- a/greybook/blueprints/blog.py +++ b/greybook/blueprints/blog.py @@ -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: diff --git a/greybook/core/commands.py b/greybook/core/commands.py index c31f97c..2be410f 100644 --- a/greybook/core/commands.py +++ b/greybook/core/commands.py @@ -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 @@ -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() diff --git a/greybook/core/errors.py b/greybook/core/errors.py index 606799d..2701634 100644 --- a/greybook/core/errors.py +++ b/greybook/core/errors.py @@ -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 diff --git a/greybook/forms.py b/greybook/forms.py index 0493950..9aa1c52 100644 --- a/greybook/forms.py +++ b/greybook/forms.py @@ -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() @@ -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)]) diff --git a/greybook/templates/base.html b/greybook/templates/base.html index 3e59326..6e83508 100644 --- a/greybook/templates/base.html +++ b/greybook/templates/base.html @@ -11,7 +11,7 @@ href="{{ url_for('static', filename='css/%s.min.css' % request.cookies.get('theme', 'default')) }}"> {% if admin.custom_css %} - + {% endif %} {% endblock head %} @@ -82,7 +82,7 @@

{% if admin.custom_footer %} - {{ admin.custom_footer | safe }} + {{ admin.custom_footer|safe }} {% else %} © 2024 {{ admin.name }} ยท @@ -110,7 +110,7 @@ {% if admin.custom_js %} - + {% endif %} {% endblock %} diff --git a/greybook/templates/errors/400.html b/greybook/templates/errors/400.html index b0ac9ee..27ce37b 100644 --- a/greybook/templates/errors/400.html +++ b/greybook/templates/errors/400.html @@ -8,7 +8,7 @@

400 Error

-

{{ description | default('Bad Request') }}

+

{{ description|default('Bad Request') }}

-

{{ description | default('Page Not Found') }}

+

{{ description|default('Page Not Found') }}

-

{{ description | default('Internal Server Error') }}

+

{{ description|default('Internal Server Error') }}