Exemple #1
0
    def test_nodes(self):
        with self.assertCache(sets=1):
            cio.set('sv-se@rest/label/email', u'E-post')

        with self.assertDB(calls=1), self.assertCache(calls=1,
                                                      misses=1,
                                                      hits=1,
                                                      sets=0):
            url = reverse('admin:djedi:rest:nodes')
            response = self.client.post(url,
                                        json.dumps({
                                            'rest/page/body.md':
                                            u'# Foo Bar',
                                            'rest/label/email':
                                            u'E-mail',
                                        }),
                                        content_type='application/json')

        json_content = json.loads(response.content)

        self.assertIn('i18n://sv-se@rest/page/body.md', json_content.keys())
        self.assertEqual(json_content['i18n://sv-se@rest/page/body.md'],
                         u'<h1>Foo Bar</h1>')
        self.assertIn('i18n://sv-se@rest/label/email.txt#1',
                      json_content.keys())
        self.assertEqual(json_content['i18n://sv-se@rest/label/email.txt#1'],
                         u'E-post')
Exemple #2
0
    def test_forced_empty_content(self):
        with self.assertRaises(ValueError):
            cio.set('i18n://sv-se@none', None)

        node = cio.set('i18n://[email protected]', u'')
        node = cio.get(node.uri, default=u'fallback')
        self.assertEqual(six.text_type(node), u'')
Exemple #3
0
    def test_load(self):
        response = self.get('api.load', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 200)
        json_content = json.loads(response.content)
        self.assertEqual(json_content['uri'], 'i18n://sv-se@page/title.txt')
        self.assertIsNone(json_content['data'])
        self.assertEqual(len(json_content['meta'].keys()), 0)

        # TODO: Should get 404
        # response = self.get('api.load', 'i18n://sv-se@page/title#1')
        # self.assertEqual(response.status_code, 404)

        cio.set('i18n://sv-se@page/title.md', u'# Djedi')

        response = self.get('api.load', 'sv-se@page/title')
        self.assertEqual(response.status_code, 200)
        node = json_node(response, simple=False)
        meta = node.pop('meta', {})
        self.assertDictEqual(node, {'uri': 'i18n://sv-se@page/title.md#1', 'data': u'# Djedi', 'content': u'<h1>Djedi</h1>'})
        self.assertKeys(meta, 'modified_at', 'published_at', 'is_published')

        response = self.get('api.load', 'i18n://sv-se@page/title#1')
        json_content = json.loads(response.content)
        self.assertEqual(json_content['uri'], 'i18n://sv-se@page/title.md#1')

        self.assertEqual(len(cio.revisions('i18n://sv-se@page/title')), 1)
Exemple #4
0
    def test_blocknode_tag(self):
        with self.assertRaises(TemplateSyntaxError):
            self.render("{% blocknode 'page/body' arg %}{% endblocknode %}")

        html = self.render(u"""
            {% blocknode 'page/body.md' edit=False %}
                # Djedi
                Lightning *fast*!
            {% endblocknode %}
        """)
        assert html == u'<h1>Djedi</h1>\n<p>Lightning <em>fast</em>!</p>'

        cio.set('i18n://sv-se@page/body.txt', u'Lightning fast!')
        html = self.render(u"""
            {% blocknode "page/body" %}
                Lorem ipsum
            {% endblocknode %}
        """)
        assert html == u'<span data-i18n="sv-se@page/body">Lightning fast!</span>'

        cio.set('i18n://sv-se@page/body.txt', u'')
        html = self.render(
            u"{% blocknode 'page/body' edit=False %}Lorem ipsum{% endblocknode %}"
        )
        assert html == u''
Exemple #5
0
    def test_node_tag(self):
        html = self.render(u"{% node 'page/title' edit=False %}")
        assert html == u''

        cio.set('i18n://sv-se@page/title.txt', u'Djedi')
        cio.set('i18n://sv-se@page/body.txt', u'Lightning fast!')

        with self.assertCache(calls=1, misses=0):
            with self.assertDB(calls=0):
                html = self.render(
                    u"<h1>{% node 'page/title' edit=False %}</h1><p>{% node 'page/body' edit=False %}</p>"
                )
                assert html == u'<h1>Djedi</h1><p>Lightning fast!</p>'

        cache.clear()

        with self.assertCache(calls=1, misses=2):
            with self.assertDB(calls=1):
                html = self.render(
                    u"<h1>{% node 'page/title' edit=False %}</h1><p>{% node 'page/body' %}</p>"
                )
                assert html == u'<h1>Djedi</h1><p><span data-i18n="sv-se@page/body">Lightning fast!</span></p>'

        html = self.render(u"{% node 'foo/bar' default='bogus' %}")
        assert html == u'<span data-i18n="sv-se@foo/bar">bogus</span>'
        html = self.render(u"{% node 'l10n://foo/bar' default='bogus' %}")
        self.assertEqual(html, u'<span data-i18n="djedi@foo/bar">bogus</span>')
