Exemple #1
0
    def constraintregion(self):
        uuid = self.request.form.get('uuid')
        exp = uuidToObject(uuid)
        if not exp:
            self.record_error('Not Found', 404, 'Experiment not found',
                              {'parameter': 'uuid'})
            raise NotFound(self, 'constraintregion', self.request)

        downloadurl = None
        if IExperiment.providedBy(exp):
            downloadurl = '{}/@@download/modelling_region.json'.format(
                exp.absolute_url())
        elif not IExperiment.providedBy(exp.__parent__):
            # this is an exp result file, so get exp folder
            exp = exp.__parent__

        if IExperiment.providedBy(exp.__parent__):
            # This is an exp result folder
            if IProjectionExperiment.providedBy(exp.__parent__):
                if exp.job_params['projection_region']:
                    downloadurl = '{}/@@download/modelling_region.json'.format(
                        exp.absolute_url())
                else:
                    # Get constraint from SDM experiment result file.
                    # Use the modelling_region.json file in the sdm result if available.
                    if not 'species_distribution_models' in exp.job_params:
                        self.record_error(
                            'NotFound', 404, 'SDM model not found',
                            {'parameter': 'species_distribution_models'})
                        raise NotFound(self, 'species_distribution_models',
                                       self.request)
                    sdmuuid = exp.job_params['species_distribution_models']
                    sdmobj = uuidToObject(sdmuuid).__parent__

                    # Return the modelling_region attribute only if no modelling_region.json file
                    if not 'modelling_region.json' in sdmobj.keys():
                        downloadurl = '{}/@@download/modelling_region.json'.format(
                            sdmobj.absolute_url())
                    else:
                        # Redirect to download the modelling_region.json
                        constraint_region = sdmobj.get('modelling_region.json')
                        remoteUrl = getattr(constraint_region, 'remoteUrl',
                                            None)
                        if remoteUrl is None:
                            raise NotFound(self, 'remoteUrl', self.request)
                        # Generate temp url
                        tool = getUtility(ISwiftUtility)
                        try:
                            downloadurl = tool.generate_temp_url(url=remoteUrl)
                        except:
                            downloadurl = remoteUrl
            else:
                downloadurl = '{}/@@download/modelling_region.json'.format(
                    exp.absolute_url())

        if downloadurl is None:
            self.record_error('Not Found', 404, 'Constraint region not found',
                              {'parameter': 'uuid'})
            raise NotFound(self, 'constraintregion', self.request)
        return self.request.RESPONSE.redirect(downloadurl.encode('utf-8'))
    def getRequestItems(self):
        request = self.request
        if 'list_files[]' in request.keys() or 'list_files' in request.keys():
            values = request.get('list_files[]', request.get('list_files'))
            type = request.get('type')
            if values:
                if type == 'structure':
                    L = []
                    try:
                        if isinstance(values, str):
                            values = eval(values)

                        for uuid in values:
                            obj = uuidToObject(uuid)
                            if obj:
                                struc = obj.getStructures()
                                if struc not in L:
                                    L.append(struc)
                        return L
#                        return [uuidToObject(uuid).getStructures() for uuid in values if uuidToObject(uuid)]
                    except (NameError, SyntaxError):
                        return [uuidToObject(values).getStructures()]
                else:
                    themes = self.request.get('document-theme[]', self.request.get('document-theme'))
                    if themes:
                        try:
                            if isinstance(themes, str):
                                themes = eval(themes)
                            return themes
                        except (SyntaxError, NameError):
                            return [themes]
                    else:
                        return self.getAllKeyword('ThemeNews').keys()
 def search_content(self, query, portal_type, results):
     kwargs = {
         'Title': query,
         'portal_type': portal_type,
         'sort_on': 'portal_type'
     }
     # if we are searching a ticket, search the ID
     if len(portal_type) == 1 and portal_type[0] == 'PoiIssue':
         del kwargs['Title']
         kwargs['getId'] = {
             'query': (query, self.next_query(query)),
             'range': 'min:max'
         }
     filter_context = self.request.form.get('filter_context')
     frozen_refs = self.request.form.get('frozen_refs')
     exclude_uuids = set()
     if filter_context or frozen_refs:
         if filter_context:
             filter_context = json.loads(filter_context)
         else:
             filter_context = []
         if frozen_refs:
             filter_context += json.loads(frozen_refs)
         project = None
         for item in filter_context:
             exclude_uuids.add(item['uuid'])
             if item['portal_type'] == 'Project':
                 project = uuidToObject(item['uuid'])
                 break
             else:
                 story = uuidToObject(item['uuid'])
                 project = get_project(story)
         if project is not None:
             kwargs['portal_type'] = tuple(t for t in kwargs['portal_type']
                                           if t != 'Project')
             kwargs['path'] = {'query': '/'.join(project.getPhysicalPath())}
     catalog = getToolByName(self.context, 'portal_catalog')
     get_breadcrumb = BreadcrumbGetter(catalog)
     # first we search by title
     brains = catalog.searchResults(**kwargs)
     if len(brains) == 0 and 'Title' in kwargs:
         # but if that gives no result (remember we write down IDs)
         # we try the id.startswith thing.
         # we have a special case (tickets) where we already search by id
         del kwargs['Title']
         kwargs['getId'] = {
             'query': (query, self.next_query(query)),
             'range': 'min:max'
         }
         brains = catalog.searchResults(**kwargs)
     brains = brains[:self.MAX_RESULTS]
     for brain in brains:
         if brain['UID'] not in exclude_uuids:
             results.append({
                 'portal_type': brain['portal_type'],
                 'uuid': brain['UID'],
                 'title': brain['Title'].decode('utf-8'),
                 'id': brain['id'],
                 'breadcrumb': get_breadcrumb(brain)
             })
    def get_referenced_objects(self):
        """Get referenced objects from cover object.

        :returns: a set of objects referenced
        :rtype: set of objects
        """
        refs = set()
        for tile_uuid in self.list_tiles():
            tile = self.get_tile(tile_uuid)
            uuid = tile.data.get('uuid', None)
            if uuid is not None:
                refs |= set([uuidToObject(uuid)])
            if IListTile.providedBy(tile):
                uuids = tile.data.get('uuids', [])
                if uuids is None:
                    continue
                for uuid in uuids:
                    refs |= set([uuidToObject(uuid)])
            elif IRichTextTile.providedBy(tile):
                value = tile.data.get('text')
                if value is None:
                    continue
                value = value.raw
                links = extractLinks(value)
                refs |= getObjectsFromLinks(self, links)
        return refs
 def __call__(self):
     uid = ''
     if 'uid' in self.request:
         uid = self.request['uid']
     else:
         # If no uid parameter redirect to root
         root = getToolByName(self.context, 'portal_url')
         self.request.RESPONSE.redirect(root.url())
     # Look for the element
     storage = getUtility(IMultilingualStorage)
     if storage.get_canonical(uid):
         canonical = storage.get_canonical(uid)
         # The negotiated language
         ltool = getToolByName(self.context, 'portal_languages')
         if len(ltool.getRequestLanguages()) > 0:
             language = ltool.getRequestLanguages()[0]
             target_uuid = canonical.get_item(language)
             if target_uuid:
                 target_object = uuidToObject(target_uuid)
                 self.request.RESPONSE.redirect(
                     target_object.absolute_url())
             else:
                 target_object = uuidToObject(uid)
                 self.request.RESPONSE.redirect(
                     target_object.absolute_url() +
                     NOT_TRANSLATED_YET_TEMPLATE)
     else:
         # If no uid parameter redirect to root
         root = getToolByName(self.context, 'portal_url')
         self.request.RESPONSE.redirect(root.url())
