Example #1
0
    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()})
Example #2
0
    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()
        })
Example #3
0
    def delete(self, id):
        """DELETE /blogs/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('blog', id=ID),
        #           method='delete')
        # url('blog', id=ID)
        if id is None:
            abort(404)
        blog = Session.query(Blog).filter_by(id = id).first()
        if blog is None:
            abort(404)

        if request.params.get('_method') == 'DELETE':
            Session.delete(blog)
            Session.commit()
            context = {'confirm': True}
        else:
            context = {'id': id}

        return render('blogs/delete.html', context)
Example #4
0
    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
            })