def allow_comment(id): comment = Comment.query.get_or_404(id) comment.disabled = True db.session.add(comment) db.session.commit() flash('允许通过') # 发送邮件 admin_mail = current_app.config['ADMIN_MAIL'] if comment.replied_id: reply_to_comment = Comment.query.get_or_404(comment.replied_id) reply_email = reply_to_comment.email if reply_email != admin_mail: # 邮件配置 to_addr = reply_email # 站点链接 base_url = current_app.config['WEB_URL'] # 收件人就是被回复的人 nickname = reply_to_comment.author com = comment.comment post_url = '' if comment.post: post_url = 'http://{}'.format('/'.join( map(str, [ base_url, comment.post.year, comment.post.month, comment.post.url_name ]))) elif comment.page: post_url = 'http://{0}/page/{1}'.format( base_url, comment.page.url_name) elif comment.article: post_url = 'http://{0}/column/{1}/{2}'.format( base_url, comment.article.column.url_name, str(comment.article.id)) # 发送邮件 msg = render_template('user_mail.html', nickname=nickname, comment=com, url=post_url) asyncio_send(to_addr, msg) page = comment.page post = comment.post if page and page.url_name == 'guest-book': # 清除缓存 cache_tool.update_global(global_cache_key.GUEST_BOOK_COUNT, 1, cache_tool.ADD) elif post and isinstance(post, Post): # 更新文章缓存 cache_key = '_'.join( map(str, ['post', post.year, post.month, post.url_name])) post_cache = cache_tool.get(cache_key) if post_cache: post_cache['comment_count'] += 1 cache_tool.set(cache_key, post_cache) return redirect(url_for('admin.admin_comments'))
def write_talk(): form = TalkForm() if form.validate_on_submit(): talk = Talk(talk=form.talk.data) db.session.add(talk) db.session.commit() flash('Posted successfully.') # 清除缓存 cache_tool.update_global(global_cache_key.TALK, talk.body_to_html) return redirect(url_for('admin.write_talk')) return render_template('admin/edit_talk.html', title='写说说', form=form)
def delete_link(id): link = SiteLink.query.get_or_404(id) db.session.delete(link) db.session.commit() # update cache if link.is_friend: cache_tool.update_global(global_cache_key.FRIEND_COUNT, 1, cache_tool.ADD) else: cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_links'))
def delete_post(id): post = Post.query.get_or_404(id) _category = Category.query.get_or_404(post.category_id) if _category.posts.count() == 1: db.session.delete(_category) db.session.delete(post) db.session.commit() flash('Deleted successfully.') # update cache if post.draft is False: cache_tool.update_global(global_cache_key.POST_COUNT, 1, cache_tool.REMOVE) return redirect(url_for('admin.admin_posts'))
def delete_talk(id): count = Talk.query.count() if count == 1: flash('There must be one talk.') return redirect(url_for('admin.admin_talk')) talk = Talk.query.get_or_404(id) db.session.delete(talk) db.session.commit() flash('Deleted successfully.') # update cache new_talk = Talk.query.order_by(Talk.timestamp.desc()).first() value = new_talk.body_to_html if new_talk else '这家伙啥都不想说...' cache_tool.update_global(global_cache_key.TALK, value) return redirect(url_for('admin.admin_talk'))
def add_link(): form = SocialLinkForm() fr_form = FriendLinkForm() # 社交链接 if form.submit.data and form.validate_on_submit(): exist_link = SiteLink.query.filter_by(link=form.link.data).first() if exist_link: flash('Link already exists...') return redirect(url_for('admin.add_link')) url = form.link.data name = form.name.data link = SiteLink(link=url, name=name, is_friend=False) db.session.add(link) db.session.commit() flash('Added successfully.') # update cache cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.add_link')) # 友链 if fr_form.submit2.data and fr_form.validate_on_submit(): exist_link = SiteLink.query.filter_by(link=fr_form.link.data).first() if exist_link: flash('Link already exists...') return redirect(url_for('admin.add_link')) link = SiteLink(link=fr_form.link.data, name=fr_form.name.data, info=fr_form.info.data, is_friend=True) db.session.add(link) db.session.commit() flash('Added successfully.') # update cache cache_tool.update_global(global_cache_key.FRIEND_COUNT, 1, cache_tool.ADD) return redirect(url_for('admin.add_link')) return render_template('admin/edit_link.html', title="站点链接", form=form, fr_form=fr_form)
def delete_comment(id): comment = Comment.query.get_or_404(id) page = comment.page post = comment.post db.session.delete(comment) db.session.commit() flash('Deleted successfully.') if comment.disabled is True: if page and page.url_name == 'guest-book': # 清除缓存 cache_tool.update_global(global_cache_key.GUEST_BOOK_COUNT, 1, cache_tool.REMOVE) elif post and isinstance(post, Post): # 删除文章缓存 cache_key = '_'.join( map(str, ['post', post.year, post.month, post.url_name])) post_cache = cache_tool.get(cache_key) post_cache['comment_count'] -= 1 cache_tool.set(cache_key, post_cache) return redirect(url_for('admin.admin_comments'))