Example #1
0
 def __init__(self, context):
     self.context = context
     annotations = IAnnotations(context)
     mapping = annotations.get(KEY)
     if mapping is None:
         mapping = annotations[KEY] = PersistentDict({'status' : 'Bad'})
     self.mapping = mapping
Example #2
0
    def challenge( self, request, response, **kw ):

        """ Challenge the user for credentials.
        
        Prevent redirect to login page for paths mentioned in control panel"
        """
        
        portal = getSite()
        annotations = IAnnotations(portal)
        do_basic_auth_paths = annotations.get('rohberg.doorman.do_basic_auth_paths', [])
        if do_basic_auth_paths:
            realm = response.realm
            
            do_basic_auth = False
            vup = request.get("VIRTUAL_URL_PARTS", None)
            if vup:
                vup = list(vup)
                if len(vup) > 2:
                    do_basic_auth = vup[2] in do_basic_auth_paths 
            if do_basic_auth:
                if realm:
                    response.addHeader('WWW-Authenticate',
                                   'basic realm="%s"' % realm)
                m = "<strong>You are not authorized to access this resource.</strong>"

                response.setBody(m, is_error=1)
                response.setStatus(401)
                return 1
        return 0
Example #3
0
    def get_reference_mapping(self, obj=None):
        type_key = self.get_type_key(obj)
        annotations = IAnnotations(self.context)

        if not annotations.get(type_key):
            annotations[type_key] = PersistentDict({})
        return annotations[type_key]
Example #4
0
def storeResponseInRAMCache(request, response, result, globalKey=PAGE_CACHE_KEY, annotationsKey=PAGE_CACHE_ANNOTATION_KEY):
    """Store the given response in the RAM cache.
    
    ``result`` should be the response body as a string.
    
    ``globalKey`` is the global cache key. This needs to be the same key
    as the one used to fetch the data.
    
    ``annotationsKey`` is the key in annotations on the request from which 
    the (resource-identifying) caching key should be retrieved. The default
    is that used by the ``cacheInRAM()`` helper function.
    """
    
    annotations = IAnnotations(request, None)
    if annotations is None:
        return
    
    key = annotations.get(annotationsKey)
    if not key:
        return
    
    cache = getRAMCache(globalKey)
    if cache is None:
        return
    
    status = response.getStatus()
    headers = dict(request.response.headers)
    gzipFlag = response.enableHTTPCompression(query=True)
    
    cache[key] = (status, headers, result, gzipFlag)
Example #5
0
def get_content_image(context,
                      scale='large',
                      width=None,
                      height=None):
    request = getRequest()
    modification = context.ModificationDate()
    key = "cache-%s-%s-%s-%s-%s" % (context, modification, scale, width, height)
    cache = IAnnotations(request)
    img = cache.get(key, None)
    if not img:
        view, field = get_images_view(context)
        if view:
            try:
                sizes = view.getImageSize(field)
            except AttributeError:
                sizes = img = None
            if sizes:
                kwargs = {}
                if not (width or height):
                    kwargs['scale'] = scale
                else:
                    new = (width, height)
                    width, height = _image_size(sizes, new)
                    kwargs['width'] = width
                    kwargs['height'] = height
                    kwargs['direction'] = 'down'
                try:
                    img = view.scale(fieldname=field, **kwargs)
                except AttributeError:
                    img = None
        cache[key] = img
    return img
    def _update_AgentInfoPortlet_ProfilePage(self, folders, data):
        """Override Annotation for plone.mls.listing AgentInfo inside AgentProfilePages"""
        #get agents portrait/ avatar url
        avatar_url = self.membershiptool.getPersonalPortrait(id=self.userid).absolute_url()
        #get AgencyInfo
        agency = self.__AgencyInfo

        for folder in folders:

            if IAgentFolder.providedBy(folder) and ILocalAgencyInfo.providedBy(folder):
                #get annotations for this folder
                mls_ano = IAnnotations(folder).get("plone.mls.listing.localagencyinfo", None)

                if mls_ano is None:
                    #initialize Annotations
                    anno = IAnnotations(folder)
                    anno.get("plone.mls.listing.localagencyinfo", anno.setdefault("plone.mls.listing.localagencyinfo", {}))
                    mls_ano = IAnnotations(folder).get("plone.mls.listing.localagencyinfo", {})

                # set global Agency Info
                mls_ano['agency_name'] = agency.get('agency_name', u'Krain Real Estate')
                mls_ano['agency_logo_url'] = agency.get('agency_logo_url', u'')
                mls_ano['agency_office_phone'] = agency.get('agency_office_phone', u'')
                mls_ano['agency_website'] = agency.get('agency_website', u'')
                
                #Agent Info
                mls_ano['agent_name'] = data.get('fullname', u'')
                mls_ano['agent_office_phone'] = data.get('office_phone', u'')
                mls_ano['agent_cell_phone'] = data.get('cell_phone', u'')
                mls_ano['agent_email'] = data.get('email', u'')
                mls_ano['agent_avatar_url'] = avatar_url

                #force overrding of Any other agent
                mls_ano['force'] = 'selected'
Example #7
0
 def get_number(self, obj):
     ann = IAnnotations(obj)
     if SEQUENCE_NUMBER_ANNOTATION_KEY not in ann.keys():
         generator = getAdapter(obj, ISequenceNumberGenerator)
         value = generator.generate()
         ann[SEQUENCE_NUMBER_ANNOTATION_KEY] = value
     return ann.get(SEQUENCE_NUMBER_ANNOTATION_KEY)
def upgrade_executed_jobs_storage(portal_setup):
    """The executed jobs storage has changed from PersistentList
    to IOBTree storage, so we need to migrate the storage.

    """
    portal = portal_setup.portal_url.getPortalObject()
    queue = IQueue(portal)
    annotations = IAnnotations(portal)

    if 'publisher-executed' not in annotations:
        # No data to migrate.
        return

    # get jobs directly from the annotations - accessing with
    # queue methods is not possible yet
    jobs = list(annotations.get('publisher-executed', []))

    # drop the current list
    del annotations['publisher-executed']

    # add every single job with the new methods
    for job in jobs:
        queue.append_executed_job(job)

    # check if it worked
    assert len(jobs) == queue.get_executed_jobs_length()
 def __init__(self, context):
     self.context = context
     annotations = IAnnotations(self.context)
     self.__mapping = annotations.get(self.ANNO_KEY, None)
     if self.__mapping is None:
         self.__mapping = PersistentList()
         annotations[self.ANNO_KEY] = self.__mapping
