def test_follow_posts(self): # make four users u1 = User(nickname='john', email='*****@*****.**') u2 = User(nickname='susan', email='*****@*****.**') u3 = User(nickname='mary', email='*****@*****.**') u4 = User(nickname='david', email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.add(u3) db.session.add(u4) # make four posts utcnow = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=utcnow + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=utcnow + timedelta(seconds=2)) p3 = Post(body="post from mary", author=u3, timestamp=utcnow + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=utcnow + timedelta(seconds=4)) db.session.add(p1) db.session.add(p2) db.session.add(p3) db.session.add(p4) db.session.commit() # setup the followers u1.follow(u1) # john follows himself u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u2) # susan follows herself u2.follow(u3) # susan follows mary u3.follow(u3) # mary follows herself u3.follow(u4) # mary follows david u4.follow(u4) # david follows himself db.session.add(u1) db.session.add(u2) db.session.add(u3) db.session.add(u4) db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() assert len(f1) == 3 assert len(f2) == 2 assert len(f3) == 2 assert len(f4) == 1 assert f1 == [p4, p2, p1] assert f2 == [p3, p2] assert f3 == [p4, p3] assert f4 == [p4]
def form_valid(self, form): title = form.cleaned_data.get('title') slug = form.cleaned_data.get('slug') comment = form.cleaned_data.get('comments') post = Post(title=title) post.slug = slug # post.comments = comment post.save() return HttpResponseRedirect(reverse_lazy('user:user_index'))
def new(): method = 'POST' action = '.' form = PostForm(request.form) if form.validate_on_submit(): post = Post() post = set_post(post,form) print(post) post.put() flash('Post created!') return redirect(url_for('.index')) return render_template("posts/new.html", form=form, method=method, action=action)
def create(): title = request.form.get('title') content = request.form.get('content') poster_id = request.form.get('poster_id') pst = Post(title, content, poster_id) pst.createPost() message = { "data": { "status": "success", "action": "create", "scope": "post" + str(title) } } return jsonify(message), 201
def page_test(): tags = [tag_event] page = Post( title="My Test Page", description="My Test Page Description", content="My test page content", post_type="page", post_status="publisehd", author=test_user, category=category_worship, tags=tags, ) page.url = page.slugify(page.title) page.put() return page
def post_test(): tags = [tag_pastor(), tag_computer()] post = Post( title="My Test Post", description="My Test Description", content="My test content", post_type="post", post_status="published", author=test_user(), category=category_sermon(), tags=tags, ) post.url = post.slugify(post.title) post.put() return post
def post(self): schema = PostSchema() args = post_parser.parse_args() text = args.get('text') if text is None: text = '' image = args.get('image') if image is None: return {'msg': 'you must post file.'}, 422 file_name = str(int(datetime.now().timestamp() * 1000)) + '-' + secure_filename(image.filename) image.save(str(Path(current_app.config['UPLOAD_FOLDER']) / file_name)) post = Post(user_id=current_user.id, user_name=current_user.name, text=text, image=file_name) db.session.add(post) db.session.commit() return {"msg": "post created", "post": schema.dump(post)}, 201
def getPostsByUser(user_id): created_at = request.args.get('created_at', None) updated_at = request.args.get('updated_at', None) title = request.args.get('title', None) content = request.args.get('content', None) args = { "created_at": created_at, "updated_at": updated_at, "title": title, "content": content } posts = Post.getPostsByUserArg(user_id, args) # posts = Post.getPostsByUser(user_id) if posts is None: return jsonify({"error": {"message": "wrong user id"}}), 404 data = {"data": []} for post in posts: pst = { "post_id": post.id, "title": post.title, "content": post.content, "poster": post.poster.name } data["data"].append(pst) return jsonify(data), 200
def post(self): schema = PostSchema() args = post_parser.parse_args() text = args.get('text') if text is None: text = '' image = request.files['image'] if not image: flash('必须添加图片!') return redirect(url_for('home.index')) #422 if not allowed_file(image.filename): flash('仅支持 jpeg、jpg、png 图片!') return redirect(url_for('home.index')) #422 file_name = str(int(datetime.now().timestamp() * 1000)) + '-' + secure_filename(image.filename) image.save(str(Path(current_app.config['UPLOAD_FOLDER']) / file_name)) post = Post(user_id=current_user.id, user_name=current_user.name, text=text, image=file_name) db.session.add(post) db.session.commit() flash('发布成功!') return redirect(url_for('home.index')) #201
def deletePostById(post_id): dpst = Post.deletePostById(post_id) if dpst is None: return jsonify({"error": {"message": "wrong post id"}}), 404 message = { "data": { "status": "success", "action": "delete", "scope": "post" } } return jsonify(message), 204
def new(): if not current_user.is_authenticated and not current_user.is_confirmed: return redirect(url_for('main.home')) form = PostForm() if form.validate_on_submit(): p = Post(images=form.images.data, caption=form.caption.data, user=current_user) db.session.add(p) db.session.commit() return redirect(url_for('main.home')) return render_template('new.html', form=form)
def getPostById(post_id): post = Post.getPostById(post_id) if post is None: return jsonify({"error": {"message": "wrong post id"}}), 404 pst = { "post": { "id": post.id, "title": post.title, "content": post.content, "poster": post.poster.name } } return jsonify(pst), 200
def get_posts(): page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', 10, type=int), 100) data = Post.to_collection_dict(Post.query, page, per_page) if data: return jsonify(data), 200 else: return jsonify({ 'status': 'ok', 'code': 200, 'msg': 'No data found', 'data': [] }), 200
def create_post(): form = CreatePost() #set dynamic for post type choices form.post_type.choices = [(a.id, a.post_type) for a in Post_type.query.all()] #Handle Post method if form.validate_on_submit(): post = Post(form.title.data, form.content.data, form.post_type.data, current_user.id) db.session.add(post) db.session.commit() flash("Postingan anda telah tersimpan") return redirect(url_for('.create_post')) return render_template('create_post.html', form=form)
def new(): if not (current_user.is_authenticated and current_user.is_confirmed): flash('Go and confirm your email, Baka', 'link') return redirect(url_for('main.home')) form = PostForm() if form.validate_on_submit(): p = Post(images=form.images.data, caption=form.caption.data, user=current_user) db.session.add(p) db.session.commit() flash('New post added', 'success') return redirect(url_for('main.home')) return render_template('new.html', form=form)
def getAllPosts(): posts = Post.getAllPosts() data = {"data": []} for post in posts: pst = { "post": { "id": post.id, "title": post.title, "content": post.content, "poster": post.poster.name } } data['data'].append(pst) return data, 200
def updatePostById(post_id): title = request.form.get('title') content = request.form.get('content') info = {"title": title, "content": content} uppst = Post.updatePostById(post_id, info) if uppst is None: return jsonify({"error": {"message": "wrong post id"}}), 404 message = { "data": { "status": "success", "action": "update", "scope": "post" } } return jsonify(message), 200
def edit(id): method = 'POST' action = '.' post = Post.get_by_id(id) form = PostForm(request.form, post) form.category.data = post.category.name tags = [] for tag in post.tags: tags.append(tag.name) form.tags.data = ','.join(tags) if form.validate_on_submit(): post = set_post(post,form) post.put() flash('Post updated!') return render_template("posts/edit.html", form=form, method=method, action=action)
def create_post(): if request.method == 'POST': title = request.form['title'] content = request.form['content'] is_form_valid = True if title.strip() == '': is_form_valid = False flash('username should be filled!', 'error') if content.strip() == '': is_form_valid = False flash('content should be filled!', 'error') # check if the post request has the file part if 'image_file' not in request.files: flash('No file part', 'error') return render_template('create_post.html') filename = None file = request.files['image_file'] # if user does not select file, browser also # submit an empty part without filename # if file.filename == '': # flash('No selected file') # return redirect(request.url) if file and not allowed_file(file.filename): is_form_valid = False flash('file not allowed!', 'error') elif file: filename = secure_filename(file.filename) now = datetime.datetime.now() filename = now.strftime("%d%m%Y%H%M%S") + filename file.save(os.path.join(app.app.config['UPLOAD_FOLDER'], filename)) if not is_form_valid: return render_template('create_post.html') username = session.get('username') user = User.query.filter_by(username=username).first() if user is None: flash('user not found!', 'error') return render_template('create_post.html') post = Post() post.user = user post.title = title post.content = content post.image_path = filename db.session.add(post) db.session.commit() return redirect(url_for('[post].list_post')) else: return render_template('create_post.html')
def page_test(): tags = [tag_event] page = Post(title='My Test Page', description='My Test Page Description', content='My test page content', post_type='page', post_status='publisehd', author=test_user, category=category_worship, tags=tags) page.url = page.slugify(page.title) page.put() return page
def post_test(): tags = [tag_pastor(), tag_computer()] post = Post(title='My Test Post', description='My Test Description', content='My test content', post_type='post', post_status='published', author=test_user(), category=category_sermon(), tags=tags) post.url = post.slugify(post.title) post.put() return post
def insert_post(): content = request.get_json() try: post = Post(title=content['title'], body=content['body'], sub_body='test', category_id=content['category_id']) data, errors = PostSchema().load(post.__dict__) if errors: return jsonify({'status': 'ko', 'code': 422, 'msg': errors}), 422 except: return jsonify({'status': 'ko', 'code': 415, 'msg': 'Fail'}), 415 try: db.session.add(post) db.session.commit() return jsonify({ 'status': 'ok', 'code': 201, 'msg': 'Create a new post successfully!' }), 201 except: return jsonify({'status': 'ko', 'code': 403, 'msg': 'Fail'})
def find_by_url(url): post = Post.query(Post.url == url, Post.post_status != 'draft').get() return render_template("site/post.html", post=post)
def find_by_url(url): post = Post.query(Post.url==url, Post.post_status!='draft').get() return render_template("site/post.html", post=post)
def find_by_tag_name(tag): posts = Post.query(Post.tags==Tag(name=tag), Post.post_status!='draft').fetch() return render_template("site/post_list.html", posts=posts)
def find_by_tag_name(tag): posts = Post.query(Post.tags == Tag(name=tag), Post.post_status != 'draft').fetch() return render_template("site/post_list.html", posts=posts)
def index(): posts = Post.query().order(-Post.created_at) return render_template("posts/index.html", posts=posts)