# -*- coding: utf-8 # Copyright (c) 2008, iCamp Consortium # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 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 AccessControl import ClassSecurityInfo, getSecurityManager from Products.PageTemplates.PageTemplateFile import PageTemplateFile from interfaces import ITool, IAffordanceManager, IUsersManager class Tool(Persistent, SimpleItem): """ Affordance class """ implements(ITool) meta_type = "Tool" security = ClassSecurityInfo() security.declareProtected('Manage portal', 'onto_values_tab') onto_values_tab = PageTemplateFile('resources/tool_onto.pt', globals()) onto_values_tab._owner = None manage_options = ( {'label' : 'Onto values', 'action' : 'onto_values_tab'}, ) + SimpleItem.manage_options 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 self._p_changed = True 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 = '
' right += self.getOntoForm(True) right += '
' res = '
' res += left + right 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 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')) self._p_changed = True user = getSecurityManager().getUser() userobj = getUtility(IUsersManager).getUser(str(user)) # 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) val = int(val) userobj.setToolData(self.id, name, val) return "0" def getOntoForm(self, combined=False): """ get onto form """ res = "" am = getUtility(IAffordanceManager) odd = False user = getSecurityManager().getUser() userobj = None if user is None or user.getUserName() == 'Anonymous User': combined = False else: userobj = getUtility(IUsersManager).getUser(str(user)) for a in am.getAffordances(): if not odd: odd = True else: odd = False rtmp = "" if odd: rtmp += '
' else: rtmp += '
' rtmp += '
'+a.title.encode('utf-8')+"
" rtmp += '
' #rtmp += '
'+a.title.encode('utf-8')+'
' af_val = self.getAffordanceData(a.id) uafval = -1 if combined: # get user's evaluation instead rtmp += "base: " + str(af_val) uafval = userobj.getToolData(self.id, a.id) if uafval != -1: af_val = uafval rtmp += '
' rtmp += '' rtmp += '
' rtmp += '
' # field_edit_div rtmp += '
' rtmp += '
' for x in range(0,11): rtmp += '
' rtmp += '
' # scale rtmp += '
' # field_view_div rtmp += '
' res += rtmp return res security.declareProtected('Manage portal', 'saveBaseValues') def saveBaseValues(self, REQUEST): """ save base values """ # 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 REQUEST.RESPONSE.redirect('onto_values_tab') #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