Exemple #1
0
    def test_ignoring_fields_of_field_data_adapter_plone_4(self):
        self.grant('Manager')
        folder = create(Builder('folder').titled(u'Foo'))
        data = encode_after_json(json.loads(Extractor()(folder, 'push', {})))
        self.assertIn('field_data_adapter', data)
        self.assertIn('description', data['field_data_adapter'])

        IConfig(self.portal).set_ignored_fields({'Folder': ['description']})
        data = encode_after_json(json.loads(Extractor()(folder, 'push', {})))
        self.assertNotIn('description', data['field_data_adapter'])
Exemple #2
0
    def test_dexterity_image_extractor(self):
        self.grant('Manager')
        self.maxDiff = None

        with freeze(datetime(2030, 1, 2, 4, 5)):
            image = create(
                Builder('image').titled(u'An image').with_dummy_content().
                having(description=u'Description of the image').within(
                    create(Builder('folder').titled(u'Foo'))))

        self.assertDictEqual(
            encode_after_json(json.loads(self.asset('image_dx.json').text())),
            encode_after_json(json.loads(Extractor()(image, 'push', {}))))
Exemple #3
0
    def test_archetypes_folder_extractor(self):
        self.grant('Manager')
        self.maxDiff = None

        with freeze(datetime(2030, 1, 2, 4, 5)):
            folder = create(
                Builder('folder').titled(u'A folder').having(
                    description=u'Description of the folder').within(
                        create(Builder('folder').titled(u'Foo'))))

        self.assertDictEqual(
            encode_after_json(json.loads(self.asset('folder_at.json').text())),
            encode_after_json(json.loads(Extractor()(folder, 'push', {}))))
