diff options
| author | ianb <devnull@localhost> | 2005-04-22 02:44:48 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2005-04-22 02:44:48 +0000 |
| commit | 4431828c677e6104b7abc62b31a933d2793ff6cf (patch) | |
| tree | 887f7b87f167a988566657304331ba8bb15ed4d6 /examples/todo_sql | |
| download | paste-4431828c677e6104b7abc62b31a933d2793ff6cf.tar.gz | |
Renamed WSGIKit to Paste
Diffstat (limited to 'examples/todo_sql')
| -rw-r--r-- | examples/todo_sql/__init__.py | 6 | ||||
| -rw-r--r-- | examples/todo_sql/db.py | 12 | ||||
| -rw-r--r-- | examples/todo_sql/server.conf | 20 | ||||
| -rw-r--r-- | examples/todo_sql/sitepage.py | 35 | ||||
| -rw-r--r-- | examples/todo_sql/templates/generic_error.pt | 7 | ||||
| -rw-r--r-- | examples/todo_sql/templates/index.pt | 23 | ||||
| -rw-r--r-- | examples/todo_sql/templates/standard_template.pt | 43 | ||||
| -rw-r--r-- | examples/todo_sql/templates/view_list.pt | 57 | ||||
| -rw-r--r-- | examples/todo_sql/web/__init__.py | 17 | ||||
| -rw-r--r-- | examples/todo_sql/web/edit_list.py | 29 | ||||
| -rw-r--r-- | examples/todo_sql/web/index.py | 7 | ||||
| -rw-r--r-- | examples/todo_sql/web/static/stylesheet.css | 13 | ||||
| -rw-r--r-- | examples/todo_sql/web/view_list.py | 48 |
13 files changed, 317 insertions, 0 deletions
diff --git a/examples/todo_sql/__init__.py b/examples/todo_sql/__init__.py new file mode 100644 index 0000000..39d0c09 --- /dev/null +++ b/examples/todo_sql/__init__.py @@ -0,0 +1,6 @@ +from wsgikit.util.thirdparty import add_package + +add_package('ZopePageTemplates') +add_package('Component') +add_package('ZPTKit') +add_package('sqlobject') diff --git a/examples/todo_sql/db.py b/examples/todo_sql/db.py new file mode 100644 index 0000000..21f36fb --- /dev/null +++ b/examples/todo_sql/db.py @@ -0,0 +1,12 @@ +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) diff --git a/examples/todo_sql/server.conf b/examples/todo_sql/server.conf new file mode 100644 index 0000000..0b38b2a --- /dev/null +++ b/examples/todo_sql/server.conf @@ -0,0 +1,20 @@ +# -*- python -*- Note: this file is in Python syntax + +import os +database = 'sqlite:%s/data.db' % os.path.dirname(__file__) + +app_template = 'webkit_zpt' +app_name = 'todo_sql' +webkit_dir = '/var/www/todo_sql/web' +sys_path = ['/var/www'] + +## 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 diff --git a/examples/todo_sql/sitepage.py b/examples/todo_sql/sitepage.py new file mode 100644 index 0000000..9c88685 --- /dev/null +++ b/examples/todo_sql/sitepage.py @@ -0,0 +1,35 @@ +import os +from Component import CPage +from Component.notify import NotifyComponent +from ZPTKit import ZPTComponent +from todo_sql.db import * + +class SitePage(CPage): + + components = [ + ZPTComponent([os.path.join(os.path.dirname(__file__), + 'templates')]), + NotifyComponent()] + + def title(self): + return self.options.get('title', CPage.title(self)) + + def awake(self, trans): + CPage.awake(self, trans) + self.baseURL = self.request().environ()['todo_sql.base_url'] + self.baseStaticURL = self.baseURL + '/static' + self.options.lists = list(TodoList.select()) + self.setup() + + def setup(self): + pass + + def sleep(self, trans): + self.teardown() + CPage.sleep(self, trans) + + def teardown(self): + pass + + def writeHTML(self): + self.writeTemplate() diff --git a/examples/todo_sql/templates/generic_error.pt b/examples/todo_sql/templates/generic_error.pt new file mode 100644 index 0000000..7b32820 --- /dev/null +++ b/examples/todo_sql/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_sql/templates/index.pt b/examples/todo_sql/templates/index.pt new file mode 100644 index 0000000..8db6839 --- /dev/null +++ b/examples/todo_sql/templates/index.pt @@ -0,0 +1,23 @@ +<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.baseURL, 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> +<tal:block replace="servlet/request/environ" /> +</metal:body> +</html> diff --git a/examples/todo_sql/templates/standard_template.pt b/examples/todo_sql/templates/standard_template.pt new file mode 100644 index 0000000..e3ff01d --- /dev/null +++ b/examples/todo_sql/templates/standard_template.pt @@ -0,0 +1,43 @@ +<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.baseStaticURL + '/stylesheet.css'"> + +<metal:slot define-slot="extra_head"></metal:slot> + +</head> +<body> + +<h1 id="header"><tal:mark replace="servlet/title"/></h1> + +<table width="100%"> +<tr> +<td id="sidenav" width="20%"> +<a tal:attributes="href servlet/baseURL">Home</a><br> +<tal:for repeat="list options/lists"> + <a tal:attributes="href python: '%s/view_list?id=%s' % + (servlet.baseURL, list.id)" + tal:content="list/description">link</a><br> +</tal:for> +</td> + +<td> +<span tal:replace="structure servlet/messageText"> +This is where the notification messages go. +</span> + +<div id="content"> +<metal:tpl define-slot="body"> +[This page has not customized its "body" slot] +</metal:tpl> +</div> + +</td></tr></table> + +</body> +</html> +</metal:tpl> diff --git a/examples/todo_sql/templates/view_list.pt b/examples/todo_sql/templates/view_list.pt new file mode 100644 index 0000000..2a85b13 --- /dev/null +++ b/examples/todo_sql/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&item_id=%s' + % 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_sql/web/__init__.py b/examples/todo_sql/web/__init__.py new file mode 100644 index 0000000..835887d --- /dev/null +++ b/examples/todo_sql/web/__init__.py @@ -0,0 +1,17 @@ +import os +from wsgikit import wsgilib +from wsgikit.util.thirdparty import add_package +add_package('sqlobject') +import sqlobject + +sql_set = False + +def urlparser_hook(environ): + global sql_set + if not environ.has_key('todo_sql.base_url'): + environ['todo_sql.base_url'] = environ['SCRIPT_NAME'] + if not sql_set: + sql_set = True + db_uri = environ['wsgikit.config']['database'] + sqlobject.sqlhub.processConnection = sqlobject.connectionForURI( + db_uri) diff --git a/examples/todo_sql/web/edit_list.py b/examples/todo_sql/web/edit_list.py new file mode 100644 index 0000000..0bd3372 --- /dev/null +++ b/examples/todo_sql/web/edit_list.py @@ -0,0 +1,29 @@ +from todo_sql.sitepage import SitePage +from todo_sql.db import * + +class edit_list(SitePage): + + def setup(self): + self.list_id = self.request().field('id') + if self.list_id != 'new': + self.list = TodoList.get(int(self.list_id)) + self.options.title = 'Edit list ' + + def actions(self): + return ['save', 'destroy'] + + def save(self): + desc = self.request().field('description') + if self.list_id == 'new': + self.list = TodoList(description=desc) + self.message('List created') + else: + self.list.description = desc + self.message('List updated') + self.sendRedirectAndEnd('./view_list?id=%s' % self.list.id) + + def destroy(self): + desc = self.list.description + self.list.destroySelf() + self.message('List %s deleted' % desc) + self.sendRedirectAndEnd('./') diff --git a/examples/todo_sql/web/index.py b/examples/todo_sql/web/index.py new file mode 100644 index 0000000..55b1f98 --- /dev/null +++ b/examples/todo_sql/web/index.py @@ -0,0 +1,7 @@ +from todo_sql.sitepage import SitePage +from todo_sql.db import * + +class index(SitePage): + + def setup(self): + self.options.title = 'List of Lists' diff --git a/examples/todo_sql/web/static/stylesheet.css b/examples/todo_sql/web/static/stylesheet.css new file mode 100644 index 0000000..d70e9ec --- /dev/null +++ b/examples/todo_sql/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_sql/web/view_list.py b/examples/todo_sql/web/view_list.py new file mode 100644 index 0000000..a7105d6 --- /dev/null +++ b/examples/todo_sql/web/view_list.py @@ -0,0 +1,48 @@ +from todo_sql.sitepage import SitePage +from todo_sql.db import * + +class view_list(SitePage): + + def setup(self): + self.options.list = TodoList.get(int(self.request().field('id'))) + self.options.list_items = list(self.options.list.items) + self.options.title = 'List: %s' % self.options.list.description + + def actions(self): + return ['check', 'add', 'destroy'] + + def check(self): + field = self.request().field + for item in self.options.list.items: + checked = field('item_%s' % item.id, False) + if not checked and item.done: + self.message('Item %s marked not done' + % item.description) + item.done = False + if checked and not item.done: + self.message('Item %s marked done' + % item.description) + item.done = True + self.sendRedirectAndEnd( + './view_list?id=%s' % self.options.list.id) + + def add(self): + desc = self.request().field('description') + if not desc: + self.message('You must give a description') + else: + TodoItem(todo_list=self.options.list, + description=desc) + self.message('Item added') + self.sendRedirectAndEnd( + './view_list?id=%s' % self.options.list.id) + + def destroy(self): + id = int(self.request.field('item_id')) + item = TodoItem.get(id) + assert item.todo_list.id == self.options.list.id + desc = description + item.destroySelf() + self.message("Item %s removed" % desc) + self.sendRedirectAndEnd( + './view_list?id=%s' % self.options.list.id) |