Exemple #6
0
 def __call__(self):
     uid = ''
     if 'uid' in self.request:
         uid = self.request['uid']
     else:
         # If no uid parameter redirect to root
         root = getToolByName(self.context, 'portal_url')
         self.request.RESPONSE.redirect(root.url())
     # Look for the element
     storage = getUtility(IMultilingualStorage)
     if storage.get_canonical(uid):
         canonical = storage.get_canonical(uid)
         # The negotiated language
         ltool = getToolByName(self.context, 'portal_languages')
         if len(ltool.getRequestLanguages()) > 0:
             language = ltool.getRequestLanguages()[0]
             target_uuid = canonical.get_item(language)
             if target_uuid:
                 target_object = uuidToObject(target_uuid)
                 self.request.RESPONSE.redirect(target_object.absolute_url())
             else:
                 target_object = uuidToObject(uid)
                 self.request.RESPONSE.redirect(target_object.absolute_url() + NOT_TRANSLATED_YET_TEMPLATE)
     else:
         # If no uid parameter redirect to root
         root = getToolByName(self.context, 'portal_url')
         self.request.RESPONSE.redirect(root.url())
Exemple #7
0
def migrate_linkintegrity_relations(context):
    """Migrate linkintegrity-relation from reference_catalog to zc.relation.
    """
    reference_catalog = getToolByName(context, REFERENCE_CATALOG, None)
    intids = getUtility(IIntIds)
    if reference_catalog is not None:
        # Only handle linkintegrity-relations ('isReferencing').
        # [:] copies the full result list to make sure
        # it won't change while we delete references below
        for brain in reference_catalog(relationship=referencedRelationship)[:]:
            try:
                source_obj = uuidToObject(brain.sourceUID)
                target_obj = uuidToObject(brain.targetUID)
            except AttributeError:
                source_obj = target_obj = None
            if source_obj is None or target_obj is None:
                # reference_catalog may be inconsistent
                log.info(
                    'Cannot delete relation since the relation_catalog is inconsistent.'
                )  # noqa: E501
                continue
            # Delete old reference
            reference_catalog.deleteReference(
                source_obj, target_obj, relationship=referencedRelationship)

            # Trigger the recreation of linkintegrity-relation in
            # the relation_catalog (zc.relation)
            target_id = ensure_intid(target_obj, intids)
            if target_id is None:
                continue
            rel = RelationValue(target_id)
            _setRelation(source_obj, referencedRelationship, rel)
 def __call__(self, transmogrifier):
     """
     """
     # nel transmogrifier c'e' una lista di tuple:
     # (path, fieldname, value) per le quali vanno rifatte le relations
     logger.info("## Fix Relations ##")
     relations = getattr(transmogrifier, "fixrelations", [])
     for (path, fieldname, value) in relations:
         if not value:
             continue
         obj = api.content.get(path)
         if not obj:
             logger.warning(
                 "[FIX RELATIONS] - Unable to find {path}. No relations fixed."
                 .format(  # noqa
                     path=path))
             continue
         logger.info("fix {0} {1} {2}".format(path, fieldname, value))
         for schemata in iterSchemata(obj):
             for name, field in getFieldsInOrder(schemata):
                 if name == fieldname:
                     if isinstance(value, six.string_types):
                         value = uuidToObject(value)
                     else:
                         value = [uuidToObject(uuid) for uuid in value]
                     deserializer = queryMultiAdapter((field, obj),
                                                      IDeserializer)
                     value = deserializer(value, [], {},
                                          True,
                                          logger=logger)
                     field.set(field.interface(obj), value)
                     notify(ObjectModifiedEvent(obj))
def addSpeciesInfo(bccvlmd, result):
    if ISDMExperiment.providedBy(result.__parent__):
        spds = uuidToObject(result.job_params['species_occurrence_dataset'])
    if IProjectionExperiment.providedBy(result.__parent__):
        spds = uuidToObject(result.job_params['species_distribution_models'])
    speciesmd = IBCCVLMetadata(spds).get('species', None)
    if speciesmd:
        bccvlmd['species'] = speciesmd.copy()