Exemple #6
0
    def test_forced_empty_content(self):
        with self.assertRaises(ValueError):
            cio.set('i18n://sv-se@none', None)

        node = cio.set('i18n://[email protected]', u'')
        node = cio.get(node.uri, default=u'fallback')
        self.assertEqual(six.text_type(node), u'')
Exemple #7
0
    def test_node_meta(self):
        node = cio.set('sv-se@page/title', u'', author=u'lundberg')
        self.assertEqual(node.meta.get('author'), u'lundberg')

        node = cio.get('page/title')
        self.assertEqual(len(node.meta.keys()), 0)  # Cached node has no meta

        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'modified_at', 'published_at',
                        'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')

        cio.set('sv-se@page/title#1', u'', comment=u'This works!')
        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'comment', 'modified_at',
                        'published_at', 'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')
        self.assertEqual(meta.get('comment'), u'This works!')

        cio.set('sv-se@page/title#1', u'', comment=None)
        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'modified_at', 'published_at',
                        'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')
        self.assertNotIn('comment', meta)
Exemple #8
0
    def test_uri_redirect(self):
        cio.set('i18n://sv-se@page/title.txt', u'Title')

        node = cio.get('i18n://sv-se@page/title', u'Default')
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(node.content, u'Title')

        node = cio.get('i18n://sv-se@page/title.up',
                       u'Default Upper',
                       lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.up')
        self.assertEqual(
            node.content, u'DEFAULT UPPER'
        )  # Cache still contains 'Title', but plugin diff and skipped
        cached_node = cache.get(node.uri)
        self.assertDictEqual(cached_node, {
            'uri': node.uri,
            'content': u'DEFAULT UPPER'
        })

        cache.clear()
        node = cio.get('i18n://sv-se@page/title.up',
                       u'Default-Upper',
                       lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.up')
        self.assertEqual(
            node.content, u'DEFAULT-UPPER'
        )  # Cache cleared, storage plugin mismatch, default fallback
Exemple #9
0
    def test_pipes_hits(self):
        with cio.env(i18n=('sv-se', 'en-us')):
            with self.assertDB(inserts=2):
                with self.assertCache(calls=2, sets=2):
                    cio.set('i18n://sv-se@label/email.txt', u'epost')
                    cio.set('i18n://en-us@label/surname.txt', u'surname')

            # Lazy gets
            with self.assertDB(calls=0):
                with self.assertCache(calls=0):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer')

            # with self.assertDB(calls=2), self.assertCache(calls=5, hits=1, misses=2, sets=2):
            with self.assertDB(calls=4, selects=4):
                with self.assertCache(calls=2, hits=1, misses=2, sets=2):
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            with self.assertDB(calls=0):
                with self.assertCache(calls=1, hits=3):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer')
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            self.assertIsNotNone(repr(node1))
            self.assertIsNotNone(str(node1))
Exemple #10
0
    def test_node_meta(self):
        node = cio.set('sv-se@page/title', u'', author=u'lundberg')
        self.assertEqual(node.meta.get('author'), u'lundberg')

        node = cio.get('page/title')
        self.assertEqual(len(node.meta.keys()), 0)  # Cached node has no meta

        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')

        cio.set('sv-se@page/title#1', u'', comment=u'This works!')
        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'comment', 'modified_at', 'published_at', 'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')
        self.assertEqual(meta.get('comment'), u'This works!')

        cio.set('sv-se@page/title#1', u'', comment=None)
        node = cio.load('sv-se@page/title#1')
        meta = node['meta']
        self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published')
        self.assertEqual(meta.get('author'), u'lundberg')
        self.assertNotIn('comment', meta)
Exemple #11
0
    def test_pipes_hits(self):
        with cio.env(i18n=('sv-se', 'en-us')):
            with self.assertDB(inserts=2):
                with self.assertCache(calls=2, sets=2):
                    cio.set('i18n://sv-se@label/email.txt', u'epost')
                    cio.set('i18n://en-us@label/surname.txt', u'surname')

            # Lazy gets
            with self.assertDB(calls=0):
                with self.assertCache(calls=0):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode',
                                    default=u'postnummer')

            # with self.assertDB(calls=2), self.assertCache(calls=5, hits=1, misses=2, sets=2):
            with self.assertDB(calls=4, selects=4):
                with self.assertCache(calls=2, hits=1, misses=2, sets=2):
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            with self.assertDB(calls=0):
                with self.assertCache(calls=1, hits=3):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode',
                                    default=u'postnummer')
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            self.assertIsNotNone(repr(node1))
            self.assertIsNotNone(str(node1))
