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 love_me(): data = request.get_json() if data.get('i_am_handsome', '') == 'yes': # 更新缓存 global_cache = cache_tool.get(cache_tool.GLOBAL_KEY) global_cache[global_cache_key.LOVE_COUNT] += 1 cache_tool.set(cache_tool.GLOBAL_KEY, global_cache) love_me_counts = LoveMe.query.first() love_me_counts.love_count += 1 db.session.add(love_me_counts) db.session.commit() return jsonify(counts=love_me_counts.love_count) return jsonify(you_are_sb='yes')
async def _global_data(): """ 所有页面共有的,比如侧栏的标签集合,社交链接,博主信息, 和导航栏的所有分类。 :return: global_data = { 'admin': admin, // model object 'social_links': socaial_links, // object list 'friend_links': friend_links, // ... 'tags': tags, // ... 'categories': categories, // ... 'pages': pages, // ... } """ global_data = {} administrator = Admin.query.first() links = SiteLink.query.order_by(SiteLink.id.desc()).all() categories = Category.query.filter_by(is_show=True).all() tags = Tag.query.all() pages = Page.query.filter_by(show_nav=True).all() love_me_counts = LoveMe.query.first() posts = Post.query.filter_by(draft=False).count() talk = Talk.query.order_by(Talk.timestamp.desc()).first() guest_book = Page.query.filter_by(url_name='guest-book').first() boxes = SideBox.query.order_by(SideBox.id.desc()).all() global_data[global_cache_key.ADMIN] = administrator global_data[global_cache_key.TAGS] = tags global_data[global_cache_key.CATEGORIES] = categories global_data[global_cache_key.PAGES] = pages global_data[global_cache_key.LOVE_COUNT] = love_me_counts.love_count global_data[global_cache_key.POST_COUNT] = posts global_data[global_cache_key.TALK] = talk.body_to_html guest_book_counts = guest_book.comments.count() if guest_book and guest_book.comments else 0 global_data[global_cache_key.GUEST_BOOK_COUNT] = guest_book_counts if links: social_links = [link for link in links if link.is_friend is False] friend_links_counts = len(links) - len(social_links) global_data[global_cache_key.SOCIAL_LINKS] = social_links global_data[global_cache_key.FRIEND_COUNT] = friend_links_counts if boxes: adv_boxes = [box for box in boxes if box.unable is False and box.is_advertising is True] global_data[global_cache_key.ADS_BOXES] = adv_boxes my_boxes = [box for box in boxes if box.unable is False and box.is_advertising is False] global_data[global_cache_key.MY_BOXES] = my_boxes cache_tool.set(cache_tool.GLOBAL_KEY, global_data, timeout=60 * 60 * 24 * 30) return global_data
def set_model_cache(key): """设置博客文章缓存""" _type, *_, query_field = key.split('_') if _type not in ModelType.MODEL_TYPES: raise NoPostException('Set the post cache exception.') data = {} if _type == ModelType.POST: data = _generate_post_cache(query_field) elif _type == ModelType.ARTICLE: data = _generate_article_cache(query_field) elif _type == ModelType.COLUMN: data = _generate_column_cache(query_field) cache_tool.set(key, data, timeout=60 * 60 * 24 * 30) return data
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'))