def f(): u = current_user() comment_id = request.args['id'] comment_one = BlogComment.one(id=int(comment_id)) blog_one = Blog.one(id=int(comment_one.blog_id)) if int(comment_one.user_id) == int(u.id) or int( blog_one.user_id) == int(u.id): return route_function() else: d = dict(message="权限不足") return jsonify(d)
def comment(): print('api.blog.comment was called') form = request.form u = current_user() c = BlogComment(form) c.name = u.username c.user_id = u.id c.blog_id = int(form.get('blog_id', -1)) print('评论博客的id:', c.blog_id) r = {'data': []} if c.valid(): c.save() print('评论保存成功') r['success'] = True r['data'] = c.json() else: r['success'] = False message = c.error_message() r['message'] = message print('评论中r的内容r的内容::', r) return json.dumps(r, ensure_ascii=False)
def comment(): print('blog.comment was called ') u = current_user() if u is not None: form = request.form c = BlogComment(form) c.name = u.username if c.valid(): c.save() print('blogcomment was saved',c) # c.avatar = c.get_avatar() return c.json()
def f(*args, **kwargs): print('blog comment ownner required') print('args = <{}>'.format(args)) print('kwargs = <{}>'.format(kwargs)) u = current_user() if request.method == 'GET': bc_id = request.args.get('id') else: bc_id = request.form['id'] bc = BlogComment.find_one(id=bc_id) print('blog comment = <{}>'.format(bc)) if bc.user_id == u.id: return func(*args, **kwargs) else: return redirect('/blog/index')
def delete(self): bcs = BlogComment.find_all(blog_id=self.id) for bc in bcs: bc.delete_one(id=bc.id) self.delete_one(id=self.id) return 'Delete Success!'
def comments(self): cs = BlogComment.find_all(blog_id=self.id) return cs
def generate_fake_data(): # 生成示例表单 # 调用类方法将数据插入数据库 user_form1 = dict( username='******', password='******', email='*****@*****.**', ) user_form2 = dict( username='******', password='******', email='*****@*****.**', ) with open('markdown_demo.md', encoding='utf8') as f: md_content = f.read() blog_form1 = dict( title='New Blog', content='Hello World!', ) markdown_blog_form = dict( title='Markdown样例', content=md_content, ) blog_comment_form1 = dict( content='commment 1', user_id=1, blog_id=1, ) blog_comment_form2 = dict( content=md_content, user_id=1, blog_id=1, ) weibo_form1 = dict(content='微博1测试1', ) weibo_form2 = dict(content='weibo test 2', ) weibo_comment_form1 = dict( content='weibo 1 comment 1', weibo_id=1, ) weibo_comment_form2 = dict( content='微博1测试2', weibo_id=1, ) weibo_comment_form3 = dict( content='guest评论', weibo_id=2, ) u1 = User.register(user_form1) print('u1=<{}>'.format(u1)) u2 = User.register(user_form2) print('u2=<{}>'.format(u2)) b = Blog.new(form=blog_form1, user_id=1) print('b=<{}>'.format(b)) bc1 = BlogComment.new(form=blog_comment_form1, user_id=1) print('bc1=<{}>'.format(bc1)) bc2 = BlogComment.new(form=blog_comment_form2, user_id=1) print('bc2=<{}>'.format(bc2)) mb = Blog.new(form=markdown_blog_form, user_id=1) print('mb=<{}>'.format(mb)) w1 = Weibo.new(form=weibo_form1, user_id=1) print('w1=<{}>'.format(w1)) w2 = Weibo.new(form=weibo_form2, user_id=1) print('w1=<{}>'.format(w2)) wc1 = WeiboComment.new(form=weibo_comment_form1, user_id=1) print('wc1=<{}>'.format(wc1)) wc2 = WeiboComment.new(form=weibo_comment_form2, user_id=1) print('wc1=<{}>'.format(wc2)) wc3 = WeiboComment.new(form=weibo_comment_form3, user_id=2) print('wc1=<{}>'.format(wc3))
def blog_comment_get(): bc_id = int(request.args.get('id')) bc = BlogComment.find_one(id=bc_id) blog_id = bc.blog_id BlogComment.delete_one(id=bc.id) return redirect('/blog/detail/{}'.format(blog_id))
def blog_comment_update(): form = request.form.to_dict() bc_id = int(form['id']) print('blog_comment_update form = <{}>'.format(form)) bc = BlogComment.update(_id=bc_id, **form) return redirect('/blog/detail/{}'.format(form['blog_id']))
def blog_comment_edit(): bc_id = request.args.get('id') bc = BlogComment.find_one(id=bc_id) return render_template('blog/blog_comment_edit.html', bc=bc)
def blog_comment_add(): form = request.form.to_dict() u = current_user() print('blog_add form = <{}>'.format(form)) bc = BlogComment.new(form=form, user_id=u.id) return redirect('/blog/detail/{}'.format(form['blog_id']))
def generate_fake_date(): form = dict( username='******', password='******', email='*****@*****.**', ) u = User.register(form) form = dict( username='******', password='******', email='*****@*****.**', ) u = User.register(form) boardList = [] form = dict(title='spin') b = Board.new(form) boardList.append(b) form = dict(title='qa') b = Board.new(form) boardList.append(b) form = dict(title='share') b = Board.new(form) boardList.append(b) with open('markdown_demo.md', encoding='utf8') as f: content = f.read() for i in range(10): print('begin topic <{}>'.format(i)) topic_form = dict(title='markdown demo ' + boardList[i % 3].title, board_id=boardList[i % 3].id, content=content) t = Topic.new(topic_form, u.id) reply_form = dict( content='reply test', topic_id=t.id, ) for j in range(5): Reply.new(reply_form, u.id) # for blog with open('markdown_demo.md', encoding='utf-8') as f: demo = f.read() # form = dict( title='markdown demo', content=demo, ) Blog.new(form, u) form = dict( title='blog demo', content='blog demo content', ) Blog.new(form, u) form = dict(content='comment demo content', blog_id='1') BlogComment.new(form, u) with open('demo_comment.md', encoding='utf-8') as f: demo_comment = f.read() form = dict(content=demo_comment, blog_id='1') BlogComment.new(form, u)