[Checkins] SVN: grok/branches/luciano-tutorial/doc/ completed
explanation of the "delete" part of CRUD
Luciano Ramalho
luciano at ramalho.org
Fri Jul 13 17:15:54 EDT 2007
Log message for revision 77887:
completed explanation of the "delete" part of CRUD
Changed:
U grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app.py
U grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app_templates/index.pt
A grok/branches/luciano-tutorial/doc/groktut/crud2/
U grok/branches/luciano-tutorial/doc/tutorial.txt
-=-
Modified: grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app.py
===================================================================
--- grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app.py 2007-07-13 20:51:34 UTC (rev 77886)
+++ grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app.py 2007-07-13 21:15:54 UTC (rev 77887)
@@ -1,5 +1,4 @@
import grok
-from zope import interface, schema
class Pebbles(grok.Application, grok.Container):
@@ -25,12 +24,7 @@
self.context.eatMammoth(name)
self.redirect(self.url('index'))
-class IMammoth(interface.Interface):
- name = schema.TextLine(title=u"Name", required=True)
- weight = schema.Int(title=u'Weight', min=0)
-
class Mammoth(grok.Model):
- #implements(IMammoth)
def __init__(self, name, weight):
self.name = name
self.weight = weight
@@ -38,8 +32,4 @@
class MammothDetails(grok.View):
grok.context(Mammoth)
grok.name('index')
-
-class Edit(grok.EditForm):
- grok.context(Mammoth)
- pass
Modified: grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app_templates/index.pt
===================================================================
--- grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app_templates/index.pt 2007-07-13 20:51:34 UTC (rev 77886)
+++ grok/branches/luciano-tutorial/doc/groktut/crud/src/pebbles/app_templates/index.pt 2007-07-13 21:15:54 UTC (rev 77887)
@@ -3,8 +3,10 @@
</head>
<body>
<h2>Mammoths</h2>
-
- <form tal:attributes="action python:view.url('eat')" method="POST">
+
+ <em tal:condition="not:context/keys">(no mammoths)</em>
+ <form tal:condition="context/keys"
+ tal:attributes="action python:view.url('eat')" method="POST">
<ul>
<li tal:repeat="key context/keys">
<input type="checkbox" name="names:list" tal:attributes="value key">
Copied: grok/branches/luciano-tutorial/doc/groktut/crud2 (from rev 77881, grok/branches/luciano-tutorial/doc/groktut/crud)
Modified: grok/branches/luciano-tutorial/doc/tutorial.txt
===================================================================
--- grok/branches/luciano-tutorial/doc/tutorial.txt 2007-07-13 20:51:34 UTC (rev 77886)
+++ grok/branches/luciano-tutorial/doc/tutorial.txt 2007-07-13 21:15:54 UTC (rev 77887)
@@ -1196,9 +1196,6 @@
if you need to store large quantities of items.
-
-
-
Containers
----------
@@ -1329,6 +1326,53 @@
modifications, you would be on your way to building your own content
management system with Grok.
+CRUD
+----
+Many web apps do CRUD (Create, Retrieve, Update, Delete). Our ``Pebbles`` apps already allows the creation of and retrieval of ``Mammoth`` instances, so we are already half way there. The rest is easy as well.
+First let's implement the delete operation. Here is the new ``index.pt`` template::
+.. include:: groktut/containers/src/pebbles/app_templates/index.pt
+ :literal:
+
+Here we have transformed the listing into a form, with one checkbox for
+each mammoth. If there are no mammoths, we use ``tal:condition`` statements
+to omit the form and display the text "(no mammoths)".
+
+The form ``action`` attribute is generated by the ``view.url('eat')`` call.
+In this case, the ``url`` method will produce the URL of the view named ``eat`` in the same context, that is, the ``Pebble`` app.
+
+The ``name`` attribute for all checkboxes is the same. The ``:list`` suffix
+tells Grok that the values should be transformed into a list. This simplifies
+the coding of the method that will handle the action, because it will always
+receive a list in the ``name`` argument, even if only one checkbox is
+selected.
+
+.. sidebar:: Request data marshalling
+
+ One of the many services provided by the Zope is the automatic conversion of
+ form fields to Python types, including collection types. The `Zope 2.6
+ manual`__ describes the field name suffixes available, of which ``:list``
+ is just one example.
+
+ __ http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/ScriptingZope.stx/#2-77
+
+Now let's see the changes in ``app.py``::
+
+.. include:: groktut/crud/src/pebbles/app.py
+ :literal:
+
+First we implemented the ``eatMammoth`` method in the ``Pebbles`` application.
+Again, we can handle the container like a regular Python dictionary, so there
+is no special API call: we just delete the item.
+
+The delete form submits to the ``Eat`` view. Here we implement the ``render```
+method instead of the ``update`` as before, because the ``Eat`` view has no
+template. The ``render`` method just iterates over the received names and
+calls the ``context.eatMammoth`` method for each of them, and then redirects
+to the index.
+
+
+
+ action of that form view that handles the is the target of the ``index.pt`` form action.
More information about the Checkins
mailing list