shopr

Artifact Content
Login

Artifact 464eeeb4bff64a8c3ba0e88e1cb5e4352091cc5c77b5f9c05d3470ed3bdfe93d:


#
# post.py
#	Mixin to implement HTML POST operations
#
# /l/<listname>
#	Create a new list
#
import urllib
import ilist, delta

# The POST part of our handling
class POST_mixin(object):

    # Configure our WPlayer GET treatment
    def __init__(self):

	# POST handlers
	self.dispatchers.append( ("POST", self.post_newlist) )
	self.dispatchers.append( ("POST", self.post_newitem) )

    # A new list requested?
    def post_newlist(self, buf):
	# /rest/l/LISTNAME
	pp = self.paths
	if (len(pp) != 3) or \
		(pp[0] != 'rest') or (pp[1] != 'l') or \
		(not pp[1]):
	    return False,None
	nm = urllib.unquote_plus(pp[2])
	if not ilist.ok_obname(nm):
	    resp = {"code": "Bad list name"}
	    return True,self.send_json(resp)

	# Can't already exist
	if nm in ilist.ilists:
	    resp = {"code": "List already exists"}
	    return True,self.send_json(resp)
	l = ilist.IList(self.user, nm)
	l.dump()

	# We're in business
	resp = {"code": "OK"}
	return True,self.send_json(resp)

    # A new item requested?
    def post_newitem(self, buf):
	# /rest/l/LISTNAME/COLUMN/ITEM
	#   0   1    2       3     4
	pp = self.paths
	if (len(pp) != 5) or \
		(pp[0] != 'rest') or (pp[1] != 'l') or \
		(not pp[2]) or (not pp[3]) or (not pp[4]):
	    return False,None

	# Verify list name
	lnm = urllib.unquote_plus(pp[2])
	l = ilist.ilists.get(lnm)
	if l is None:
	    resp = {"code": "No such list"}
	    return True,self.send_json(resp)

	# Column name
	colnm = urllib.unquote_plus(pp[3])
	col = l.getCol(colnm)
	if col is None:
	    resp = {"code": "No such column"}
	    return True,self.send_json(resp)
	colidx = l.cols.index(col)

	# Item name.  Can't already exist.
	nm = urllib.unquote_plus(pp[4])
	if nm in col.data:
	    resp = {"code": "Item already exists"}
	    return True,self.send_json(resp)
	if not ilist.ok_obname(nm):
	    resp = {"code": "Bad item name"}
	    return True,self.send_json(resp)

	# Ok, add to items and re-sort
	col.data.append(nm)
	col.data.sort()

	# Sync to filesystem
	l.dump()

	# The display of the item--for this user and all others--comes
	#  from the delta update
	col.gen += 1
	delta.changed("add", col.gen, ("l", lnm, colidx, nm))
	delta.commit()

	# We're in business
	resp = {"code": "OK"}
	return True,self.send_json(resp)