Exemple #12
0
 def test_search(self):
     cio.set('i18n://sv-se@label/email.txt', u'e-post')
     uris = cio.search()
     self.assertEqual(len(uris), 1)
     uris = cio.search('foo/')
     self.assertEqual(len(uris), 0)
     uris = cio.search('label/')
     self.assertEqual(len(uris), 1)
Exemple #13
0
 def test_search(self):
     cio.set('i18n://sv-se@label/email.txt', u'e-post')
     uris = cio.search()
     self.assertEqual(len(uris), 1)
     uris = cio.search('foo/')
     self.assertEqual(len(uris), 0)
     uris = cio.search('label/')
     self.assertEqual(len(uris), 1)
Exemple #14
0
    def test_revisions(self):
        cio.set('sv-se@page/title', u'Djedi 1')
        cio.set('sv-se@page/title', u'Djedi 2')

        response = self.get('api.revisions', 'sv-se@page/title')
        assert response.status_code == 200

        content = json.loads(response.content)
        assert content == [['i18n://sv-se@page/title.txt#1', False], ['i18n://sv-se@page/title.txt#2', True]]
Exemple #15
0
    def test_cache_encoding(self):
        cio.set(self.uri, u'epost')

        cached_node = cache.get(self.uri)
        content = cached_node['content']
        self.assertIsInstance(content, six.text_type)
        self.assertEqual(content, u'epost')

        cache.set('i18n://sv-se@label/email.txt#1', u'epost')
        nodes = cache.get_many((self.uri, self.uri))
        self.assertDictEqual(nodes, {self.uri: {'uri': 'i18n://sv-se@label/email.txt#1', 'content': u'epost'}})
    def test_node_tag_with_default_scheme(self):
        cio.set('i18n://sv-se@page/title.txt', u'Swedish Djedi')
        html = self.render(u"{% node 'page/title' edit=False %}")
        assert html == u'Swedish Djedi'

        with self.settings(DJEDI={'URI_DEFAULT_SCHEME': 'l10n'}):
            html = self.render(u"{% node 'page/title' edit=False %}")
            assert html == u''

            cio.set('l10n://djedi@page/title.txt', u'Local Djedi')
            html = self.render(u"{% node 'page/title' edit=False %}")
            assert html == u'Local Djedi'
Exemple #17
0
    def test_abandoned_buffered_node(self):
        cio.set('sv-se@foo/bar', u'foobar')

        node = cio.get('foo/bar')
        self.assertFalse(node._flushed)
        self.assertIn('get', pipeline._buffer._buffer)

        # Mess things up...
        pipeline.clear()
        self.assertFalse(node._flushed)
        self.assertNotIn('get', pipeline._buffer._buffer)

        self.assertEqual(node.content, u'foobar')
        self.assertTrue(node._flushed)
Exemple #18
0
    def test_abandoned_buffered_node(self):
        cio.set('sv-se@foo/bar', u'foobar')

        node = cio.get('foo/bar')
        self.assertFalse(node._flushed)
        self.assertIn('get', pipeline._buffer._buffer)

        # Mess things up...
        pipeline.clear()
        self.assertFalse(node._flushed)
        self.assertNotIn('get', pipeline._buffer._buffer)

        self.assertEqual(node.content, u'foobar')
        self.assertTrue(node._flushed)
Exemple #19
0
    def test_get(self):
        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 404)

        cio.set('i18n://sv-se@page/title.md', u'# Djedi', publish=False)

        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 404)

        response = self.get('api', 'i18n://sv-se@page/title#draft')
        self.assertEqual(response.status_code, 200)
        node = json_node(response)
        self.assertKeys(node, 'uri', 'content')
        self.assertEqual(node['uri'], 'i18n://sv-se@page/title.md#draft')
        self.assertEqual(node['content'], u'<h1>Djedi</h1>')
    def test_plugin(self):
        self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md']))

        settings.configure(PLUGINS=[
            'cio.plugins.txt.TextPlugin',
            'cio.plugins.md.MarkdownPlugin',
            'tests.UppercasePlugin'
        ])

        self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md', 'up']))

        node = cio.set('sv-se@page/title.up', {'name': u'lundberg'}, publish=False)
        self.assertListEqual(node._content, [{'name': u'lundberg'}, u'{"name": "lundberg"}', u'LUNDBERG'])

        cio.publish(node.uri)

        node = cio.get('page/title')
        raw_content = storage.get(node.uri)

        self.assertIsNotNone(raw_content)
        self.assertEqual(raw_content['uri'], 'i18n://sv-se@page/title.up#1')
        self.assertEqual(raw_content['content'], u'{"name": "lundberg"}')
        self.assertEqual(node.content, u'LUNDBERG')
        self.assertEqual(node.uri.ext, 'up')

        self.assertSetEqual(set(p for p in plugins), set(('txt', 'md', 'up')))
