def create(self, blogid): """POST /comments: Create a new item""" # url('comments') create_form = comment_form.bind(Comment, data=request.POST) if create_form.validate(): # Validate captcha if create_form.captcha.value.strip().lower() != "green": return render("/comments/error.html") comment_args = { "referid": blogid, "name": create_form.name.value.strip(), "email": create_form.email.value.strip(), "content": create_form.content.value.strip(), } comment = Comment(**comment_args) Session.add(comment) Session.commit() session["flash"] = "Great success! Your comment was posted." session["flash_class"] = "success" session.save() redirect("/journal/%s" % blogid) blog = Session.query(Blog).filter_by(id=int(blogid)).first() session["flash"] = "There was a problem with your comment." session["flash_class"] = "fail" session.save() return render("/blogs/show.html", {"blog": blog, "comment_form": create_form.render()})
def categories(self, id=None, format='html'): """GET /blogs/categories: All items in the collection""" # url('blogs') if id is None: return 'nothing feels right' #redirect(url(controller='blogs', action='index')) # If none are found, redirect to index and flash a message tag_count = Session.query(Tag).filter(Tag.name == id).count() if int(tag_count) < 1: session['flash'] = 'Tag not found.' session.save() redirect(url(controller='blogs', action='index')) # Query the blog table for blogs tagged with tag blogs = Session.query(Blog).join('tags').filter(Tag.name == id).order_by(Blog.id.desc()) # Limit the output to 10 entries per page blog_paginator = paginate.Page( blogs, page = int(request.params.get('page', 1)), items_per_page = 20, controller = 'blogs', action = 'categories', ) # Get all tags and denote the selected tag tags = Tag.find_all() selected_tag = id return render('/blogs/index.html', { 'blogs': blog_paginator, 'tags': tags, 'selected_tag': selected_tag })
def new(self, format='html'): """GET /blogs/new: Form to create a new item""" # url('new_blog') new_form = blog_form.bind(Blog, session=Session) return render('/blogs/edit.html', { 'blog_form': new_form.render() })
def new(self, format='html'): """GET /pages/new: Form to create a new item""" # url('new_page') # Render edit.html with a blank page object context = { 'page_form': page_form.render(), } return render('/pages/edit.html', context)
def show(self, id, format='html'): """GET /pages/id: Show a specific item""" # url('page', id=ID) if id is None: abort(404) page = Session.query(Page).filter_by(id = id).first() if page is None: abort(404) context = {'page': page} return render('/pages/show.html', context)
def edit(self, id, format='html'): """GET /pages/id/edit: Form to edit an existing item""" # url('edit_page', id=ID) if id is not None: page = Session.query(Page).filter_by(id = id).first() if page is None: abort(404) else: redirect('/pages/new') edit_form = page_form.bind(page) context = { 'page_form': edit_form.render(), 'page': page } return render('pages/edit.html', context)
def edit(self, id, format='html'): """GET /blogs/id/edit: Form to edit an existing item""" # url('edit_blog', id=ID) if id is not None: blog = Session.query(Blog).filter_by(id = id).first() if blog is None: abort(404) else: redirect('/blogs/new') edit_form = blog_form.bind(blog) return render('/blogs/edit.html', { 'blog_form': edit_form.render(), 'blog': blog })
def create(self): """POST /pages: Create a new item""" # url('pages') create_form = page_form.bind(Page, data=request.POST) if request.POST and create_form.validate(): page_args = { 'title': create_form.title.value, 'content': create_form.content.value } page = Page(**page_args) Session.add(page) Session.commit() redirect('/pages/show/%s' % page.id) context = { 'page_form': create_form.render() } return render('pages/edit.html', context)
def create(self): """POST /blogs: Create a new item""" # url('blogs') create_form = blog_form.bind(Blog, data=request.POST) if request.POST and create_form.validate(): blog_args = { 'title': create_form.title.value.strip(), 'entry': create_form.entry.value.strip(), 'date': create_form.date.value } blog = Blog(**blog_args) Session.add(blog) Session.commit() redirect('/blogs/show/%s' % blog.id) return render('/blogs/edit.html', { 'blog_form': create_form.render() })
def index(self, format='html'): """GET /blogs: All items in the collection""" # url('blogs') blogs = Session.query(Blog).order_by(Blog.date.desc()) blog_paginator = paginate.Page( blogs, page = int(request.params.get('page', 1)), items_per_page = 20, controller = 'blogs', action = 'index', ) tags = Tag.find_all() return render('/blogs/index.html', { 'blogs': blog_paginator, 'tags': tags })
def delete(self, id): """DELETE /pages/id: Delete an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="DELETE" /> # Or using helpers: # h.form(url('page', id=ID), # method='delete') # url('page', id=ID) if id is None: abort(404) page = Session.query(Page).filter_by(id = id).first() if page is None: abort(404) if request.params.get('_method') == 'DELETE': Session.delete(page) Session.commit() context = {'confirm': True} else: context = {'id': id} return render('pages/delete.html', context)
def show(self, id=None, format='html'): """GET /blogs/id: Show a specific item""" # url('blog', id=ID) if id is None: return redirect(url(controller='blogs', action='index')) blog_q = Session.query(Blog).filter_by(id=int(id)).first() if blog_q is None: # TODO FLash an alert, redirect to index abort(404) comment_form.append( Field(name='captcha').required().with_metadata( instructions='What color is the grass? (hint: green)' )) return render('/blogs/show.html', { 'blog': blog_q, 'comment_form': comment_form.render() })
def document(self): """Render the error document""" request = self._py_object.request resp = request.environ.get('pylons.original_response') content = literal(resp.body) or cgi.escape(request.GET.get('message', '')) #page = error_document_template % \ # dict(prefix=request.environ.get('SCRIPT_NAME', ''), # code=cgi.escape(request.GET.get('code', str(resp.status_int))), # message=content) #return page if resp: content = literal(resp.status) code = cgi.escape(str(resp.status_int)) if not code: raise Exception('No status code was found') return render('/errors/error.html', { 'code': code, 'message': content })
def update(self, id): """PUT /pages/id: Update an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="PUT" /> # Or using helpers: # h.form(url('page', id=ID), # method='put') # url('page', id=ID) if id is not None: page = Session.query(Page).filter_by(id = id).first() if page is None: abort(404) edit_form = page_form.bind(page, data=request.POST) if request.POST and edit_form.validate(): edit_form.sync() Session.commit() redirect('/pages/show/%s' % id) context = { 'edit_form': edit_form.render(), 'page': page } return render('pages/edit.html', context)
def update(self, id): """PUT /blogs/id: Update an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="PUT" /> # Or using helpers: # h.form(url('blog', id=ID), # method='put') # url('blog', id=ID) if id is not None: blog = Session.query(Blog).filter_by(id = id).first() if blog is None: abort(404) edit_form = blog_form.bind(blog, data=request.POST) if request.POST and edit_form.validate(): edit_form.sync() Session.commit() redirect('/blogs/show/%s' % id) return render('blogs/edit.html', { 'blog_form': edit_form.render(), 'blog': blog })
def login(self): return render('users/login.html')