Exemple #4
0
    def test_delete_job_get_metadata_plone_5(self):
        self.grant('Manager')
        with freeze(datetime(2030, 1, 2, 4, 5)) as clock:
            folder = create(
                Builder('folder').titled(u'The Folder').within(
                    create(Builder('folder').titled(u'Foo'))))
            clock.forward(hours=1)
            data = encode_after_json(
                json.loads(Extractor()(folder, 'delete', {})))

        self.maxDiff = None
        self.assertEquals(
            {
                'metadata': {
                    'UID': 'testdeletejobgetmetadatapl000002',
                    'action': 'delete',
                    'id': 'the-folder',
                    'modified': '2030/01/02 04:05:00 GMT+1',
                    'physicalPath': '/foo/the-folder',
                    'portal_type': 'Folder',
                    'review_state': '',
                    'sibling_positions': {
                        'the-folder': 0
                    }
                }
            }, data)
    def test_portlet_visibility_settings(self):
        assignment = self.layer['right_portlets'].get('title2', False)
        settings = PersistentMapping({'visible': False})
        IPortletAssignmentSettings(assignment).data = settings

        # get the data
        adapter = getAdapter(self.layer['folder1'],
                             IDataCollector,
                             name="portlet_data_adapter")

        data = adapter.getData()
        jsondata = json.dumps(utils.decode_for_json(data))
        data = utils.encode_after_json(json.loads(jsondata))

        self.assertEqual(settings,
                         data['plone.rightcolumn']['title2']['settings'])

        # set the data
        adapter2 = getAdapter(self.layer['folder2'],
                              IDataCollector,
                              name="portlet_data_adapter")
        adapter2.setData(data, metadata=None)

        new_assignment = self.layer['right_portlets'].get('title2', False)
        self.assertEqual(settings,
                         IPortletAssignmentSettings(new_assignment).data)
    def __call__(self):
        logger = getLogger()

        action = self.request.form['action']
        filepath = self.request.form['filepath']
        path = self.request.form['path']
        additional_data = encode_after_json(
            json.loads(self.request.form['additional_data']))
        obj = api.portal.get().unrestrictedTraverse(path, None)
        require_token = self.request.form['token']
        attempt = self.request.form['attempt']

        if obj is None:
            if attempt < MAX_ATTEMPTS:
                sleep(TIMEOUT_BETWEEN_ATTEMPTS)
                return enqueue_deferred_extraction(None,
                                                   action,
                                                   filepath,
                                                   additional_data,
                                                   attempt=attempt + 1,
                                                   token=require_token,
                                                   path=path)
            else:
                os.remove(filepath)
                logger.warning(
                    'Removed "{0}", since the destination {1} no longer '
                    'exists.'.format(filepath, path))
                return 'JSON File "{0}" removed'.format(filepath)

        current_token = IAnnotations(obj).get(TOKEN_ANNOTATION_KEY, None)

        if current_token != require_token:
            # The current version of the object is not the version we have
            # planned to extract.
            if attempt < MAX_ATTEMPTS:
                # Lets retry for solving the problem that the worker is too
                # early and the transaction which triggered the action was not
                # yet commited to the database.
                sleep(TIMEOUT_BETWEEN_ATTEMPTS)
                return enqueue_deferred_extraction(obj,
                                                   action,
                                                   filepath,
                                                   additional_data,
                                                   attempt=attempt + 1,
                                                   token=require_token)

            else:
                raise Exception(
                    'Unexpected object version' +
                    ' after {!r} attempts.'.format(attempt) +
                    ' Required token: {!r},'.format(require_token) +
                    ' got token: {!r}'.format(current_token))

        extractor = Extractor()
        data = extractor(obj, action, additional_data)

        with open(filepath, 'w') as target:
            target.write(data)

        return 'OK'
    def test_sender(self, browser):
        intercepted_data = {}

        class MockedReceiverView(BrowserView):
            def __call__(self):
                intercepted_data['jsondata'] = self.request.form.get(
                    'jsondata')
                return createResponse(SuccessState())

        config = IConfig(self.portal)
        config.appendRealm(
            Realm(1, self.portal.absolute_url(), SITE_OWNER_NAME,
                  SITE_OWNER_PASSWORD))
        transaction.commit()

        with view_registered(MockedReceiverView,
                             'global_statusmessage_config_receiver',
                             required=(IPloneSiteRoot, Interface)):
            browser.login(SITE_OWNER_NAME).open(
                view='@@global_statusmessage_config')
            browser.fill(self.form_data)
            browser.click_on('Save and publish')

        self.assertDictEqual(
            self.expected_settings,
            encode_after_json(json.loads(intercepted_data['jsondata'])))

        self.assertEqual(['Changes saved and published.'], info_messages())
    def test_sender(self, browser):
        intercepted_data = {}

        class MockedReceiverView(BrowserView):
            def __call__(self):
                intercepted_data['jsondata'] = self.request.form.get('jsondata')
                return createResponse(SuccessState())

        config = IConfig(self.portal)
        config.appendRealm(Realm(1, self.portal.absolute_url(), SITE_OWNER_NAME, SITE_OWNER_PASSWORD))
        transaction.commit()

        with view_registered(MockedReceiverView, 'global_statusmessage_config_receiver',
                             required=(IPloneSiteRoot, Interface)):
            browser.login(SITE_OWNER_NAME).open(view='@@global_statusmessage_config')
            browser.fill(self.form_data)
            browser.click_on('Save and publish')

        self.assertDictEqual(
            self.expected_settings,
            encode_after_json(json.loads(intercepted_data['jsondata']))
        )

        self.assertEqual(
            ['Changes saved and published.'],
            info_messages()
        )
