[Grok-dev] Persistence issue with grok.Model

Sebastian Ware sebastian at urbantalk.se
Tue Dec 30 03:48:36 EST 2008


Maybe this has been answered, but I had similar problems when adding  
dictionary items to grok.Container too early. I started using:

@grok.subscribe(Accounting, grok.IObjectAddedEvent)
def assign_files(obj, evt):
     if not obj.has_key('files'):
         obj['files'] = grok.Container()

And in your case I would have stored the jobs in a temporary list  
during creation of the Accounting object. Then I use that list to  
populate the container at the grok.IObjectAddedEvent.

Mvh Sebastian


29 dec 2008 kl. 20.43 skrev Andreas Jung:

> Hi there,
>
> I am current encountering a persistence issue with the following:
> A Grok app "Server" contains a container "Accounting" holding  
> instances of "Job":
>
> class Server(grok.Application, grok.Container):
>    """ This is the base of all evil """
>
>    interface.implements(IServer)
>
>    title = u''
>    spool_directory = default_tempdir
>
>    def addJob(self, *args, **kw):
>        if not 'accounting' in self:
>            self['accounting'] = Accounting()
>        self['accounting'].addJob(*args, **kw)
>        self._p_changed = 1
>
> class Accounting(grok.Container):
>    """ A folder for Job objects """
>
>    num_items = 0
>
>    def addJob(self, *args, **kw):
>        self.num_items += 1
>        self[str(self.num_items)] = Job(*args, **kw)
>        self._p_changed = 1
>
>
> class Job(grok.Model):
>    """ Holds informations about each conversion job """
>
>    def __init__(self, *args, **kw):
>        super(Job, self).__init__()
>        for k,v in kw.items():
>            setattr(self, k, v)
>
>
> So Server.addJob(**kw) create a new Job instance within the Accouting
> container where the **kw parameters are added as instances attributes
> (I need this very generic approach).
>
> The mess starts after having created a view for displaying the jobs:
>
> class ShowJobs(grok.View):
>    grok.context(Zopyx_smartprintng_server)
>
>    def getJobs(self):
>        lst = list()
>        for id, job in self.context['accounting'].items():
>            print id, job.__dict__
>            lst.append(job.__dict__.copy())
>        return lst
>
> The instance dict of a Job instance holds the **kw dict until I  
> restart
> my Grok instance. After the restart the instance dict is empty. Is  
> there something I am missing or some further magic involved (do I  
> need a schema for a Grok Model in order for having the instance dict  
> properly
> persisted)?
>
> Andreas
>
>
> <lists.vcf>_______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list