def edit_category(): """ 更新category的名称 """ logger.debug('request.url = ' + str(request.url)) # 获取id category_id = request.args.get('category_id') # 获取新的名字 category_name = request.args.get('category_name') # 获取category对象 category = Category.query.get_or_404(category_id) # 更新 category.name = category_name info = dict() try: # 如果更新了category的name,那么就删除所有相关的文章缓存 for post in category.posts: CacheUtil.delete_posts_from_cache(post.id) db.session.commit() info['success'] = True info['description'] = 'Update Success' except Exception as e: logger.debug('update category fail: ' + str(e.args)) info['success'] = False info['success'] = 'Update Fail, Please try it again' return json.dumps(info)
def edit_post(post_id): logger.debug('request.url = ' + str(request.url)) form = PostForm() # 根据要编辑的博客id,获取Post对象 post = Post.query.get_or_404(post_id) # 如果是修改文章时提交的form对象 if form.validate_on_submit(): # 更新post对象 post.title = form.title.data post.body = form.body.data post.category = Category.query.get(form.category.data) # 更新缓存 CacheUtil.save_posts_to_cache(post, post.category) flash('Post updated', 'success') # 提交 db.session.commit() # 跳转到博客详细页面 return redirect(url_for('blog.show_post', post_id=post.id)) # 如果是跳转到编辑文章页面去,那么将post的值赋值给form对象 # html页面中,将会显示这些值 form.title.data = post.title form.body.data = post.body form.category.data = post.category_id return render_template('admin/edit_post.html', form=form)
def manage_categories(): logger.debug('request.url = ' + str(request.url)) categories = Category.query.all() context = {} context.update(categories=categories) return render_template('admin/manage_categories.html', **context)
def change_theme(theme_name): logger.debug('request.url = ' + str(request.url)) """ 更改博客主题 :param theme_name: 主题名称 """ if theme_name not in current_app.config['BLUE2BLOG_THEMES'].keys(): abort(404) response = make_response(redirect_back()) logger.debug('response = ' + str(response)) response.set_cookie('theme', theme_name, max_age=30 * 24 * 60 * 60) return response
def delete_comment(comment_id): """ 删除评论 :param comment_id: 评论id :return: 原页面 """ logger.debug('request.url = ' + str(request.url)) comment = Comment.query.get_or_404(comment_id) db.session.delete(comment) db.session.commit() return redirect_back()
def manage_posts(): logger.debug('request.url = ' + str(request.url)) # 页数设置 page = request.args.get('page', 1, type=int) per_page = current_app.config.get('BLUE2BLOG_MANAGE_POST_PER_PAGE', 15) pagination = Post.query.order_by(Post.timestamp.desc()).paginate( page, per_page=per_page) posts = pagination.items context = dict() context.update(posts=posts) context.update(pagination=pagination) return render_template('admin/manage_posts.html', **context)
def review_comment(comment_id): """ 评论审核通过 :param comment_id: 评论id :return: 原页面 """ logger.debug('request.url = ' + str(request.url)) comment = Comment.query.get_or_404(comment_id) # 更新值 comment.reviewed = True db.session.commit() return redirect_back()
def delete_post(post_id): """ 删除博客 :param post_id: 博客id :return: 返回到原页面去 """ logger.debug('request.url = ' + str(request.url)) # 根据id获取,如果不存在,报404错误 post = Post.query.get_or_404(post_id) CacheUtil.delete_posts_from_cache(post.id) # 删除 db.session.delete(post) db.session.commit() flash('Post deleted', 'success') return redirect_back()
def show_category(category_id): logger.debug('request.url = ' + str(request.url)) # 根据id获取Category对象 category = Category.query.get_or_404(category_id) page = request.args.get("page", 1, type=int) per_page = current_app.config.get("BLUE2BLOG_POST_PER_PAGE", 15) # with_parent # order_by: 传入排序的属性 # paginate: 第一个参数是分页后的第n页,第二个参数是取出n个数据 pagination = Post.query.with_parent(category).order_by( Post.timestamp.desc()).paginate(page, per_page=per_page) posts = pagination.items context = {} context.update(posts=posts) context.update(category=category) context.update(pagination=pagination) return render_template("blog/category.html", **context)
def reply_comment(comment_id): logger.debug('request.url = ' + str(request.url)) comment = Comment.query.get_or_404(comment_id) url = redirect( url_for( "blog.show_post", # 博客id post_id=comment.post_id, # 回复的评论id reply=comment_id, # 回复评论,发表该评论的作者名字 # 会在博客页面下方的评论表单上加上作者名字 author=comment.author, ) + "#comment-form") logger.debug('redirect.url = ' + str(url)) # 转发到show_post中去处理 return url
def index(page): logger.debug('request.url = ' + str(request.url)) # 得到当前页数 # page = request.args.get('page', 1, type=int) # 每一页的数量,从配置中读取,如果没有配置,则设置为15 per_page = current_app.config.get("BLUE2BLOG_POST_PER_PAGE", 15) # 从SQLAlchemy中获取Pagination对象,分页对象 pagination = Post.query.order_by(Post.timestamp.desc()).paginate( page, per_page=per_page) # 获取分页中的博客数据 posts = pagination.items # 传递数据到html中 # posts = Post.query.order_by(Post.timestamp.desc()).all() context = {} context.update(posts=posts) context.update(pagination=pagination) return render_template("blog/index.html", **context)
def comment_enabled(): logger.debug('request.url = ' + str(request.url)) post_id = request.args.get('post_id') post = Post.query.get_or_404(post_id) # 将comment_enabled值转成相反的即可 # if post.comment_enabled: # flash('Comment Disabled', 'info') # post.comment_enabled = False # else: # flash('Comment Enabled', 'info') # post.comment_enabled = True info = dict() info['result'] = True post.comment_enabled = not post.comment_enabled try: CacheUtil.delete_posts_from_cache(post.id) db.session.commit() except: info['result'] = False info['description'] = 'Setting Comment Disabled Failed' return json.dumps(info)
def manage_comments(): logger.debug('request.url' + str(request.url)) page = request.args.get('page', 1, type=int) filter_rule = request.args.get('filter', 'all') per_page = current_app.config.get('BLUE2BLOG_COMMENT_PER_PAGE', 15) if filter_rule == 'unread': filter_comments = Comment.query.filter_by(reviewed=False) elif filter_rule == 'admin': filter_comments = Comment.query.filter_by(from_admin=True) else: filter_comments = Comment.query pagination = filter_comments.order_by(Comment.timestamp.desc()).paginate( page, per_page) comments = pagination.items context = dict() context.update(pagination=pagination) context.update(comments=comments) return render_template('admin/manage_comments.html', **context)
def new_post(): logger.debug('request.url = ' + str(request.url)) form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data # 根据id得到category对象 category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category) # 添加 db.session.add(post) db.session.commit() # 需要加到提交之后的位置,不然无法获取post的id # CacheUtil.save_posts_to_cache(post, category) flash('Post created', 'success') logger.debug('publish a new post, id = ' + str(post.id)) # 添加完成后跳转到博客详情页面查看具体内容 return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def new_category(): logger.debug('request.url = ' + str(request.url)) category_name = request.args.get('category_name') category = Category(name=category_name) db.session.add(category) info = {} try: db.session.commit() info['success'] = True info['description'] = 'Add category success' except Exception as e: logger.debug('添加category失败: ' + str(e.args)) info['success'] = False info['description'] = 'Category already exists, please input again' db.session.rollback() logger.debug('info = ' + str(json.dumps(info))) return json.dumps(info)
def show_post(post_id): logger.debug('request.url = ' + str(request.url)) try: # 如果文章没有缓存,那么久会报错 post = CacheUtil.get_posts_from_cache(post_id) except TypeError: # 博客内容 # 文章没有缓存,就从数据库中读取,并且缓存 post = Post.query.get_or_404(post_id) category = Category.query.get(post.category.id) CacheUtil.save_posts_to_cache(post, category) # 根据post_id获取文章评论 page = request.args.get("page", 1, type=int) per_page = current_app.config["BLUE2BLOG_COMMENT_PER_PAGE"] if current_user.is_authenticated: # 如果管理员已经登录,那么将显示所有的评论 pagination = (Comment.query.options( joinedload_all('*')).with_parent(post).order_by( Comment.timestamp.asc()).paginate(page, per_page)) else: pagination = ( Comment.query.options( joinedload_all('*')).with_parent(post).filter_by( reviewed=True) # 没有登录,则过滤出已经审核的评论 .order_by(Comment.timestamp.asc()).paginate(page, per_page)) comments = pagination.items # 如果当前用户已经登录 if current_user.is_authenticated: # if False: # 如果管理员登录了,那么久使用AdminCommentForm form = AdminCommentForm() # 直接从登录的管理员中取出需要的数据 form.author.data = current_user.name form.email.data = current_app.config["BLUE2BLOG_EMAIL"] form.site.data = url_for("blog.index", _external=True) from_admin = True # 管理员发表的评论不需要审核 reviewed = True else: # 如果没有登录,则构建CommentForm对象 form = CommentForm() from_admin = False reviewed = False # 从提交的POST请求中读取form数据 if form.validate_on_submit(): author = form.author.data email = form.email.data site = form.site.data body = form.body.data comment = Comment( author=author, email=email, site=site, body=body, from_admin=from_admin, post=post, reviewed=reviewed, ) # 如果是回复某一条评论的话,获取评论的id # 判断是否从reply_comment转发过来的 # 如果有数据,则说明是对评论进行评论 comment_id = request.args.get('reply') logger.debug('comment_id = ' + str(comment_id)) if comment_id: # 获取被评论的Comment对象 replied_comment = Comment.query.options( joinedload_all('*')).get_or_404(comment_id) logger.debug('replied_comment.id = ' + str(replied_comment.id)) # 管理评论与被评论对象 comment.replied_id = replied_comment.id # send_new_reply_mail(comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: # if False: flash("Comment published.", "success") else: flash("Thanks, your comment will be published after reviewed.", "info") # send_new_comment_mail(post) return redirect(url_for("blog.show_post", post_id=post_id)) context = {} context.update(post=post) context.update(comments=comments) context.update(pagination=pagination) context.update(form=form) return render_template("blog/post.html", **context)
def about(): logger.debug('request.url = ' + str(request.url)) return render_template("blog/about.html")
def logout(): logger.debug('user logout') # 退出,详细可以查看代码,很容易理解 logout_user() flash('Logout success.', 'info') return redirect(url_for('blog.index'))
def login(): logger.debug('is_authenticated = ' + str(current_user.is_authenticated)) # 如果当前已经登录,则跳转到主页 if current_user.is_authenticated: return redirect(url_for('blog.index')) form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data remember = form.remember.data logger.debug('admin.username = '******'login success') # 传入remember参数,表示是否记得长时间登录 login_user(admin, remember) flash('Welcome back', 'info') return redirect_back() logger.debug('login fail') logger.debug( f'username is right ? {username},----{admin.username}') logger.debug('password is right ? ' + str(admin.validate_password_hash(password))) else: flash('No account.', 'warning') logger.debug('redirect to login.html') # 没有登录,则跳转到登录页面 return render_template('auth/login.html', form=form)