class BlogPostHandler(BaseHandler): """Manage individual blog posts""" @admin_required def get(self, post_id=None, **kwargs): """Return a post to edit or an empty form to create""" template = 'admin/blog/new.html' context = { 'form': self.form, } # render edit if post_id is not None: post = BlogPost.get_by_id(post_id) if post: self.form = BlogPostForm(obj=post) self.form.tags.data = ', '.join(post.tags) context.update({'form': self.form}) template = 'admin/blog/edit.html' else: return redirect('admin/blog/') # render new return self.render_response(template, **context) @admin_required def post(self, post_id=None, **kwargs): """Handle submitted form data""" # validate form if self.form.validate(): title = self.form.title.data lead = self.form.lead.data content = self.form.content.data live = self.form.live.data tags = self.form.tags.data language = self.form.language.data if tags is not None: tags = [tag.strip() for tag in tags.split(',') if tag != ''] # saving edited if post_id: post = BlogPost.get_by_id(post_id) post.title = title post.lead = lead post.content = content post.live = live post.tags = tags post.language = language # creating new else: post = BlogPost(title=title, lead=lead, content=content, live=live, tags=tags, language=language) if post.put(): return redirect('admin/blog') return self.get(**kwargs) @cached_property def form(self): """Form instance as cached_property""" return BlogPostForm(self.request)
class BlogPostHandler(BaseHandler): """Manage individual blog posts""" @admin_required def get(self, post_id=None, **kwargs): """Return a post to edit or an empty form to create""" template = 'admin/blog/new.html' context = { 'form': self.form, } # render edit if post_id is not None: post = BlogPost.get_by_id(post_id) if post: self.form = BlogPostForm(obj=post) self.form.tags.data = ', '.join(post.tags) context.update({ 'form': self.form }) template = 'admin/blog/edit.html' else: return redirect('admin/blog/') # render new return self.render_response(template, **context) @admin_required def post(self, post_id=None, **kwargs): """Handle submitted form data""" # validate form if self.form.validate(): title = self.form.title.data lead = self.form.lead.data content = self.form.content.data live = self.form.live.data tags = self.form.tags.data language = self.form.language.data if tags is not None: tags = [tag.strip() for tag in tags.split(',') if tag != ''] # saving edited if post_id: post = BlogPost.get_by_id(post_id) post.title = title post.lead = lead post.content = content post.live = live post.tags = tags post.language = language # creating new else: post = BlogPost(title=title, lead=lead, content=content, live=live, tags=tags, language=language) if post.put(): return redirect('admin/blog') return self.get(**kwargs) @cached_property def form(self): """Form instance as cached_property""" return BlogPostForm(self.request)
def post(self): form = BlogPostForm(self.request.POST) if form.validate(): post = BlogPost(**form.data) post.save() self.redirect_to("admin-blog-post-edit-extra", post_id = post.key().id(), extra="saved") return { "admin_section": "admin-blog-posts-new", "form": form, }
def post(): form = BlogPostForm(request.form) if request.method == 'POST': if form.validate(): post = Post(user=form.author.data, title=form.title.data, content=form.content.data) current_session.add(post) current_session.commit() flash('Post created!') return redirect(url_for('post')) else: flash('Form is not valid! Post was not created.') posts = current_session.query(Post).all() return render_template('post.html', form=form, posts=posts)
def post(self, post_id, extra=""): form = BlogPostForm(self.request.POST) success = False post = BlogPost.get_by_id(long(post_id)) if form.validate(): form.populate_obj(post) post.save() success = True return { "admin_section": "admin-blog-posts", "form": form, "success": success, "post": post, }
def new(): """ Create a new blog post. """ error = None form = BlogPostForm(request.form) if request.method == 'POST' and form.validate(): new_blog_post = Post(form.title.data, form.content.data, form.published.data, current_user.get_id()) g.db_session.add(new_blog_post) flash('New Blog Post Created!') return redirect(url_for('blog.show_posts')) try: return render_template('blog_new.html', form=form, error=error) except TemplateNotFound: abort(404)
def home(): storage = Storage() if request.method == 'POST': form = BlogPostForm(request.form) if form.validate(): model = BlogPostModel(form.data) storage.appending(model.model_to_dict()) else: logger.error('Someone have submitted an incorrect form!') else: form = BlogPostForm() return render_template( 'home.html', form=form, items=storage.posts, )
def new_post(): initialize_json() post_result = None if request.method == 'POST': form = BlogPostForm(request.form) if form.validate(): new_post = BlogPostModel(form.data) save_to_json(new_post) post_result = "Posted successful" else: form = BlogPostForm() return render_template( 'new_post.html', form=form, post_result=post_result, )
def profile(id): form = BlogPostForm() if id == current_user.id and request.method == 'GET': posts = current_session.query(Post).filter( Post.user == current_user).all() return render_template('post.html', form=form, posts=posts) elif request.method == 'POST' and id == current_user.id and form.validate( ): post = Post(user=current_user, title=form.title.data, content=form.content.data) current_session.add(post) current_session.commit() flash('post_created') route_to = '/profile/' + str(current_user.id) return redirect(route_to) else: return redirect(url_for('login_route.login'))
def home(): storage = Storage() all_items = storage.items if request.method == 'POST': form = BlogPostForm(request.form) if form.validate(): model = BlogPostModel(form.data, len(all_items) + 1) all_items.append(model) storage.save() else: logger.error('Someone have submitted an incorrect form!') else: form = BlogPostForm() return render_template( 'home.html', form=form, items=all_items, )
def edit(post_id): """ Edit a post. """ post = g.db_session.query(Post).filter_by(id=post_id).first() form = BlogPostForm(request.form) if request.method == 'POST' and form.validate(): post = g.db_session.query(Post).filter_by(id=post_id).first() post.title = form.title.data post.content = form.content.data post.published = form.published.data return redirect(url_for('blog.show_posts')) form.title.data = post.title form.content.data = post.content form.published.data = post.published try: return render_template('blog_edit.html', form=form, post=post) except TemplateNotFound: abort(404)