def addSpeciesInfo(bccvlmd, result):
    if ISDMExperiment.providedBy(result.__parent__):
        spds = uuidToObject(result.job_params['species_occurrence_dataset'])
    if IProjectionExperiment.providedBy(result.__parent__):
        spds = uuidToObject(result.job_params['species_distribution_models'])
    speciesmd = IBCCVLMetadata(spds).get('species', None)
    if speciesmd:
        bccvlmd['species'] = speciesmd.copy()
 def go_back_title(self):
     if self.request.form.get('form.button.Save', None) is not None \
             or self.request.form.get('form.button.Delete', None) is \
             not None:
         if self.request.form.get("came_from", None):
             came_from = self.request.form.get("came_from")
             if uuidToObject(came_from):
                 return _(u"Go back to ") + uuidToObject(came_from).title
 def go_back_url(self):
     if self.request.form.get('form.button.Save', None) is not None \
             or self.request.form.get('form.button.Delete', None) \
             is not None:
         if self.request.form.get("came_from", None):
             came_from = self.request.form.get("came_from")
             if uuidToObject(came_from):
                 return uuidToObject(came_from).absolute_url()
def projection_listing_details(expbrain):

    # TODO: duplicate code here... see org.bccvl.site.browser.widget.py
    # TODO: generated list here not very useful,.... all layers over all sdms are concatenated
    # TODO: whata about future datasets?
    details = {}
    exp = expbrain.getObject()
    inputexps = set()
    futureenvs = set()
    for env_uuid in exp.future_climate_datasets:
        futureenvs.add(get_title_from_uuid(env_uuid, u'(Unavailable)'))

    for sdmuuid in exp.species_distribution_models:
        inputexps.add(get_title_from_uuid(sdmuuid, u'(Unavailable)'))
        sdmexp = uuidToObject(sdmuuid)
        if sdmexp is not None:
            # TODO: absence data
            envlayers = []
            # TODO: list all the subset layers??
            if sdmexp.portal_type == 'org.bccvl.content.mmexperiment':
                environmental_datasets = sdmexp.datasubsets[0].get(
                    'environmental_datasets')
            else:
                environmental_datasets = sdmexp.environmental_datasets
            for envuuid, layers in sorted(environmental_datasets.items()):
                envbrain = uuidToCatalogBrain(envuuid)
                envtitle = envbrain.Title if envbrain else u'Missing dataset'
                envlayers.append({'title': envtitle, 'layers': sorted(layers)})
                # TODO: job_params has only id of function not uuid ... not sure how to get to the title
                toolkits = ', '.join(
                    uuidToObject(sdmmodel).__parent__.job_params['function']
                    for sdmmodel in exp.species_distribution_models[sdmuuid])
                if sdmexp.portal_type in ('org.bccvl.content.sdmexperiment',
                                          'org.bccvl.content.mmexperiment'):
                    species_occ = get_title_from_uuid(
                        sdmexp.species_occurrence_dataset, u'(Unavailable)'
                    ) if sdmexp.species_occurrence_dataset else ''
                else:
                    # not sdm,... probably msdm?
                    species_occ = get_title_from_uuid(
                        sdmexp.species_occurrence_collections, u'(Unavailable)'
                    ) if sdmexp.species_occurrence_collections else ''
        else:
            toolkits = 'missing experiment'
            species_occ = ''
            envlayers = []

        details.update({
            'type': 'PROJECTION',
            'functions': toolkits,
            'species_occurrence': species_occ,
            'species_absence': '',
            'environmental_layers': envlayers,
            'input_experiments': inputexps,
            'future_env_datasets': futureenvs
        })
    return details
Exemple #14
0
 def get_slides(self):
     slides = []
     root = etree.fromstring(self.context.content)
     tiles = root.findall(
         './/{http://www.w3.org/1999/xhtml}div[@data-tile]')
     for tile in tiles:
         tile_path = tile.get('data-tile')
         if 'castle.cms.slide/' in tile_path:
             parsed = urlparse(tile_path)
             data = parse_qs(parsed.query)
             slide = {}
             try:
                 image = uuidToObject(data['image:list'][0])
                 slide['image'] = image.absolute_url()
             except Exception:
                 slide['image'] = None
             try:
                 video = uuidToObject(data['video:list'][0])
                 slide['video'] = video.absolute_url()
             except Exception:
                 slide['video'] = None
             slide['displayType'] = data.get('display_type',
                                             ['background-image'])[0]
             if slide['displayType'] == 'resource-slide':
                 try:
                     related_uuids = data['related_items:list']
                     slide['related_resources'] = [
                         uuidToObject(uuid) for uuid in related_uuids
                     ]
                 except Exception:
                     pass
             slide['title'] = data.get('title', [None])[0]
             slide['text'] = data.get('text:text', [None])[0]
             slide['vert'] = data.get('vert_text_position', ['middle'])[0]
             hor_text_position = data.get('hor_text_position',
                                          ['center'])[0]
             slide['hor'] = hor_text_position
             slide[
                 'width_class'] = 'full-width' if hor_text_position == 'center' else 'half-width'
             justify_wrapped_text = data.get('justify_wrapped_text:boolean',
                                             [0])[0] == '1'
             slide['justify_wrapped_text'] = justify_wrapped_text
             slide[
                 'justify_wrap_class'] = 'justify-wrap' if justify_wrapped_text else ''
             slide['text_alignment'] = data.get('text_alignment',
                                                ['center'])[0]
             slide['customize_left_slide_mobile'] = \
                 data.get('customize_left_slide_mobile:boolean', [0])[0] == '1'
             if slide['customize_left_slide_mobile']:
                 slide['left_mobile_hor'] = \
                     data.get('left_slide_mobile_hor_text_position', ['default'])[0]
                 slide['left_mobile_vert'] = \
                     data.get('left_slide_mobile_vert_text_position', ['default'])[0]
                 slide['left_mobile_alignment'] = \
                     data.get('left_slide_mobile_text_alignment', ['default'])[0]
             slides.append(slide)
     return slides
