diff options
Diffstat (limited to 'examples/todo_wareweb')
| -rw-r--r-- | examples/todo_wareweb/__init__.py | 5 | ||||
| -rw-r--r-- | examples/todo_wareweb/db.py | 16 | ||||
| -rw-r--r-- | examples/todo_wareweb/server.conf | 24 | ||||
| -rw-r--r-- | examples/todo_wareweb/sitepage.py | 14 | ||||
| -rw-r--r-- | examples/todo_wareweb/templates/edit_list.pt | 0 | ||||
| -rw-r--r-- | examples/todo_wareweb/templates/generic_error.pt | 7 | ||||
| -rw-r--r-- | examples/todo_wareweb/templates/index.pt | 22 | ||||
| -rw-r--r-- | examples/todo_wareweb/templates/standard_template.pt | 31 | ||||
| -rw-r--r-- | examples/todo_wareweb/templates/view_list.pt | 57 | ||||
| -rw-r--r-- | examples/todo_wareweb/web/__init__.py | 29 | ||||
| -rw-r--r-- | examples/todo_wareweb/web/edit_list.py | 31 | ||||
| -rw-r--r-- | examples/todo_wareweb/web/index.py | 9 | ||||
| -rw-r--r-- | examples/todo_wareweb/web/static/stylesheet.css | 13 | ||||
| -rw-r--r-- | examples/todo_wareweb/web/view_list.py | 51 |
14 files changed, 309 insertions, 0 deletions
diff --git a/examples/todo_wareweb/__init__.py b/examples/todo_wareweb/__init__.py new file mode 100644 index 0000000..0454673 --- /dev/null +++ b/examples/todo_wareweb/__init__.py @@ -0,0 +1,5 @@ +from paste.util.thirdparty import add_package + +add_package('ZopePageTemplates') +add_package('Component') +add_package('ZPTKit') diff --git a/examples/todo_wareweb/db.py b/examples/todo_wareweb/db.py new file mode 100644 index 0000000..174f0f5 --- /dev/null +++ b/examples/todo_wareweb/db.py @@ -0,0 +1,16 @@ +from sqlobject import * + +class TodoList(SQLObject): + + description = StringCol(notNull=True) + items = MultipleJoin('TodoItem') + +class TodoItem(SQLObject): + + todo_list = ForeignKey('TodoList') + description = StringCol(notNull=True) + done = BoolCol(notNull=True, default=False) + +def check_db(): + for soClass in (TodoList, TodoItem): + soClass.createTable(ifNotExists=True) diff --git a/examples/todo_wareweb/server.conf b/examples/todo_wareweb/server.conf new file mode 100644 index 0000000..70d1b49 --- /dev/null +++ b/examples/todo_wareweb/server.conf @@ -0,0 +1,24 @@ +# -*- python -*- Note: this file is in Python syntax + +import os + +app_name = 'todo_wareweb' +publish_dir = os.path.join(os.path.dirname(__file__), 'web') +sys_path = [os.path.dirname(os.path.dirname(__file__))] + +database = 'sqlite:%s/data.db' % os.path.dirname(__file__) +root_path = os.path.dirname(__file__) + +## Server options: + +verbose = True +# The name of the server-type to start: +server = 'wsgiutils' +# If true, files will be regularly polled and the server restarted +# if files are modified: +reload = True +# If true, tracebacks will be shown in the browser: +debug = True +lint = True +session_type = 'memory' +show_exceptions_in_error_log = False diff --git a/examples/todo_wareweb/sitepage.py b/examples/todo_wareweb/sitepage.py new file mode 100644 index 0000000..52af53c --- /dev/null +++ b/examples/todo_wareweb/sitepage.py @@ -0,0 +1,14 @@ +from paste.wareweb import Servlet, Notify +from ZPTKit.zptwareweb import ZPTComponent + +class SitePage(Servlet): + + zpt = ZPTComponent() + message = Notify() + + def awake(self): + self.app_static_url = self.app_url + '/static' + super(SitePage, self).awake() + + def title__get(self): + return self.options.get('title', super(SitePage, self).title) diff --git a/examples/todo_wareweb/templates/edit_list.pt b/examples/todo_wareweb/templates/edit_list.pt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/examples/todo_wareweb/templates/edit_list.pt diff --git a/examples/todo_wareweb/templates/generic_error.pt b/examples/todo_wareweb/templates/generic_error.pt new file mode 100644 index 0000000..7b32820 --- /dev/null +++ b/examples/todo_wareweb/templates/generic_error.pt @@ -0,0 +1,7 @@ +<html metal:use-macro="here/standard_template.pt/macros/page"> +<metal:body fill-slot="body"> + +<tal:block replace="structure options/error_message" /> + +</metal:body> +</html>
\ No newline at end of file diff --git a/examples/todo_wareweb/templates/index.pt b/examples/todo_wareweb/templates/index.pt new file mode 100644 index 0000000..704fca8 --- /dev/null +++ b/examples/todo_wareweb/templates/index.pt @@ -0,0 +1,22 @@ +<html metal:use-macro="here/standard_template.pt/macros/page"> +<metal:body fill-slot="body"> + +<ul> + <li tal:repeat="list options/lists"> + <a tal:attributes="href python: '%s/view_list?id=%s' % (servlet.app_url, list.id)" + tal:content="list/description">list</a></li> +</ul> + +<p tal:condition="not: options/lists"> + There are no lists to display +</p> + +<form action="./edit_list" method="POST"> + Create a new list:<br> + <input type="hidden" name="id" value="new"> + <input type="hidden" name="_action_" value="save"> + <input type="text" name="description"> + <input type="submit" value="Create!!!!"> +</form> +</metal:body> +</html> diff --git a/examples/todo_wareweb/templates/standard_template.pt b/examples/todo_wareweb/templates/standard_template.pt new file mode 100644 index 0000000..0abe2dd --- /dev/null +++ b/examples/todo_wareweb/templates/standard_template.pt @@ -0,0 +1,31 @@ +<metal:tpl define-macro="page"> +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" + "http://www.w3.org/TR/REC-html40/loose.dtd"> +<html lang="en-US"> +<head> +<title tal:content="servlet/title">title</title> +<link rel="stylesheet" type="text/css" + tal:attributes="href python: servlet.app_static_url + '/stylesheet.css'"> + +<metal:slot define-slot="extra_head"></metal:slot> + +</head> +<body> + +<h1><tal:mark replace="servlet/title"/></h1> + +<div class="notifyMessage" + tal:condition="servlet/message" + tal:content="structure servlet/message/html"> + [This is where the notification messages go] +</div> + +<div id="content"> +<metal:tpl define-slot="body"> +[This page has not customized its "body" slot] +</metal:tpl> +</div> + +</body> +</html> +</metal:tpl> diff --git a/examples/todo_wareweb/templates/view_list.pt b/examples/todo_wareweb/templates/view_list.pt new file mode 100644 index 0000000..5ba99c8 --- /dev/null +++ b/examples/todo_wareweb/templates/view_list.pt @@ -0,0 +1,57 @@ +<html metal:use-macro="here/standard_template.pt/macros/page"> +<metal:body fill-slot="body"> + +<form action="view_list?_action_=check" method="POST"> +<input type="hidden" name="id" + tal:attributes="value options/list/id"> + +<table> +<tr> + <th>Done?</th> + <th>Description</th> +</tr> + +<tr tal:repeat="item options/list_items"> + <tal:let define="name python: 'item_%s' % item.id"> + <td> + <input type="checkbox" + tal:attributes="name name; + id name; + checked item/done"> + </td> + <td> + <label tal:attributes="for name" + tal:content="item/description">description</label> + </td> + <td><a tal:attributes=" + href python: 'view_list?_action_=destroy&id=%s&item_id=%s' + % (options['list'].id, item.id)">[delete]</a></td> + </tal:let> +</tr> + +<tr tal:condition="not: options/list_items"> + <td colspan=2>No items to display</td> +</tr> + +</table> + +<input type="submit" value="Update item status"> +</form> + +<form action="view_list?_action_=add" method="POST"> +<input type="hidden" name="id" + tal:attributes="value options/list/id"> +Add item:<br> +<input type="text" style="width: 100%" name="description"><br> +<input type="submit" value="Add"> +</form> + +<form action="edit_list?_action_=destroy" method="POST"> +<input type="hidden" name="id" + tal:attributes="value options/list/id"> +<input type="submit" value="Delete this list" + onclick="return window.confirm('Really delete this list?')"> +</form> + +</metal:body> +</html> diff --git a/examples/todo_wareweb/web/__init__.py b/examples/todo_wareweb/web/__init__.py new file mode 100644 index 0000000..0fca385 --- /dev/null +++ b/examples/todo_wareweb/web/__init__.py @@ -0,0 +1,29 @@ +from paste.util import thirdparty +thirdparty.add_package('sqlobject') +import sqlobject +from todo_wareweb import db + +conns = {} + +def urlparser_hook(environ): + if not environ.has_key('todo_wareweb.base_url'): + environ['todo_wareweb.base_url'] = environ['SCRIPT_NAME'] + +def urlparser_wrap(environ, start_response, app): + db_uri = environ['paste.config']['database'] + first_conn = False + if db_uri in conns: + conn = conns[db_uri] + else: + first_conn = True + conns[db_uri] = conn = sqlobject.connectionForURI(db_uri) + sqlobject.sqlhub.threadConnection = conn + if first_conn: + db.check_db() + # @@: This doesn't allow for using database connections during + # the application iterator + try: + return app(environ, start_response) + finally: + sqlobject.sqlhub.threadConnection = None + diff --git a/examples/todo_wareweb/web/edit_list.py b/examples/todo_wareweb/web/edit_list.py new file mode 100644 index 0000000..0b79339 --- /dev/null +++ b/examples/todo_wareweb/web/edit_list.py @@ -0,0 +1,31 @@ +from todo_wareweb.sitepage import SitePage +from todo_wareweb.db import * +from paste.wareweb import ActionDispatch, public + +class edit_list(SitePage): + + dispatch = ActionDispatch() + + def setup(self): + self.list_id = self.fields.id + if self.list_id != 'new': + self.list = TodoList.get(int(self.list_id)) + self.options.title = 'Edit list ' + + @public + def save(self): + desc = self.fields.description + if self.list_id == 'new': + self.list = TodoList(description=desc) + self.message.write('List created') + else: + self.list.description = desc + self.message.write('List updated') + self.redirect('view_list', id=self.list.id) + + @public + def destroy(self): + desc = self.list.description + self.list.destroySelf() + self.message.write('List %s deleted' % desc) + self.redirect('./') diff --git a/examples/todo_wareweb/web/index.py b/examples/todo_wareweb/web/index.py new file mode 100644 index 0000000..b5cbaf1 --- /dev/null +++ b/examples/todo_wareweb/web/index.py @@ -0,0 +1,9 @@ +from todo_wareweb.sitepage import SitePage +from todo_wareweb.db import * + +class index(SitePage): + + def setup(self): + self.options.title = 'List of Lists' + self.options.lists = list(TodoList.select()) + diff --git a/examples/todo_wareweb/web/static/stylesheet.css b/examples/todo_wareweb/web/static/stylesheet.css new file mode 100644 index 0000000..d70e9ec --- /dev/null +++ b/examples/todo_wareweb/web/static/stylesheet.css @@ -0,0 +1,13 @@ +body { + font-family: Helvetica, Arial, sans-serif; +} + +pre { + overflow: auto; +} + +div.notifyMessage { + border: 2px solid black; + background-color: #007700; + color: #ffffff; +} diff --git a/examples/todo_wareweb/web/view_list.py b/examples/todo_wareweb/web/view_list.py new file mode 100644 index 0000000..99750c2 --- /dev/null +++ b/examples/todo_wareweb/web/view_list.py @@ -0,0 +1,51 @@ +from todo_wareweb.sitepage import SitePage +from todo_wareweb.db import * +from paste.wareweb import ActionDispatch, public + +class view_list(SitePage): + + dispatch = ActionDispatch() + + def setup(self): + self.options.list = TodoList.get(int(self.fields.id)) + self.options.list_items = list(self.options.list.items) + self.options.title = 'List: %s' % self.options.list.description + + @public + def check(self): + for item in self.options.list.items: + checked = self.fields.get('item_%s' % item.id, False) + if not checked and item.done: + self.message.write('Item %s marked not done' + % item.description) + item.done = False + if checked and not item.done: + self.message.write('Item %s marked done' + % item.description) + item.done = True + self.redirect('view_list', id=self.options.list.id) + + @public + def add(self): + desc = self.fields.description + if not desc: + self.message.write('You must give a description') + else: + TodoItem(todo_list=self.options.list, + description=desc) + self.message.write('Item added') + self.redirect( + 'view_list', id=self.options.list.id) + + @public + def destroy(self): + id = int(self.fields.item_id) + item = TodoItem.get(id) + assert item.todo_list.id == self.options.list.id, ( + "You are trying to delete %s, which does not " + "belong to the list %s" % (item, self.options.list)) + desc = item.description + item.destroySelf() + self.message.write("Item %s removed" % desc) + self.redirect('view_list', id=self.options.list.id) + |
