On Mon, Jul 7, 2008 at 3:58 PM, Mike Orr <[EMAIL PROTECTED]> wrote: > On Mon, Jul 7, 2008 at 3:29 PM, Wichert Akkerman <[EMAIL PROTECTED]> wrote: > Putting database lookup values into 'g' was an idea I had not thought > of. When I've come across that situation, I put a function in the > model that returned or updated a cache dict (module global). It is > assumed this function will be valid only if init_model() has been > called. Of course it will fail with a predictable "no database engine > available" error if this is not the case. Now, this structure is safe > if the data does not vary across application instances coexisisting in > the same process, and is ideal if it's read-only. If the data does > vary across application instances, you'll have to put it on 'g' > because that's what 'g' is for -- as a safe place to put > application-instance-specific data.
I should point out that getting non-request data into the validators is a general weakness in Pylons, one that we haven't found a definitive answer for. So far I've seen these strategies: - Pass the data in through the 'state' argument. That's what the argument is for, yet it's impossible to do with @validate because the wrapper is run before the action method has started, so there's no opportunity to pass request-specific data such as the current database record. - You can inline @validate code into the action method so that it can call the validator properly with 'state'. However, the current structure of the @validate code makes it difficult to port because so much of it has to do with contingencies in the arguments, making it difficult to tell which code needs to be copied to the action. Or you can try to piece together code based on the FormEncode manual, but that's not Pylons-specific so there's the danger of missing an important 'if' somewhere. There's a ticket to split @validate into three parts which can be called individually by actions, and make @validate a simple wrapper to the parts. I'm hoping this or something similar will get into Pylons 0.9.8 (the second-next version). http://pylonshq.com/project/pylonshq/ticket/405 - You can allow the validator to access the model directly and look up database records. This is a possible violation of MVC and adds a dependency from the validator to the model, but if the validator is considered a "controller thing" or a "model thing" then it's arguably legit. This can be combined with a cache accessor/updater in the model as both I and Alberto have described, to minimize the number of database queries for infrequently-changing lookup data. - But how do you get the current record ID in the URL? The action knows it, but the validator doesn't if you're using @validate. One way is to look up the ID in 'c', using a little-known (and I think undocumented) feature of Pylons that copies all routing args to 'c' variables. - But what if the ID is non-numeric, points to a nonexistent record, or the user does not have permission to view the record? Oh dear, this is all processing the action does. Does the validator have to duplicate all this processing? Currently it does if you're using @validate and letting the validator access the model directly. You could inline the validation call into the action, but that gets into our second problem above. - And now we've seen that 'g' can also be used as a rendezvous point for validators to find their control data. The pros and cons of this have already been discussed in this thread so I won't repeat them. But I do want to thank Jonathan for describing this strategy, because as I said it's not one I'd thought of. Are you the same Jonathan who posted the validate parts patch? - There may be better strategies we haven't discovered yet. -- Mike Orr <[EMAIL PROTECTED]> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