def openFileReference(transmogrifier, ref):
    """
    Get an open file handle in one of the following forms:

    * importcontext:file.txt
    * dotted.package:file.txt
    * /file/system/file.txt

    Where "importcontext:" means find the file in the GS import context.
    Return None if there was no file to be found
    """
    if ref.startswith('importcontext:'):
        try:
            from collective.transmogrifier.genericsetup import IMPORT_CONTEXT
            from zope.annotation.interfaces import IAnnotations
            context = IAnnotations(transmogrifier).get(IMPORT_CONTEXT, None)
            (subdir, filename) = os.path.split(ref.replace('importcontext:', ''))
            if subdir == '':
                # Subdir of '' results import contexts looking for a ''
                # directory I think
                subdir = None
            if hasattr(context, "openDataFile"):
                return context.openDataFile(filename, subdir=subdir)
            if hasattr(context, "readDataFile"):
                import StringIO
                return StringIO.StringIO(
                    context.readDataFile(filename, subdir=subdir))
        except ImportError:
            return None
    # Either no import context or not there.
    filename = resolvePackageReferenceOrFile(ref)
    if os.path.isfile(filename):
        return open(filename, 'r')
    return None
Example #11
0
    def show(self):
        """ Removes all status messages (including HTML) and returns them 
            for display.
        """
        context = self.context
        annotations = IAnnotations(context)
        msgs = annotations.get(STATUSMESSAGEKEY,
                                context.cookies.get(STATUSMESSAGEKEY))
        msgs = msgs and adapter._decodeCookieValue(msgs) or []

        html_msgs = annotations.get(HTMLMESSAGEKEY,
                                context.cookies.get(HTMLMESSAGEKEY))
        html_msgs = html_msgs and adapter._decodeCookieValue(html_msgs) or []

        for msg in html_msgs:
            msg.message = literal(sanitize(msg.message, cleaner=msgcleaner, wrap=None))

        value = msgs + html_msgs
        
        # clear the existing cookie entries, except on responses that don't
        # actually render in the browser (really, these shouldn't render
        # anything so we shouldn't get to this message, but some templates
        # are sloppy).
        if self.context.response.getStatus() not in (301, 302, 304):
            context.cookies[STATUSMESSAGEKEY] = None
            context.response.expireCookie(STATUSMESSAGEKEY, path='/')
            annotations[STATUSMESSAGEKEY] = None

            context.cookies[HTMLMESSAGEKEY] = None
            context.response.expireCookie(HTMLMESSAGEKEY, path='/')
            annotations[HTMLMESSAGEKEY] = None
        
        return value