Exemple #9
0
    def test_portlet_visibility_settings(self):
        assignment = self.layer['right_portlets'].get('title2', False)
        settings = PersistentMapping({'visible': False})
        IPortletAssignmentSettings(assignment).data = settings

        # get the data
        adapter = getAdapter(self.layer['folder1'], IDataCollector,
                             name="portlet_data_adapter")

        data = adapter.getData()
        jsondata = json.dumps(utils.decode_for_json(data))
        data = utils.encode_after_json(json.loads(jsondata))

        self.assertEqual(
            settings,
            data['plone.rightcolumn']['title2']['settings'])

        # set the data
        adapter2 = getAdapter(self.layer['folder2'], IDataCollector,
                              name="portlet_data_adapter")
        adapter2.setData(data, metadata=None)

        new_assignment = self.layer['right_portlets'].get('title2', False)
        self.assertEqual(
            settings,
            IPortletAssignmentSettings(new_assignment).data)
    def _set_field_data(self, obj, data, metadata=None, json=False):
        collector = self._get_collector_for(obj)

        if json:
            data = loads(data)
            data = utils.encode_after_json(data)

        return collector.setData(data, metadata or {})
    def transport(self, data, additional_encodings=None):
        """Converts data so that it can be transported and converts it back.
        It should be equally to the original data afterwards.
        """

        transport_data = json.dumps(utils.decode_for_json(data))
        result_data = utils.encode_after_json(json.loads(transport_data))
        return result_data
    def __call__(self):
        logger = getLogger()

        action = self.request.form['action']
        filepath = self.request.form['filepath']
        path = self.request.form['path']
        additional_data = encode_after_json(json.loads(self.request.form['additional_data']))
        obj = api.portal.get().unrestrictedTraverse(path, None)
        require_token = self.request.form['token']
        attempt = self.request.form['attempt']

        if obj is None:
            if attempt < MAX_ATTEMPTS:
                sleep(TIMEOUT_BETWEEN_ATTEMPTS)
                return enqueue_deferred_extraction(
                    None, action, filepath, additional_data,
                    attempt=attempt + 1,
                    token=require_token,
                    path=path)
            else:
                os.remove(filepath)
                logger.warning(
                    'Removed "{0}", since the destination {1} no longer '
                    'exists.'.format(filepath, path))
                return 'JSON File "{0}" removed'.format(filepath)

        current_token = IAnnotations(obj).get(TOKEN_ANNOTATION_KEY, None)

        if current_token != require_token:
            # The current version of the object is not the version we have
            # planned to extract.
            if attempt < MAX_ATTEMPTS:
                # Lets retry for solving the problem that the worker is too
                # early and the transaction which triggered the action was not
                # yet commited to the database.
                sleep(TIMEOUT_BETWEEN_ATTEMPTS)
                return enqueue_deferred_extraction(
                    obj, action, filepath, additional_data,
                    attempt=attempt + 1,
                    token=require_token)

            else:
                raise Exception(
                    'Unexpected object version' +
                    ' after {!r} attempts.'.format(attempt) +
                    ' Required token: {!r},'.format(require_token) +
                    ' got token: {!r}'.format(current_token))

        extractor = Extractor()
        data = extractor(obj, action, additional_data)

        with open(filepath, 'w') as target:
            target.write(data)

        return 'OK'
    def test_roundtrip_stores_data_persistently(self):
        annotations = IAnnotations(self.layer['portal'])
        component = getAdapter(self.layer['portal'], IDataCollector,
                               name='ftw.servicenavigation')

        annotations[ANNOTATION_KEY] = EXAMPLE_DATA
        wired_data = decode_for_json(component.getData())
        del annotations[ANNOTATION_KEY]

        component.setData(encode_after_json(wired_data))
        data = annotations[ANNOTATION_KEY]
        self.assertEqual(EXAMPLE_DATA, data)
        self.assertIsInstance(data, Persistent)
        self.assertIsInstance(data['links'], Persistent)
Exemple #14
0
    def __call__(self):
        data = encode_after_json(json.loads(self.request.form.get('jsondata')))

        if not data:
            return createResponse(states.InvalidRequestError())

        registry = getUtility(IRegistry)
        settings = registry.forInterface(IStatusMessageConfigForm, check=False)

        for field_name in getFieldNames(IStatusMessageConfigForm):
            if field_name in data:
                setattr(settings, field_name, data[field_name])

        return createResponse(states.SuccessState())
    def test_roundtrip_stores_data_persistently(self):
        annotations = IAnnotations(self.layer['portal'])
        component = getAdapter(self.layer['portal'],
                               IDataCollector,
                               name='ftw.servicenavigation')

        annotations[ANNOTATION_KEY] = EXAMPLE_DATA
        wired_data = decode_for_json(component.getData())
        del annotations[ANNOTATION_KEY]

        component.setData(encode_after_json(wired_data))
        data = annotations[ANNOTATION_KEY]
        self.assertEqual(EXAMPLE_DATA, data)
        self.assertIsInstance(data, Persistent)
        self.assertIsInstance(data['links'], Persistent)
    def __call__(self):
        data = encode_after_json(
            json.loads(self.request.form.get('jsondata'))
        )

        if not data:
            return createResponse(states.InvalidRequestError())

        registry = getUtility(IRegistry)
        settings = registry.forInterface(IStatusMessageConfigForm, check=False)

        for field_name in getFieldNames(IStatusMessageConfigForm):
            if field_name in data:
                setattr(settings, field_name, data[field_name])

        return createResponse(states.SuccessState())