Exemple #15
0
    def constraintregion(self):
        uuid = self.request.form.get('uuid')
        exp = uuidToObject(uuid)
        if not exp:
            self.record_error('Not Found', 404,
                              'Experiment not found',
                              {'parameter': 'uuid'})
            raise NotFound(self, 'constraintregion', self.request)

        downloadurl = None
        if IExperiment.providedBy(exp):
            downloadurl = '{}/@@download/modelling_region.json'.format(exp.absolute_url())
        elif not IExperiment.providedBy(exp.__parent__):
            # this is an exp result file, so get exp folder
            exp = exp.__parent__

        if IExperiment.providedBy(exp.__parent__):
            # This is an exp result folder
            if IProjectionExperiment.providedBy(exp.__parent__):
                if exp.job_params['projection_region']:
                    downloadurl = '{}/@@download/modelling_region.json'.format(exp.absolute_url())
                else:
                    # Get constraint from SDM experiment result file. 
                    # Use the modelling_region.json file in the sdm result if available.
                    if not 'species_distribution_models' in exp.job_params:
                        self.record_error('NotFound', 404, 
                                          'SDM model not found',
                                          {'parameter': 'species_distribution_models'})
                        raise NotFound(self, 'species_distribution_models', self.request)
                    sdmuuid = exp.job_params['species_distribution_models']
                    sdmobj = uuidToObject(sdmuuid).__parent__

                     # Return the modelling_region attribute only if no modelling_region.json file
                    if not 'modelling_region.json' in sdmobj.keys():
                        downloadurl = '{}/@@download/modelling_region.json'.format(sdmobj.absolute_url())
                    else:
                        # Redirect to download the modelling_region.json
                        constraint_region = sdmobj.get('modelling_region.json')
                        remoteUrl = getattr(constraint_region, 'remoteUrl', None)
                        if remoteUrl is None:
                            raise NotFound(self, 'remoteUrl', self.request)
                        # Generate temp url
                        tool = getUtility(ISwiftUtility)
                        try:
                            downloadurl = tool.generate_temp_url(url=remoteUrl)
                        except:
                            downloadurl = remoteUrl
            else:
                downloadurl = '{}/@@download/modelling_region.json'.format(exp.absolute_url())

        if downloadurl is None:
            self.record_error('Not Found', 404,
                              'Constraint region not found',
                              {'parameter': 'uuid'})
            raise NotFound(self, 'constraintregion', self.request)
        return self.request.RESPONSE.redirect(downloadurl.encode('utf-8'))
Exemple #16
0
    def __call__(self):
        # respect field level security as defined in plone.autoform
        # check if attribute access would be allowed!
        # url = guarded_getattr(self.context, 'remoteUrl', None)

        exp = self.context
        if IProjectionExperiment.providedBy(exp):
            if exp.projection_region:
                return exp.projection_region.data
            if not exp.species_distribution_models:
                raise NotFound(self, 'species_distribution_models', self.request)
            # Return the SDM's modelling region
            sdmuuid = exp.species_distribution_models.keys()[0]
            sdmobj = uuidToObject(sdmuuid)
            if sdmobj and sdmobj.modelling_region:
                return sdmobj.modelling_region.data
        elif IExperiment.providedBy(exp):
            if exp.modelling_region:
                return exp.modelling_region.data
        else:
            # Move one level up if this is an exp result file
            if not IExperiment.providedBy(exp.__parent__):
                exp = exp.__parent__
        if IExperiment.providedBy(exp.__parent__):
            # this is the result folder
            if IProjectionExperiment.providedBy(exp.__parent__):
                if exp.job_params['projection_region']:
                    return exp.job_params['projection_region'].data
                # Get constraint from SDM experiment result file. 
                # Use the modelling_region.json file in the sdm result if available.
                if not 'species_distribution_models' in exp.job_params:
                    raise NotFound(self, 'species_distribution_models', self.request)
                sdmuuid = exp.job_params['species_distribution_models']
                sdmobj = uuidToObject(sdmuuid).__parent__
            else:
                sdmobj = exp

            # Return the modelling_region attribute only if no modelling_region.json file
            if not 'modelling_region.json' in sdmobj.keys():
                return sdmobj.modelling_region.data

            # Redirect to download the modelling_region.json
            constraint_region = sdmobj.get('modelling_region.json')
            remoteUrl = getattr(constraint_region, 'remoteUrl', None)
            if remoteUrl is None:
                raise NotFound(self, 'remoteUrl', self.request)
            # Generate temp url
            tool = getUtility(ISwiftUtility)
            try:
                url = tool.generate_temp_url(url=remoteUrl)
            except:
                url = remoteUrl
            return self.request.RESPONSE.redirect(url.encode('utf-8'))
        else:
            raise NotFound(self, 'constraint_region', self.request)
