On 5 Dec, 2005, at 9:20 pm, Michael Schneider wrote:
Sorry for the stupid question, but what would the Controller code look
like for option #1? for CRUD

1) http://yoursite/articles/10/edit
2) http://yoursite/articles/edit/10
3) http://yoursite/articles/edit?id=10

I'm certain some will suggest this is unpythonic, but here goes:

class CrudController(turbogears.Controller):
    def __init__( self, crudClass, templateBase, instance ):
        self.template_base= templateBase
        self.instance= instance
        self.crud_class= crudClass

    @turbogears.expose()
    def create( self, **kw ):
        template= "%s.created" % self.template_base
# Create a new instance and save it to the DB based on the keywords
        return dict( tg_template=template )

    @turbogears.expose()
    def retrieve( self ):
        template= "%s.edit" % self.template_base
        return dict( instance= self.instance, tg_template=template )

    @turbogears.expose()
    def update( self, **kw ):
        # update the instance based on the keywords and save to DB
        return self.retrieve()

    @turbogears.expose()
    def delete( self ):
        self.instance.destroySelf()
        template= "%s.deleted" % self.template_base
        return dict( tg_template=template )

    @turbogears.expose()
    def default( self, *args, **kw ):
        # determine method based on HTTP method
        methodDict= {   'GET': self.retrieve,
                        'PUT': self.update,
                        'DELETE': self.delete
                    }
        method= methodDict[cherrypy.request.method]
        return method( *args, **kw )


class CrudClassController(turbogears.Controller):
    def __init__( self, crudClass, keyMethod, templateBase ):
        self.crud_class= crudClass
        self.template_base= templateBase
        self.key_method= keyMethod

    @turbogears.expose()
    def index( self, **kw ):
        if 'POST'==cherrypy.request.method:
            # need to perform create
return CrudController( self.crud_class, self.template_base ).create( **kw )
        # Otherwise, assume it's a GET request and return all instances
        all= self.crud_class.select()
        template= "%s.list" % self.template_base
        return dict( all=all, tg_template=template )

    def __getattr__( self, objectKey ):
        '''
        Look up the object by key and return a controller for it.
        '''
instance= getattr(self.crud_class, self.key_method) ( objectKey ) return CrudController( self.crud_class, self.template_base, instance )


class Root(turbogears.RootController):
    # the CRUD controller for all articles
articles= CrudController( blog.model.Article, blog.model.Article.bySlug,
                              "blog.templates.article.crud" )


Completely composed in Mail.app and untested.

--
Jeff Watkins
http://newburyportion.com/

In the USDA study [of the meat packing industry conducted in 1996] 78.6 percent of the ground beef contained microbes that are spread primarily by fecal material. The medical literature on the causes of food poisoning is full of euphemisms and dry scientific terms: coliform levels, aerobic plate counts, sorbitol, MacConkey agar, and so on. Behind them lies a simple explanation for why eating a hamburger can now make you seriously ill: There is shit in the meat.
-- Eric Schlosser, Fast Food Nation


Reply via email to