class TilesPermissions(object):
    """
    An adapter that will provide store permissions for a tile
    """

    implements(ITilesPermissions)

    def __init__(self, context, request, tile):
        self.context = context
        self.request = request
        self.tile = tile
        self.annotations = IAnnotations(self.context)
        self.key = '{0}.{1}'.format(ANNOTATIONS_KEY_PREFIX, tile.id)

    def get_allowed_edit(self):
        permissions = dict(self.annotations.get(self.key, {}))

        return permissions.get('edit', ())

    def set_allowed_edit(self, group_ids):
        permissions = dict(self.annotations.get(self.key, {}))

        if isinstance(group_ids, list):
            group_ids = tuple(group_ids)
        elif isinstance(group_ids, basestring):
            group_ids = (group_ids,)

        permissions['edit'] = group_ids

        self.annotations[self.key] = PersistentDict(permissions)

    def delete(self):
        self.annotations.pop(self.key, None)
        return
    def handleUpdate(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        annotations = IAnnotations(self.context)
        storage = annotations.setdefault('geoportlet', PersistentMapping())
        database = Software77GeoDatabase(storage)

        url = data['url']
        url = url and url.encode('utf-8') or None

        try:
            count = database.update(url)
        except IOError as exc:
            IStatusMessage(self.request).addStatusMessage(
                _(u"An error occurred: ${error}.",
                  mapping={'error': exc}), "info")
        else:
            try:
                count = '{0:,}'.format(count)
            except ValueError:
                pass

            IStatusMessage(self.request).addStatusMessage(
                _(u"Database updated (${count} records read).",
                  mapping={'count': count}),
                "info")
Example #14
0
 def cleanComments(self):
     """
     """
     annotations = IAnnotations(self.context)
     if annotations.has_key(self.key):
         del annotations[self.key]
     self._comments = None
    def handle_layout(self):
        """
        This will check if the file does not have the
        document viewer display selected.

        In addition, if the currently selected display
        is for wc.pageturner, we'll clean out the annotations
        from that product. Additionally, we'll also look
        for wildcard.pdfpal related values.
        """
        current = self.context.getLayout()
        if current != 'documentviewer':
            self.context.layout = 'documentviewer'
        annotations = IAnnotations(self.context)
        # remove page turner related
        data = annotations.get('wc.pageturner', None)
        if data:
            del annotations['wc.pageturner']

        # remove pdfpal related
        field = self.context.getField('ocrText')
        if field:
            field.set(self.context, '')

        data = annotations.get('wildcard.pdfpal', None)
        if data:
            del annotations['wildcard.pdfpal']
Example #16
0
 def clean_folder(self, catalog, storage_loc):
     if not os.path.isdir(storage_loc):
         return 0
     count = 0
     for foldername in os.listdir(storage_loc):
         if len(foldername) == 1:
             # we're in a container, check inside
             count += self.clean_folder(catalog,
                 os.path.join(storage_loc, foldername))
         else:
             #foldername should be file uid
             brains = catalog(UID=foldername)
             folderpath = os.path.join(storage_loc, foldername)
             if len(brains) == 0:
                 shutil.rmtree(folderpath)
                 count += 1
             else:
                 obj = brains[0].getObject()
                 settings = Settings(obj)
                 if obj.getLayout() != 'documentviewer':
                     if not settings.converting:
                         shutil.rmtree(folderpath)
                         count += 1
                         # also delete settings
                         annotations = IAnnotations(obj)
                         data = annotations.get('collective.documentviewer',
                                                None)
                         if data:
                             del annotations['collective.documentviewer']
                 elif settings.storage_type == 'Blob':
                     shutil.rmtree(folderpath)
                     count += 1
     return count
Example #17
0
    def test_annotations_key_is_cleared(self):
        annotations = IAnnotations(self.document)
        self.assertEquals(TEST_USER_ID, annotations.get(CHECKIN_CHECKOUT_ANNOTATIONS_KEY))

        self.manager.checkin()

        self.assertEquals(None, annotations.get(CHECKIN_CHECKOUT_ANNOTATIONS_KEY))
Example #18
0
    def migrate_portlet_assignments(self, obj):
        from ftw.subsite.portlets.teaserportlet import ITeaserPortlet

        try:
            annotations = IAnnotations(obj)
        except TypeError:
            # The obj has no annotations
            return

        known_manager = annotations.get(
            'plone.portlets.contextassignments', [])

        for managername in tuple(known_manager):
            if managername not in self.MANAGER_NAMES:
                continue

            manager = getUtility(IPortletManager, name=managername,
                                 context=obj)
            assignments = getMultiAdapter((obj, manager),
                                          IPortletAssignmentMapping,
                                          context=obj)
            assignments._p_changed = True

            for assignment in assignments.values():
                if not ITeaserPortlet.providedBy(assignment):
                    continue

                if not assignment.__dict__.get('image'):
                    continue

                assignment.image = assignment.__dict__.pop('image', None)
                assignment._p_changed = True
def localPortletAssignmentMappingAdapter(context, manager):
    """This is pretty much the same code of the original one from

    `plone.app.portlets.assignable.localPortletAssignmentMappingAdapter`

    but it changes the assignment klass with `Mapping`.

    This is needed in order to use our custom view '+'
    for adding the portlet.
    """
    annotations = IAnnotations(context)
    local = annotations.get(CONTEXT_ASSIGNMENT_KEY, None)
    if local is None:
        local = annotations[CONTEXT_ASSIGNMENT_KEY] = OOBTree()

    portlets = local.get(manager.__name__, None)
    if portlets is None:
        portlets = local[manager.__name__] = Mapping(manager=manager.__name__,
                                                     category=CONTEXT_CATEGORY)

    # XXX: For graceful migration
    if not getattr(portlets, '__manager__', ''):
        portlets.__manager__ = manager.__name__

    if not getattr(portlets, '__category__', ''):
        portlets.__category__ = CONTEXT_CATEGORY

    return portlets
 def get_member_vote(self, object):
     """ """
     if not IAttributeAnnotatable.providedBy(object):
         alsoProvides(object, IAttributeAnnotatable)
     annotations = IAnnotations(object)
     voters = annotations.get('opkode.rateablediscussion.voters', {})
     return voters.get(self.member.getId(), None)
Example #21
0
    def getAccessibilityCompliant(self):
        """ Get the Accessibility Compliant value from the object  """
        annotations = IAnnotations(self.context)
	if annotations.has_key('eduCommons.accessible'):
            return annotations['eduCommons.accessible']
        else:
	    return
Example #22
0
 def __init__(self, context, request):
     """self:zope.app.pagetemplate.simpleviewclass.SimpleViewClass -> 
                 templates/workspace-index.pt
        context:bungeni.core.content.Section
     """
     LD = IAnnotations(request)["layer_data"]
     assert interfaces.IWorkspaceSectionLayer.providedBy(request)
     assert LD.get("workspaces") is not None
     super(WorkspaceSectionView, self).__init__(context, request)
     cls_name = self.__class__.__name__ 
     # NOTE: Zope morphs this class's name to "SimpleViewClass from ..." 
     log.debug("%s.__init__ %s context=%s url=%s" % (
                         cls_name, self, self.context, request.getURL()))
     
     # transfer layer data items, for the view/template
     self.user_id = LD.user_id
     self.user_group_ids = LD.user_group_ids
     self.government_id = LD.government_id # may be None
     self.ministries = LD.ministries # may be None
     if self.ministries:
         # then, ONLY if an ancestor container is actually a Ministry, 
         # this must be a MinisterWorkspace
         if misc.get_parent_with_interface(self, model_interfaces.IMinistry):
             interface.alsoProvides(self, interfaces.IMinisterWorkspace)
     
     # roles are function of the context, so always recalculate
     roles = get_roles(self.context)
     for role_id in roles:
         iface = self.role_interface_mapping.get(role_id)
         if iface is not None:
             interface.alsoProvides(self, iface)
     log.debug("%s.__init__ %s" % (cls_name, debug.interfaces(self)))
Example #23
0
 def update(self):
     super(ShopArticleListingViewlet, self).update()
     annotations = IAnnotations(self.context)
     current_objs_p_mtime = annotations.get('_objs_p_mtime')
     objs_p_mtime = [obj.image._p_mtime for obj in self._objs()]
     if current_objs_p_mtime != objs_p_mtime:
         annotations['_objs_p_mtime'] = objs_p_mtime
 def __init__(self, context, request):
     super(SQLDexterityPublishTraverse, self).__init__(context, request)
     annotations = IAnnotations(context)
     self.fti_id = annotations.get('collective.behavior.sql.sql_type')
     if self.fti_id:
         self.fti = getUtility(IDexterityFTI, name=self.fti_id)
         name = getattr(ISQLTypeSettings(self.fti), 'sql_folder_id', self.fti_id)
         if name and IRelationValue.providedBy(name):
             obj = name.to_object
             if obj:
                 name = obj.getId()
             else:
                 name = self.fti_id
             
         elif name and name.startswith('/'):
             portal = getToolByName(getSite(), 'portal_url').getPortalObject()
             obj = portal.restrictedTraverse(name)
             if obj:
                 name = obj.getId()
             else:
                 name = self.fti_id
         elif not name:
             name = self.fti_id
         self.name = name
         self.Title = self.fti.Title()
Example #25
0
    def selectedPacket(self):
        annotations = IAnnotations(self.context)
        packet_key = annotations.get(PACKETS_KEY + '.type')
        packet_fields = annotations.get(PACKETS_KEY + '.fields')
        packet_mapui = annotations.get(PACKETS_KEY + '.mapui')

        return dict(packet_key=packet_key, value=packet_fields.get(packet_mapui.get('codi')), element=packet_fields.get(packet_mapui.get('element')))
Example #26
0
    def has_videos(self):
        view = getMultiAdapter((self.context, self.request), name='nota')
        videos = []
        if view:
            context_path = '/'.join(self.context.getPhysicalPath())
            query = {'Type': ('Link',)}
            query['path'] = {'query': context_path,
                             'depth': 1, }
            query['sort_on'] = 'getObjPositionInParent'
            query['limit'] = None

            results = self.context.getFolderContents(contentFilter=query,
                                                     batch=False,
                                                     b_size=10,
                                                     full_objects=False)

            for link in results:
                link_obj = link.getObject()
                annotations = IAnnotations(link_obj)
                is_video = annotations.get('thumbnail_pequeno', None)
                if is_video:
                    videos.append({'obj': link,
                                   'url': link_obj.absolute_url() + '/@@thumbnail_mediano'})

        return videos
 def getContent(self):
     """Get the annotations with the local MLS config."""
     annotations = IAnnotations(self.context)
     return annotations.get(
         CONFIGURATION_KEY,
         annotations.setdefault(CONFIGURATION_KEY, {}),
     )
    def get_journal_data(self):
        if IAnnotationsJournalizable.providedBy(self.context):
            annotations = IAnnotations(self.context)
            data = annotations.get(JOURNAL_ENTRIES_ANNOTATIONS_KEY, [])
            return deepcopy(data)

        return []
 def getSourceName(self, instance):
     """Get the name of the data source that is used in this context."""
     mapping = IAnnotations(instance).setdefault(
         'collective.table',
         PersistentMapping()
     )
     return mapping.get('source_name', self.defaultSourceName)
Example #30
0
def purge_unassigned_notes(context):
    # this indiscriminately purge all notes not found within an file's
    # specified exposure file type.
    # XXX implement an alternate option that will retain any specified
    # notes
    # XXX don't make this method available in the upgrade step just yet.
    from zope.annotation.interfaces import IAnnotations

    prefix = 'pmr2.annotation.notes-'
    catalog = getToolByName(context, 'portal_catalog')
    files = catalog(portal_type='ExposureFile')
    for b in files:
        file = b.getObject()
        ftpath = file.file_type
        if not ftpath:
            continue

        cftviews = filetypes[ftpath]  # file type views for current file

        # remove unreferenced annotations
        # XXX this assumes users don't manually assign views after
        # specifying file types.
        annotations = IAnnotations(file)
        for k in annotations.keys():
            if not k.startswith(prefix):
                continue
            viewname = k[len(prefix):]
            if not viewname in cftviews:
                del annotations[k]
Example #31
0
def get_total(context, paragrafo_id):
    """ Devolve um dicionário com total de concordâncias e discordâncias.
    """
    inicializa_anotacoes(context)
    annotations = IAnnotations(context)
    ressalvas = 0

    for voto in annotations[concordancias][paragrafo_id].itervalues():
        if 'ressalva' in voto.keys():
            ressalvas += 1
    return {
        'concordancias':
        (len(annotations[concordancias][paragrafo_id]) - ressalvas),
        'concordancias_ressalva':
        ressalvas,
        'discordancias':
        len(annotations[discordancias][paragrafo_id])
    }
Example #32
0
def avoid_profile_reinstallation(event):
    profile = re.sub('^profile-', '', event.profile_id)
    assert profile not in FORBIDDEN_PROFILES, \
        'It is not allowed to install the profile {!r}.'.format(profile)

    request = getSite().REQUEST
    annotations = IAnnotations(request)
    key = 'opengever.core:avoid_profile_reinstallation'
    if key not in annotations:
        annotations[key] = []

    if should_prevent_duplicate_installation(profile):
        assert profile not in annotations[key], \
            'Profile {!r} should not be installed twice.'.format(profile)
    elif profile in annotations[key]:
        LOG.warning('%r installed twice', profile)

    annotations[key].append(profile)
    def test_inactive_links_if_document_is_checked_out(self, browser):
        """Check the document overview when the document is checked out,
        by another user:
        - checked out information
        - edit link is inactive
        """
        IAnnotations(
            self.document)[CHECKIN_CHECKOUT_ANNOTATIONS_KEY] = 'hugo.boss'

        transaction.commit()

        browser.login().visit(self.document, view='tabbedview_view-overview')

        self.assertEquals('Checkout and edit',
                          browser.css('.function-edit-inactive').first.text)
        self.assertEquals(
            'Download copy',
            browser.css('.function-download-copy-inactive').first.text)
Example #34
0
    def action_save(self, action, data):
        request = self.context.request
        context = Acquisition.aq_inner(self.context)
        canonical = context.getCanonical()
        annotations = IAnnotations(canonical)
        annotations[FEED_KEY] = data[FEED_KEY]
        if data[PRESS_CONTACTS_KEY]:
            annotations[PRESS_CONTACTS_KEY] = \
                        request.form.get('form.press_contacts', [])

        if data[KEYWORDS_KEY]:
            annotations[KEYWORDS_KEY] = \
                            (data[KEYWORDS_KEY]).strip().split(' ')
        else:
            annotations[KEYWORDS_KEY] = []

        return request.RESPONSE.redirect('%s/@@dynamic-pressroom/' %
                                         '/'.join(context.getPhysicalPath()))
Example #35
0
    def test_standard_fs_workflow(self):
        # Register a backend that requires filesystem
        self.portal.getSiteManager().registerUtility(self.backend,
                                                     provided=IStorageBackend,
                                                     name=u'dummy_fs_backend')
        utilities = getUtility(IUtilityRegistry, 'repodono.storage.backends')
        utilities.enable('dummy_fs_backend')

        installer = getUtility(IStorageInstaller)
        installer(self.folder, 'dummy_fs_backend')

        self.assertIn('repodono.storage.base.StorageInfo',
                      IAnnotations(self.folder).keys())

        storage = IStorage(self.folder)
        self.assertEqual(storage.path,
                         join(self.tempdir, 'plone', TEST_FOLDER_ID))
        self.assertTrue(exists(storage.path))
Example #36
0
    def set_json(self, value):
        """ Set json dict

        Items are not persisted within annotations, they should be
        dynamically generated each time according with JSON['properties']

            >>> visualization.json = {
            ...   'items': [1, 2, 3],
            ...   'properties': {'a': 'b'}}
            >>> visualization.json
            {'items': [], 'properties': {'a': 'b'}}

        """

        value['items'] = []
        value.setdefault('properties', {})
        anno = IAnnotations(self.context)
        anno[ANNO_JSON] = PersistentDict(value)
Example #37
0
    def test_annotation_source_schema_violation_recovery(self):
        dummy = IDummy3(self.portal)
        dummy.field2 = u'A valid value'

        annotations = IAnnotations(self.portal)
        value = annotations['repodono.storage.tests.test_annotation.Dummy3']
        value['field2'] = 222
        self.assertRaises(TypeError, IDummy3, self.portal)

        # can still extract with a bare alternative class that isn't
        # registered in ZCA but references the correct call

        @annotator(IDummy3, to_key(__name__, 'Dummy3'))
        class Dummy3alt(object):
            pass

        dummy = Dummy3alt(self.portal)
        self.assertEqual(dummy.field2, 222)
Example #38
0
    def test_no_purger(self):
        request = FauxRequest()
        alsoProvides(request, IAttributeAnnotatable)

        IAnnotations(request)['plone.cachepurging.urls'] = set(['/foo', '/bar'])

        registry = Registry()
        registry.registerInterface(ICachePurgingSettings)
        provideUtility(registry, IRegistry)

        settings = registry.forInterface(ICachePurgingSettings)
        settings.enabled = True
        settings.cachingProxies = ('http://localhost:1234',)

        try:
            notify(PubSuccess(request))
        except:
            self.fail()
def upgrade_181_190_1(context, logger=None):
    if logger is None:
        logger = LOG
    # Run GS steps
    setup = getToolByName(context, 'portal_setup')
    setup.runImportStepFromProfile(PROFILE_ID, 'controlpanel')
    setup.runImportStepFromProfile(PROFILE_ID, 'catalog')
    setup.runImportStepFromProfile(PROFILE_ID, 'actions')
    setup.runImportStepFromProfile(PROFILE_ID, 'plone.app.registry')
    # finally remove the internal rdf graph which may still linger around
    pannots = IAnnotations(api.portal.get())
    if 'gu.plone.rdf' in pannots:
        del pannots['gu.plone.rdf']
    # rebuild the catalog to make sure new indices are populated
    logger.info("rebuilding catalog")
    pc = getToolByName(context, 'portal_catalog')
    pc.clearFindAndRebuild()
    logger.info("finished")
Example #40
0
def send_report(portal, lines):
    """Send email if required."""
    emails = api.portal.get_registry_record(
        'collective.contact.importexport.interfaces.IPipelineConfiguration.emails'
    )
    if not emails:
        return
    msg = create_html_email(u'\n'.join(
        [u'<p>{}</p>'.format(line) for line in lines]))
    annot = IAnnotations(portal).get(ANNOTATION_KEY)
    for filename in ('ie_input_errors.log', 'ie_shortlog.log'):
        path = os.path.join(annot['wp'], filename)
        add_attachment(msg, filename, filepath=path)
    mfrom = portal.getProperty('email_from_address')
    ret, error = send_email(msg, u'Contact import report', mfrom, emails)
    if not ret:
        with open(os.path.join(annot['wp'], 'ie_input_errors.log'), 'a') as f:
            f.write('Your email has not been sent: {}'.format(error))
Example #41
0
class FeedSettings:
    adapts(ISyndicatable)

    def __init__(self, context):
        self.context = context
        self.annotations = IAnnotations(context)
        self.needs_saving = False

        self._metadata = self.annotations.get(FEED_SETTINGS_KEY, None)
        if self._metadata is None:
            self._metadata = PersistentDict()
            self.needs_saving = True

        registry = getUtility(IRegistry)
        self.site_settings = registry.forInterface(ISiteSyndicationSettings,
                                                   check=False)

    def _set(self):
        """
        what are we doing here you might ask?
        well, this causes us to write on read so only set on annotation
        if we need to
        """
        if self.needs_saving:
            self.annotations[FEED_SETTINGS_KEY] = self._metadata

    def __setattr__(self, name, value):
        if name in ('context', '_metadata', 'site_settings', 'annotations',
                    'needs_saving'):
            self.__dict__[name] = value
        else:
            self._metadata[name] = value
            self._set()

    def __getattr__(self, name):
        default = None
        if name in ISiteSyndicationSettings.names():
            default = getattr(self.site_settings, name)
        elif name == 'enabled' and self.site_settings.default_enabled:
            default = True
        elif name in IFeedSettings.names():
            default = IFeedSettings[name].default

        return self._metadata.get(name, default)
Example #42
0
    def get_event_list(self, start=None, stop=None):
        now = DateTime()
        events = (i.getObject() for i in self.get_events()
                  if not (getattr(i, 'outdated', False) and isExpired(i)))
        months = []
        month_info = []
        old_month_year = None
        for event in events:
            start = event.start()
            month = str(start.month())
            year = str(start.year())
            month_year = year + month
            if month_year != old_month_year:
                old_month_year = month_year
                if month_info:
                    months.append(month_info)
                month_info = {
                    'month': start.month(),
                    'year': start.year(),
                    # e.g. month_oct as used in the plonelocales i18n:domain
                    'month_name':
                    "month_%s" % (start.strftime("%B").lower()[:3]),
                    'events': []
                }
            isDateToBeConfirmed = (True if hasattr(event, "dateToBeConfirmed")
                                   and event.dateToBeConfirmed else False)
            isOutdated = IAnnotations(event).get("slc.outdated", False)
            event_dict = {
                'event': event,
                'day': start.day(),
                'start': start,
                'end': event.end(),
                'location': event.getLocation(),
                'title': event.Title(),
                'description': event.Description(),
                'url': event.absolute_url(),
                'is_tbc': isDateToBeConfirmed,
                'is_outdated': isOutdated
            }
            month_info['events'].append(event_dict)

        if month_info:
            months.append(month_info)
        return months
Example #43
0
def annotation_alias(annotation_name,
                     annotation_property=None,
                     default=None,
                     delete=False,
                     delete_quiet=True,
                     doc=None):
    """
    Returns a property that is a read/write alias for
    a value stored as a :class:`zope.annotation.interface.IAnnotations`.

    The object itself may be adaptable to an IAnnotations, or a property
    of the object may be what is adaptable to the annotation. The later is intended
    for use in adapters when the context object is what should be adapted.

    :keyword bool delete: If ``True`` (not the default), then the property can be used
            to delete the annotation.
    :keyword bool delete_quiet: If ``True`` and `delete` is also True, then the property
            will ignore key errors when deleting the annotation value.
    :keyword str annotation_property: If set to a string, it is this property
            of the object that will be adapted to IAnnotations. Most often this will
            be ``context`` when used inside an adapter.
    """

    if doc is None:
        doc = 'Alias for annotation ' + annotation_name

    factory = IAnnotations
    if annotation_property:
        factory = lambda self: IAnnotations(getattr(self, annotation_property))

    fget = lambda self: factory(self).get(annotation_name, default)
    fset = lambda self, nv: operator.setitem(factory(self), annotation_name, nv
                                             )
    fdel = None
    if delete:

        def fdel(self):
            try:
                del factory(self)[annotation_name]
            except KeyError:
                if not delete_quiet:
                    raise

    return property(fget, fset, fdel, doc=doc)
Example #44
0
class PersistentTileDataManager(object):
    """A data reader for persistent tiles operating on annotatable contexts.
    The data is retrieved from an annotation.
    """

    def __init__(self, tile):
        self.tile = tile
        self.tileType = queryUtility(ITileType, name=tile.__name__)

        self.context = getMultiAdapter(
            (tile.context, tile.request, tile), ITileDataContext)
        self.annotations = IAnnotations(self.context)

        self.key = '.'.join([ANNOTATIONS_KEY_PREFIX, str(tile.id)])

    def _get_default_request_data(self):
        # If we don't have a schema, just take the request
        if self.tileType is None or self.tileType.schema is None:
            data = self.tile.request.form.copy()
        else:
            # Try to decode the form data properly if we can
            try:
                data = decode(self.tile.request.form,
                              self.tileType.schema, missing=True)
            except (ValueError, UnicodeDecodeError,):
                LOGGER.exception(u'Could not convert form data to schema')
                return self.data.copy()
        return data

    def get(self):
        data = self._get_default_request_data()
        data.update(dict(self.annotations.get(self.key, {})))
        if self.tileType is not None and self.tileType.schema is not None:
            for name, field in getFields(self.tileType.schema).items():
                if name not in data:
                    data[name] = field.missing_value
        return data

    def set(self, data):
        self.annotations[self.key] = PersistentDict(data)

    def delete(self):
        if self.key in self.annotations:
            del self.annotations[self.key]
Example #45
0
    def add_rule(self, data, trigger_type):
        '''
        Create new rule
        require data related to field values to create new rule
        '''

        rule_name = _(
            u'IFTTT ${trigger_type} for "${ifttt_event_name}" on '
            u'content_types ${content_types} at ${path}',
            mapping=dict(
                trigger_type=trigger_type,
                ifttt_event_name=data.get('ifttt_event_name'),
                content_types=(', ').join(data.get('content_types')),
                title=self.context.Title(),
                path=self.context.absolute_url_path(),
            ))

        rule_description = _(
            u'This rule triggers an IFTTT event '
            u'"${ifttt_event_name}" on the ${title} folder',
            mapping=dict(
                ifttt_event_name=data.get('ifttt_event_name'),
                title=safe_unicode(self.context.Title()),
            ))

        portal = api.portal.get()
        adding = getMultiAdapter((portal, self.request), name='+rule')

        addview = getMultiAdapter((adding, self.request),
                                  name='plone.ContentRule')
        addview.form_instance.update()
        content = addview.form_instance.create({
            'title': rule_name,
            'description': rule_description,
            'enabled': True,
            'stop': False,
            'cascading': False,
            'event': data.get('event')
        })
        addview.form_instance.add(content)

        # clear status messages from pipeline
        annotations = IAnnotations(self.request)
        annotations[STATUSMESSAGEKEY] = None
Example #46
0
    def test_migrate_comment(self):

        # Create a comment
        talkback = self.discussion.getDiscussionFor(self.doc)
        self.doc.talkback.createReply('My Title', 'My Text', Creator='Jim')
        reply = talkback.getReplies()[0]
        reply.setReplyTo(self.doc)
        reply.creation_date = DateTime(2003, 3, 11, 9, 28, 6, 'GMT')
        reply.modification_date = DateTime(2009, 7, 12, 19, 38, 7, 'GMT')

        self._publish(reply)
        self.assertEqual(reply.Title(), 'My Title')
        self.assertEqual(reply.EditableBody(), 'My Text')
        self.assertTrue('Jim' in reply.listCreators())
        self.assertEqual(talkback.replyCount(self.doc), 1)
        self.assertEqual(reply.inReplyTo(), self.doc)

        # Call migration script
        self.view()

        # Make sure a conversation has been created
        self.assertTrue(
            'plone.app.discussion:conversation' in IAnnotations(self.doc))
        conversation = IConversation(self.doc)

        # Check migration
        self.assertEqual(conversation.total_comments, 1)
        self.assertTrue(conversation.getComments().next())
        comment1 = conversation.values()[0]
        self.assertTrue(IComment.providedBy(comment1))
        self.assertEqual(comment1.Title(), 'My Title')
        self.assertEqual(comment1.text, '<p>My Text</p>\n')
        self.assertEqual(comment1.mime_type, 'text/html')
        self.assertEqual(comment1.Creator(), 'Jim')
        self.assertEqual(comment1.creation_date,
                         datetime(2003, 3, 11, 9, 28, 6))
        self.assertEqual(comment1.modification_date,
                         datetime(2009, 7, 12, 19, 38, 7))
        self.assertEqual([{
            'comment': comment1,
            'depth': 0,
            'id': long(comment1.id)
        }], list(conversation.getThreads()))
        self.assertFalse(self.doc.talkback)
Example #47
0
 def delete_portlet(self, obj, portlet):
     """ Delete the defined portlet on obj """
     ann = IAnnotations(obj)
     columnkey = 'plone.leftcolumn'
     if not 'plone.portlets.contextassignments' in ann:
         logger.error("No portlets defined in this context")
     elif not columnkey in ann['plone.portlets.contextassignments']:
         logger.error("Column '%s' not found in portlets definition" %
                      columnkey)
     elif not portlet in ann['plone.portlets.contextassignments'][columnkey]:
         logger.error(
             "Portlet '%s' in '%s' not found in portlets definition" %
             (portlet, columnkey))
     else:
         fixing_up = contained.fixing_up
         contained.fixing_up = True
         del ann['plone.portlets.contextassignments'][columnkey][portlet]
         # revert our fixing_up customization
         contained.fixing_up = fixing_up
Example #48
0
 def __init__(self, transmogrifier, name, options, previous):
     self.previous = previous
     self.portal = transmogrifier.context
     self.storage = IAnnotations(transmogrifier).get(ANNOTATION_KEY)
     self.ids = self.storage['ids']
     creating_group = safe_unicode(options.get('creating_group', '').strip())
     if not creating_group:
         raise Exception(u'{}: You have to set creating_group value in this section !'.format(name))
     reg = api.portal.get_registry_record('imio.dms.mail.browser.settings.IImioDmsMailConfig.contact_group_encoder')
     if not reg:
         raise Exception(u'{}: You have to activate the contact creating group option in iadocs config'.format(name))
     # get imio.dms.mail.ActiveCreatingGroupVocabulary
     from imio.dms.mail.vocabularies import ActiveCreatingGroupVocabulary
     voc = ActiveCreatingGroupVocabulary()(self.portal)
     creating_groups = {term.title: term.value for term in voc}
     if creating_group not in creating_groups:
         raise Exception(u"{}: given creating_group '{}' isn't an active creating group organization".format(
                         name, creating_group))
     self.creating_org = creating_groups[creating_group]
Example #49
0
    def test_remove_orphan_annotations(self):
        from collective.cover.tiles.configuration import ANNOTATIONS_KEY_PREFIX
        from zope.annotation.interfaces import IAnnotations

        title = u'Remove orphan annotations'
        step = self._get_upgrade_step(title)
        self.assertIsNotNone(step)

        # simulate state on previous version
        c1 = self._create_cover('c1', 'Layout A')
        annotations = IAnnotations(c1)
        foo = ANNOTATIONS_KEY_PREFIX + '.foo'
        annotations[foo] = 'bar'  # add orphan annotation

        self._create_cover('c2', 'Layout B')  # cover with no annotations

        # simulate state on previous version
        self._do_upgrade_step(step)
        self.assertNotIn(foo, annotations)
def __convert_document(obj, named_file, target_extension, fusion_data):
    # section of convert_document process that should be run asyncronously
    converted_file = get_converted_file(
        named_file,
        target_extension,
        fusion_data,
    )
    annotations = IAnnotations(obj)
    previous_status = annotations[STATUS_STORAGE_KEY]
    if converted_file is None:
        new_status = TASK_FAILED
        annotations[DATA_STORAGE_KEY] = None
    else:
        new_status = TASK_SUCCEEDED
        annotations[DATA_STORAGE_KEY] = converted_file

    annotations[STATUS_STORAGE_KEY] = new_status
    if new_status != previous_status:
        obj.setModificationDate(DateTime())
Example #51
0
 def _init_search_term(self):
     if self.query_term not in self.request.form:
         return
     annotations = IAnnotations(self.request)
     if 'trysuggestion' not in annotations:
         annotations['trysuggestion'] = True
         suggestion = self.suggest(term=self.request.form[self.query_term],
                                   encode='utf-8')
         if suggestion and suggestion != self.request.form[self.query_term]:
             logger.info(
                 "query:%s suggestion:%s term:%s",
                 self.request.form[self.query_term],
                 suggestion,
                 self.query_term,
             )
             self.request.form[self.query_term +
                               "Original"] = self.request.form[
                                   self.query_term]
             self.request.form[self.query_term] = suggestion
Example #52
0
    def __call__(self, REQUEST, RESPONSE):

        annotations = IAnnotations(self.context)

        minuta_exportada_csv = u'Planilha de dados da minuta de participação\n'

        texto = self.context.text
        if not texto:
            return ''
        pq_texto = pq(texto.output)

        for i, paragrafo in enumerate(pq_texto.find('.paragrafo')):
            paragrafo_id = i + 1
            minuta_exportada_csv += u'Parágrafo %d; %s;\n' % (paragrafo_id,
                                                              paragrafo.text)

            # Criação das linhas de dados de usuário que concordaram
            concordancias_paragrafo = annotations[concordancias][paragrafo_id]
            linha_minuta_concordantes = u'Usuários Concordantes: ;'
            linha_minuta_ressalvas = u'Ressalvas: ;'
            for usuario_concordante in concordancias_paragrafo:
                voto = concordancias_paragrafo[usuario_concordante]
                if voto.get('has_voted', False):
                    linha_minuta_concordantes += usuario_concordante + u';'
                    linha_minuta_ressalvas += safe_unicode(voto['ressalva'].replace('\n', '   ')) + u';' \
                        if 'ressalva' in voto else u' ;'
            minuta_exportada_csv += linha_minuta_concordantes + u'\n'
            minuta_exportada_csv += linha_minuta_ressalvas + u'\n'

            # Criação das linhas de dados de usuário que discordam
            discordancias_paragrafo = annotations[discordancias][paragrafo_id]
            linha_minuta_discordantes = u'Usuários Discordantes: ;'
            for usuario_discordante in discordancias_paragrafo:
                voto = discordancias_paragrafo[usuario_discordante]
                if voto.get('has_voted', False):
                    linha_minuta_discordantes += usuario_discordante + u';'
            minuta_exportada_csv += linha_minuta_discordantes + u'\n'

        RESPONSE.setHeader('Content-Type', 'text/csv; charset=ISO-8859-1')
        RESPONSE.setHeader('Content-Length', len(minuta_exportada_csv))
        RESPONSE.setHeader('Content-Disposition',
                           'attachment; filename="Relatorio.csv"')
        return minuta_exportada_csv
Example #53
0
 def _getBlacklist(self, create=False):
     if IAnnotations.providedBy(self.context):
         annotations = self.context
     else:
         annotations = queryAdapter(self.context, IAnnotations)
     local = annotations.get(CONTEXT_BLACKLIST_STATUS_KEY, None)
     if local is None:
         if create:
             local = annotations[
                 CONTEXT_BLACKLIST_STATUS_KEY] = PersistentDict()
         else:
             return None
     blacklist = local.get(self.manager.__name__, None)
     if blacklist is None:
         if create:
             blacklist = local[self.manager.__name__] = PersistentDict()
         else:
             return None
     return blacklist
Example #54
0
    def __iter__(self):
        start_time = time()
        count = 0
        problematic = 0
        for item in self.previous:
            # source sections add store path of current generated item in annotation
            # it gives posibility to monitor what items go through all pipeline
            # sections between source section and this section and what don't
            if self.pathkey in item and item[self.pathkey] in self.storage:
                self.storage.remove(item[self.pathkey])
            count += 1
            # print item data stored on keys given as option
            items = []
            for key in item.keys():
                if self.keys(key)[0] is not None:
                    items.append("%s=%s" % (key, item[key]))
            if items:
                msg = ", ".join(items)
                logging.getLogger(self.logger).info(msg)
            yield item

        working_time = int(round(time() - start_time))

        # log items that maybe have some problems
        if self.storage:
            problematic = len(self.storage)
            logging.getLogger(self.logger).warning('\nNext objects didn\'t go through full pipeline:\n%s' % \
                '\n'.join(['\t'+i for i in self.storage]))
        # delete validation data from annotations
        anno = IAnnotations(self.transmogrifier)
        if VALIDATIONKEY in anno:
            del anno[VALIDATIONKEY]

        seconds = working_time % 60
        minutes = working_time / 60 % 60
        hours = working_time / 3600
        stats = "\nPipeline processing time: %02d:%02d:%02d\n" % (
            hours, minutes, seconds)
        stats += "\t%4d items were generated in source sections\n" % (
            count + problematic)
        stats += "\t%4d went through full pipeline\n" % count
        stats += "\t%4d were discarded in some section" % problematic
        logging.getLogger(self.logger).info(stats)
 def test_consumePermanentToken(self):
     """When oneTime is False for at leat one action"""
     token = self.utility.enablePowerToken(self.doc, 'foo', oneTime=False)
     configuration = IAnnotations(self.doc)[config.MAIN_TOKEN_NAME]
     self.assertEquals(len(configuration), 1)
     self.assertEquals(len(configuration[token]), 1)
     self.utility.consumeToken(self.doc, token)
     self.utility.consumeToken(self.doc, token)
     self.assertEquals(len(configuration), 1)
     # now add another non-permanent action
     self.utility.addAction(self.doc, token, 'anotherFoo')
     self.assertEquals(len(configuration), 1)
     self.assertEquals(len(configuration[token]), 2)
     self.utility.consumeToken(self.doc, token)
     self.assertEquals(len(configuration), 1)
     self.assertEquals(len(configuration[token]), 1)
     self.utility.consumeToken(self.doc, token)
     self.assertEquals(len(configuration), 1)
     self.assertEquals(len(configuration[token]), 1)
Example #56
0
    def test_enabled_no_paths(self):
        context = FauxContext()

        request = FauxRequest()
        alsoProvides(request, IAttributeAnnotatable)
        setRequest(request)

        registry = Registry()
        registry.registerInterface(ICachePurgingSettings)
        provideUtility(registry, IRegistry)

        settings = registry.forInterface(ICachePurgingSettings)
        settings.enabled = True
        settings.cachingProxies = ('http://localhost:1234',)

        notify(Purge(context))

        self.assertEqual({'plone.cachepurging.urls': set()},
                         dict(IAnnotations(request)))
Example #57
0
 def _exportNode(self):
     """
     Export the contents as an xml node
     """
     node = MembraneToolXMLAdapter._exportNode(self)
     mbtool = getToolByName(self.context, 'membrane_tool')
     annot = IAnnotations(mbtool)
     try:
         htype = annot[ANNOT_KEY]['hash_type']
         child = self._doc.createElement('hash-type')
         child.setAttribute('name', htype)
         fragment = self._doc.createDocumentFragment()
         fragment.appendChild(child)
         node.appendChild(fragment)
         self._logger.info("Remember hash-type exported: %s" % htype)
     except KeyError:
         # no hash_type annotated on mbtool, no need to add node
         pass
     return node
Example #58
0
    def __call__(self):
        """info is a dict of uid:[list of chart ids] values
        """
        form = self.request.form
        uid = form.get("daviz_uid", "").strip()

        obj = self.context
        annot = IAnnotations(obj)

        if not KEY in annot:
            annot[KEY] = PersistentMapping()

        info = annot[KEY].get(uid)
        if not info:
            info = annot[KEY][uid] = OrderedDict()

        previous_info = dict(info)
        info.clear()


        #this is a string like: 'chart_1=preview&chart_2=live'
        #from urlparse import parse_qs
        #cannot use parse_qs because it doesn't guarantee order
        req_charts = form.get("charts", "").strip()
        charts = []

        for pair in req_charts.split("&"):
            if pair:
                chart_id, embed = pair.split("=")
                chart_settings = PersistentMapping()
                chart_settings['type'] = embed
                for prev_key, prev_val in previous_info.get(chart_id, {}).\
                    items():
                    if prev_key != 'type':
                        chart_settings[prev_key] = prev_val
                charts.append((chart_id, chart_settings))


        info.update(charts)

        self.context._p_changed = True

        return "OK"
Example #59
0
    def __iter__(self):
        for item in self.previous:

            properties = item['_properties']
            for prop in properties:

                if not prop[0] == 'default_page':
                    yield item
                    continue

                obj = self.context.unrestrictedTraverse(
                    str(item['_path']).lstrip('/'), None)

                # Hack to know in an other migration if the object is
                # a default view object.
                IAnnotations(obj)[MIGR_KEY] = '%s/%s' % (item['_path'],
                                                         prop[1])

            yield item
Example #60
0
 def indexObject(obj, path):
     if (base_hasattr(obj, 'reindexObject')
             and safe_callable(obj.reindexObject)):
         try:
             self.reindexObject(obj, idxs=idxs)
             # index conversions from plone.app.discussion
             annotions = IAnnotations(obj)
             if DISCUSSION_ANNOTATION_KEY in annotions:
                 conversation = annotions[DISCUSSION_ANNOTATION_KEY]
                 conversation = conversation.__of__(obj)
                 for comment in conversation.getComments():
                     try:
                         self.indexObject(comment, idxs=idxs)
                     except StopIteration:  # pragma: no cover
                         pass
         except TypeError:
             # Catalogs have 'indexObject' as well, but they
             # take different args, and will fail
             pass