[Grok-dev] Re: model schema declaration

Philipp von Weitershausen philipp at weitershausen.de
Sun Jul 15 17:02:06 EDT 2007


Leonardo Rochael Almeida wrote:
> The other day, Luciano and Martijn where talking about the deprecated
> inner class idiom for schema declaration

Deprecated? Have I missed something? WHen did we deprecate this?

> and this made me think of
> ways of making the schema declaration more lightweght, specially since
> I was reading the new section on CRUD that the Brazillian grok
> sprinters wrote, and then it struck me:
> 
> If a module contains a model, and an interface with the same name as
> the model but with an "I" before the name, this would automatically
> mean the model implements the interface. For instance, in the example
> below, the "interface.implements(IMammoth)" would be unnecessary as it
> would be declared automatically when the module is grokked:
> 
> class IMammoth(interface.Interface):
>     name = schema.TextLine(title=u"Name", required=True)
>     weight = schema.Int(title=u'Weight', min=0)
> 
> class Mammoth(grok.Model):
>     interface.implements(IMammoth) # <-- the default when the class is 
> grokked
>     def __init__(self, name, weight):
>         self.name = name
>         self.weight = weight
> 
> Taking the conv-over-conf mantra even further, we could make it so
> that the above interface wouldn't even need to be in the same module
> as the model class, as long as it was in the top-level of the
> "interfaces" package/module that is a neighbour to the model module.
> However, this might be taking the CoC mantra too far.
> 
> One disadvantage with this approach, however, is that the interface
> wouldn't be associated at "module import" time, but only at "grokking"
> time, which might be okay most of the time, but which would probably
> mean that the "interface-association" grokker has to be one of the
> first to run. And it could make reuse of the class without grokking
> the module slightly more involved, if this use requires knowing the
> interface association.

-1

Too much magic and, as you say yourself, only active after grokking...

If you want something quick'n'dirty, inline the schema fields into the 
form's form_fields or put them on the Model class.


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Grok-dev mailing list