def getModulePaths(self): """ Return a list of all the top-level modules belonging to this survey. """ session_id = self.session.id if not session_id: return [] profile = extractProfile(self.request.survey, SessionManager.session) module_query = self.module_query( sessionid=session_id, optional_modules=profile.keys() ) module_res = module_query.all() modules_and_profiles = {} for row in module_res: if row.module is not None: if row.module.find('profile') > 0: path = row.module[:3] modules_and_profiles[path] = 'profile' else: modules_and_profiles[row[0]] = '' module_paths = [ m.module for m in module_res if m.module is not None] module_paths = modules_and_profiles.keys() module_paths = sorted(module_paths) self.modules_and_profiles = modules_and_profiles return module_paths
def setupSession(self): """Setup the session for the context survey. This will rebuild the session tree if the profile has changed. Set up the survey session using a given profile. :param survey: the survey to use :type survey: :py:class:`euphorie.content.survey.Survey` :param survey_session: survey session to update :type survey_session: :py:class:`euphorie.client.model.SurveySession` :param dict profile: desired profile :rtype: :py:class:`euphorie.client.model.SurveySession` :return: the update session (this might be a new session) This will rebuild the survey session tree if the profile has changed. """ survey = self.context.aq_parent survey_session = self.session profile = self.getDesiredProfile() if not survey_session.hasTree(): BuildSurveyTree(survey, profile, survey_session) return survey_session current_profile = extractProfile(survey, survey_session) if current_profile == profile and not treeChanges( survey_session, survey, profile): # At this stage, we actually do not need to touch the session. # It is enough that it gets touched when a Risk is edited, or if the # tree gets rebuilt due to changes. # Touch means: the modification timestamp is set. # But we need to make sure the refreshed marker is up to date! survey_session.refresh_survey(survey) return survey_session params = {} # Some values might not be present, depending on the type of survey session _marker = object() for column in survey_session.__table__.columns: if column.key not in ( "id", "account_id", "title", "created", "modified", "zodb_path", ): value = getattr(survey_session, column.key, _marker) if value is not _marker: params[column.key] = value survey_view = api.content.get_view("index_html", survey, self.request) new_session = survey_view.create_survey_session( survey_session.title, survey_session.account, **params) BuildSurveyTree(survey, profile, new_session, survey_session) new_session.copySessionData(survey_session) object_session(survey_session).delete(survey_session) new_session.refresh_survey() return new_session
def do_GET(self): survey = self.survey() answers = extractProfile(survey, self.context) result = [] for (key, answer) in answers.items(): question = survey[key] info = {'id': question.id, 'question': question.question or question.title, 'value': answer} result.append(info) return {'id': self.context.id, 'type': 'profile', 'title': self.context.title, 'profile': result}
def do_GET(self): survey = self.survey() answers = extractProfile(survey, self.context) result = [] for (key, answer) in answers.items(): question = survey[key] info = { 'id': question.id, 'question': question.question or question.title, 'value': answer } result.append(info) return { 'id': self.context.id, 'type': 'profile', 'title': self.context.title, 'profile': result }
def getModulePaths(self): """Return a list of all the top-level modules belonging to this survey.""" session_id = self.session.id if not session_id: return [] profile = extractProfile(self.context.aq_parent, self.context.session) module_query = self.module_query(sessionid=session_id, optional_modules=profile.keys()) module_res = module_query.all() modules_and_profiles = {} for row in module_res: if row.module is not None: if row.module.find("profile") > 0: path = row.module[:3] modules_and_profiles[path] = "profile" else: modules_and_profiles[row[0]] = "" module_paths = [m.module for m in module_res if m.module is not None] module_paths = modules_and_profiles.keys() module_paths = sorted(module_paths) self.modules_and_profiles = modules_and_profiles return module_paths
def getModules(self): """ Return a list of dicts of all the top-level modules and locations belonging to this survey. """ session = Session() session_id = SessionManager.id base_url = "%s/identification" % self.request.survey.absolute_url() profile = extractProfile(self.request.survey, SessionManager.session) module_query = self.module_query( sessionid=session_id, optional_modules=len(profile) and "(%s)" % (','.join( ["'%s'" % k for k in profile.keys()])) or None ) module_res = session.execute(module_query).fetchall() modules_and_profiles = {} for row in module_res: if row[0] is not None: if row[0].find('profile') > 0: path = row[0][:3] modules_and_profiles[path] = 'profile' else: modules_and_profiles[row[0]] = '' module_paths = [ p[0] for p in session.execute(module_query).fetchall() if p[0] is not None] module_paths = modules_and_profiles.keys() module_paths = sorted(module_paths) parent_node = orm.aliased(model.Module) titles = dict(session.query(model.Module.path, model.Module.title) .filter(model.Module.session_id == session_id) .filter(model.Module.path.in_(module_paths))) location_titles = dict(session.query( model.Module.path, parent_node.title ).filter( model.Module.session_id == session_id).filter( model.Module.path.in_(module_paths)).filter( sql.and_( parent_node.session_id == session_id, parent_node.depth < model.Module.depth, model.Module.path.like(parent_node.path + "%") ) )) modules = {} toc = {} title_custom_risks = utils.get_translated_custom_risks_title(self.request) for path in module_paths: number = ".".join(self.slicePath(path)) # top-level module, always include it in the toc if len(path) == 3: title = titles[path] if title == 'title_other_risks': title = title_custom_risks toc[path] = { 'path': path, 'title': title, 'locations': [], 'number': number, } # If this is a profile (aka container for locations), skip # adding to the list of modules if modules_and_profiles[path] == 'profile': continue # sub-module (location) or location container else: if path in location_titles: title = u"{0} - {1}".format(location_titles[path], titles[path]) toc[path[:3]]['locations'].append({ 'path': path, 'title': titles[path], 'number': number, }) else: log.warning( "Status: found a path for a submodule {0} for which " "there's no location title.".format(path)) continue modules[path] = { 'path': path, 'title': title, 'url': '%s/%s' % (base_url, '/'.join(self.slicePath(path))), 'todo': 0, 'ok': 0, 'postponed': 0, 'risk_with_measures': 0, 'risk_without_measures': 0, 'number': number, } self.tocdata = toc return modules
def extractProfile(self, *a, **kw): return extractProfile(*a, **kw)
def extractProfile(self, *a, **kw): from euphorie.client.profile import extractProfile return extractProfile(*a, **kw)
def current_profile(self): return extractProfile(self.context.aq_parent, self.session)