예제 #1
0
def test_and():
    from flamingo.core.data_model import Content, Q

    c = Content(a=10, b=20)

    assert (Q(a=10) & Q(b=20)).check(c)
    assert not (Q(a=10) & Q(b=21)).check(c)
예제 #2
0
def test_or():
    from flamingo.core.data_model import Content, Q

    q = Q(a=10) | Q(b=10)

    assert q.check(Content(a=10, b=20))
    assert q.check(Content(a=20, b=10))

    assert not q.check(Content(b=20))
    assert not q.check(Content(a=20))
예제 #3
0
def test_f():
    from flamingo.core.data_model import Content, Q, F

    q = Q(a=F('b'))

    assert q.check(Content(a=1, b=1))
    assert not q.check(Content(a=1, b=2))
예제 #4
0
def test_not():
    from flamingo.core.data_model import Content, Q

    q = ~Q(a=1)

    assert q.check(Content(a=2))
    assert not q.check(Content(a=1))
예제 #5
0
    def build(self, clean=True):
        self.run_plugin_hook('pre_build')

        # remove previous artifacts
        if clean and os.path.exists(self.settings.OUTPUT_ROOT):
            self.rm_rf(self.settings.OUTPUT_ROOT)

        # render contents
        if self.settings.CONTENT_PATHS:
            contents = self.contents.filter(
                Q(path__in=self.settings.CONTENT_PATHS)
                | Q(i18n_path__in=self.settings.CONTENT_PATHS), )

        else:
            contents = self.contents

        for content in contents:
            output_path = os.path.join(self.settings.OUTPUT_ROOT,
                                       content['output'])

            self.mkdir_p(output_path)
            self.write(output_path, self.render(content))

        self.run_plugin_hook('post_build')
예제 #6
0
    def contents_parsed(self, context):
        # settings
        content_key = getattr(context.settings, 'I18N_CONTENT_KEY', 'id')
        languages = getattr(context.settings, 'I18N_LANGUAGES', ['en', 'de'])

        default_language = getattr(context.settings, 'I18N_DEFAULT_LANGUAGE',
                                   'en')

        enforce_redirect = getattr(context.settings, 'I18N_ENFORCE_REDIRECT',
                                   True)

        ignore = getattr(context.settings, 'I18N_IGNORE',
                         {'i18n_ignore__isnull': False})

        for content in context.contents.exclude(ignore):
            # set lang tag if not set
            if not content['lang']:
                content['lang'] = default_language

            # if no content_key is set the path becomes the content_key
            if not content[content_key]:
                content[content_key] = content['path']

            # find translations
            translation_langs = context.contents.filter(
                **{content_key: content[content_key]}).values('lang')

            # add missings translations
            for l in languages:
                if l not in translation_langs:
                    content_data = copy(content.data)

                    if 'path' in content_data:
                        content_data['i18n_path'] = content_data['path']
                        del content_data['path']

                    content_data['lang'] = l

                    context.contents.add(**content_data)

        # set output and url according to lang code
        # set translations
        for content in context.contents.exclude(ignore):
            content['output'] = os.path.join(content['lang'],
                                             content['output'])

            content['url'] = os.path.join('/', content['lang'],
                                          content['url'][1:])

            content['translations'] = context.contents.filter(
                Q(**{content_key: content[content_key]}),
                ~Q(lang=content['lang']),
            )

        # enforce redirect
        if enforce_redirect:
            context.contents.add(**{
                'output': 'index.html',
                'url': '/',
                'content_body': INDEX_PAGE.format(default_language),
            })
예제 #7
0
def test_q_api():
    from flamingo.core.data_model import Content, Q

    # to few arguments
    with pytest.raises(TypeError) as excinfo:
        Q()

    assert str(excinfo.value) == 'to few arguments'

    # to many arguments
    with pytest.raises(TypeError) as excinfo:
        Q(Q(a=1), b=2)

    assert str(excinfo.value) == 'to many arguments'

    c = Content(a=10, b=20)

    assert Q(a=10).check(c)
    assert Q({'a': 10}).check(c)
    assert not Q(a=20).check(c)
    assert Q(Q(Q(Q(Q(a=10))))).check(Content(a=10))