shopr

Artifact Content
Login

Artifact 4a900877ea35556b80a12dce58f7c844e1f075b949b2423c6d7f5a1501000cab:


#
# put.py
#	Mixin to implement HTML PUT operations
#
# /rest/l/<listname>/<colname>?dx=<dir>
#	Move item right/left (dx = 1/-1)
#
import urllib
import ilist, delta

# The PUT part of our handling
class PUT_mixin(object):

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

	# PUT handlers
	self.dispatchers.append( ("PUT", self.put_dx) )
	self.dispatchers.append( ("PUT", self.put_colname) )

    # /l/<listname>/<colname>
    #  0     1         2
    # Change column's name
    def put_colname(self, buf):
	pp = self.paths
	if (len(pp) != 3) or (pp[0] != 'l'):
	    return False,None

	# List and column name/index
	lnm = urllib.unquote_plus(pp[1])
	l = ilist.ilists.get(lnm)
	if l is None:
	    return False,None
	colnm = urllib.unquote_plus(pp[2]);
	col = l.getCol(colnm)
	if col is None:
	    return False,None
	colidx = l.cols.index(col)

	# Ok name?
	if not ilist.ok_obname(buf):
	    res = {"code": "Illegal name"}
	    return True,self.send_json(res)

	# Change data structure
	col.name = buf
	l.dump()

	# Tell about the change
	delta.changed("change", col.gen, ["l", lnm, colidx], buf)
	if colidx > 0:
	    col2 = l.cols[colidx-1]
	    col2.gen += 1
	    delta.changed("change", col2.gen,
		["l", lnm, colidx-1, "next"], buf)
	if colidx < (len(l.cols)-1):
	    col2 = l.cols[colidx+1]
	    col2.gen += 1
	    delta.changed("change", col2.gen,
		["l", lnm, colidx+1, "prev"], buf)
	delta.commit()

	# Success
	res = {"code": "OK"}
	return True,self.send_json(res)

    # Item motion?
    # /rest/l/<listname>/<colname>/<item>?dx=<dir>
    #   0   1     2          3       4
    def put_dx(self, buf):
	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

	# Direction
	try:
	    dx = int(self.vals["dx"])
	    if dx not in (-1, 1):
		return False,None
	except:
	    return False,None

	# List name
	lnm = urllib.unquote_plus(pp[2])
	l = ilist.ilists.get(lnm)
	if l is None:
	    return False,None

	# Column name & current index
	colnm = urllib.unquote_plus(pp[3]);
	col = l.getCol(colnm)
	if col is None:
	    return False,None
	colidx = l.cols.index(col)

	# Item name
	nm = urllib.unquote_plus(pp[4])
	if (not nm) or (nm not in col.data):
	    return False,None
	nmidx = col.data.index(nm)

	# Not going to be here
	del col.data[nmidx]
	col.gen += 1
	delta.changed("del", col.gen, ["l", lnm, colidx, nm])

	# Going left?
	if dx < 0:
	    # Move to previously column (it's deleted
	    #  once you swipe it off column 0)
	    if colidx > 0:
		col2idx = colidx-1
		col2 = l.cols[col2idx]
	    else:
		col2idx = col2 = None
	else:
	    # Going right
	    col2idx = colidx+1
	    if colidx == (len(l.cols)-1):
		# Need a new column
		col2name = "%d" % (col2idx,)
		col2 = ilist.ICol(col2name)
		l.cols.append(col2)

		# The list changes with a new column
		l.gen += 1
		delta.changed("add", l.gen, ["l", lnm, col2idx])

		# And the notion of "next" becomes real for
		#  the current column
		col.gen += 1
		delta.changed("change", col.gen,
		    ["l", lnm, colidx, "next"], col2name)

	    else:
		col2 = l.cols[col2idx]

	# If our item moved to a different column, update it
	if col2 is not None:
	    col2.data.append(nm)
	    col2.data.sort()
	    col2.gen += 1
	    delta.changed("add", col2idx, ["l", lnm, col2idx, nm])

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