def __call__(self, context): countries = {} if ISectorContainer.providedBy(context): for country in context.values(): if not ICountry.providedBy(country): continue countries.update({ utils.getRegionTitle(context.REQUEST, country.id, country.title).encode('utf-8'): country.id }) elif ICountry.providedBy(context): countries.update({ utils.getRegionTitle(context.REQUEST, context.id, context.title).encode('utf-8'): context.id }) elif ISector.providedBy(context) or ISurveyGroup.providedBy(context): if ISector.providedBy(context): country = aq_parent(context) elif ISurveyGroup.providedBy(context): country = aq_parent(aq_parent(context)) if ICountry.providedBy(country): countries.update({ utils.getRegionTitle(context.REQUEST, country.id, country.title).encode('utf-8'): country.id }) return SimpleVocabulary.fromItems(sorted(countries.items()))
def __call__(self, context): countries = {} if ISectorContainer.providedBy(context): for country in context.values(): if not ICountry.providedBy(country): continue countries.update({ utils.getRegionTitle(context.REQUEST, country.id, country.title).encode('utf-8'): country.id }) elif ICountry.providedBy(context): countries.update({ utils.getRegionTitle( context.REQUEST, context.id, context.title).encode('utf-8'): context.id }) elif ISector.providedBy(context) or ISurveyGroup.providedBy(context): if ISector.providedBy(context): country = aq_parent(context) elif ISurveyGroup.providedBy(context): country = aq_parent(aq_parent(context)) if ICountry.providedBy(country): countries.update({ utils.getRegionTitle( context.REQUEST, country.id, country.title).encode('utf-8'): country.id }) return SimpleVocabulary.fromItems(sorted(countries.items()))
def getSurvey(context): from euphorie.content.surveygroup import ISurveyGroup from euphorie.content.survey import ISurvey obj = context while obj and not ISurveyGroup.providedBy(obj): if ISurvey.providedBy(obj): return obj obj = aq_parent(obj)
def evaluation_algorithm(context): """Return the evaluation algorithm used in a given context. The algorithm is determined by the `evaluation_algorithm` flag for the parent :py:class:`euphorie.content.surveygroup.SurveyGroup`. """ from euphorie.content.surveygroup import ISurveyGroup # XXX Circular for parent in aq_chain(aq_inner(context)): if ISurveyGroup.providedBy(parent): return parent.evaluation_algorithm return "kinney"
def __call__(self, context): tools = [] if ISectorContainer.providedBy(context): for country in context.values(): if not ICountry.providedBy(country): continue tools += self.getToolsInCountry(country) elif ICountry.providedBy(context): tools += self.getToolsInCountry(context) elif ISector.providedBy(context): tools += self.getToolsInSector(context) elif ISurveyGroup.providedBy(context): tools += self.getToolsInSector(aq_parent(context)) return SimpleVocabulary.fromValues(tools)
def evaluation_algorithm(context): """Return the evaluation algorithm used in a given context. The algorithm is determined by the `evaluation_algorithm` flag for the parent :py:class:`euphorie.content.surveygroup.SurveyGroup`. """ from euphorie.content.surveygroup import ISurveyGroup # XXX Circular for parent in aq_chain(aq_inner(context)): if IFrenchEvaluation.providedBy(parent): return u"french" elif IKinneyEvaluation.providedBy(parent): return u"kinney" if ISurveyGroup.providedBy(parent): return parent.evaluation_algorithm return u"kinney"
def get_library(context): """Get a list of sectors, based on the the euphorie.library registry record :returns: A list of dicts with details for sectors :rtype: list """ record = api.portal.get_registry_record("euphorie.library", default="") if not record: return [] paths = [path.lstrip("/") for path in safe_nativestring(record).split()] if not paths: return [] site = getPortal(context) library = [] for path in paths: try: sector = site.restrictedTraverse(path) except (AttributeError, KeyError): log.warning("Invalid library path (not found): %s" % path) continue if not ISector.providedBy(sector): log.warning("Invalid library path (not a sector): %s", path) continue sector_library = [] survey_groups = [ sg for sg in sector.values() if ISurveyGroup.providedBy(sg) and not sg.obsolete ] for sg in survey_groups: surveys = [s for s in sg.values() if ISurvey.providedBy(s)] if len(surveys) != 1: log.warning( "Ignoring surveygroup due to multiple versions: %s", "/".join(sg.getPhysicalPath()), ) continue tree = build_survey_tree(aq_inner(context), surveys[0]) tree["title"] = sg.title sector_library.append(tree) if sector_library: sector_library.sort(key=lambda s: s["title"]) library.append({ "title": sector.title, "url": sector.absolute_url(), "path": "/".join(sector.getPhysicalPath()), "surveys": sector_library, }) library.sort(key=lambda s: s["title"]) return library
def get_library(context): """ Get a list of sectors, based on the configuration in euphorie.ini :returns: A list of dicts with details for sectors :rtype: list """ config = getUtility(IAppConfig).get('euphorie', {}) paths = [path.lstrip('/') for path in config.get('library', '').split()] if not paths: return [] site = getPortal(context) library = [] for path in paths: try: sector = site.restrictedTraverse(path) except (AttributeError, KeyError): log.warning('Invalid library path (not found): %s' % path) continue if not ISector.providedBy(sector): log.warning('Invalid library path (not a sector): %s', path) continue sector_library = [] survey_groups = [sg for sg in sector.values() if ISurveyGroup.providedBy(sg) and not sg.obsolete] for sg in survey_groups: surveys = [s for s in sg.values() if ISurvey.providedBy(s)] if len(surveys) != 1: log.warning('Ignoring surveygroup due to multiple versions: %s', '/'.join(sg.getPhysicalPath())) continue tree = build_survey_tree(aq_inner(context), surveys[0]) tree['title'] = sg.title sector_library.append(tree) if sector_library: sector_library.sort(key=lambda s: s['title']) library.append({'title': sector.title, 'url': sector.absolute_url(), 'path': '/'.join(sector.getPhysicalPath()), 'surveys': sector_library}) library.sort(key=lambda s: s['title']) return library
def getSurveys(context): """Return a list of all surveys for the current sector. The return value is a sorted list of dictionaries describing the surveygroups for the sector. Each dictionary has the following keys: * ``id``: surveygroup id * ``title`` surveygroup title * ``url``: URL for the surveygroup * ``published``: boolean indicating if this surveygroup is published * ``surveys``: list of surveys for the surveygroup. Each entry is a dictionary with the following keys: * ``id``: survey id * ``title``: survey title * ``url``: URL for the survey * ``published``: boolean indicating if this survey is the currently published version of the surveygroup * ``current``: boolean indicating if the *context* is inside this survey * ``versions``: list of published versions """ current_version = None for sector in aq_chain(aq_inner(context)): if ISurvey.providedBy(sector): current_version = aq_base(sector) if ISector.providedBy(sector): break else: return [] result = [] groups = [group for group in sector.values() if ISurveyGroup.providedBy(group)] repository = getToolByName(context, "portal_repository") allow_history = checkPermission(context, AccessPreviousVersions) def morph(group, survey): published = survey.id == group.published info = { "id": survey.id, "title": survey.title, "url": survey.absolute_url(), "published": published, "publication_date": published and survey.published or None, "current": aq_base(survey) is current_version, "modified": isDirty(survey), "versions": [], } if not allow_history: return info history = repository.getHistoryMetadata(survey) if history: for id in range(history.getLength(countPurged=False) - 1, -1, -1): meta = history.retrieve(id, countPurged=False)["metadata"][ "sys_metadata" ] info["versions"].append( { "timestamp": datetime.datetime.fromtimestamp(meta["timestamp"]), "history_id": meta["parent"]["history_id"], "version_id": meta["parent"]["version_id"], "location_id": meta["parent"]["location_id"], } ) info["versions"].sort(key=lambda x: x["timestamp"], reverse=True) return info for group in groups: info = { "id": group.id, "title": group.title, "url": group.absolute_url(), "published": bool(group.published), } info["surveys"] = [ morph(group, survey) for survey in group.values() if ISurvey.providedBy(survey) ] info["surveys"].sort(key=lambda s: s["title"].lower()) result.append(info) result.sort(key=lambda g: g["title"].lower()) return result
def getSurveys(context): """Return a list of all surveys for the current sector. The return value is a sorted list of dictionaries describing the surveygroups for the sector. Each dictionary has the following keys: * ``id``: surveygroup id * ``title`` surveygroup title * ``url``: URL for the surveygroup * ``published``: boolean indicating if this surveygroup is published * ``surveys``: list of surveys for the surveygroup. Each entry is a dictionary with the following keys: * ``id``: survey id * ``title``: survey title * ``url``: URL for the survey * ``published``: boolean indicating if this survey is the currently published version of the surveygroup * ``current``: boolean indicating if the *context* is inside this survey * ``versions``: list of published versions """ current_version = None for sector in aq_chain(aq_inner(context)): if ISurvey.providedBy(sector): current_version = aq_base(sector) if ISector.providedBy(sector): break else: return [] result = [] groups = [group for group in sector.values() if ISurveyGroup.providedBy(group)] repository = getToolByName(context, "portal_repository") allow_history = checkPermission(context, AccessPreviousVersions) def morph(group, survey): info = {'id': survey.id, 'title': survey.title, 'url': survey.absolute_url(), 'published': survey.id == group.published, 'current': aq_base(survey) is current_version, 'modified': isDirty(survey), 'versions': []} if not allow_history: return info history = repository.getHistoryMetadata(survey) if history: for id in range(history.getLength(countPurged=False) - 1, -1, -1): meta = history.retrieve(id, countPurged=False)["metadata"]["sys_metadata"] info["versions"].append({ 'timestamp': datetime.datetime.fromtimestamp( meta["timestamp"]), 'history_id': meta["parent"]["history_id"], 'version_id': meta["parent"]["version_id"], 'location_id': meta["parent"]["location_id"]}) info["versions"].sort(key=lambda x: x["timestamp"], reverse=True) return info for group in groups: info = {'id': group.id, 'title': group.title, 'url': group.absolute_url(), 'published': bool(group.published)} info["surveys"] = [morph(group, survey) for survey in group.values() if ISurvey.providedBy(survey)] info["surveys"].sort(key=lambda s: s["title"].lower()) result.append(info) result.sort(key=lambda g: g["title"].lower()) return result