class Decoder(object):
    """
    Decodes json data to dictionary and validates it.
    It also validates and decodes all schema field values.
    """

    security = ClassSecurityInformation()

    def __init__(self, context):
        """
        Constructor: stores context as object attribute
        @param context:         Plone object
        @type:                  Plone Object
        """
        self.context = context
        self.logger = getLogger()

    security.declarePrivate('__call__')
    def __call__(self, jsondata):
        """
        Decodes the jsondata to a dictionary, validates it,
        unserializes the field values and returns it.
        @return:        Data dictionary
        @rtype:         dict
        """
        self.data = self.decodeJson(jsondata)
        self.validate()
        return self.data

    security.declarePrivate('decodeJson')
    def decodeJson(self, jsondata):
        """
        Decodes the JSON data with the json module.
        If the json module cannot decode the string, a
        DecodeError is raised.
        @param jsondata:    JSON data
        @type jsondata:     string
        @return:            Decode Data dictionary
        @rtype:             dict
        @raise:             DecodeError
        """
        try:
            data = json.loads(jsondata)
        except Exception, e:
            raise states.DecodeError(str(e))
        data = encode_after_json(data)
        return data
Exemple #18
0
    def test_shop_item_ranks(self):
        folder = create(Builder('folder'))
        category_1 = create(
            Builder('shop category').titled('Category 1').within(folder))
        category_2 = create(
            Builder('shop category').titled('Category 2').within(folder))

        shop_item_1 = create(
            Builder('shop item').titled('Raindrops').within(category_1))
        shop_item_1.addToCategory(category_1)
        shop_item_1.setRankForCategory(category_1, 5)
        shop_item_1.addToCategory(category_2)
        shop_item_1.setRankForCategory(category_2, 55)
        adapter_shop_item_1 = getAdapter(
            shop_item_1,
            IDataCollector,
            name='shop_categorizable_ranks_adapter')

        shop_item_2 = create(
            Builder('shop item').titled('Snowflakes').within(category_1))
        adapter_shop_item_2 = getAdapter(
            shop_item_2,
            IDataCollector,
            name='shop_categorizable_ranks_adapter')

        self.assertNotEqual(
            adapter_shop_item_1.getData(),
            adapter_shop_item_2.getData(),
        )

        getter_data = adapter_shop_item_1.getData()
        getter_json = json.dumps(utils.decode_for_json(getter_data))
        setter_data = utils.encode_after_json(json.loads(getter_json))
        adapter_shop_item_2.setData(setter_data, None)

        self.assertEqual(
            adapter_shop_item_1.getData(),
            adapter_shop_item_2.getData(),
        )
    def test_shop_item_ranks(self):
        folder = create(Builder('folder'))
        category_1 = create(Builder('shop category')
                            .titled('Category 1')
                            .within(folder))
        category_2 = create(Builder('shop category')
                            .titled('Category 2')
                            .within(folder))

        shop_item_1 = create(Builder('shop item')
                             .titled('Raindrops')
                             .within(category_1))
        shop_item_1.addToCategory(category_1)
        shop_item_1.setRankForCategory(category_1, 5)
        shop_item_1.addToCategory(category_2)
        shop_item_1.setRankForCategory(category_2, 55)
        adapter_shop_item_1 = getAdapter(shop_item_1, IDataCollector,
                                         name='shop_categorizable_ranks_adapter')

        shop_item_2 = create(Builder('shop item')
                             .titled('Snowflakes')
                             .within(category_1))
        adapter_shop_item_2 = getAdapter(shop_item_2, IDataCollector,
                                         name='shop_categorizable_ranks_adapter')

        self.assertNotEqual(
            adapter_shop_item_1.getData(),
            adapter_shop_item_2.getData(),
        )

        getter_data = adapter_shop_item_1.getData()
        getter_json = json.dumps(utils.decode_for_json(getter_data))
        setter_data = utils.encode_after_json(json.loads(getter_json))
        adapter_shop_item_2.setData(setter_data, None)

        self.assertEqual(
            adapter_shop_item_1.getData(),
            adapter_shop_item_2.getData(),
        )
    def test_portlets_with_settings(self):
        # IPortletAssignmentSettings creates settings in the annotations
        ori_assignment = self.layer['right_portlets'].get('title2', False)
        ori_settings = getAdapter(ori_assignment, IPortletAssignmentSettings)
        ori_settings['foo'] = 'bar'

        # extract the data
        adapter = getAdapter(self.layer['folder1'],
                             IDataCollector,
                             name="portlet_data_adapter")
        getterdata = adapter.getData()
        getterdata = utils.decode_for_json(getterdata)
        jsondata = json.dumps(getterdata)

        # set the data on the folder2
        data = json.loads(jsondata)
        data = utils.encode_after_json(data)
        adapter2 = getAdapter(self.layer['folder2'],
                              IDataCollector,
                              name="portlet_data_adapter")
        adapter2.setData(data, metadata=None)

        # test the assignment settings
        new_assignment = self.layer['right_portlets'].get('title2', False)
        new_settings = getAdapter(new_assignment, IPortletAssignmentSettings)
        self.assertEqual(new_settings['foo'], 'bar')

        assignment_annotations = new_assignment.__annotations__
        self.assertTrue(
            isinstance(assignment_annotations,
                       OOBTree), 'annotations are not OOBTree but %s' %
            type(assignment_annotations))

        settings = assignment_annotations.get(ASSIGNMENT_SETTINGS_KEY)
        self.assertTrue(
            IPortletAssignmentSettings.providedBy(settings),
            'Portlet settings is not PortletAssignmentSettings but %s' %
            (type(settings)))
