# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import BooleanField, ImageField, StringField, TextField from Products.Archetypes.public import BooleanWidget, ImageWidget, StringWidget, SelectionWidget, RichWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME from BaseTest import BaseTask schema = BaseTask.schema.copy() + Schema(( TextField('textbody', accessor='getTextBody', #primary=True, validators = ('isTidyHtmlWithCleanup',), default_output_type = 'text/x-html-safe', allowable_content_types = ('text/html',), widget = RichWidget( label="Text area", description="", label_msgid='label_text_body', description_msgid='description_text_body', visible={'view':'visible','edit':'visible'}, i18n_domain="psyhvel", rows = 10 ), ), StringField('tableWidth', accessor = 'getTableWidth', vocabulary = 'getNumbers', default = '5', widget = SelectionWidget( format = 'select', label = 'Table width', label_msgid='label_table_width', description = 'How many squers are in table by horizontally', description_msgid = 'desc_table_width', i18n_domain="psyhvel", visible = {'view': 'invisible', 'edit':'visible'}, ), ), StringField('tableHeight', accessor = 'getTableHeight', vocabulary = 'getNumbers', default = '5', widget = SelectionWidget( format = 'select', label = 'Table height', label_msgid='label_table_height', description = 'How many squers are in table by vertically', description_msgid = 'desc_table_height', i18n_domain="psyhvel", visible = {'view': 'invisible', 'edit':'visible'}, ), ), StringField('taskType', required=1, accessor="getTaskType", default='hedgehog', vocabulary=['hedgehog','dog'], widget=SelectionWidget( format="radio", label="Task type", label_msgid='label_task_type', description="Select actor type, hedgehog or dog, test properties like number of steps in different landscape are depending on that", description_msgid='desc_hedgehogtask_type', i18n_domain="psyhvel"), ), BooleanField('askNumberOfSteps', accessor = 'getAskNumberOfSteps', default = True, widget = BooleanWidget( label = 'Ask number of steps', label_msgid='label_ask_number_of_steps', description = 'Check if you want to get number of steps answered by user', description_msgid = 'desc_ask_number_of_steps', i18n_domain="psyhvel", visible = {'view': 'invisible', 'edit':'visible'}, ), ), )) schema.delField('multipleChoice') schema.delField('answerRequired') schema.delField('time') #schema.moveField('time', pos='bottom') class HedgehogFunctions: """ Functions that are needed to run hedgehog task in test view""" def getAllData(self): """returns all data to js as array""" return self.getCurrentSheet().getAllData() def moveHedgehog(self,row,col,track): """hedgehog moves""" self.getCurrentSheet().moveHedgehog(self,row,col,track) def getLandscape(self): """returns lanscape""" return self.getCurrentSheet().getLandscape() def saveMyHedgehogTask(self, REQUEST): """sets answer to db""" answers = {} current = self.getCurrentSheet() current_id = current.id answers['total'] = REQUEST.get('answer_hedgehogmoves_total') answers['empty'] = REQUEST.get('answer_hedgehogmoves_empty') answers['hill'] = REQUEST.get('answer_hedgehogmoves_hill') answers['river'] = REQUEST.get('answer_hedgehogmoves_river') answers['answer'] = REQUEST.get('answer_hedgehogmoves_count') answers['moves'] = REQUEST.get('answer_hedgehogmoves_array') if current.meta_type in ['HedgehogTask',]: if REQUEST.get('answer_hedgehogmoves_count'): self.setDataToDatabase(current_id,answers) print "datasse l2hevad "+str(current_id)+" ja "+str(answers) self.setCurrentPage() else: return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'?error=true') self.submitToTheNextPage(REQUEST) class HedgehogTask(BaseTask): """ Hedgehog Task""" meta_type = "HedgehogTask" archetype_name = "Hedgehog Task" global_allow = 0 exclude_from_nav = True filter_content_types = True allowed_content_types = () security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/hedgehog_task', 'permissions': ('View',), }, { 'id':'edit', 'name':'Configuration', 'action':'string:${object_url}/base_edit', 'permissions': ('Modify portal content',), }, { 'id':'create', 'name':'Create', 'action':'string:${object_url}/hedgehog_task_edit', 'permissions': ('Modify portal content',), }, { 'id':'metadata', 'name':'Info', 'action':'string:${object_url}/base_metadata', 'permissions': ('Modify portal content',), } ) def __init__(self, id): self.id = id self.landscape = [] self.moves = [] self.rightChoice = [0,0] def createTable(self): """returns table""" table = [] rows = int(self.getTableHeight()) cols = int(self.getTableWidth()) for row in range(0,rows): table.append([]) for col in range(0,cols): table[row].append("empty") return table def getLandscape(self): """returns table""" if not hasattr(self, 'landscape'): self.landscape = [] if self.landscape == []: self.landscape = self.createTable() if int(self.getTableHeight()) != len(self.landscape) and len(self.landscape) > 0: if int(self.getTableWidth()) != len(self.landscape[0]): self.landscape = self.createTable() self.moves = self.createTable() return self.landscape def getMoves(self): """returns table""" if not hasattr(self, 'moves'): self.moves = [] if self.moves == []: self.moves = self.createTable() return self.moves def getAllData(self): """returns all data to js as array""" actor = self.getTaskType() friend="dog" if actor==friend: friend = "hedgehog" return [self.getLandscape(), self.getMoves(), actor, friend] def modifyLandscape(self,row,col,tool): """modify landscape""" self.landscape[int(row)][int(col)] = tool self.p_changed = 1 def moveHedgehog(self,row,col,track): """modify landscape""" self.moves[int(row)][int(col)] = track self.p_changed = 1 def getNumbers(self): """return numbers for table length and width""" nrs = [] for nr in range(4,11, 1): nrs.append(nr) return nrs def saveMyHedgehogTask(self): """saves table, like they saved Rayan (not Angelos)""" pass def objectSpecData(self, key, val): """objects spec data""" if key == "landscape": import pickle self.landscape = pickle.loads(val) self.p_changed = 1 def getObjectsXML(self, files): """special data""" import pickle lndsc = pickle.dumps(self.getLandscape()) xml = self.getBaseXML() xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '' return [xml, files] registerType(HedgehogTask, PROJECT_NAME)