def restore_references(context, relationship_fieldname_mapping=None):
    """Recreate all references stored in an annotation on the context.

    Iterate over the stored references and restore them all according to
    the content-types framework.

    Accepts an optional relationship_fieldname_mapping argument.
    This must be a dictionary with a relationship name as key and fieldname as value.
    For example:
    relationship_fieldname_mapping =  {
        'advisory_contact': 'contact',
        'study_contact': 'contact',
    }
    In this case, old Archetypes content types Advisory and Study both had a
    reference field 'contact' to a content type Contact.
    This relationship was stored under different names for the two contenttypes.
    After migration to Dexterity, the above mapping makes sure the relation is still
    stored on the 'contact' field in both cases.
    The attribute_name of the RelationValue will be the same as this fieldname,
    which is what happens by default when setting relations.

    By default we will also map the 'relatesTo' relation to the 'relatedItems' field.
    This is needed for ATContentTypes.
    """
    if relationship_fieldname_mapping is None:
        relationship_fieldname_mapping = {}
    if 'relatesTo' not in relationship_fieldname_mapping:
        # ATContentTypes used this relation.
        relationship_fieldname_mapping['relatesTo'] = 'relatedItems'
    key = 'ALL_REFERENCES'
    all_references = IAnnotations(context)[key]
    logger.info('Restoring {0} relations.'.format(len(all_references)))
    for index, ref in enumerate(all_references, 1):
        source_obj = uuidToObject(ref['from_uuid'])
        target_obj = uuidToObject(ref['to_uuid'])
        relationship = ref['relationship']
        if source_obj and target_obj:
            relationship = ref['relationship']
            # By default use the relationship as fieldname.  Fall back to the relationship.
            fieldname = relationship_fieldname_mapping.get(
                relationship, relationship)
            link_items(context, source_obj, target_obj, relationship,
                       fieldname)
        else:
            logger.warn('Could not restore reference from uid '
                        '"{0}" to uid "{1}" on the context: {2}'.format(
                            ref['from_uuid'], ref['to_uuid'],
                            '/'.join(context.getPhysicalPath())))
        if not index % 100:
            logger.info('Restoring relations: {}/{}'.format(
                index, len(all_references)))
    del IAnnotations(context)[key]
def restore_references(context):
    """Recreate all references stored in an annotation on the context.

    Iterate over the stored references and restore them all according to
    the content-types framework.
    """
    key = 'ALL_REFERENCES'
    for ref in IAnnotations(context)[key]:
        source_obj = uuidToObject(ref['from_uuid'])
        target_obj = uuidToObject(ref['to_uuid'])
        relationship = ref['relationship']
        link_items(context, source_obj, target_obj, relationship)
    del IAnnotations(context)[key]
Exemple #19
0
def restore_references(context):
    """Recreate all references stored in an annotation on the context.

    Iterate over the stored references and restore them all according to
    the content-types framework.
    """
    key = 'ALL_REFERENCES'
    for ref in IAnnotations(context)[key]:
        source_obj = uuidToObject(ref['from_uuid'])
        target_obj = uuidToObject(ref['to_uuid'])
        relationship = ref['relationship']
        link_items(context, source_obj, target_obj, relationship)
    del IAnnotations(context)[key]
Exemple #20
0
def dg_doc_info(self):
    if not check_zope_admin():
        return "You must be a zope manager to run this script"
    annot = IAnnotations(self)
    dic = annot['documentgenerator']
    ret = []
    ret.append(str(dic))
    if 'context_uid' in dic:
        ret.append("'context_uid': '%s'" %
                   uuidToObject(dic['context_uid']).absolute_url())
    if 'template_uid' in dic:
        ret.append("'template_uid': '%s'" %
                   uuidToObject(dic['template_uid']).absolute_url())
    return '\n'.join(ret)
def restoreReferences(portal):
    """ iterate over all Dexterity Objs and restore es Dexterity Reference. """
    out = ''
    catalog = getToolByName(portal, "portal_catalog")
    # Seems that these newly created objs are not reindexed
    catalog.clearFindAndRebuild()
    intids = getUtility(IIntIds)
    results = catalog.searchResults(
        object_provides=IDexterityContent.__identifier__)
    for brain in results:
        obj = brain.getObject()

        # refs
        try:
            if not getattr(obj, 'relatedItems', None):
                obj.relatedItems = PersistentList()
            elif type(obj.relatedItems) != type(PersistentList()):
                obj.relatedItems = PersistentList(obj.relatedItems)
            for uuid in obj._relatedItems:
                to_obj = uuidToObject(uuid)
                to_id = intids.getId(to_obj)
                obj.relatedItems.append(RelationValue(to_id))
                out += str('Restore Relation from %s to %s \n' % (obj, to_obj))
            del obj._relatedItems
        except AttributeError:
            pass

        # backrefs
        try:
            backrefobjs = [uuidToObject(uuid) for uuid in obj._backrefs]
            for backrefobj in backrefobjs:
                # Dexterity and
                if IDexterityContent.providedBy(backrefobj):
                    if not getattr(backrefobj, 'relatedItems', None):
                        backrefobj.relatedItems = PersistentList()
                    elif type(backrefobj.relatedItems) != type(PersistentList()):
                        backrefobj.relatedItems = PersistentList(
                            obj.relatedItems)
                    to_id = intids.getId(obj)
                    backrefobj.relatedItems.append(RelationValue(to_id))

                # Archetypes
                elif IATContentType.providedBy(backrefobj):
                    backrefobj.setRelatedItems(obj)
                out += str(
                    'Restore BackRelation from %s to %s \n' % (backrefobj, obj))
            del obj._backrefs
        except AttributeError:
            pass
    return out
Exemple #22
0
    def list_campaign(self):
        result = {'related': [], 'not_related': []}

        reference_catalog = getToolByName(self.context, REFERENCE_CATALOG)
        references = reference_catalog.getBackReferences(
            self.context, relationship="campaignItems")
        related = [a.sourceUID for a in references]

        portal_catalog = getToolByName(self.context, 'portal_catalog')
        brains = portal_catalog(portal_type='Campaign')
        not_related = [a.UID for a in brains if a.UID not in related]

        result['related'] = [uuidToObject(u) for u in related]
        result['not_related'] = [uuidToObject(u) for u in not_related]
        return result
