from OFS.Folder import Folder
from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
from Globals import Persistent, Acquisition
from utils import cookString
from zope.interface import implements
import zope
from zope.component import getUtility
from ZPublisher.HTTPRequest import FileUpload
from interfaces import IToolsManager, ITool, IAffordanceManager
from Tool import Tool
class ToolsManager(Persistent, Folder, Acquisition.Implicit):
implements(IToolsManager)
meta_type = 'ToolsManager'
id = 'tools'
def getTools(self):
return self.objectValues('Tool')
def getToolIds(self):
return self.objectIds('Tool')
def listTools(self):
""" list tools for "Tools" tab.
{'toolid': {'title':'Yahoo!', 'img'} }
"""
res = []
for t in self.getTools():
if t.getHasThumbnail():
turl = 'tools/'+t.id+'/getThumbnail'
else:
turl = '++resource++styles/tool_default.png'
res.append({'id': t.id.encode('utf-8'), 'title': t.title.encode('utf-8'), 'img': turl})
return str(res).replace("'", '"')
def newToolForm(self):
""" new Tool adding form """
# 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): # let's assume bytes are always upload forms
ltmp += ''
else:
raise 'Panic! Unknown field type', f
#ltmp += '
'
ltmp += '
'
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 += '
'
rtmp += '
'+a.title.encode('utf-8')+'
'
rtmp += ''
rtmp += '
'
right += rtmp
right += '
'
res = ''
return res
def createNewTool(self, REQUEST):
""" create a new tool. returns an ID of new tool just created """
#raise 'we got post!'
sch = zope.schema.getFields(ITool)
t = Tool('tmp_id', 'tmp title')
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(t, val.read())
t.setHasThumbnail(True)
else:
print "not setting thumbnail"
else:
f.set(t, 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
t.setAffordanceData(name, val)
t.id = cookString(t.title).encode('utf-8')
print "id:", t.id, type(t.id)
self._setObject(t.id, t)
return t.id