def __call__(self): session = Session() custom_risks = session.query(SurveyTreeItem).filter( and_( SurveyTreeItem.zodb_path.notlike("custom-risks%"), SurveyTreeItem.zodb_path.like("%custom-risks%"), SurveyTreeItem.type == "risk", ) ) count = 0 for risk in custom_risks: i = 1 num = risk.zodb_path.split("/")[-1] while i < 100: new_zodb_path = "custom-risks/{}".format(num) conflict = session.query(SurveyTreeItem).filter( and_( SurveyTreeItem.session_id == risk.session_id, SurveyTreeItem.zodb_path == new_zodb_path, ) ) if conflict.count(): num = num = str(int(num) + i) else: risk.zodb_path = new_zodb_path count += 1 break i += 1 return "Fixed %d omega risks" % count
def update_tool_info(surveygroup): survey = None if surveygroup.published: survey = surveygroup.get(surveygroup.published) creation_date = survey.created() if survey else surveygroup.created() if not isinstance(creation_date, datetime): try: creation_date = creation_date.asdatetime() except AttributeError: log.warn("Cannot handle creation date {}".format(creation_date)) creation_date = None # cut out the part of the ZODB path that's used in postgresql # (country / sector / tool) zodb_path = "/".join(surveygroup.getPhysicalPath()[-3:]) published_date = None if surveygroup.published and survey: if isinstance(survey.published, datetime): published_date = survey.published elif isinstance(survey.published, tuple): published_date = survey.published[2] EuphorieSession.query(Survey).filter( Survey.zodb_path == zodb_path).delete() EuphorieSession.add( Survey( zodb_path=zodb_path, language=survey.Language() if survey else surveygroup.Language(), published=bool(surveygroup.published), published_date=published_date, creation_date=creation_date, ))
def __call__(self): site = api.portal.get() self.client = getattr(site, "client") query = ( Session.query(ActionPlan, SurveyTreeItem, SurveySession) .filter( sql.and_( ActionPlan.plan_type == "in_place_standard", ActionPlan.solution_id == None, # noqa: E711 ) ) .join(SurveyTreeItem, ActionPlan.risk_id == SurveyTreeItem.id) .join(SurveySession, SurveyTreeItem.session_id == SurveySession.id) .order_by(SurveySession.zodb_path) ) count = 0 ret = "We have a total of %d measures\n" % query.count() for action_plan, risk, session in query.all(): tool = self.get_tool(session.zodb_path) risk = self.get_risk(tool, risk.zodb_path) for solution in risk._solutions: if ( solution.action and action_plan.action and solution.action.strip() == action_plan.action.strip() ): action_plan.solution_id = str(solution.id) count += 1 break ret += "Updated %d measures" % count return ret
def get_sessions_query( self, context=None, searchable_text=None, order_by=False, include_archived=False, filter_by_account=True, filter_by_group=False, table=None, ): """Method to return a query that looks for sessions :param context: limits the sessions under this context :param searchable_text: filters on survey title :param order_by: is by default on survey title but you can pass None to disable ordering or a SQLAlchemy expression :param include_archived: unless explicitely set to a truish value, archived sessions are excluded :param filter_by_account: True means current account. A falsish value means do not filter. Otherwise try to interpret the user input: a string or an int means the account_id should be that value, an object account will be used to extract the account id, from an iterable we will try to extract the account ids :param filter_by_group: True means current account group, A falsish value means do not filter. Otherwise try to interpret the user input: a string or an int means the group_id should be that value, an object group will be used to extract the group id, and from an iterable we will try to extract the group ids :param table: if None use the default table in `cls.survey_session_model` otherwise use the one passed by the user """ if table is None: table = self.survey_session_model query = Session.query(table) query = query.filter(table.get_context_filter(context or self.context)) if filter_by_account: query = query.filter( table.get_account_filter(account=filter_by_account)) if filter_by_group: query = query.filter(table.get_group_filter(group=filter_by_group)) if not include_archived: query = query.filter(table.get_archived_filter()) if searchable_text: query = query.filter(table.title.ilike(searchable_text)) if order_by is None: pass # Do not append any order_by elif order_by: query = query.order_by(order_by) else: query = query.order_by(table.modified.desc(), table.title) return query
def publishTraverse(self, request, pathid): """Return a traversable SQL object""" query = ( Session.query(SurveyTreeItem) .filter(SurveyTreeItem.session == self.context.session) .filter(SurveyTreeItem.path == self._make_path(pathid)) ) sqlobj = query.one() if sqlobj is None: raise NotFound return sqlobj.__of__(self.context)
def update_completion_percentage(self, session=None): if not session: session = self.traversed_session.session query = ( Session.query(SurveyTreeItem).filter( SurveyTreeItem.session_id == session.id).filter( SurveyTreeItem.type == "module").filter( SurveyTreeItem.skip_children == False) # noqa: E712 ).order_by(SurveyTreeItem.depth) total_risks = 0 answered_risks = 0 @memoize def recursive_skip_children(module): return module.skip_children or ( module.parent and recursive_skip_children(module.parent)) for module in query: if not module.path: # XXX When does a module not have a path? continue if recursive_skip_children(module): continue total_risks_query = (Session.query(Risk).filter( Risk.session_id == session.id).filter( Risk.path.like(module.path + "%")).filter( Risk.depth == module.depth + 1)) total_risks = total_risks + total_risks_query.count() answered_risks_query = total_risks_query.filter( Risk.identification != None # noqa: E711 ) answered_risks = answered_risks + answered_risks_query.count() completion_percentage = (int( round((answered_risks * 1.0 / total_risks * 1.0) * 100.0)) if total_risks else 0) session.completion_percentage = completion_percentage return completion_percentage
def __call__(self): if self.request.method == "POST": self.log("Updating completion_percentage") b_size = self.request.get("b_size", 1000) b_start = self.request.get("b_start", 0) self.log("Limiting to {} sessions; starting at {}".format(b_size, b_start)) query = Session.query(SurveySession) if "overwrite" not in self.request.form: query = query.filter( SurveySession.completion_percentage == None # noqa: E711 ) self.log("Ignoring sessions with existing non-null values") else: self.log("Overwriting existing non-null values") query = ( query.join( Account, sql.and_( Account.id == SurveySession.account_id, Account.account_type != "guest", ), ) .order_by(SurveySession.modified.desc()) .offset(b_start) .limit(b_size) ) total = query.count() self.log("Found {} sessions".format(total)) cnt = 0 for session in query: self.update_completion_percentage(session) cnt += 1 if cnt % self.flush_threshold == 0: self.log("Handled {0} out of {1} sessions".format(cnt, total)) self.log("Done") return self.index()
def session(self): try: return (Session.query(SurveySession).filter( SurveySession.id == self.session_id).one()) except NoResultFound: raise NotFound