Exemple #23
0
def restore_backrefs(portal, obj):
    """Restore backreferences stored in the attribute _backrefs.
    """
    intids = getUtility(IIntIds)
    uid_catalog = getToolByName(portal, 'uid_catalog')
    try:
        backrefobjs = [uuidToObject(uuid) for uuid in obj._backrefs]
        for backrefobj in backrefobjs:
            # Dexterity and
            if IDexterityContent.providedBy(backrefobj):
                relitems = getattr(backrefobj, 'relatedItems', None)
                if not relitems:
                    backrefobj.relatedItems = PersistentList()
                elif not isinstance(obj.relatedItems, PersistentList):
                    backrefobj.relatedItems = PersistentList(obj.relatedItems)
                to_id = intids.getId(obj)
                backrefobj.relatedItems.append(RelationValue(to_id))

            # Archetypes
            elif IATContentType.providedBy(backrefobj):
                # reindex UID so we are able to set the reference
                path = '/'.join(obj.getPhysicalPath())
                uid_catalog.catalog_object(obj, path)
                backrefobj.setRelatedItems(obj)
            logger.info('Restored BackRelation from %s to %s' %
                        (backrefobj, obj))
    except AttributeError:
        pass
    def __call__(self):
        settings = get_settings()
        self.organization_name = settings.organization_name
        self.campaign = self.context.get_fundraising_campaign()
        self.page = self.context.get_fundraising_campaign_page()
        self.donation_receipt_legal = self.campaign.donation_receipt_legal

        self.products = []
        if self.context.products:
            for product in self.context.products:
                price, quantity, product_uuid = product.split('|', 2)
                total = int(price) * int(quantity)
                product = uuidToObject(product_uuid)
                if not product:
                    continue
                if product.donation_only:
                    price = total
                    quantity = '-'
                self.products.append({
                    'price': price,
                    'quantity': quantity,
                    'product': product,
                    'total': total,
                })

        self.is_personal = self.page.is_personal()

        module = os.sys.modules[martian.util.caller_module()]
        _prefix = os.path.dirname(module.__file__)

        pt = PageTemplateFile('donation_templates/receipt.pt')
        return pt.pt_render({'view': self})
Exemple #25
0
    def get_objs(self):
        value = self.request.cookies.get(self.key)
        if value:
            uuids = json.loads(value.decode('base64'))
            return [uuidToObject(uuid) for uuid in uuids]

        return None
Exemple #26
0
 def get_latest_report(self, uuid):
     item = uuidToObject(uuid)
     results = item.restrictedTraverse('@@folderListing')(
         portal_type='xpose.seodash.report',
         sort_on='modified',
         sort_order='reverse')
     return results[0]
Exemple #27
0
    def persons(self, start=0, size=11):
        """Get all persons in this folder.
        """

        catalog = getToolByName(self.context, 'portal_catalog')
        #import pdb; pdb.set_trace()
        #family = [dict(url=person.getURL(), name=person.Title,
        #          personal_id=person.personal_id) for person in 
        family =  catalog({'object_provides': IPerson.__identifier__,
                  'path': dict(query='/'.join(self.context.getPhysicalPath()),
                  depth=1), 'sort_on': 'sortable_title', 'b_start': start,
                  'b_size': size,})# ]

        family_objs = [brain.getObject() for brain in family]

        from plone.app.uuid.utils import uuidToObject
        persons =[]

        for item in family_objs:
            clubs = [uuidToObject(relation.getObject().foreign_id) 
                    for relation in
                    catalog({'object_provides': IRelation.__identifier__,
                    'review_state': ('pending', 'approved'),                                                                
                    'path': dict(query='/'.join(item.getPhysicalPath()),
                    depth=1),'sort_on': 'sortable_title'})]

            clubs = [x for x in clubs if x]
            persons.append({'name': item.first_name + ' ' + item.last_name, 
                            'clubs': clubs, 
                            'person': item})

        return persons
Exemple #28
0
 def validateAction(self, data):
     # TODO: check data ...
     # ...
     datasets = data.get('projection', {})
     if not tuple(chain.from_iterable(x for x in datasets.values())):
         raise WidgetActionExecutionError(
             'projection',
             Invalid('No projection dataset selected.')
         )
     # check if threshold values are in range
     for dataset in (x for x in datasets.values()):
         if not dataset:
             raise WidgetActionExecutionError(
                 'projection',
                 Invalid('Please select at least one dataset within experiment'))
         # key: {label, value}
         dsuuid = dataset.keys()[0]
         ds = uuidToObject(dsuuid)
         value = dataset[dsuuid]['value']
         md = IBCCVLMetadata(ds)
         # ds should be a projection output which has only one layer
         # FIXME: error message is not clear enough and
         #        use widget.errors instead of exception
         # also it will only verify if dateset has min/max values in
         # metadata
         layermd = md['layers'].values()[0]
         if 'min' in layermd and 'max' in layermd:
             # FIXME: at least layermd['min'] may be a string '0', when
             # comparing to decimal from threshold selector, this comparison
             # fails and raises the widget validation error
             if value <= float(layermd['min']) or value >= float(layermd['max']):
                 raise WidgetActionExecutionError(
                     'projection',
                     Invalid('Selected threshold is out of range'))
Exemple #29
0
def count_dataset_created(obj, event):
    # IBLobDataset
    # IRemoteDataset
    # IDatasetCollection -> IMultiSpeciesDataset
    if IAnnotations(obj.REQUEST).get('org.bccvl.site.stats.delay'):
        # skip this event, we have been called from transmogrify chain, where
        # we collect stats later
        # tell stats collector that we really created a new object
        IAnnotations(obj.REQUEST)['org.bccvl.site.stats.created'] = True
        return
    dataSrc = obj.dataSource
    if not dataSrc:
        if obj.part_of:
            # part of multispecies file ... get dataSource from master file
            master = uuidToObject(obj.part_of)
            dataSrc = master.dataSource
    if not dataSrc:
        # find default
        # check whether we are inside an experiment:
        if defaults.EXPERIMENTS_FOLDER_ID in obj.getPhysicalPath():
            dataSrc = 'experiment'
        else:
            dataSrc = 'upload'
    getUtility(IStatsUtility).count_dataset(
        source=dataSrc,
        portal_type=obj.portal_type
    )
Exemple #30
0
 def internal_url(self):
     object_path = self.data.internal_target
     internal_obj = uuidToObject(object_path)
     if internal_obj:
         return internal_obj.absolute_url()
     else:
         return ''
