On Aug 6, 8:53 pm, BrianTheLion <[email protected]> wrote: > It seems like building a REST application is The Right Thing To Do. I > tend to want to create a controller for each entity in my object > model, and the rationale behind the REST paradigm gives this idea some > support. > > I also appreciate the "Pylons is for HTTP and nothing more" > convention. I am, however, left to wonder how I should go about > building all the other stuff that we need from a web application, the > machinery that lives between the database and the templates in > particular. Assume for a moment that we are writing a Facebook work- > alike in Pylons. Then, a GET ofhttp://pylonsfacebook.com/user/brianthelion > would be a request - and I'll use Facebook-speak here - to view > BrianTheLion's "profile." If we're following the REST formula that > we've argued for, then UserController.show(id=brianthelion) is getting > executed. Awesome.
Why wouldn't the URL be /profile/brianthelion that talks to a ProfileController that works with a Profile model that encapsulates all the stuff you mention below? There's no reason a URL has to map directly to a database table or to anything in particular really, so long as it's *some* kind of logical entity. So, you can create whatever hybrid entities you need built from ORM classes or whatever, providing access via the "standard" RESTful API (index, show, etc). > At this point in the example, though, there is giant code-hole. On > Facebook, a user's profile is an extremely complicated UI. It's a > "hybrid view" (is there a better term?) of data from many different > model entities: user, event, photo, message, mail, news, etc, etc. To > build our work-alike, we need to take information about the user, the > events he's invited to, the photo's he's in, the things his friends > are saying, etc, and render all of that to fun-to-look-at HTML. So, > the context of our PylonsFacebook, the question is: How does > UserController.show() go about producing this immensely complicated > output? How should all of the code between HTTP and HTML - and there's > a lot of it; the majority, in fact - be encapsulated? How can we best > make it reusable? > > SQLAlchemy aside, this territory seemed pretty lawless to me. In my > naivety I came up with the idea of "named views." For example, a GET > ofhttp://pylonsfacebook.com/photo/981388324/iconandhttp://pylonsfacebook.com/photo/981388324/fullwould > render the "icon" > and "full res" views of that photo, respectively. Pretty self- > explanatory. This made some nice sense because then for the > PhotoController I'd have a "show_icon" method and a "show_fullres" > method, as well as a "photo.mako" with "icon" and "fullres" defs. The > rendered HTML output of the aforementioned URLs served as nice visual > building blocks for more complicated web pages, and GET-ing them > allowed me to test the code easily. > > I ran into problems, though, when I started needing to build "hybrid" > views, named views that depended on other named views. For > example,http://pylonsfacebook.com/user/brianthelion/photos would call > UserController.show_photos(id=brianthelion). It's output would depend > on the output of, say, PhotoController.show_icon(id=29459285) and > PhotoController.show_icon(id=299498594). So then I'd need to > instantiate a PhotoController from within my UserController, and that > seemed - at least in MY mind - to break the Pylons paradigm. I've seen controller code that does this type of thing, and I don't know if it's "wrong", but it sure feels wrong to me. In this particular example, if a photo is always owned by a user, I think I'd create a PhotoController that looks up the user first, then goes about its business in the usual way. If photos can be owned by users and also by other types, I might create a UserPhotoController instead. In either case, your controller would do something like this: def show(self, id): # user was already looked up photo = Session.query(Photo).filter_by(userid=self.user.id).filter_by(id=id).one() # ... # Render the photo template, which is a customization of the base layout template # Or, maybe just render the photo if the requested format is .image Anyway, just some off-the-cuff thoughts there. Hopefully it's useful in some way. -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
