Source

LazyBobo / lazybobo / views.py

import bobo
from lazybobo.models import Post
from lazybobo import templates
from lazybobo import db
from lazybobo import oembed_consumer
from formalchemy.ext.couchdb import FieldSet
from urllib import quote

fields = FieldSet(Post)


@bobo.subroute("/", scan=True)
class Tumblog(object):
    def __init__(self, request):
        self.request = request

    @bobo.query("")
    def index(self):
        #Get the post model with this process's DB
        WrappedPost = db(Post)
        #Query the all view for our previous posts, passing descending to get them in blog style
        all_posts = WrappedPost.view("tumblog/all", descending=True).all()
        template = templates.get_template("index.html")
        #pass them as context to the template
        return template.render(all_posts = all_posts)

    @bobo.query("/new")
    def create_form(self):
        #Create a post object to bind to our form
        new_post = Post()
        #Get fields for it
        fieldset = fields.bind(new_post)
        #Set the order of the fields and which ones to include, setting the description as a textarea
        fieldset.configure(include = [fieldset.title, fieldset.subtitle, fieldset.external_url, fieldset.description],
                           options = [fieldset.description.textarea(size=(30,10)),])
        #get the HTMl for the form
        form_html = fieldset.render()
        template = templates.get_template("create_form.html")
        #pass it as context to the template
        return template.render(form_html = form_html)
        
    @bobo.post("/add_entry")
    def add_entry(self):
        data = self.request.POST
        #Setup a post to pass data into
        new_post = Post()
        #create a fieldset object and bind the post data to it
        fieldset = fields.bind(new_post, data = data or None)
        #configure the fieldset so it doesn't complain about omitted fields like the timestamp
        fieldset.configure(include = [fieldset.title, fieldset.subtitle, fieldset.external_url, fieldset.description],)
        if data:
            #sync the fieldset and the post object
            fieldset.sync()
            #persist the new object
            db(new_post).save()
            return bobo.redirect("/")
        else:
            return bobo.redirect("/new")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.