Exemple #21
0
    def test_plugin(self):
        self.assertSetEqual(set(plugins.plugins.keys()), set(['txt', 'md']))

        settings.configure(PLUGINS=[
            'cio.plugins.txt.TextPlugin', 'cio.plugins.md.MarkdownPlugin',
            'tests.UppercasePlugin'
        ])

        self.assertSetEqual(set(plugins.plugins.keys()),
                            set(['txt', 'md', 'up']))

        node = cio.set('sv-se@page/title.up', {'name': u'lundberg'},
                       publish=False)
        self.assertListEqual(node._content, [{
            'name': u'lundberg'
        }, u'{"name": "lundberg"}', u'LUNDBERG'])

        cio.publish(node.uri)

        node = cio.get('page/title')
        raw_content = storage.get(node.uri)

        self.assertIsNotNone(raw_content)
        self.assertEqual(raw_content['uri'], 'i18n://sv-se@page/title.up#1')
        self.assertEqual(raw_content['content'], u'{"name": "lundberg"}')
        self.assertEqual(node.content, u'LUNDBERG')
        self.assertEqual(node.uri.ext, 'up')

        self.assertSetEqual(set(p for p in plugins), set(('txt', 'md', 'up')))
Exemple #22
0
    def test_uri_redirect(self):
        cio.set('i18n://sv-se@page/title.txt', u'Title')

        node = cio.get('i18n://sv-se@page/title', u'Default')
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(node.content, u'Title')

        node = cio.get('i18n://sv-se@page/title.md', u'# Default Markdown', lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.md')
        self.assertEqual(node.content, u'<h1>Default Markdown</h1>')  # Cache still contains 'Title', but plugin diff and skipped
        cached_node = cache.get(node.uri)
        self.assertDictEqual(cached_node, {'uri': node.uri, 'content': u'<h1>Default Markdown</h1>'})

        cache.clear()
        node = cio.get('i18n://sv-se@page/title.md', u'Default Markdown', lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.md')
        self.assertEqual(node.content, u'<p>Default Markdown</p>')  # Cache cleared, storage plugin mismatch, default fallback
Exemple #23
0
    def test_cache_encoding(self):
        cio.set(self.uri, u'epost')

        cached_node = cache.get(self.uri)
        content = cached_node['content']
        self.assertIsInstance(content, six.text_type)
        self.assertEqual(content, u'epost')

        cache.set('i18n://sv-se@label/email.txt#1', u'epost')
        nodes = cache.get_many((self.uri, self.uri))
        self.assertDictEqual(
            nodes, {
                self.uri: {
                    'uri': 'i18n://sv-se@label/email.txt#1',
                    'content': u'epost'
                }
            })
Exemple #24
0
    def test_uri_redirect(self):
        cio.set('i18n://sv-se@page/title.txt', u'Title')

        node = cio.get('i18n://sv-se@page/title', u'Default')
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(node.content, u'Title')

        node = cio.get('i18n://sv-se@page/title.up', u'Default Upper', lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.up')
        self.assertEqual(node.content, u'DEFAULT UPPER')  # Cache still contains 'Title', but plugin diff and skipped
        cached_node = cache.get(node.uri)
        self.assertDictEqual(cached_node, {'uri': node.uri, 'content': u'DEFAULT UPPER'})

        cache.clear()
        node = cio.get('i18n://sv-se@page/title.up', u'Default-Upper', lazy=False)
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.up')
        self.assertEqual(node.content, u'DEFAULT-UPPER')  # Cache cleared, storage plugin mismatch, default fallback
Exemple #25
0
    def test_fallback(self):
        with cio.env(i18n=('sv-se', 'en-us', 'en-uk')):
            node1 = cio.set('i18n://bogus@label/email.txt', u'epost')
            node2 = cio.set('i18n://en-uk@label/surname.txt', u'surname')

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6, selects=6):
                    node3 = cio.get('i18n://label/email')
                    node4 = cio.get('i18n://label/surname', u'efternamn')
                    self.assertIsNone(node3.content)
                    self.assertEqual(node4.content, u'surname')

            cache.clear()

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6):
                    cio.get('i18n://label/email', lazy=False)
                    cio.get('i18n://label/surname', u'lastname', lazy=False)
Exemple #26
0
    def post(self, request, uri):
        """
        Set node data for uri, return rendered content.

        JSON Response:
            {uri: x, content: y}
        """
        uri = self.decode_uri(uri)
        data, meta = self.get_post_data(request)
        meta['author'] = auth.get_username(request)
        node = cio.set(uri, data, publish=False, **meta)
        return self.render_to_json(node)
Exemple #27
0
    def post(self, request, uri):
        """
        Set node data for uri, return rendered content.

        JSON Response:
            {uri: x, content: y}
        """
        uri = self.decode_uri(uri)
        data, meta = self.get_post_data(request)
        meta['author'] = auth.get_username(request)
        node = cio.set(uri, data, publish=False, **meta)
        return self.render_to_json(node)
Exemple #28
0
    def test_set(self):
        with self.assertRaises(URI.Invalid):
            cio.set('page/title', 'fail')

        with self.assertRaises(URI.Invalid):
            cio.set('page/title.txt', 'fail')

        node = cio.set('i18n://sv-se@label/email.md', u'e-post')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.md#1')
        cache.clear()
        node = cio.get('label/email', u'fallback')
        self.assertEqual(node.content, u'<p>e-post</p>')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.md#1')
        self.assertEqual(node.initial, u'fallback')
        self.assertEqual(len(node.meta.keys()), 0)  # No meta returned from non-versioned api get
        self.assertEqual(repr(node._node), '<Node: i18n://sv-se@label/email.md#1>')
        self.assertEqual(node.for_json(), {
            'uri': node.uri,
            'content': node.content,
            'meta': node.meta
        })

        node = cio.set('sv-se@label/email', u'e-post', publish=False)
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#draft')
        self.assertKeys(node.meta, 'modified_at', 'is_published')

        node = cio.publish(node.uri)
        self.assertKeys(node.meta, 'modified_at', 'published_at', 'is_published')
        self.assertTrue(node.meta['is_published'])

        node = cio.get('label/email')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#2')
        self.assertEqual(node.content, u'e-post')
        self.assertEqual(node.uri.ext, 'txt')
        self.assertEqual(len(node.meta.keys()), 0)
Exemple #29
0
    def post(self, request, uri):
        uri = self.decode_uri(uri)
        data, meta = self.get_post_data(request)
        meta['author'] = auth.get_username(request)
        node = cio.set(uri, data, publish=False, **meta)

        context = cio.load(node.uri)
        context['content'] = node.content

        if request.is_ajax():
            return self.render_to_json(context)
        else:
            return self.render_plugin(request, context)
Exemple #30
0
    def post(self, request, uri):
        uri = self.decode_uri(uri)
        data, meta = self.get_post_data(request)
        meta['author'] = auth.get_username(request)
        node = cio.set(uri, data, publish=False, **meta)

        context = cio.load(node.uri)
        context['content'] = node.content

        if request.is_ajax():
            return self.render_to_json(context)
        else:
            return self.render_plugin(request, context)
Exemple #31
0
    def test_replace_node_in_render_and_load(self):
        settings.configure(PLUGINS=[
            'cio.plugins.txt.TextPlugin', 'cio.plugins.md.MarkdownPlugin',
            'tests.ReplacerPlugin'
        ])

        node = cio.set('sv-se@page/mine.rpl#1', "My own content")
        self.assertNotEqual(node.uri.path, 'page/mine.rpl')
        self.assertEqual(node.uri.path, 'page/rendered.rpl')
        self.assertEqual(node.content, 'REPLACED')

        node = cio.load('sv-se@page/loaded.rpl')
        self.assertEqual(node['uri'].path, 'page/loaded')
    def test_blocknode_with_context(self):
        cio.set('i18n://sv-se@page/title.txt', u'Hej {name}!')

        source = u"""
            {% blocknode 'page/title' edit=False name=user.get_full_name %}
                Hello {name}!
            {% endblocknode %}
        """

        context = {'user': User(first_name=u'Jonas', last_name=u'Lundberg')}
        html = self.render(source, context)
        assert html == u'Hej Jonas Lundberg!'

        with cio.env(i18n='en-us'):
            html = self.render(source, context)
            assert html == u'Hello Jonas Lundberg!'

        html = self.render(u"""
            {% blocknode 'page/title' edit=False %}
                Hello {name}!
            {% endblocknode %}
        """)
        assert html == u'Hej {name}!'
    def test_blocknode_tag(self):
        with self.assertRaises(TemplateSyntaxError):
            self.render("{% blocknode 'page/body' arg %}{% endblocknode %}")

        html = self.render(u"""
            {% blocknode 'page/body.md' edit=False %}
                # Djedi
                Lightning *fast*!
            {% endblocknode %}
        """)
        assert html == u'<h1>Djedi</h1>\n<p>Lightning <em>fast</em>!</p>'

        cio.set('i18n://sv-se@page/body.txt', u'Lightning fast!')
        html = self.render(u"""
            {% blocknode "page/body" %}
                Lorem ipsum
            {% endblocknode %}
        """)
        assert html == u'<span data-i18n="sv-se@page/body">Lightning fast!</span>'

        cio.set('i18n://sv-se@page/body.txt', u'')
        html = self.render(u"{% blocknode 'page/body' edit=False %}Lorem ipsum{% endblocknode %}")
        assert html == u''
    def test_node_tag(self):
        html = self.render(u"{% node 'page/title' edit=False %}")
        assert html == u''

        cio.set('i18n://sv-se@page/title.txt', u'Djedi')
        cio.set('i18n://sv-se@page/body.txt', u'Lightning fast!')

        with self.assertCache(calls=1, misses=0):
            with self.assertDB(calls=0):
                html = self.render(u"<h1>{% node 'page/title' edit=False %}</h1><p>{% node 'page/body' edit=False %}</p>")
                assert html == u'<h1>Djedi</h1><p>Lightning fast!</p>'

        cache.clear()

        with self.assertCache(calls=1, misses=2):
            with self.assertDB(calls=1):
                html = self.render(u"<h1>{% node 'page/title' edit=False %}</h1><p>{% node 'page/body' %}</p>")
                assert html == u'<h1>Djedi</h1><p><span data-i18n="sv-se@page/body">Lightning fast!</span></p>'

        html = self.render(u"{% node 'foo/bar' default='bogus' %}")
        assert html == u'<span data-i18n="sv-se@foo/bar">bogus</span>'
        html = self.render(u"{% node 'l10n://foo/bar' default='bogus' %}")
        self.assertEqual(html, u'<span data-i18n="djedi@foo/bar">bogus</span>')
Exemple #35
0
    def test_blocknode_with_context(self):
        cio.set('i18n://sv-se@page/title.txt', u'Hej {name}!')

        source = u"""
            {% blocknode 'page/title' edit=False name=user.get_full_name %}
                Hello {name}!
            {% endblocknode %}
        """

        context = {'user': User(first_name=u'Jonas', last_name=u'Lundberg')}
        html = self.render(source, context)
        assert html == u'Hej Jonas Lundberg!'

        with cio.env(i18n='en-us'):
            html = self.render(source, context)
            assert html == u'Hello Jonas Lundberg!'

        html = self.render(u"""
            {% blocknode 'page/title' edit=False %}
                Hello {name}!
            {% endblocknode %}
        """)
        assert html == u'Hej {name}!'
Exemple #36
0
    def test_delete(self):
        with self.assertRaises(URI.Invalid):
            cio.delete('foo/bar')

        node = cio.set('i18n://sv-se@label/email.txt', u'e-post')
        uri = node.uri
        self.assertEqual(cache.get(uri)['content'], u'e-post')

        uris = cio.delete('sv-se@label/email#1', 'sv-se@foo/bar')
        self.assertListEqual(uris, ['sv-se@label/email#1'])

        with self.assertRaises(NodeDoesNotExist):
            storage.get(uri)

        self.assertIsNone(cache.get(uri))
Exemple #37
0
    def test_delete(self):
        with self.assertRaises(URI.Invalid):
            cio.delete('foo/bar')

        node = cio.set('i18n://sv-se@label/email.txt', u'e-post')
        uri = node.uri
        self.assertEqual(cache.get(uri)['content'], u'e-post')

        uris = cio.delete('sv-se@label/email#1', 'sv-se@foo/bar')
        self.assertListEqual(uris, ['sv-se@label/email#1'])

        with self.assertRaises(NodeDoesNotExist):
            storage.get(uri)

        self.assertIsNone(cache.get(uri))
Exemple #38
0
    def test_fallback(self):
        with cio.env(i18n=('sv-se', 'en-us', 'en-uk')):
            cio.set('i18n://bogus@label/email.txt', u'epost')
            cio.set('i18n://en-uk@label/surname.txt', u'surname')

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6, selects=6):
                    node1 = cio.get('i18n://label/email')
                    node2 = cio.get('i18n://label/surname', u'efternamn')
                    self.assertEqual(
                        node1.uri.namespace, 'sv-se'
                    )  # No fallback, stuck on first namespace, sv-se
                    self.assertEqual(node1.namespace_uri.namespace, 'sv-se')
                    self.assertIsNone(node1.content)
                    self.assertEqual(node2.uri.namespace, 'en-uk')
                    self.assertEqual(node2.namespace_uri.namespace, 'sv-se')
                    self.assertEqual(node2.content, u'surname')

            cache.clear()

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6):
                    cio.get('i18n://label/email', lazy=False)
                    cio.get('i18n://label/surname', u'lastname', lazy=False)
