from Globals import Persistent
from OFS.SimpleItem import SimpleItem
from zope.interface import implements
from zope.component import getUtility
import zope
from ZPublisher.HTTPRequest import FileUpload
from interfaces import ITool, IAffordanceManager
class Tool(Persistent, SimpleItem):
""" Affordance class """
implements(ITool)
meta_type = "Tool"
def __init__(self, id, title):
self.id = id
self.title = title
self._affData = {}
self.hasThumbnail = False
def getAffordanceData(self, id):
""" get tool's affordance value. id is the affordance's id"""
return self._affData.get(id, 0)
def setAffordanceData(self, afid, val):
vv = int(val)
if vv > 10:
raise 'Too big value!'
self._affData[afid] = vv
def setHasThumbnail(self, val):
self.hasThumbnail = val
def getHasThumbnail(self):
return getattr(self, 'hasThumbnail', False)
def getDetailedView(self):
""" returns a detailed view of Tool. HTML """
# general parameters
left = '
'
sch = zope.schema.getFields(ITool)
# l.order
#
lres = []
for x in sch.keys():
f = sch.get(x)
ltmp = ""
ltmp += '
'
ltmp += '
'+f.title.encode('utf-8')+"
"
ltmp += '
'
ltmp += '
'+f.description.encode('utf-8')+"
"
if 'TextLine' in str(f):
ltmp += '
'
elif 'Text' in str(f):
ltmp += ''
elif 'Bytes' in str(f):
if self.getHasThumbnail():
ltmp += ''
else:
ltmp += ''
ltmp += ''
else:
raise 'Panic! Unknown field type'
ltmp += '
' # class="field_edit_div"
ltmp += '
'
if 'Bytes' in str(f):
if self.getHasThumbnail():
ltmp += ''
else:
ltmp += ''
else:
fval = f.get(self)
if fval is not None:
ltmp += fval.encode('utf-8')
ltmp += '
' # class="field_view_div"
ltmp += '
' # class="field"
lres.append([f.order, ltmp])
lres.sort()
lres = [ x[1] for x in lres]
left += ''.join(lres)
left += "
"
left += '
'
# affordances
right = '
'
am = getUtility(IAffordanceManager)
for a in am.getAffordances():
rtmp = ""
rtmp += '
'
res = ''
return res
def getThumbnail(self):
""" get Image """
return self.img
def saveTool(self, REQUEST):
""" save tool """
sch = zope.schema.getFields(ITool)
for x in sch.keys():
f = sch.get(x)
name = f.getName()
val = REQUEST.get(name, None)
if val is None:
raise 'cannot find', name
print name, val
if isinstance(val, FileUpload):
if val.filename:
print "setting thumbnail"
f.set(self, val.read())
self.setHasThumbnail(True)
else:
print "not setting thumbnail"
else:
f.set(self, val.decode('utf-8'))
# affordances are prefixed with 'af'
am = getUtility(IAffordanceManager)
for a in am.getAffordances():
name = a.id
val = REQUEST.get('af'+name, None)
if val is None:
raise 'cannot find affordance value', name
self.setAffordanceData(name, val)
return 0
#def getAffordanceData(self, uname, id):
# """ get tool's affordance value. uname is Username. id is the affordance's id"""
# ts = self._affData.get(uname, {})
# return ts.get(id, 0)
#def saveAffordanceData(self, req):
# """ save user's affordance ecaluation for tool """
# am = getUtility(IAffordanceManager)
# uname = req.get('username') # XXX need to save the thing for authenticated user. Some checker method that user id selected from the list and the user id of the currently authenticated user match should determin if we show the save button or not. Additional check inside the runction should also be useful. Also meke the method protected to some permission of the authenticated user.
# workset = self._affData.get(uname, {})
# for a in am.getAffordances():
# workset[a.id] = # XXX get data from request
# self._affData[pers] = workset
# self._d_changed = True
# return 0