from Products.Five.site.localsite import FiveSite as BaseSite from OFS.Folder import Folder from zope.interface import implements, Interface from Globals import Persistent, InitializeClass from zope.schema.fieldproperty import FieldProperty from zope.component import getUtility from interfaces import IFolio, IAffordanceManager, IToolsManager, IUsersManager class iFolio(Persistent, Folder, BaseSite): implements(IFolio) title = FieldProperty(IFolio['title']) def __init__(self, *data, **kw): from Products.Five.component import enableSite enableSite(self) #security.declarePrivate('getSkinsFolderName') def getSkinsFolderName(self): return "portal_skins" # # getter methods # def getAffordances(self, REQUEST): """ get a list of affordances """ am = getUtility(IAffordanceManager) l = am.getAffordances() tmp = [] for a in l: tmp.append([a.id, str(a.title.encode('utf-8'))]) return str(tmp).replace("'", '"') def getPerspectiveData(self, REQUEST): """ returns perspective data. """ v = {} uname = REQUEST.get('username') pers = REQUEST.get('perspective', 'default') am = getUtility(IAffordanceManager) userobj = getUtility(IUsersManager).getUser(uname) if userobj is None: return v am.getAffordances() for a in am.getAffordances(): v[a.id] = userobj.getAffordanceData(pers, a.id) return str(v).replace("'", '"') def getToolsData(self, REQUEST): """ returns perspective data: tool aff1 aff2 aff3 aff4 toolid1 0 2 1 2 1 2 str({'toolids': ['toolid1', 'toolid2'], 'toolid1': { 'x_title':'Flickr', 'x_url': 'http://www.flickr.com', 'Affordance': 8, 'blah': 6, 'ff': 7}, 'toolid2': { 'x_title':'Google', 'x_url': 'http://www.google.com', 'Affordance': 6, 'blah': 8, 'ff': 1} } ).replace("'", '"') """ tm = getUtility(IToolsManager) tools = tm.getTools() affordances = getUtility(IAffordanceManager).getAffordances() res = {} res['toolids'] = tm.getToolIds() for t in tools: tmp = {} tmp['x_title'] = t.title.encode('utf-8') tmp['x_url'] = t.url.encode('utf-8') for a in affordances: tmp[a.id] = t.getAffordanceData(a.id) res[t.id] = tmp return str(res).replace("'", '"') # # Data saving methods # def saveUserPerspective(self, REQUEST): """ save user's perspective """ print "saving user's perspective:", REQUEST.get('perspective') pers = REQUEST.get('perspective') uname = REQUEST.get('uname') if not pers or not uname: raise 'no perspective or username given' um = getUtility(IUsersManager) um.getUser(uname).savePerspective(REQUEST) return 0 # # calc # def calculate(self, REQUEST): """ calculate tools positions """ #sliders = {'searching': 10, 'timemanage': 10} sliders = REQUEST tools = getUtility(IToolsManager).getTools() affordances = getUtility(IAffordanceManager).getAffordances() affordances = [ a.id for a in affordances ] res = {} import random for t in tools: res[t.id] = [random.randint(-100,100),random.randint(-100,100)] #return str(res).replace("'", '"') from calculator import Calculator calc = Calculator(sliders, tools, affordances, res) calc._prepare() calc._cache_ontodistance() calc._finalize() tmp = calc.calculate() res = {} for r in tmp.keys(): res[r] = [int(tmp.get(r)[0]), int(tmp.get(r)[1])] return str(res).replace("'", '"') # # registration # def register_handler(self, REQUEST): """ handler user's registration """ uname = REQUEST.get('username') fname = REQUEST.get('firstname') lname = REQUEST.get('lastname') email = REQUEST.get('email') if not uname or not fname or not lname or not email: return '{"errno":2}' # all ok from utils import _genPwd exu = self.acl_users.getUserById(uname) if exu is not None: return '{"errno":3}' pwd = _genPwd() self.acl_users._doAddUser(uname, pwd, [], []) self.SecureMailHost.secureSend('Your password:'+pwd, email, 'iFolio@htk.tlu.ee', 'iFolio registration', charset='utf-8') return '{"errno":0}' InitializeClass(iFolio) def added(obj, event): # add portal_skins from OFS.Folder import manage_addFolder # add folders sm = obj.getSiteManager() from AffordanceManager import AffordanceManager from UsersManager import UsersManager from ToolsManager import ToolsManager # add affordances folder am = AffordanceManager() obj._setObject(am.id, am) sm.registerUtility(getattr(obj, am.id), IAffordanceManager) # add tools folder tm = ToolsManager() obj._setObject(tm.id, tm) sm.registerUtility(getattr(obj, tm.id), IToolsManager) # add users folder um = UsersManager() obj._setObject(um.id, um) sm.registerUtility(getattr(obj, um.id), IUsersManager)