import random import copy import simplejson from Products.Five import BrowserView as BaseView from Globals import InitializeClass from zope.component import getUtility from Products.mFolio.interfaces import ImFolio, IAffordanceManager, IToolsManager, IUsersManager from calculator import Calculator as BC class Calculators(BaseView): """ calculators """ def __init__(self, context, request): self.request = request self.context = context def calculateGoogle(self): """ calculate google view """ self.request.set('areasize', 1100) res = simplejson.loads(self.calculateTools()) r = u'' r += '' r += '' r += """ %s 40 -40 40 -40 """ % ('http://www.tlu.ee/~vahur/mfolio_back2.png') #""" % (self.absolute_url() + '/++resource++kml/back.png') basex = float(1) basey = float(1) mm = 500 tm = getUtility(IToolsManager, context=self.context) for tid, coords in res.items(): r += '' r += ''+tm.getToolTitleById(tid)+'' r += ''+self.context.absolute_url()+'/++resource++kml/style1.xml#placemarkDecoration1' r += '' r += '' x = coords[0] y = coords[1] r += str(basex+(x/mm))+","+str(basey+(y/mm)) r += '' r += '' r += '' r += '' r += '' return simplejson.dumps(r) def calculateTools(self): """ calculate tools positions """ #sliders = {'searching': 10, 'timemanage': 10} sliders = self.request tools = getUtility(IToolsManager, context=self.context).getTools() affordances = getUtility(IAffordanceManager, context=self.context).getAffordances() affordances = [ a.id for a in affordances ] res = {} areasize = sliders.get('areasize', 200) area_start = sliders.get('areastart', 0) make_new_seed = True if hasattr(self.context, 'seeds'): if self.context.seeds.has_key(areasize): make_new_seed = False res = copy.copy(self.context.seeds[areasize]) else: self.context.seeds = {} if make_new_seed: for t in tools: res[t.id] = [random.randint(area_start, area_start+areasize),random.randint(area_start, area_start+areasize)] self.context.seeds[areasize] = res #return str(res).replace("'", '"') calc = BC(sliders, tools, affordances, res, areasize/2) 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 simplejson.dumps(res) def calculateUsers(self): """ calculate users positions """ sliders = self.request users = getUtility(IUsersManager, context=self.context)._getAllUsers() affordances = getUtility(IAffordanceManager, context=self.context).getAffordances() affordances = [ a.id for a in affordances ] res = {} for u in users: res[u.id] = [random.randint(-100,100),random.randint(-100,100)] #return str(res).replace("'", '"') calc = BC(sliders, users, affordances, res) calc.stash = 'default' 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 simplejson.dumps(res) def calculateToolsKML(self, perspective): sliders = perspective tools = getUtility(IToolsManager, context=self.context).getTools() affordances = getUtility(IAffordanceManager, context=self.context).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("'", '"') calc = BC(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 res InitializeClass(Calculators)