def post(self,post_id=None): if post_id: abort(405) else: args = post_post_parser.parse_args(strict=True) user = User.verify_auth_token(args['token']) if not user: abort(401) new_post = Post(args['title']) new_post.user = user new_post.publish_date=datetime.datetime.now() new_post.text = args['text'] if args['tags']: for item in args['tags']: tag = Tag.query.filter_by( title=item ).first() if tag: new_post.tags.append(tag) else: new_tag = Tag(item) new_post.tags.append(new_tag) db.session.add(new_post) db.session.commit() return new_post.id,201
def new(type): form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.cover = form.cover.data new_post.publish_date = datetime.now() new_post.update_date = new_post.publish_date new_post.user_id = current_user.id new_post.type = type new_post.summary = form.summary.data new_post.video = form.video.data tagStrList = form.tags.data.split(',') for tagStr in tagStrList: tagStr = tagStr.strip() tag = Tag.query.filter_by(title=tagStr).first() if not tag: tag = Tag(tagStr) new_post.tags.append(tag) for photo_url in form.photos.data: if photo_url != '': photo = Photo(photo_url) new_post.photos.append(photo) db.session.add(new_post) db.session.commit() return redirect(url_for('blog.post', post_id=new_post.id)) form.type.data = type return render_template("edit_{}.html".format(type), form=form, type=type)
def populate_default_data(db, app): db.app = app db.create_all() user = User() user.username = "******" user.set_password("jim") db.session.add(user) db.session.commit() tag_one = Tag(title="Python") tag_two = Tag(title="Flask") tag_three = Tag(title="SQLAlchemy") tag_four = Tag(title="Jinja") tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Example text" for i in range(100): new_post = Post(title="Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def post(self, post_id=None): if post_id: abort(400) else: args = post_post_parser.parse_args(strict=True) user = User.verify_auth_token(args['token']) if not user: abort(401) new_post = Post(args['title']) new_post.user = user new_post.date = datetime.datetime.now() new_post.text = args['text'] if args['tags']: for item in args['tags']: tag = Tag.query.filter_by(title=item).first() # Add the tag if it exists. If not, make a new tag if tag: new_post.tags.append(tag) else: new_tag = Tag(item) new_post.tags.append(new_tag) db.session.add(new_post) db.session.commit() return new_post.id, 201
def setUp(self): db.app = test_app db.create_all() user = User() user.username = self.username user.set_password(self.password) db.session.add(user) comment = Comment() comment.name = self.comment_name comment.text = self.comment_text tag = Tag() tag.title = self.tag_title post = Post() post.title = self.post_title post.text = self.post_text post.publish_date = self.post_publish_date # add relationships to other tables post.user = user post.tags = [tag] post.comments = [comment] db.session.add(user) db.session.add(comment) db.session.add(tag) db.session.add(post) db.session.commit()
def new_post(): form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() db.session.add(new_post) db.session.commit() return render_template('new.html', form=form)
def post(self): data = reqparse.RequestParser() data.add_argument('title', type=str, required=True) data.add_argument('text', type=str, required=True) args = data.parse_args() post = Post(args['title']) post.text = args['text'] db.session.add(post) db.session.commit()
def new_post(): form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() new_post.user = User.query.filter_by( username=current_user.username).one() db.session.add(new_post) db.session.commit() return render_template('new.html', form=form)
def new_post(): form=PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() db.session.add(new_post) db.session.commit() return render_template('new.html', form=form)
def new_post(): form = PostForm() print("new_post") if form.validate_on_submit(): new_post = Post() new_post.title = form.title.data new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() user = User.objects(id=current_user.id).first() new_post.user = user new_post.save() return render_template('new.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() new_post.user = User.query.filter_by( username=current_user.username ).one() db.session.add(new_post) db.session.commit() return render_template('new.html', form=form)
def new_post(): form = PostForm() if not current_user: return redirect(url_for('main.login')) if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() db.session.add(new_post) db.session.commit() return render_template('new.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): new_post = Post() new_post.title = form.title.data new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() db.session.add(new_post) db.session.commit() return redirect(url_for('blog.post', post_id=new_post.id)) recent, top_tags = sidebar_data() return render_template('new.html', form=form, recent=recent, top_tags=top_tags)
def new_post(): # 此处验证用login_required装饰器代替 ''' if not g.current_user: return redirect(url_for('main.login')) ''' form = PostForm() if form.validate_on_submit(): new_post = Post(form.title.data) new_post.text = form.text.data new_post.publish_date = datetime.datetime.now() new_post.user = current_user db.session.add(new_post) db.session.commit() return redirect(url_for('.post', post_id=new_post.id)) return render_template('new.html', form=form)
def setup_db(): db.create_all() admin_role = Role() admin_role.name = "admin" admin_role.description = "admin" db.session.add(admin_role) default_role = Role() default_role.name = "default" default_role.description = "default" db.session.add(default_role) admin = User() admin.username = "******" admin.set_password("password") admin.roles.append(admin_role) admin.roles.append(default_role) db.session.add(admin) tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Body text" for i in xrange(100): new_post = Post("Post " + str(i)) new_post.user = admin new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def new_post(): if request.method == 'POST': jsonStr = request.get_data() data = json.loads(jsonStr) if hasattr(data, 'key') or data['key'] != "wysj3910": return "密钥错误!" new_post = Post(data['title']) # 创建帖子 new_post.text = data['text'] # 内容 new_post.cover = data['cover'] # 封面 new_post.publish_date = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime(data['publish_date'])) # 发布日期 new_post.update_date = datetime.datetime.now() #更新的日期 user = User.query.filter_by(username=data['username']).first() # 获取用户 if not user: user = User(data['username']) if hasattr(data, 'nickname'): user.nickname = data['nickname'] new_post.user = user # 用户 new_post.type = data['type'] # 类型 new_post.summary = data['summary'] # 简介 new_post.video = data['video'] # 视频地址 new_post.read = data['read'] # 阅读量 new_post.post_hash = data['post_hash'] #贴的hash值 for photo_url in data['photos']: # 图片列表 if photo_url != '': photo = Photo(photo_url) new_post.photos.append(photo) for tagStr in data['tags']: # 标签 tagStr = tagStr.strip() tag = Tag.query.filter_by(title=tagStr).first() if not tag: tag = Tag(tagStr) new_post.tags.append(tag) db.session.add(new_post) db.session.commit() return json.dumps({'status': 0})
def post(self, post_id=None): if post_id: abort(400) else: args = post_post_parser.parse_args(strict=True) username = args['username'] password = args['password'] user = User.query.filter_by(username=username).first() if not user: abort(401) if not user.check_password(password): abort(401) new_post = Post() new_post.user_id = user.id new_post.title = args['title'] new_post.publish_date = datetime.datetime.now() new_post.text = args['text'] if args['tags']: for t_title in args['tags']: tag = Tag.query.filter_by(title=t_title).first() # Add the tag if it exists. If not make a new tag. if tag: new_post.tags.append(tag) else: new_tag = Tag() new_tag.title = t_title new_post.tags.append(new_tag) db.session.add(new_post) db.session.commit() return new_post.id, 201
def insert_data(): with app.app_context(): # 不需要在这里创建库,应该使用数据库升级命令`db upgrade`来创建库 # db.create_all() # 这里设定了3种角色 role_admin = Role(name='admin') role_admin.description = "administrator role" role_poster = Role(name='poster') role_poster.description = "the registered user role" role_default = Role(name='default') role_default.description = 'the unregistered user role' db.session.add(role_admin) db.session.add(role_poster) db.session.add(role_default) # add User admin = User(username='******') admin.email = '*****@*****.**' admin.password = '******' admin.confirmed = True admin.roles.append(role_admin) admin.roles.append(role_poster) admin.roles.append(role_default) db.session.add(admin) user01 = User(username='******') user01.email = '*****@*****.**' user01.password = '******' user01.confirmed = True user01.roles.append(role_poster) user01.roles.append(role_default) db.session.add(user01) user02 = User(username='******') user02.email = '*****@*****.**' user02.password = '******' user02.confirmed = True user02.roles.append(role_poster) user02.roles.append(role_default) db.session.add(user02) # add Tag and Post tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Example Text" for i in xrange(1, 101): new_post = Post("Post {}".format(i)) if i % 2: new_post.user = user01 else: new_post.user = user02 new_post.publish_date = datetime.datetime.utcnow() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) # add comment comment01 = Comment() comment01.name = 'comment01' comment01.text = 'comment text' comment01.post_id = 99 comment01.date = datetime.datetime.utcnow() db.session.add(comment01) comment02 = Comment() comment02.name = 'comment02' comment02.text = 'comment text' comment02.post_id = 100 comment02.date = datetime.datetime.utcnow() db.session.add(comment02) db.session.commit()
import random import datetime from webapp.models import db, User, Post, tags, Tag, Comment user = User.query.get(1) tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Example text" for i in range(100): new_post = Post("Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def test_comments(self): # add two users r = Role.query.filter_by(name='poster').first() self.assertIsNotNone(r) u1 = User('john') u1.email = '*****@*****.**' u1.password = '******' u1.confirmed = True u1.roles.append(r) u2 = User('susan') u2.email = '*****@*****.**' u2.password = '******' u2.confirmed = True u2.roles.append(r) db.session.add_all([u1, u2]) db.session.commit() # add a post post = Post(title='title of the post') post.text = 'body of the post' post.user = u1 db.session.add(post) db.session.commit() # write a comment response = self.client.post(url_for('api.new_post_comment', id=post.id), headers=self.get_api_headers( 'susan', 'dog'), data=json.dumps({ 'name': 'comment name', 'text': 'Good [post](http://example.com)!' })) self.assertTrue(response.status_code == 201) json_response = json.loads(response.data.decode('utf-8')) url = response.headers.get('Location') self.assertIsNotNone(url) self.assertTrue(json_response['name'] == 'comment name') self.assertTrue( json_response['text'] == 'Good [post](http://example.com)!') # get the new comment response = self.client.get(url, headers=self.get_api_headers('john', 'cat')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertTrue(json_response['url'] == url) self.assertTrue(json_response['name'] == 'comment name') self.assertTrue( json_response['text'] == 'Good [post](http://example.com)!') # add another comment comment = Comment(name='another comment name') comment.text = 'Thank you!' comment.user = u1 comment.post = post db.session.add(comment) db.session.commit() # get the two comments from the post response = self.client.get(url_for('api.get_post_comments', id=post.id), headers=self.get_api_headers( 'susan', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2) # get all the comments response = self.client.get(url_for('api.get_comments', id=post.id), headers=self.get_api_headers( 'susan', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2)