def get(self, *args, **kwargs): # profiling 性能分析 # from profiling.tracing import TracingProfiler # # # profile your program. # profiler = TracingProfiler() # profiler.start() current_page = get_cleaned_query_data(self, [ 'page', ], blank=True)['page'] current_page = get_page_number(current_page) posts, page_number_limit = Post.list_recently(page_number=current_page) top_posts, _ = Post.list_top() pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) self.render('index/index.html', posts=posts, top_posts=top_posts, topic_category_cache=topic_category_cache, hot_post_cache=hot_post_cache, systatus=system_status_cache, current_topic=None, pages=pages, pages_prefix_url='/?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 == 'support-postreply': try: postreply = PostReply.get(PostReply.id == data['postreply']) except: self.write(json_result(1, '请输入正确的回复')) return postreply.up_like() self.write(json_result(0, 'success')) # 收藏该主题 elif opt == 'collect-post': try: post = Post.get(Post.id == data['post']) except: self.write(json_result(1, '请输入正确的Post')) return CollectPost.create(post=post, user=self.current_user) self.write(json_result(0, 'success')) # 取消收藏该主题 elif opt == 'cancle-collect-post': try: post = Post.get(Post.id == data['post']) except: self.write(json_result(1, 'CollectPost不正确')) return collectpost = CollectPost.get(post=post, user=self.current_user) collectpost.delete_instance() self.write(json_result(0, 'success')) else: self.write(json_result(1, 'opt不支持'))
def get(self, topic_id, *args, **kwargs): try: topic = PostTopic.get(PostTopic.str == topic_id) except PostTopic.DoesNotExist: self.redirect("/static/404.html") return current_page = get_cleaned_query_data(self, [ 'page', ], blank=True)['page'] if current_page: current_page = int(current_page) if current_page < 1: self.redirect("/static/404.html") return posts, page_number_limit = Post.list_by_topic( topic, page_number=current_page) else: current_page = 1 posts, page_number_limit = Post.list_by_topic(topic) top_posts, _ = Post.list_top() pages = get_page_nav(current_page, page_number_limit, config.default_page_limit) self.render('index/index.html', posts=posts, top_posts=top_posts, topic_category_cache=topic_category_cache, hot_post_cache=hot_post_cache, systatus=system_status_cache, current_topic=topic, pages=pages, pages_prefix_url='/topic/' + topic.str + '?page=')
def get_topic_index_info(topic_id, current_page, page_limit=config.default_page_limit): try: topic = PostTopic.get(PostTopic.str == topic_id) except PostTopic.DoesNotExist: return None current_page = get_page_number(current_page) posts, page_number_limit = Post.list_by_topic(topic, page_number=current_page) top_posts, _ = Post.list_top() pages = get_page_nav(current_page, page_number_limit, page_limit) return topic, posts, top_posts, pages
def post(self, *args, **kwargs): post_data = get_cleaned_post_data(self, ['post', 'title', 'content', 'topic']) try: post = Post.get(Post.id == post_data['post'], Post.is_delete == False) except Post.DoesNotExist: self.write(json_result(1, '请选择正确主题')) return if not post.check_auth(self.current_user): self.redirect404_json() return try: topic = PostTopic.get(PostTopic.str == post_data['topic']) except PostTopic.DoesNotExist: self.write(json_result(1, '请选择正确主题')) return post.topic = topic post.title = post_data['title'] post.content = post_data['content'] post.save() # 添加通知, 通知给其他关注的用户 Notification.new_post(post) self.write(json_result(0, {'post_id': post.id}))
def get(self, user_id, *args, **kwargs): user = User.get(User.id == user_id) profile = Profile.get_by_user(user) posts = Post.select().where(Post.user == user, Post.is_delete == False).limit(10) postreplys = PostReply.select().where(PostReply.user == user).limit(10) collectposts = CollectPost.select().where( CollectPost.user == user).limit(10) who_follow = Follower.select( Follower.follower).where(Follower.user == user) follow_who = Follower.select( Follower.user).where(Follower.follower == user) # 是否显示关注 is_follow = True if Follower.is_follow(user, self.current_user) else False is_online = True if WebsocketChatHandler.is_online( user.username) else False self.render('user/profile.html', user=user, who_follow=who_follow, follow_who=follow_who, profile=profile, posts=posts, postreplys=postreplys, is_follow=is_follow, is_online=is_online, collectposts=collectposts)
def get(self, post_id, *args, **kwargs): post = Post.get_by_id(post_id) if not post.check_auth(self.current_user): self.redirect404() return self.render('post/post_modify.html', post=post, topic_category_cache=topic_category_cache)
def update_hot_post_cache(): ''' 更新 '热门文章' 缓存 :return: ''' hot_post_cache['reply'] = [] hot_post_cache['visit'] = [] posts = Post.select().where(Post.is_delete == False).order_by(Post.reply_count.desc()).limit(4) for post in posts: hot_post_cache['reply'].append(post)
def update_hot_post_cache(): """ ignore... :return: """ hot_post_cache['reply'] = [] hot_post_cache['visit'] = [] posts = Post.select().where(Post.is_delete == False).order_by(Post.reply_count.desc()).limit(4) for post in posts: hot_post_cache['reply'].append(post)
def get(self, post_id, *args, **kwargs): post = Post.get(Post.id == post_id) post.up_visit() post_detail = post post_replys = PostReply.list_all(post) self.render('post/post_detail.html', post_detail=post_detail, post_replys=post_replys, is_collect=CollectPost.is_collect(post, self.current_user), hot_post_cache=hot_post_cache, topic_category_cache=topic_category_cache)
def get(self, post_id, *args, **kwargs): post, post_replys = Post.get_detail_and_replys(post_id) if not post: self.redirect404() return ext = get_post_user_ext(post, self.current_user) self.render('post/post_detail.html', post=post, post_replys=post_replys, ext=ext, hot_post_cache=hot_post_cache, topic_category_cache=topic_category_cache)
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-list': data = [] posts = Post.select() for p in posts: tp = obj2dict(p, ['id', 'title']) data.append(tp) self.write(json_result(0, data))
def post(self, *args, **kwargs): post_data = get_cleaned_post_data(self, ['title', 'content', 'topic']) try: topic = PostTopic.get(PostTopic.str == post_data['topic']) except PostTopic.DoesNotExist: self.write(json_result(1, '请选择正确主题')) return post = Post.create(topic=topic, title=post_data['title'], content=post_data['content'], user=self.current_user) #添加通知, 通知给其他关注的用户 Notification.new_post(post) self.write(json_result(0, {'post_id': post.id}))
def test_post(): user = User.get_by_username('admin') post = Post.create(title="test_vul_scan", content="vul_scan_content_04.27_12:47", user=user) post.up_visit() post.up_collect() print('new post <test_vul_scan:admin>') postreply = PostReply.create(post=post, user=user, content='test_reply') print('new postreply <test_reply:admin>') postreply.up_like() post.up_reply() post_test = Post.create( title="IDS是一种积极主动的安全防护技术", content= "入侵检测系统的核心价值在于通过对全网信息的分析,了解信息系统的安全状况,进而指导信息系统安全建设目标以及安全策略的确立和调整,而入侵防御系统的核心价值在于安全策略的实施—对黑客行为的阻击", user=user) postreply_test = PostReply.create( post=post, user=user, content= '入侵检测系统需要部署在网络内部,监控范围可以覆盖整个子网,包括来自外部的数据以及内部终端之间传输的数据,入侵防御系统则必须部署在网络边界,抵御来自外部的入侵,对内部攻击行为无能为力。' )
def post(self, *args, **kwargs): post_data = get_cleaned_post_data(self, ['post', 'content']) try: post = Post.get(Post.id == post_data['post'], Post.is_delete == False) except PostTopic.DoesNotExist: self.write(json_result(1, '请选择正确post')) return postreply = PostReply.create( post=post, user=self.current_user, content=post_data['content'], ) post.update_latest_reply(postreply) Notification.new_reply(postreply, post) self.write(json_result(0, {'post_id': post.id}))
def get(self, user_id, *args, **kwargs): user = User.get(User.id == user_id) profile = Profile.get_by_user(user) posts = Post.select().where(Post.user == user).limit(10) postreplys = PostReply.select().where(PostReply.user == user).limit(10) collectposts = CollectPost.select().where( CollectPost.user == user).limit(10) # 是否显示关注 is_follow = Follower.is_follow(user, self.current_user) self.render('user/profile.html', user=user, profile=profile, posts=posts, postreplys=postreplys, is_follow=is_follow, collectposts=collectposts)
def get(self, user_id, *args, **kwargs): user = User.get(User.id == user_id) profile = Profile.get_by_user(user) posts = Post.select().where(Post.user == user, Post.is_delete == False).limit(10) postreplys = PostReply.select().where(PostReply.user == user).limit(10) collectposts = CollectPost.select().where(CollectPost.user == user).limit(10) who_follow = Follower.select(Follower.follower).where(Follower.user == user) follow_who = Follower.select(Follower.user).where(Follower.follower == user) # 是否显示关注 is_follow = True if Follower.is_follow(user, self.current_user) else False is_online = True if WebsocketChatHandler.is_online(user.username) else False self.render('user/profile.html', user=user, who_follow=who_follow, follow_who=follow_who, profile=profile, posts=posts, postreplys=postreplys, is_follow=is_follow, is_online=is_online, collectposts=collectposts)
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 get_index_info(current_page, page_limit=config.default_page_limit): current_page = get_page_number(current_page) posts, page_number_limit = Post.list_recently(page_number=current_page) top_posts, _ = Post.list_top() pages = get_page_nav(current_page, page_number_limit, page_limit) return posts, top_posts, pages
def get(self, *args, **kwargs): user_count = User.select().count() post_count = Post.select().count() self.render('dashboard/pages/index.html', user_count=user_count, post_count=post_count)
def get(self, *args, **kwargs): posts = Post.select() self.render('dashboard/pages/db-post-list.html', posts=posts)
def get_index_user_info(user): posts = Post.select().where(Post.user == user, Post.is_delete == False).count() collectposts = CollectPost.select().where(CollectPost.user == user).count() return {'posts_count': posts, 'collectposts_count': collectposts}
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='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')
import sys, os sys.path.append(os.path.dirname(sys.path[0])) from db.mysql_model import db_mysql from db.mysql_model.user import User from db.mysql_model.post import Post, PostReply if User.table_exists(): print('[User]表已经存在') else: db_mysql.create_table(User) if Post.table_exists(): print('[Post]表已经存在') else: db_mysql.create_table(Post) if PostReply.table_exists(): print('[PostReply]表已经存在') else: db_mysql.create_table(PostReply) PostReply.delete().execute() Post.delete().execute() User.delete().execute() def test_user(): user = User.get_by_username('admin') if user: user.delete_instance()
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='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')