Ejemplo n.º 1
0
    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
        })
Ejemplo n.º 2
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()})
Ejemplo n.º 3
0
 def __call__(self, environ, start_response):
     """Invoke the Controller"""
     # WSGIController.__call__ dispatches to the Controller method
     # the request is routed to. This routing information is
     # available in environ['pylons.routes_dict']
     try:
         self.identity = environ.get('repoze.who.identity')
         return WSGIController.__call__(self, environ, start_response)
     finally:
         Session.remove()
Ejemplo n.º 4
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()
        })
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
    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
        })
Ejemplo n.º 7
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
            })
Ejemplo n.º 8
0
    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
        })
Ejemplo n.º 9
0
    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()
        })
Ejemplo n.º 10
0
def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)
Ejemplo n.º 11
0
 def prev(self):
     return Session.query(Blog).filter(Blog.id < self.id).order_by(desc(Blog.id)).first()
Ejemplo n.º 12
0
 def next(self):
     return Session.query(Blog).filter(Blog.id > self.id).order_by(Blog.id).first()
Ejemplo n.º 13
0
 def find_all(self):
     tags = Session.query(Tag).join(BlogTag).all()
     return [str(tag.name) for tag in tags]