Exemple #39
0
    def test_publish(self):
        node = cio.set('sv-se@page/title', u'Djedi', publish=False)

        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 404)

        response = self.put('api.publish', node.uri)
        self.assertEqual(response.status_code, 200)

        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json_node(response), {'uri': 'i18n://sv-se@page/title.txt#1', 'content': u'Djedi'})
        response = self.put('api.publish', 'i18n://sv-se@foo/bar.txt#draft')

        self.assertEqual(response.status_code, 404)
Exemple #40
0
    def test_publish(self):
        node = cio.set('sv-se@page/title', u'Djedi', publish=False)

        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 404)

        response = self.put('api.publish', node.uri)
        self.assertEqual(response.status_code, 200)

        response = self.get('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json_node(response), {'uri': 'i18n://sv-se@page/title.txt#1', 'content': u'Djedi'})
        response = self.put('api.publish', 'i18n://sv-se@foo/bar.txt#draft')

        self.assertEqual(response.status_code, 404)
Exemple #41
0
    def test_delete(self):
        response = self.delete('api', 'i18n://sv-se@page/title')
        self.assertEqual(response.status_code, 404)

        node = cio.set('i18n://sv-se@page/title.md', u'# Djedi')

        response = self.delete('api', node.uri)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, u'')

        with self.assertRaises(NodeDoesNotExist):
            storage.get('i18n://sv-se@page/title')

        node = cio.get('i18n://page/title', lazy=False)
        self.assertIsNone(node.content)
