def edit_category(category_id): if category_id == 1: flash("默认分类不可编辑", "danger") return redirect_back() form = CategoryForm() category = Category.query.get_or_404(category_id) if form.validate_on_submit(): if form.name.data == "默认分类": flash("修改失败!默认分类只能有一个", "danger") return redirect_back() category.name = form.name.data db.session.commit() flash("分类名称修改成功", "success") return redirect_back() form.name.data = category.name return render_template("admin/edit_category.html", form=form)
def login(): form = LoginForm() if form.validate_on_submit(): user = Admin.query.filter_by(username=form.username.data).first() if user is not None and user.validate_password(form.password.data): login_user(user, form.remember.data) flash("登陆成功", "success") return redirect_back() else: flash("用户名或密码错误", "danger") return render_template("auth/login.html", form=form)
def delete_category(category_id): # 再次判断,保证默认分类不被删除 if category_id == 1: flash("默认分类不能被删除", "danger") return redirect(url_for("admin.manage_category")) category = Category.query.get_or_404(category_id) # 将删除的分类所属的文章变更为默认分类 if category.posts: print(category.posts) for post in category.posts: post.category_id = 1 print(post.category_id) db.session.commit() db.session.delete(category) db.session.commit() flash("分类删除成功", "success") return redirect_back()
def delete_post(post_id): post = Post.query.get_or_404(post_id) db.session.delete(post) db.session.commit() flash("文章删除成功", "success") return redirect_back()
def close_comment(post_id): post = Post.query.get_or_404(post_id) post.can_comment = not post.can_comment db.session.commit() flash("成功操作了评论区", "success") return redirect_back()
def review_comment(comment_id): comment = Comment.query.get_or_404(comment_id) comment.reviewed = not comment.reviewed db.session.commit() flash("评论审核状态已改变", "success") return redirect_back()
def delete_comment(comment_id): comment = Comment.query.get_or_404(comment_id) db.session.delete(comment) db.session.commit() flash("评论删除成功", "success") return redirect_back()
def logout(): logout_user() flash("您已登出", "info") return redirect_back()