def process_directory(directory):
    for sub_directory in os.listdir(directory):
        if '@@' in sub_directory:
            continue
        sub_directory_path = os.path.join(directory, sub_directory)
        if not os.path.isdir(sub_directory_path):
            continue
        if sub_directory_path in obfuscated_paths:
            # private, obfuscated path
            continue
        if sub_directory in obfuscated_uids:
            continue

        if len(sub_directory) > 10:
            # check if UID
            obj = uuidToObject(sub_directory)
            if obj is None:
                stats['remove'] += 1
                print('Could not find object related to: ' + sub_directory_path)
                if args.commit:
                    shutil.rmtree(sub_directory_path)
                    continue
            else:
                stats['keep'] += 1
        process_directory(sub_directory_path)
Exemple #32
0
    def __call__(self, abctext, uuid, makeMP3):
        # need to remove 'view' at the end if present
        abctune = uuidToObject(uuid)
        sm = getSecurityManager()
        if not sm.checkPermission(ModifyPortalContent, abctune):
            return
        abctune.abc = abctext
        addTuneType(abctune)
        addOrigins(abctune)
        addKeys(abctune)
        _make_midi(abctune)
        _make_score(abctune)

        # _make_PDFscore(abctune)
        if makeMP3 != '0':
            _make_mp3(abctune)
            logger.info('lance _make_mp3')
        # import pdb;pdb.set_trace()
        abctune.modification_date = DateTime()
        logger.info(abctune.modified())
        logger.info('"' + abctune.title + '" updated')
        parent = abctune.aq_parent
        if parent.portal_type == 'abctuneset':
            # logger.info('in updateTune.updateTune')
            updateTuneSet(parent)
        site = getSite()
        catalog = site.portal_catalog
        catalog.reindexObject(abctune)
        return 1
Exemple #33
0
    def get_albuns(self):
        """ Return a list of albuns
        """
        albuns = []
        uuid = self.data.get("uuid", None)
        obj = None
        if uuid:
            obj = uuidToObject(uuid)
        if obj:
            catalog = api.portal.get_tool("portal_catalog")

            # Procuro todas subpastas na pasta do album
            path = "/".join(obj.getPhysicalPath())
            brains = catalog(
                Type="Folder", path={"query": path}, sort_on="effective", sort_order="reverse", review_state="published"
            )[:10]
            # Procuro todas subpastas na pasta do album

            # Retiro as pastas que não são albuns
            for brain in brains:
                obj = brain.getObject()
                if obj.getLayout() == "galeria_de_fotos":
                    albuns.append(obj)
            # Retiro as pastas que não são albuns

        return albuns
Exemple #34
0
    def updateWidgets(self):
        super(MultiParamGroupMixin, self).updateWidgets()

        # Copy parameters from an experiment selected if any
        uuid = self.request.get('uuid')
        expobj = None
        if uuid:
            expobj = uuidToObject(uuid)

        # update groups here
        for group in (g for groups in self.param_groups.values() for g in groups):
            try:
                group.update()

                # copy algorithm group parameters from the specified SDM experiment if any
                if not expobj or group.toolkit not in expobj.parameters:
                    continue

                # There are 3 groups of algorithm parameters: invisible, pseudo absence, and others.
                # Copy parameters from the pseudo absence, and others only.
                exp_params = expobj.parameters.get(group.toolkit, {})
                for param_group in group.groups:
                    for name in tuple(param_group.widgets):
                        pname = name.split(group.toolkit + '.')[1]
                        if pname in exp_params:
                            conv = getMultiAdapter((param_group.fields[name].field, param_group.widgets[name]), IDataConverter)
                            param_group.widgets[name].value = conv.toWidgetValue(exp_params.get(pname))

            except Exception as e:
                LOG.info("Group %s failed: %s", group.__name__, e)
    def __call__(self):
        uid = self.request.get('ploneuid', None)

        if uid is None:
            raise AttributeError('Missing a plone uid')

        return _breadcrumbs_from_obj(uuidToObject(uid))
Exemple #36
0
 def __init__(self, context, request, **info):
     tile_data = context.data
     if tile_data.get('image') not in (None, True):
         self.context = context
     elif tile_data.get('uuid') is not None:
         self.context = uuidToObject(tile_data.get('uuid'))
     self.request = request
 def internal_url(self):
     object_path = self.data.internal_target
     internal_obj = uuidToObject(object_path)
     if internal_obj:
         return internal_obj.absolute_url()
     else:
         return ''
Exemple #38
0
    def clubs(self, start=0, size=11):
        """Get all clubs related to a person.
        """
        mship = getToolByName(self.context, 'portal_membership')

        home = mship.getHomeFolder()

        catalog = getToolByName(self.context, 'portal_catalog')

        portal_workflow = getToolByName(self.context, "portal_workflow")
        
        clubs = [dict(clubobj=uuidToObject(relation.getObject().foreign_id),
                relation=relation, portal_type=relation.getObject().aq_inner.aq_parent.portal_type, 
                parentobj=relation.getObject().aq_inner.aq_parent, 
                status=portal_workflow.getStatusOf("hejasverige_relation_workflow", relation.getObject())['review_state'])
                for relation in
                catalog({'object_provides': IRelation.__identifier__,
                'review_state': ('pending', 'approved'),                                        
                'path': dict(query='/'.join(home.getPhysicalPath()),),
                'sort_on': 'sortable_title'})]

        clubs = [x for x in clubs if x['clubobj']]
        # for j in [i for i in dir(clubs[0].get('relation').getObject().aq_inner.aq_parent) if i.startswith('p')]: print j
        # clubs[0].get('relation').getObject().aq_inner.aq_parent.portal_type = 'hejasverige.person'
        return clubs
