def post(self, *args, **kwargs): json_data = get_cleaned_json_data(self, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] # 获取文章详情 if opt == 'get-post': try: post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False) except: self.write(json_result(1, '不存在该post')) return else: self.write( json_result( 0, { 'title': post.title, 'content': post.content, 'labels': BlogPostLabel.get_post_label(post), 'category': post.category.name })) return # 更新文章 elif opt == 'update-post': try: post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False) except: self.write(json_result(1, '不存在该post')) return else: cate = BlogPostCategory.get_by_name(data['category']) post.category = cate post.title = data['title'] post.content = data['content'] post.save() BlogPostLabel.update_post_label(data['labels'], post) self.write(json_result(0, 'success')) return # 创建文章 elif opt == 'create-post': cate = BlogPostCategory.get_by_name(data['category']) post = BlogPost.create(title=data['title'], category=cate, content=data['content']) BlogPostLabel.add_post_label(data['labels'], post) self.write(json_result(0, 'success')) return else: self.write(json_result(1, 'opt不支持'))
def get(self, category_name, *args, **kwargs): try: category = BlogPostCategory.get(BlogPostCategory.name == category_name) except BlogPostCategory.DoesNotExist: self.redirect("/static/404.html") return current_page = get_cleaned_query_data(self, ['page',], blank=True)['page'] current_page = get_page_number(current_page) posts, page_number_limit = BlogPost.list_by_category(category, page_number=current_page) for post in posts: post.labels = BlogPostLabel.get_post_label(post) pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) # 使用到联合、分组等查询, 得到每个分类下文章个数, 得到每个标签下文章个数 # select category_id, blogpostcategory.name, count(blogpost.id) from blogpost inner join blogpostcategory on blogpost.category_id=blogpostcategory.id group by category_id; categorys = BlogPost.select(BlogPostCategory.name, fn.COUNT(BlogPost.id).alias('count'))\ .join(BlogPostCategory, on=(BlogPostCategory.id == BlogPost.category))\ .group_by(BlogPost.category) for category in categorys: category.name = category.category.name labels = BlogPostLabel.select(BlogPostLabel.name, fn.COUNT(BlogPostLabel.post).alias('count')).where(BlogPostLabel.is_del == False).group_by(BlogPostLabel.name) self.render('blog/index.html', posts=posts, labels=labels, categorys=categorys, pages=pages, pages_prefix_url='/blog/category/'+category_name+'?page=')
def post(self, *args, **kwargs): json_data = get_cleaned_json_data(self, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] # 获取文章详情 if opt == 'get-post': try: post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False) except: self.write(json_result(1, '不存在该post')) return else: self.write(json_result(0, {'title': post.title, 'content': post.content, 'labels': BlogPostLabel.get_post_label(post), 'category': post.category.name})) return # 更新文章 elif opt == 'update-post': try: post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False) except: self.write(json_result(1, '不存在该post')) return else: cate = BlogPostCategory.get_by_name(data['category']) post.category = cate post.title = data['title'] post.content = data['content'] post.save() BlogPostLabel.update_post_label(data['labels'], post) self.write(json_result(0, 'success')) return # 创建文章 elif opt == 'create-post': cate = BlogPostCategory.get_by_name(data['category']) post = BlogPost.create(title=data['title'], category=cate, content=data['content']) BlogPostLabel.add_post_label(data['labels'], post) self.write(json_result(0, 'success')) return else: self.write(json_result(1, 'opt不支持'))
def convert_md_2_post(md_info): category = md_info.get('category') if not category: category = '未分类' cate = BlogPostCategory.get_by_name(category) post = BlogPost.create(title=md_info['title'], category=cate, content=md_info['content']) BlogPostLabel.add_post_label(md_info['tags'], post)
def get(self, category_name, *args, **kwargs): try: category = BlogPostCategory.get( BlogPostCategory.name == category_name) except BlogPostCategory.DoesNotExist: self.redirect("/static/404.html") return current_page = get_cleaned_query_data(self, [ 'page', ], blank=True)['page'] current_page = get_page_number(current_page) posts, page_number_limit = BlogPost.list_by_category( category, page_number=current_page) for post in posts: post.labels = BlogPostLabel.get_post_label(post) pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) # 使用到联合、分组等查询, 得到每个分类下文章个数, 得到每个标签下文章个数 # select category_id, blogpostcategory.name, count(blogpost.id) from blogpost inner join blogpostcategory on blogpost.category_id=blogpostcategory.id group by category_id; categorys = BlogPost.select(BlogPostCategory.name, fn.COUNT(BlogPost.id).alias('count'))\ .join(BlogPostCategory, on=(BlogPostCategory.id == BlogPost.category))\ .group_by(BlogPost.category) for category in categorys: category.name = category.category.name labels = BlogPostLabel.select( BlogPostLabel.name, fn.COUNT(BlogPostLabel.post).alias('count')).where( BlogPostLabel.is_del == False).group_by(BlogPostLabel.name) self.render('blog/index.html', posts=posts, labels=labels, categorys=categorys, pages=pages, pages_prefix_url='/blog/category/' + category_name + '?page=')
post = BlogPost.create(title=md_info['title'], category=cate, slug=md_info['slug'], content=md_info['content']) BlogPostLabel.add_post_label(md_info['tags'], post) def get_files(root_path): files = os.listdir(root_path) print(files) for file_name in files: _, suffix = os.path.splitext(file_name) if suffix == '.md': md_file_path = os.path.join(root_path, file_name) md_info = check_md_format(md_file_path) if md_info: print(md_info['title']) convert_md_2_post(md_info) if __name__ == '__main__': mysqldb.create_tables([BlogPostLabel, BlogPost, BlogPostCategory], safe=True) t = BlogPostLabel.delete() t.execute() t = BlogPost.delete() t.execute() t = BlogPostCategory.delete() t.execute() get_files(md_path)
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatLog from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 建表 --------------- db_mysql.create_tables([User, ChatLog, PostCategory, PostTopic, Post, PostReply, CollectPost, Profile, Follower, Notification, BlogPostCategory, BlogPost, BlogPostLabel], safe=True) user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(''' 版块分类 docker: docker文章 registry: registry文章、私有hub、images分享, dockerfile分享 docker集群: docker集群文章 ''') postcategory0 = PostCategory.create(name='docker', str='docker') postcategory1 = PostCategory.create(name='registry', str='registry') postcategory2 = PostCategory.create(name='docker集群', str='docker-cluster') posttopic0 = PostTopic.create(category=postcategory0, name='docker文章', str='docker-article') posttopic1 = PostTopic.create(category=postcategory1, name='registry文章', str='registry-article') posttopic2 = PostTopic.create(category=postcategory1, name='私有hub', str='private-hub') posttopic3 = PostTopic.create(category=postcategory1, name='image分享', str='image-share') posttopic4 = PostTopic.create(category=postcategory1, name='dockerfile分享', str='dockerfile-share') posttopic5 = PostTopic.create(category=postcategory2, name='docker集群文章', str='docker-cluster-article') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatLog.create(me=user_admin, other=user_test, content='self>other') chat_log_0 = ChatLog.create(me=user_test, other=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 建表 --------------- db_mysql.create_tables([User, PostCategory, PostTopic, Post, PostReply, CollectPost, Profile, Follower, Notification, BlogPostCategory, BlogPost, BlogPostLabel], safe=True) logger.debug('add user: [admin:admin], [test:test]') user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- logger.debug('add follower: [test]->[admin]') Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug('add postcategory and posttopic:') logger.debug(''' 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 ''') postcategory0 = PostCategory.create(name='学习', str='study') postcategory1 = PostCategory.create(name='专业', str='major') postcategory2 = PostCategory.create(name='生活', str='live') postcategory3 = PostCategory.create(name='爱好', str='hobby') posttopic0 = PostTopic.create(category=postcategory0, name='学习资料', str='study-material') posttopic1 = PostTopic.create(category=postcategory0, name='考研资料', str='study-advance-material') posttopic2 = PostTopic.create(category=postcategory0, name='竞赛', str='study-competition') posttopic3 = PostTopic.create(category=postcategory0, name='请教', str='study-advice') posttopic4 = PostTopic.create(category=postcategory1, name='计算机', str='major-computer') posttopic5 = PostTopic.create(category=postcategory2, name='电影资源', str='live-movie') posttopic6 = PostTopic.create(category=postcategory2, name='共享账号', str='live-account') posttopic7 = PostTopic.create(category=postcategory2, name='电脑故障', str='live-computer-repair') posttopic8 = PostTopic.create(category=postcategory3, name='摄影', str='hobby-photography') posttopic9 = PostTopic.create(category=postcategory3, name='健身', str='hobby-fitness') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- logger.debug('add post: [SICP换零钱(递归转尾递归)]') post = Post.create( topic=posttopic0, title='SICP换零钱(递归转尾递归)', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- logger.debug('add notice: [admin]->[admin\'s followers]') Notification.new_post(post) # ------------测试新回复-------------- logger.debug('add postreply: [test]->[admin]') postreply = PostReply.create( post=post, user=user_test, content='迭代需要重复利用递归产生的冗余数据' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- logger.debug('add blogpost: [tornado]') bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0)
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(""" 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 """) postcategory0 = PostCategory.create(name='学习', str='study') postcategory1 = PostCategory.create(name='专业', str='major') postcategory2 = PostCategory.create(name='生活', str='live') postcategory3 = PostCategory.create(name='爱好', str='hobby') posttopic0 = PostTopic.create(category=postcategory0, name='学习资料', str='study-material') posttopic1 = PostTopic.create(category=postcategory0, name='考研资料', str='study-advance-material') posttopic2 = PostTopic.create(category=postcategory0, name='竞赛', str='study-competition') posttopic3 = PostTopic.create(category=postcategory0, name='请教', str='study-advice') posttopic4 = PostTopic.create(category=postcategory1, name='计算机', str='major-computer') posttopic5 = PostTopic.create(category=postcategory2, name='电影资源', str='live-movie') posttopic6 = PostTopic.create(category=postcategory2, name='共享账号', str='live-account') posttopic7 = PostTopic.create(category=postcategory2, name='电脑故障', str='live-computer-repair') posttopic8 = PostTopic.create(category=postcategory3, name='摄影', str='hobby-photography') posttopic9 = PostTopic.create(category=postcategory3, name='健身', str='hobby-fitness') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create(topic=posttopic0, title='test', content=tmp_post, user=user_admin) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create(post=post, user=user_test, content='test') post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(""" 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 """) postcategory0 = PostCategory.create(name='学习', str='study') postcategory1 = PostCategory.create(name='专业', str='major') postcategory2 = PostCategory.create(name='生活', str='live') postcategory3 = PostCategory.create(name='爱好', str='hobby') posttopic0 = PostTopic.create(category=postcategory0, name='学习资料', str='study-material') posttopic1 = PostTopic.create(category=postcategory0, name='考研资料', str='study-advance-material') posttopic2 = PostTopic.create(category=postcategory0, name='竞赛', str='study-competition') posttopic3 = PostTopic.create(category=postcategory0, name='请教', str='study-advice') posttopic4 = PostTopic.create(category=postcategory1, name='计算机', str='major-computer') posttopic5 = PostTopic.create(category=postcategory2, name='电影资源', str='live-movie') posttopic6 = PostTopic.create(category=postcategory2, name='共享账号', str='live-account') posttopic7 = PostTopic.create(category=postcategory2, name='电脑故障', str='live-computer-repair') posttopic8 = PostTopic.create(category=postcategory3, name='摄影', str='hobby-photography') posttopic9 = PostTopic.create(category=postcategory3, name='健身', str='hobby-fitness') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatLog from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 建表 --------------- db_mysql.create_tables([User, ChatLog, PostCategory, PostTopic, Post, PostReply, CollectPost, Profile, Follower, Notification, BlogPostCategory, BlogPost, BlogPostLabel], safe=True) user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(""" 版块分类 docker: docker文章 registry: registry文章、私有hub、images分享, dockerfile分享 docker集群: docker集群文章 """) postcategory0 = PostCategory.create(name='docker', str='docker') postcategory1 = PostCategory.create(name='registry', str='registry') postcategory2 = PostCategory.create(name='docker集群', str='docker-cluster') posttopic0 = PostTopic.create(category=postcategory0, name='docker文章', str='docker-article') posttopic1 = PostTopic.create(category=postcategory1, name='registry文章', str='registry-article') posttopic2 = PostTopic.create(category=postcategory1, name='私有hub', str='private-hub') posttopic3 = PostTopic.create(category=postcategory1, name='image分享', str='image-share') posttopic4 = PostTopic.create(category=postcategory1, name='dockerfile分享', str='dockerfile-share') posttopic5 = PostTopic.create(category=postcategory2, name='docker集群文章', str='docker-cluster-article') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatLog.create(me=user_admin, other=user_test, content='self>other') chat_log_0 = ChatLog.create(me=user_test, other=user_admin, content='other>self')
def create_test_data(db_mysql): from db.mysql_model.user import User, Profile, Follower, ChatMessage from db.mysql_model.post import Post, PostReply, PostCategory, PostTopic, CollectPost from db.mysql_model.common import Notification from db.mysql_model.blog import BlogPost, BlogPostLabel, BlogPostCategory logger.debug("DataBase is not exist, so create test data.") # -------------------- 测试用户功能 --------------- user_admin = User.new(username='******', email='*****@*****.**', password='******') user_test = User.new(username='******', email='*****@*****.**', password='******') # -------------------- 测试关注功能 --------------- Follower.create(user=user_admin, follower=user_test) # -------------------- 测试分类功能 -------------- logger.debug(''' 版块分类 专业: 计算机 学习: 学习资料、考研资料、家教、竞赛 生活: 共享账号、电影资源、常用软件、电脑故障 爱好: 摄影、健身 未分类: 校园通知、讨论 ''') postcategory0 = PostCategory.create(name='分类', str='live') posttopic0 = PostTopic.create(category=postcategory0, name='爱学习', str='live-study') posttopic1 = PostTopic.create(category=postcategory0, name='爱生活', str='live-life') posttopic2 = PostTopic.create(category=postcategory0, name='爱管“闲事”', str='live-thing') posttopic10 = PostTopic.create(name='通知', str='notice') posttopic11 = PostTopic.create(name='讨论', str='discussion') # ---------------- 测试新文章 -------------- post = Post.create( topic=posttopic0, title='test', content=tmp_post, user=user_admin ) # ---------------- 测试通知 -------------- Notification.new_post(post) # ------------测试新回复-------------- postreply = PostReply.create( post=post, user=user_test, content='test' ) post.update_latest_reply(postreply) # ---------------- 测试Blog -------------- bpc0 = BlogPostCategory.create(name='Tornado', str='Tornado') bp0 = BlogPost.create(title='Tornado', category=bpc0, content='Tornado content') BlogPostLabel.add_post_label('python,tornado', bp0) # ---------------- 测试chat -------------- chat_log_0 = ChatMessage.create(sender=user_admin, receiver=user_test, content='self>other') chat_log_0 = ChatMessage.create(sender=user_test, receiver=user_admin, content='other>self')