async def _post(request, post_id=None): form = PostForm(request) form.status.data = int(form.status.data) post = None if post_id is not None: post = await Post.get_or_404(post_id) if request.method in ('POST', 'PUT') and form.validate(): title = form.title.data assert str(form.author_id.data).isdigit() if post_id is None: post = await Post.filter(title=title).first() if not post: post = Post() tags = form.tags.data content = form.content.data is_page = form.is_page.data del form.tags del form.content del form.is_page form.populate_obj(post) post.type = Post.TYPE_PAGE if is_page else Post.TYPE_ARTICLE await post.save() await post.update_tags(tags) await post.set_content(content) ok = True else: ok = False post = await post.to_sync_dict() post['tags'] = [t.name for t in post['tags']] return json({'post': post if post else None, 'ok': ok})
def post(self): form = PostForm() if request.method == 'POST': if form.validate_on_submit(): post = Post.create() form.populate_obj(post) f = request.files.get('file') post.user = current_user if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 post.update_score(page_view=1) post.save() if form.remain.data: url = url_for('PostsView:put', id=post.id) else: url = url_for('PostsView:get', id=post.id) return resp(url, redirect=True, message=gettext('Stamp succesfully created')) else: return resp('admin/posts/edit.html', status=False, form=form, message=gettext('Invalid submission, please check the message below')) return resp('admin/posts/edit.html', form=form)
def page(post_id=None): title = u'新增内容' form = PostForm() post = None # 读取操作 if request.method == 'GET' and post_id: title = u'修改内容' post = Post.query.get(post_id) form = PostForm(obj=post) # 新增、修改操作 elif request.method == 'POST' and form.validate_on_submit(): title = u'首页' # post = Post(user=g.user, topic=form.topic.data, title=form.title.data, body=form.body.data, sequence=form.sequence.data, timestamp=datetime.utcnow()) if form.id.data: # 修改 post = Post.query.get(form.id.data) form.populate_obj(post) else: # 新增 form.id.data = None post = Post(user=g.user, timestamp=datetime.utcnow()) form.populate_obj(post) db.session.add(post) db.session.commit() # .paginate(curpage, pagesize, <flag>) # flag:错误标记。如果是True,如果接收到超出记录范围的页面请求,那么404错误请求将会自动反馈到客户端浏览器。如果是False,那么一个空列表将会被返回,而不显示错误。 posts = Post.query.order_by(Post.sequence).filter_by(user=g.user).paginate(1, POSTS_PER_PAGE, False).items flash(u'保存成功') return redirect(url_for('index'))#render_template('index.html', title=title, posts=posts) return render_template('page.html', title=title, post=post, form=form)
def home(): from models import Post if request.method == 'POST': form = PostForm(request.form) if form.validate(): try: model = Post() form.populate_obj(model) db.session.add(model) db.session.commit() flash('Your post added!') return redirect(url_for('blog.home')) except Exception as ex: logger.error(ex) db.session.rollback() flash('Cant save post: ' + str(ex)) else: logger.error('Someone have submitted an incorrect form!') flash('Invalid form. Please check fields') else: form = PostForm() return render_template('home.html', form=form, items=Post.query.all(), comments_form=CommentForm())
def edit_post(post_id): post = post_repo.find(post_id) form = PostForm(request.form, post) if request.method == 'POST' and form.validate(): form.populate_obj(post) post_repo.update(post) return redirect(url_for('show_post', post_id=post.id)) return render_template('posts/edit.html', form=form, post=post)
def edit_post(slug): post = Post.query.filter(Post.slug==slug).first() if request.method == 'POST': form = PostForm(formdata=request.form, obj=post) form.populate_obj(post) db.session.commit() return redirect(url_for('post_detail',slug=post.slug)) form = PostForm(obj=post) return render_template('edit_post.html', post = post, form=form)
def add_post(): form = PostForm(request.form) if request.method == 'POST' and form.validate(): post = Post() form.populate_obj(post) post.author_id = g.user.id post.timestamp = datetime.utcnow() db.session.add(post) db.session.commit() return redirect(url_for('show_post', post_id=post.id)) return render_template('content_add_post.html', title='Add post', form=form, user=g.user)
def edit_post(id): post = Post.get_by_id(id) form = PostForm(request.form, post) if form.validate_on_submit(): form.tags.data = Post.convert_string_tags(form.tags.data) form.populate_obj(post) try: post.save() flash(u'Post aggiornato.', 'success') return redirect(url_for('list_posts')) except CapabilityDisabledError: flash(u'App Engine Datastore is currently in read-only mode.', 'failure') return redirect(url_for('list_posts')) return render_template('edit_post.html', form=form)
def edit_post(post_id): post = Post.get_by_id(post_id) form = PostForm(obj=post) if form.validate_on_submit(): form.tags.data = Post.convert_string_tags(form.tags.data) form.populate_obj(post) try: post.put() flash(u"Example %s successfully saved." % post.id, "success") # return redirect(url_for('edit_post.html')) except CapabilityDisabledError: flash(u"App Engine Datastore is currently in read-only mode.", "info") return redirect(url_for("list_posts")) return render_template("edit_post.html", form=form, post_id=post_id)
def edit(slug): post = Post.query.filter(Post.slug == slug).first() form = PostForm(obj=post) if request.method == 'POST': if form.valid_photo(): post.photo = form.upload_photo(post.photo) form = PostForm(formdata=request.form, obj=post) form.populate_obj(post) db.session.commit() return redirect(url_for('posts.detail', slug=post.slug)) return render_template('admin/posts/edit.html', post=post, form=form)
def post(self): form = PostForm() if form.validate_on_submit(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) remain = request.values.get('remain', False, bool) post = Post.create() form.populate_obj(post) post.user = current_user f = request.files.get('file') if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 # init the score post.update_score(page_view=1) post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if remain: url = url_for('PostsView:put', id=post.id, remain='y') else: url = url_for('PostsView:get', id=post.id) return render_view(url, redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form)
async def _post(request, post_id=None): form = PostForm(request) msg = '' if post_id is not None: post = await Post.get_or_404(post_id, sync=True) if request.method == 'POST' and form.validate(): title = form.title.data assert str(form.author_id.data).isdigit() post = await Post.filter(title=title).first() if post: await post.save() msg = 'Post was successfully updated.' else: post = Post() msg = 'Post was successfully created.' form.status.data = form.status.data == 'on' tags = form.tags.data content = form.content.data is_page = form.is_page.data del form.tags del form.content del form.is_page form.populate_obj(post) if is_page: post.type = Post.TYPE_PAGE await post.save() await post.update_tags(tags) await post.set_content(content) context = await _get_post_context() return await render_template('admin/list_posts.html', request, context) elif post_id is not None: form = PostForm(request, obj=post) form.tags.data = [tag.name for tag in post.tags] form.can_comment.data = post.can_comment form.is_page.data = post.is_page form.status.data = 'on' if post.status else 'off' form.submit.label.text = 'Update' tags = await Tag.all() authors = await User.all() return await render_template('admin/post.html', request, {'form': form, 'msg': msg, 'post_id': post_id, 'tags': tags, 'authors': authors})
def edit_post(post_id): # To edit a post, if GET check author and populate form, # if POST check author and update post = Post.query.filter_by(id=post_id).first() if ((post.author.id == g.user.id) or g.user.role == ROLE_ADMIN): form = PostForm(request.form, obj=post) if request.method == 'POST' and form.validate(): form.populate_obj(post) db.session.add(post) db.session.commit() return redirect(url_for('show_post', post_id=post.id)) return render_template('content_add_post.html', title=post.title, form=form, user=g.user, edit=post_id) return redirect(url_for('index'))
def post(self): form = PostForm() if request.method == 'POST': if form.validate_on_submit(): try: post = Post.create() form.populate_obj(post) post.user = current_user post.save() flash(gettext('Post succesfully created')) return redirect(url_for('PostsView:index')) except: flash(gettext('Error while creating the post'), 'error') else: flash(gettext('Invalid submission, please check the message below'), 'error') return render_template('admin/posts/add.html', title=gettext('Create Post'), form=form)
def edit(slug): post = Post.query.filter(Post.slug == slug).first() if not post.is_owner(): flash('Вы можете редактировать только свои посты', 'danger') return redirect(url_for('user_posts.index')) form = PostForm(obj=post) if request.method == 'POST': if form.valid_photo(): post.photo = form.upload_photo(post.photo) form = PostForm(formdata=request.form, obj=post) form.populate_obj(post) db.session.commit() return redirect(url_for('user_posts.detail', slug=post.slug)) return render_template('posts/edit.html', post=post, form=form)
def put(self, id): post = Post.get_by_id(id) if post is None or not post.can_edit(): flash(gettext('The requested stamp was not found'), 'error') return redirect(url_for('PostsView:index')) if request.method in ['POST']: form = PostForm() if form.validate_on_submit(): cover_picture_id = request.values.get('cover_picture_id', 0, int) remain = request.values.get('remain', False, bool) if post.cover_picture and cover_picture_id == 0: # remove the picture, when user request its deletion post.cover_picture.remove() c = form.category_id.data form.populate_obj(post) f = request.files.get('file') if f: if post.cover_picture: post.cover_picture.remove() picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 post.save() message = gettext('Stamp was succesfully saved') if remain: return resp('admin/posts/edit.html', form=form, post=post, message=message) return resp(url_for('PostsView:get', id=post.id), redirect=True, message=message) else: return resp('admin/posts/edit.html', status=False, form=form, post=post, message=gettext('Invalid submission, please check the message below')) else: form = PostForm(post) return resp('admin/posts/edit.html', form=form, post=post)
def post_add_edit(id=None): if id is None: # add form=PostForm(request.form) post=Post() else: # edit post=db_session.query(Post).filter_by(id=id).first() if post is None: abort(404) form=PostForm(request.form, obj=post) if request.method=='POST' and form.validate(): form.populate_obj(post) db_session.add(post) db_session.commit() return redirect(url_for('index')) return render_template('add_edit.html', form=form, id=id)
def save_post(post_id=None): """Save Post to database If post_id is None a new Post will be inserted in the database. Otherwise the existing Post will be updated. """ message = 'Post updated' orig_tags = [] post_form = PostForm(request.form) if not post_form.validate(): flash('ERROR: errors detected. Post NOT saved!', category='error') return edit_post(post_id=post_id, post_form=post_form) # test if we're creating a new post, or updating an existing one if post_id is None: post = Post() post.status_id = 1 post.user_id = session['user_id'] post.createdate = datetime.now() else: post = get_post(post_id) orig_tags = [tag for tag in post.tags] post_form.populate_obj(post) post.lastmoddate = datetime.now() # compile input to html post.compile(app.config['REPL_TAGS']) # update pubdate if post's pubdate is None and its status is set # to public if request.form['status'] == 'public' and \ unicode(post.status) != 'public' and \ post.pubdate is None: post.pubdate = datetime.now() post.status = get_status(request.form['status']) if post.slug is None: post.slug = slugify(post.title) if post_id is None: db_session.add(post) message = 'New post was successfully added' db_session.commit() for tag in orig_tags: recalculate_tagcount(tag) for tag in post.tags: if tag not in orig_tags: recalculate_tagcount(tag) db_session.commit() flash(message, category='info') return redirect(url_for('edit_post', post_id=post.id))
def put(self, id): post = Post.get_by_id(id) if post is None or not post.can_edit() or post.is_hidden: return render_view(url_for('PostsView:index'), status=False, redirect=True, message=_('POST_NOT_FOUND')) form = PostForm(post=post) if form.is_submitted(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) cover_picture_id = request.values.get('cover_picture_id', 0, int) is_draft = request.values.get('status', 0, int) == Post.POST_DRAFT remain = request.values.get('remain', False, bool) if post.cover_picture and cover_picture_id == 0: # remove the picture, when user request its deletion post.cover_picture.remove() form.populate_obj(post) f = request.files.get('file') if f: if post.cover_picture: post.cover_picture.remove() picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 if is_draft: post.status = Post.POST_DRAFT else: if post.save_count == 1 or post.created_at is None: post.created_at = Post.current_date() post.save_count = 1 post.status = Post.POST_PUBLIC post.save_count += 1 post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if not remain: return render_view(url_for('PostsView:get', id=post.id), redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form, post=post)