summaryrefslogtreecommitdiff
path: root/examples/todo_sql/web
diff options
context:
space:
mode:
Diffstat (limited to 'examples/todo_sql/web')
-rw-r--r--examples/todo_sql/web/__init__.py17
-rw-r--r--examples/todo_sql/web/edit_list.py29
-rw-r--r--examples/todo_sql/web/index.py7
-rw-r--r--examples/todo_sql/web/static/stylesheet.css13
-rw-r--r--examples/todo_sql/web/view_list.py48
5 files changed, 114 insertions, 0 deletions
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)