Exemple #42
0
    def test_publish(self):
        node = cio.set('sv-se@page/title', u'Djedi', publish=False)

        response = self.get('api', 'i18n://sv-se@page/title')
        assert response.status_code == 404

        response = self.put('api.publish', node.uri)
        assert response.status_code == 200

        response = self.get('api', 'i18n://sv-se@page/title')
        assert response.status_code == 200
        assert json_node(response) == {'uri': 'i18n://sv-se@page/title.txt#1', 'content': u'Djedi'}

        response = self.put('api.publish', 'i18n://sv-se@foo/bar.txt#draft')

        assert response.status_code == 404
Exemple #43
0
    def test_set(self):
        with self.assertRaises(URI.Invalid):
            cio.set('page/title', 'fail')

        with self.assertRaises(URI.Invalid):
            cio.set('page/title.txt', 'fail')

        node = cio.set('i18n://sv-se@label/email.up', u'e-post')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.up#1')
        cache.clear()
        node = cio.get('label/email', u'fallback')
        self.assertEqual(node.content, u'E-POST')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.up#1')
        self.assertEqual(node.initial, u'fallback')
        self.assertEqual(len(node.meta.keys()),
                         0)  # No meta returned from non-versioned api get
        self.assertEqual(repr(node._node),
                         '<Node: i18n://sv-se@label/email.up#1>')
        self.assertEqual(node.for_json(), {
            'uri': node.uri,
            'content': node.content,
            'meta': node.meta
        })

        node = cio.set('sv-se@label/email', u'e-post', publish=False)
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#draft')
        self.assertKeys(node.meta, 'modified_at', 'is_published')

        node = cio.publish(node.uri)
        self.assertKeys(node.meta, 'modified_at', 'published_at',
                        'is_published')
        self.assertTrue(node.meta['is_published'])

        node = cio.get('label/email')
        self.assertEqual(node.uri, 'i18n://sv-se@label/email.txt#2')
        self.assertEqual(node.content, u'e-post')
        self.assertEqual(node.uri.ext, 'txt')
        self.assertEqual(len(node.meta.keys()), 0)

        # Try publish non-existing node/uri
        node = cio.publish('i18n://sv-se@foo/bar.txt#draft')
        self.assertIsNone(node)
