def post_create(): current_admin = get_jwt_identity() data = json.loads(request.data) title = data['title'] content = data['content'] admin = Admin.find_admin_by_email(current_admin['email']) try: post = Post(title=title, content=content, admin_id=admin.id) db.session.add(post) db.session.commit() post_schema = PostSchema() post_result = post_schema.dump(post) response_object = { 'status': 'success', 'message': 'Successfully created a Post', 'post': post_result[0] } return make_response(jsonify(response_object)), 201 except Exception as e: response_object = { 'status': 'fail', 'message': 'Could not create a Post', 'error': ','.join(e.args) } return make_response(jsonify(response_object)), 500
def admin_post(): form = AdminPostForm(request.form) if request.method == 'POST' and form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author_id=current_user.id) db.session.add(post) db.session.commit() return redirect(url_for('main.index')) return render_template('admin/admin_post.html', form=form)
def post_form(community_id, id=None): user = auth.service.get_user() community = Community.get(community_id) post = Post.get(id) if id else Post() if not community or not post: abort(404) if not community.has_member(user) and not (id and post.author == user): abort(403) return render_template('community/post_form.html', **{'community': community, 'post': post})
def post_save(): v = Validator(request.form) v.fields('id').integer(nullable=True) v.field('title').required() v.field('text').required() v.field('community_id').required().integer() user = auth.service.get_user() if not user.is_authorized(): abort(403) if not v.is_valid(): return jsonify({ 'status': 'fail', 'errors': v.errors }) data = v.valid_data if not data.id: post = Post() post.community_id = data.community_id else: post = Post.get(data.id) if not post: abort(400) if post.author and post.author != user: abort(403) post.title = data.title post.text = data.text post.author = user db.session.add(post) db.session.commit() return jsonify({ 'status': 'ok', 'post': post.as_dict() })