Exemple #21
0
    def test_portlets_with_settings(self):
        # IPortletAssignmentSettings creates settings in the annotations
        ori_assignment = self.layer['right_portlets'].get('title2', False)
        ori_settings = getAdapter(ori_assignment, IPortletAssignmentSettings)
        ori_settings['foo'] = 'bar'

        # extract the data
        adapter = getAdapter(self.layer['folder1'], IDataCollector,
                             name="portlet_data_adapter")
        getterdata = adapter.getData()
        getterdata = utils.decode_for_json(getterdata)
        jsondata = json.dumps(getterdata)

        # set the data on the folder2
        data = json.loads(jsondata)
        data = utils.encode_after_json(data)
        adapter2 = getAdapter(self.layer['folder2'], IDataCollector,
                              name="portlet_data_adapter")
        adapter2.setData(data, metadata=None)

        # test the assignment settings
        new_assignment = self.layer['right_portlets'].get('title2', False)
        new_settings = getAdapter(new_assignment, IPortletAssignmentSettings)
        self.assertEqual(new_settings['foo'], 'bar')

        assignment_annotations = new_assignment.__annotations__
        self.assertTrue(
            isinstance(assignment_annotations, OOBTree),
            'annotations are not OOBTree but %s' % type(
                assignment_annotations))

        settings = assignment_annotations.get(ASSIGNMENT_SETTINGS_KEY)
        self.assertTrue(
            IPortletAssignmentSettings.providedBy(settings),
            'Portlet settings is not PortletAssignmentSettings but %s' % (
                type(settings)))
 def _set_field_data(self, obj, data):
     collector = self._get_collector_for(obj)
     data = loads(data)
     data = utils.encode_after_json(data)
     return collector.setData(data, {})
Exemple #23
0
 def _set_field_data(self, obj, data):
     collector = self._get_collector_for(obj)
     data = loads(data)
     data = utils.encode_after_json(data)
     return collector.setData(data, {})