Exemple #44
0
    def test_revisions(self):
        def assertRevisions(*revs):
            revisions = set(cio.revisions('i18n://sv-se@page/title'))
            assert revisions == set(revs)

        self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0)

        node = cio.load('sv-se@page/title')
        self.assertDictEqual(
            node, {
                'uri': 'i18n://sv-se@page/title.txt',
                'data': None,
                'content': None,
                'meta': {}
            })

        # First draft
        with self.assertDB(selects=1, inserts=1, updates=0):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.txt',
                               u'Content-IO',
                               publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft')

        assertRevisions(('i18n://sv-se@page/title.txt#draft', False))
        self.assertIsNone(cio.get('page/title').content)

        # Publish first draft, version 1
        with self.assertDB(calls=4, selects=2, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Second draft
        with self.assertDB(selects=1, inserts=1, updates=0):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.up',
                               u'Content-IO - Fast!',
                               publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True),
                        ('i18n://sv-se@page/title.up#draft', False))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Publish second draft, version 2
        with self.assertDB(calls=4, selects=2, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False),
                        ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!')

        # Alter published version 2
        with self.assertDB(calls=2, selects=1, inserts=0, updates=1):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.up#2',
                               u'Content-IO - Lightening fast!',
                               publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False),
                        ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(
            cio.get('page/title').content,
            u'CONTENT-IO - FAST!')  # Not published, still in cache

        # Re-publish version 2, no change
        with self.assertDB(selects=1, inserts=0, updates=0):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False),
                        ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(
            cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!')

        # Rollback version 1
        with self.assertDB(calls=3, selects=1, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish('i18n://sv-se@page/title#1')
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True),
                        ('i18n://sv-se@page/title.up#2', False))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Assert get specific version doesn't mess up the cache
        cache.clear()
        with self.assertCache(calls=0):
            self.assertEqual(
                cio.get('page/title#2').content,
                u'CONTENT-IO - LIGHTENING FAST!')
        with self.assertCache(calls=2, misses=1, sets=1):
            self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Load version 1 and 2
        data = cio.load('sv-se@page/title#1')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(data['data'], u'Content-IO')
        data = cio.load('sv-se@page/title#2')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2')
        self.assertEqual(data['data'],
                         {u'name': u'Content-IO - Lightening fast!'})

        # Load without version and expect published version
        data = cio.load('sv-se@page/title')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(data['data'], u'Content-IO')
