Author: tracyshaun
Date: 2005-03-23 06:33:02 -0700 (Wed, 23 Mar 2005)
New Revision: 2222
Modified:
WSGIKit/trunk/examples/todo/__init__.py
WSGIKit/trunk/examples/todo/lists.py
WSGIKit/trunk/examples/todo/model.py
WSGIKit/trunk/examples/todo/tests/test_model.py
Log:
* lots of little fixes to the user paths through the todo lists
* removed 'lists' from the manager object
* added some notes in the model file
Modified: WSGIKit/trunk/examples/todo/__init__.py
===================================================================
--- WSGIKit/trunk/examples/todo/__init__.py 2005-03-23 04:42:33 UTC (rev 2221)
+++ WSGIKit/trunk/examples/todo/__init__.py 2005-03-23 13:33:02 UTC (rev 2222)
@@ -6,7 +6,6 @@
def urlparser_hook(environ):
first, rest = wsgilib.path_info_split(environ.get('PATH_INFO', ''))
- print '//// looking for: %s' % `first`
if not servletExistsAt(first, environ):
environ['app.user'] = first
environ['SCRIPT_NAME'] += '/' + first
Modified: WSGIKit/trunk/examples/todo/lists.py
===================================================================
--- WSGIKit/trunk/examples/todo/lists.py 2005-03-23 04:42:33 UTC (rev 2221)
+++ WSGIKit/trunk/examples/todo/lists.py 2005-03-23 13:33:02 UTC (rev 2222)
@@ -1,3 +1,6 @@
+from urllib import pathname2url
+from urllib2 import unquote
+
from Site import StandardPage
import api
@@ -8,33 +11,74 @@
StandardPage.awake(self, trans)
username = self.request().environ()['app.user']
self.user = self.manager.getUser(username)
+ extra = self.request().extraURLPath()
+ if extra and \
+ extra[0] == '/' and \
+ extra[1:] in self.user.lists:
+ self.listname = unquote(extra[1:])
+ self.writeContent = self.writeContentForOneList
+ else:
+ self.listname = None
+ self.writeContent = self.writeContentForAllLists
def sleep(self, trans):
self.user = None
StandardPage.sleep(self, trans)
+
+ def writeContentForOneList(self):
+ todolist = self.user.getList(self.listname)
+ self.write("""
+ <h1>%s</h1>
+ <ul>
+ """ % (todolist.name,))
+ for name, item in todolist:
+ itemclass = item.done and "done" or "undone"
+ self.write("""
+ <li class="%s">%s</li>
+ """ % (itemclass, item.name))
+ self.write("""
+ </ul>
+ <form action="../lists/%s" method="post">
+ <input type="hidden" name="_action_" value="addItem" />
+ <input type="hidden" name="listname" value="%s" />
+ <p>
+ Add new item: <input type="text" name="itemname" value="" />
+ <input type="submit" value="new item" />
+ </p>
+ </form>
+ """ % (todolist.name, todolist.name,))
- def writeContent(self):
- servletPath = './' #TODO: how to get this???
- self.write("""<h1>Select a List</h1>""")
- self.write("""<ul>""")
- for name in self.user.lists:
- self.write("""<li><strong><a href="%(servletPath)s/%(name)s">%(name)s</a></strong> (<a href="%(name)s/delete">obliterate</a>)</li>""" % locals())
- self.write("""</ul>""")
- self.write("""<form action="lists" method="post">""")
- self.write("""<input type="hidden" name="_action_" value="add" />""")
- self.write("""<p>Add new list: <input type="text" name="listname" value="%s" /></p>""" % self.request().fields().get('listname',''))
- self.write("""<input type="submit">""")
- self.write("""</form>""")
-
- self.write("""<p>%s</p>""" % self.htmlEncode(str(self.user)))
+ def writeContentForAllLists(self):
+ self.write("""
+ <h1>Select a List</h1>
+ <ul>
+ """)
+ for listname, todolist in self.user:
+ self.write("""
+ <li><strong><a href="lists/%(listname)s">%(listname)s</a></strong> (<a href="%(listname)s/delete">obliterate</a>)</li>
+ """ % locals())
+ self.write("""
+ </ul>
+ <form action="lists" method="post">
+ <input type="hidden" name="_action_" value="addList" />
+ <p>
+ Add new list: <input type="text" name="listname" value="%s" />
+ <input type="submit" value="add list" />
+ </p>
+ </form>
+ """ % self.request().fields().get('listname',''))
def actions(self):
- return StandardPage.actions(self) + ["add"]
+ return StandardPage.actions(self) + ["addList", "addItem"]
- def add(self):
- """
- your code here
- """
+ def addList(self):
fields = self.request().fields()
newlist = api.List(fields.get('listname', 'new list'), owner=self.user)
+ self.sendRedirectAndEnd("/%s/lists/%s" % (self.user.name, pathname2url(newlist.name)))
+
+ def addItem(self):
+ fields = self.request().fields()
+ currentlist = self.user.getList(fields['listname'])
+ currentlist.addItem(api.Item(fields['itemname']))
self.writeHTML()
+
Modified: WSGIKit/trunk/examples/todo/model.py
===================================================================
--- WSGIKit/trunk/examples/todo/model.py 2005-03-23 04:42:33 UTC (rev 2221)
+++ WSGIKit/trunk/examples/todo/model.py 2005-03-23 13:33:02 UTC (rev 2222)
@@ -1,16 +1,24 @@
"""
-Simple ToDo list model, file-based data
+Simple To-do Lists
+
+TODO:
+ - keep track of the *order* of the items in a list
+ - problems everywhere... why can't you have two items with the same name?
+ - need to use a serial/id number for each object so that we can reference
+ them in the urls by number instead of by name (except for the username
+ which makes sense to have in the URLs...)
+ - __iter__ating through things? does that make sense?
+ - getList(name) vs. list(name) method names...
+ - re-ordering of list items?
+ - I know it's just for the sample app, but pickling the manager
+ in and out of some local directory? Is that okay?
+ - Why in List.__init__ would it be the list's responsibility to add itself
+ to its new owner? Why wouldn't the owner do that?
"""
import os
-"""
-TODO:
- user-based lists
- share-able
-"""
-
class DuplicateUserError(Exception): pass
class DuplicateListError(Exception): pass
@@ -24,6 +32,13 @@
if todolist.name in self.lists:
raise DuplicateListError("A list by the name %s already exists for user %s" % (todolist.name, self.name))
self.lists[todolist.name] = todolist
+ def getList(self, name):
+ return self.lists[name]
+ def __iter__(self):
+ keys = self.lists.keys()
+ keys.sort()
+ for key in keys:
+ yield key, self.lists[key]
class List(object):
@@ -36,6 +51,11 @@
if isinstance(item, str):
item = Item(item)
self.items[item.name] = item
+ def __iter__(self):
+ keys = self.items.keys()
+ keys.sort()
+ for key in keys:
+ yield key, self.items[key]
class Item(object):
@@ -48,19 +68,12 @@
def __init__(self, root):
self.root = os.path.abspath(root)
self.users = {}
- self.lists = {}
def addUser(self, user):
if user.name in self.users:
raise DuplicateUserError("A user by the name of %s already exists." % `user.name`)
self.users[user.name] = user
- def addList(self, todolist):
- self.lists[todolist.name] = todolist
-
- def getList(self, listname):
- return self.lists[listname]
-
def getUser(self, username):
return self.users[username]
@@ -77,15 +90,3 @@
load = classmethod(load)
-
-"""
-
-manager = Manager(root='../')
-
-if 'admin' in manager.users:
- ... do code
-else:
- ... redirect to login
-
-"""
-
Modified: WSGIKit/trunk/examples/todo/tests/test_model.py
===================================================================
--- WSGIKit/trunk/examples/todo/tests/test_model.py 2005-03-23 04:42:33 UTC (rev 2221)
+++ WSGIKit/trunk/examples/todo/tests/test_model.py 2005-03-23 13:33:02 UTC (rev 2222)
@@ -32,7 +32,7 @@
class TestListManager:
def setup_class(cls):
- cls.manager = mgr = Manager(root=os.path.dirname(os.path.join(__file__, 'data')))
+ cls.manager = mgr = Manager(root=os.path.join(os.path.dirname(__file__), 'data'))
print 'cls.manager:', cls.manager.root
user1 = User('user1', 'pass')
mgr.addUser(user1)
@@ -42,21 +42,16 @@
list1.addItem('first step')
list1.addItem('second step')
list1.addItem('third step')
- mgr.addList(list1)
list2 = List('list2', owner=user2)
- mgr.addList(list2)
def test_list_manager(self):
assert os.path.exists(self.manager.root)
assert len(self.manager.users) == 2
- assert len(self.manager.lists) == 2
+ assert len(self.manager.getUser('user1').lists) == 1
def test_list_manipulations(self):
user1 = self.manager.getUser('user1')
assert user1.name == 'user1'
- list1 = self.manager.getList('list1')
- assert list1.name == 'list1'
- assert list1.owner.name == 'user1'
def test_duplicates(self):
from py.test import raises
@@ -64,13 +59,13 @@
user1 = self.manager.getUser('user1')
raises(DuplicateListError, "List('list1', owner=user1)")
-# def test_simple_pickle(self):
-# self.manager.save()
-# duped = Manager.load(self.manager.root)
-# assert 'user1' in duped.users
-# assert 'list2' in duped.lists
-#
-# def teardown_class(cls):
-# statefile = os.path.join(cls.manager.root, 'state.pickle')
-# if os.path.exists:
-# os.remove(statefile)
+ def test_simple_persistence(self):
+ self.manager.save()
+ duped = Manager.load(self.manager.root)
+ assert 'user1' in duped.users
+ assert 'list2' in duped.getUser('user2').lists
+
+ def teardown_class(cls):
+ statefile = os.path.join(cls.manager.root, 'state.pickle')
+ if os.path.exists(statefile):
+ os.remove(statefile)
|