[Grok-dev] Trying to wrap my head around GROK

Kevin Teague kevin at bud.ca
Mon Mar 31 12:50:33 EDT 2008




sjol wrote:
> 
> I would need more clarification ... I've been able to create and
> traverse multiple containers, how does grok know where to attach
> variables to  containers ?
> 

Not sure I understand that question, are you asking about setting
attributes on containers or how Grok persists the containers to disk
or how Grok joins all of the containers together?


sjol wrote:
> 
> 
> def Sample(grok.Application, grok.Container):
>      def __init__(self):
>         super(Sample, self).__init__()
>         self['contacts'] = ContactsContainter()
> 
> class ContactsContainter(grok.Container):
>     pass
> 
> class IContact(interface.Interface):
>     """Data schema for contacts"""
>     surname = schema.TextLine(title=u'Surname',description=u'Contact
> \'s surname',)
>     name = schema.TextLine(title=u'Name', description=u'Contact\'s
> name',)
>     notes = schema.Text(title=u'Notes', description=u'Notes about this
> contact',)
> 
> class Contact(grok.Model):
>     grok.implements(IContact)
>     def __init__(self, surname, name, notes):
>         self.surname = surname
>         self.name = name
>         self.notes = notes
> 
> class AddContact(grok.AddForm):
>     grok.context(ContactsContainter)
>     form_fields = grok.AutoFields(IContact)
> 
>     @grok.action('Add contact')
>     def add(self, **data)
> 
> in AddContact should the context be Sample ? Is it possible to get
> access to Sample from the AddContact class as to have access to
> functions and variables set in it ?
> 

The context should be ContactsContainter if you want to limit the creation
of contacts only inside instances of ContactsContainters.

To get access to the root grok.Application object from within any View
you can use grok.getSite():

  sample_app = grok.getSite()

Alternatively, every container and model object has a __parent__ attribute
which is a reference to it's contained object, so in your sample app, in
the AddContact View self.context would be your 'contacts' container
instance,
so you could write:

  self.context.__parent__

And this would also return your Sample app instance. But grok.getSite() is
more flexible since you could have an app that has multiple
ContactsContainers
or have a more complex heirarchy of objects.





-- 
View this message in context: http://www.nabble.com/Trying-to-wrap-my-head-around-GROK-tp16325667p16396998.html
Sent from the Grok mailing list archive at Nabble.com.



More information about the Grok-dev mailing list