Exemple #39
0
def upgrade(context):
    # Move the Storage to catalog
    storage = getUtility(IMultilingualStorage)
    canonicals = storage.get_canonicals()
    already_added_canonicals = []
    generator = queryUtility(IUUIDGenerator)
    for canonical in canonicals.keys():
        canonical_object = canonicals[canonical]
        canonical_languages = canonical_object.get_keys()
        if id(canonical_object) not in already_added_canonicals:
            tg = generator()
            for canonical_language in canonical_languages:
                obj = uuidToObject(canonical_object.get_item(canonical_language))
                if obj is not None:
                    IMutableTG(obj).set(tg)
                    obj.reindexObject()
            already_added_canonicals.append(id(canonical_object))
    # Uninstall the utility
    getSite().getSiteManager().unregisterUtility(storage, IMultilingualStorage)
    del storage
    # Install the index and rebuild
    pcatalog = getToolByName(context, 'portal_catalog', None)
    if pcatalog is not None:
        indexes = pcatalog.indexes()
        if 'TranslationGroup' not in indexes:
            pcatalog.addIndex('TranslationGroup', 'FieldIndex')
            pcatalog.manage_reindexIndex(ids=['TranslationGroup'])
        else:
            pcatalog.clearFindAndRebuild()
    transaction.commit()
Exemple #40
0
    def results(self):
        """Return the list of objects stored in the tile as UUID. If an UUID
        has no object associated with it, removes the UUID from the list.

        :returns: a list of objects.
        """
        self.set_limit()

        # always get the latest data
        uuids = ITileDataManager(self).get().get('uuids', None)

        results = list()
        if uuids:
            ordered_uuids = [(k, v) for k, v in uuids.items()]
            ordered_uuids.sort(key=lambda x: x[1]['order'])

            for uuid in [i[0] for i in ordered_uuids]:
                obj = uuidToObject(uuid)
                if obj:
                    results.append(obj)
                else:
                    # maybe the user has no permission to access the object
                    # so we try to get it bypassing the restrictions
                    catalog = api.portal.get_tool('portal_catalog')
                    brain = catalog.unrestrictedSearchResults(UID=uuid)
                    if not brain:
                        # the object was deleted; remove it from the tile
                        self.remove_item(uuid)
                        logger.debug(
                            'Nonexistent object {0} removed from '
                            'tile'.format(uuid)
                        )

        return results[:self.limit]
    def results(self):
        self.configured_fields = self.get_configured_fields()
        size_conf = [i for i in self.configured_fields if i['id'] == 'number_to_show']

        if size_conf and 'size' in size_conf[0].keys():
            size = int(size_conf[0]['size'])
        else:
            size = 4

        offset = 0
        offset_conf = [i for i in self.configured_fields if i['id'] == 'offset']
        if offset_conf:
            try:
                offset = int(offset_conf[0].get('offset', 0))
            except ValueError:
                offset = 0

        uuid = self.data.get('uuid', None)
        obj = uuidToObject(uuid)
        if uuid and obj:
            results = obj.results(batch=False)
            if self.data.get('random', False):
                if size > len(results):
                    size = len(results)
                return random.sample(results, size)

            return results[offset:offset + size]
        else:
            self.remove_relation()
            return []
Exemple #42
0
def get(path=None, UID=None):
    """Get an object.

    :param path: Path to the object we want to get, relative to
        the portal root.
    :type path: string
    :param UID: UID of the object we want to get.
    :type UID: string
    :returns: Content object
    :raises:
        ValueError,
    :Example: :ref:`content_get_example`
    """
    if path:
        site = portal.get()
        site_absolute_path = '/'.join(site.getPhysicalPath())
        if not path.startswith('{0}'.format(site_absolute_path)):
            path = '{0}{1}'.format(site_absolute_path, path)

        try:
            return site.restrictedTraverse(path)
        except (KeyError, AttributeError):
            return None  # When no object is found don't raise an error

    elif UID:
        return uuidToObject(UID)
 def contained_objs(self):
     val = []
     for uuid in self.contained:
         obj = uuidToObject(uuid)
         if obj and not obj in val:
             val.append(obj)
     return val
Exemple #44
0
def get(path=None, UID=None):
    """Get an object.

    :param path: Path to the object we want to get, relative to
        the portal root.
    :type path: string
    :param UID: UID of the object we want to get.
    :type UID: string
    :returns: Content object
    :raises:
        ValueError,
    :Example: :ref:`content_get_example`
    """
    if path and UID:
        raise ValueError('When getting an object combining path and UID '
                         'attribute is not allowed')

    if not path and not UID:
        raise ValueError('When getting an object path or UID attribute is '
                         'required')

    if path:
        site = portal.get()
        site_id = site.getId()
        if not path.startswith('/{0}'.format(site_id)):
            path = '/{0}{1}'.format(site_id, path)

        try:
            return site.restrictedTraverse(path)
        except KeyError:
            return None  # When no object is found don't raise an error

    elif UID:
        return uuidToObject(UID)
Exemple #45
0
    def results(self):
        """Return the list of objects stored in the tile as UUID. If an UUID
        has no object associated with it, removes the UUID from the list.

        :returns: a list of objects.
        """
        self.set_limit()

        # always get the latest data
        uuids = ITileDataManager(self).get().get('uuids', None)

        results, remove = [], []
        if uuids:
            for uid in uuids:
                obj = uuidToObject(uid)
                if obj:
                    results.append(obj)
                else:
                    # maybe the user has no permission to access the object
                    # so we try to get it bypassing the restrictions
                    catalog = api.portal.get_tool('portal_catalog')
                    brain = catalog.unrestrictedSearchResults(UID=uid)
                    if not brain:
                        # the object was deleted; remove it from the tile
                        # we deal with this below as you can't modify the
                        # list directly as it's been used on the for loop
                        remove.append(uid)

            for uid in remove:
                self.remove_item(uid)
                logger.debug(
                    'Nonexistent object {0} removed from tile'.format(uid))

        return results[:self.limit]