From 4431828c677e6104b7abc62b31a933d2793ff6cf Mon Sep 17 00:00:00 2001 From: ianb Date: Fri, 22 Apr 2005 02:44:48 +0000 Subject: Renamed WSGIKit to Paste --- examples/todo_sql/__init__.py | 6 +++ examples/todo_sql/db.py | 12 +++++ examples/todo_sql/server.conf | 20 +++++++++ examples/todo_sql/sitepage.py | 35 +++++++++++++++ examples/todo_sql/templates/generic_error.pt | 7 +++ examples/todo_sql/templates/index.pt | 23 ++++++++++ examples/todo_sql/templates/standard_template.pt | 43 ++++++++++++++++++ examples/todo_sql/templates/view_list.pt | 57 ++++++++++++++++++++++++ examples/todo_sql/web/__init__.py | 17 +++++++ examples/todo_sql/web/edit_list.py | 29 ++++++++++++ examples/todo_sql/web/index.py | 7 +++ examples/todo_sql/web/static/stylesheet.css | 13 ++++++ examples/todo_sql/web/view_list.py | 48 ++++++++++++++++++++ 13 files changed, 317 insertions(+) create mode 100644 examples/todo_sql/__init__.py create mode 100644 examples/todo_sql/db.py create mode 100644 examples/todo_sql/server.conf create mode 100644 examples/todo_sql/sitepage.py create mode 100644 examples/todo_sql/templates/generic_error.pt create mode 100644 examples/todo_sql/templates/index.pt create mode 100644 examples/todo_sql/templates/standard_template.pt create mode 100644 examples/todo_sql/templates/view_list.pt create mode 100644 examples/todo_sql/web/__init__.py create mode 100644 examples/todo_sql/web/edit_list.py create mode 100644 examples/todo_sql/web/index.py create mode 100644 examples/todo_sql/web/static/stylesheet.css create mode 100644 examples/todo_sql/web/view_list.py (limited to 'examples/todo_sql') 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 @@ + + + + + + + \ 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 @@ + + + + + +

+ There are no lists to display +

+ +
+ Create a new list:
+ + + + +
+ +
+ 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 @@ + + + + +title + + + + + + + +

+ + + + + +
+Home
+ + link
+
+
+ +This is where the notification messages go. + + +
+ +[This page has not customized its "body" slot] + +
+ +
+ + + +
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 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + +
Done?Description
+ + + + [delete]
No items to display
+ + +
+ +
+ +Add item:
+
+ +
+ +
+ + +
+ +
+ 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) -- cgit v1.2.1