Exemple #45
0
    def test_revisions(self):
        def assertRevisions(*revs):
            revisions = set(cio.revisions('i18n://sv-se@page/title'))
            assert revisions == set(revs)

        self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0)

        node = cio.load('sv-se@page/title')
        self.assertDictEqual(node, {
            'uri': 'i18n://sv-se@page/title.txt',
            'data': None,
            'content': None,
            'meta': {}
        })

        # First draft
        with self.assertDB(selects=1, inserts=1, updates=0):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.txt', u'Content-IO', publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft')

        assertRevisions(('i18n://sv-se@page/title.txt#draft', False))
        self.assertIsNone(cio.get('page/title').content)

        # Publish first draft, version 1
        with self.assertDB(calls=4, selects=2, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Second draft
        with self.assertDB(selects=1, inserts=1, updates=0):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.up', u'Content-IO - Fast!', publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#draft', False))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Publish second draft, version 2
        with self.assertDB(calls=4, selects=2, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!')

        # Alter published version 2
        with self.assertDB(calls=2, selects=1, inserts=0, updates=1):
            with self.assertCache(calls=0):
                node = cio.set('i18n://sv-se@page/title.up#2', u'Content-IO - Lightening fast!', publish=False)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!')  # Not published, still in cache

        # Re-publish version 2, no change
        with self.assertDB(selects=1, inserts=0, updates=0):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish(node.uri)
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2')

        assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True))
        self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!')

        # Rollback version 1
        with self.assertDB(calls=3, selects=1, updates=2):
            with self.assertCache(calls=1, sets=1):
                node = cio.publish('i18n://sv-se@page/title#1')
                self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1')

        assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#2', False))
        self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Assert get specific version doesn't mess up the cache
        cache.clear()
        with self.assertCache(calls=0):
            self.assertEqual(cio.get('page/title#2').content, u'CONTENT-IO - LIGHTENING FAST!')
        with self.assertCache(calls=2, misses=1, sets=1):
            self.assertEqual(cio.get('page/title').content, u'Content-IO')

        # Load version 1 and 2
        data = cio.load('sv-se@page/title#1')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(data['data'], u'Content-IO')
        data = cio.load('sv-se@page/title#2')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2')
        self.assertEqual(data['data'], {u'name': u'Content-IO - Lightening fast!'})

        # Load without version and expect published version
        data = cio.load('sv-se@page/title')
        self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1')
        self.assertEqual(data['data'], u'Content-IO')
Exemple #46
0
 def test_unknown_plugin(self):
     with self.assertRaises(ImproperlyConfigured):
         cio.set('i18n://sv-se@foo/bar.baz#draft', 'raise')
Exemple #47
0
 def test_unknown_plugin(self):
     with self.assertRaises(ImproperlyConfigured):
         cio.set('i18n://sv-se@foo/bar.baz#draft', 'raise')