[Zope-dev] Re: Killer App for ZClasses

Philipp von Weitershausen philipp at weitershausen.de
Thu Apr 19 01:52:24 EDT 2007


Christopher Lozinski wrote:
> I need a very simple app.  Just 1 ZClass, an invoice object.  Not even 
> subitems on the invoice.  I want to create these ZClasses, I want a 
> table of these ZClasses, I want to be able to say which role gets to 
> create these objects, which role gets to see which fields, and which 
> role gets to edit which fields.  I would also like a search form with a 
> ZCatalog, and I would like an edit/create form.   All I want is to 
> define the fields of the Zclass, and have the rest magically appear.   I 
> want the application to be built really quickly.   Can you deliver by 
> Monday?

Let me deliver by Thursday::


import grok
from zope import schema

class InvoiceTracker(grok.Application, grok.Container):
     """Folderish object (we say 'container' in Zope 3) that contains
     Invoice objects."""

class Invoice(grok.Model):
     """A persistent Invoice class, below is a definition of the data
    fields that an invoice can carry."""

     class fields:
         date = schema.Date(title=u'Date')
         from = schema.TextLine(title=u'From')
         about = schema.TextLine(title=u'About')
         amount = schema.Float(title=u'Amount')
         status = schema.Choice(title=u'Status',
                                values=['Incoming', 'Paid', 'Rejected'])

class AddInvoice(grok.AddForm):
     """Form that lets us enter invoice data and as a result creates an
     Invoice object and puts it into the InvoiceTracker container"""

     grok.context(InvoiceTracker)
     form_fields = grok.AutoFields(Invoice)

     @grok.action("Add invoice")
     def add(self, **data):
         invoice = Invoice()
         self.applyData(invoice, **data)
         self.context[str(data['date']) + '-' + data['from'])] = invoice
         self.redirect(self.url(invoice))

class EditInvoice(grok.EditForm):
     """This lets us edit the data contained in an invoice. It is
     accessible via http://url/to/an/invoice/edit."""

     grok.context(Invoice)
     grok.name('edit')
     form_fields = grok.AutoFields(Invoice)

class DeleteInvoice(grok.View):
     """View that lets us delete an invoice from the container. You can
     invoke it via
     http://url/to/the/invoice/tracker/delete?name=name_of_invoice"""

     grok.context(InvoiceTracker)
     grok.name('delete')

     def update(self, name):
         del self.context[name]

class ListInvoices(grok.View):
     """View that lists all the invoices within an invoice tracker."""

     grok.context(InvoiceTracker)
     grok.name('index')

     # Rendering happens in Page Template app_templates/listinvoices.pt.
     # This needs to be written (the only template in this app, btw).


What you got here is a simple CRUD (create read update delete) 
application. Most web-frameworks have examples on how to create such an 
application because it's such a common pattern.

ZClasses surely was an easy way to create CRUD applications in Zope 2. 
However, apart from many technical difficulties that are ZClasses, I 
personally find that through-the-web (TTW) development is actually 
hurting Zope and the people who use it, rather than help. I've put my 
thoughts regarding that issue into a blog article recently [1]. One of 
the main points of that article is that while TTW development is bad, we 
do need an altenative to lower the bar for newcomers, quick'n'dirty 
developers, and for ourselves who can't remember every single detail of 
Zope's big API.

Grok [1] is this alternative. The above 60-line application is written 
in Grok and it should be functional except for one PageTemplate which 
yet needs to be written. Note I haven't tested the above code at all, 
there might be typos lurking.

Is this so much harder than ZClasses? I believe no (though as one of the 
original Grok developers, I'm naturally biased). Like other people have 
pointed out already, there's a Grok tutorial [3] where you can learn 
this stuff. And a while back, I made a screencast [4] that shows how to 
develop a simple CRUD application like the above (in the screencasts 
it's a TodoList) in 15 minutes. It also took me about 15 minutes to 
write the above. And it's commented.


Chris, if you want to keep ZClasses alive on your own, we can't stop 
you. If you want to rip out Zope's security in the process and simplify 
everything and what not, well that's crazy, but we can't stop you. But 
it seems that the community at this point is not interested in any of 
this. That may seem sad, but given many people's frustration with TTW in 
general and ZClasses in particular, it's not so surprising.


Give Grok a chance. Let us know how it went. I already gave you a head 
start on that invoice tracking app. :)


[1] 
http://www.z3lab.org/sections/blogs/philipp-weitershausen/2007_03_27_meet-grok-successor
[2] http://grok.zope.org
[3] http://grok.zope.org/tutorial.html
[4] http://www.archive.org/details/grok_todo_part1


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


More information about the Zope-Dev mailing list