Ejemplo n.º 1
0
def test_get_templates_info(open_m, FileSystemLoader_m):
    loader = FileSystemLoader_m.return_value
    loader.list_templates.return_value = [
        'r1/t/template.j2',
        'r2/t/template.j2',
        'r3/t/template.j2',
    ]

    open_m.return_value = MagicMock(spec=file)
    file_handle = open_m.return_value.__enter__.return_value
    error = IOError()
    error.errno = errno.ENOENT
    file_handle.readline.side_effect = ['http://b.c/r', '', error]

    manager = TemplateManager('/fake')
    templates_info = manager.get_templates_info()
    enc = urlsafe_b64encode
    expected = [
        {'id': enc('r1/t'), 'name': 'r1/t', 'data_source_url': 'http://b.c/r'},
        {'id': enc('r2/t'), 'name': 'r2/t', 'data_source_url': None},
        {'id': enc('r3/t'), 'name': 'r3/t', 'data_source_url': None},
    ]
    # last call
    open_m.assert_called_with('/fake/r3/t/data_source_url', 'rb')
    assert_items_equal(expected, templates_info)
Ejemplo n.º 2
0
def render(template_name):
    data_source = {}
    manager = TemplateManager(settings.RAFEE_REPO_DIR)
    info = manager.get_template_info(template_name)
    data_source_url = info.get('data_source_url', None)
    if data_source_url is not None:
        r = requests.get(data_source_url)
        data_source = r.json()
    return manager.render(template_name, data_source=data_source)
Ejemplo n.º 3
0
 def validate_templates(self, value):
     manager = TemplateManager(settings.RAFEE_REPO_DIR)
     bad_template_names = []
     for template_name in value.split(","):
         if not manager.template_exists(template_name):
             bad_template_names.append(template_name)
     if len(bad_template_names) > 0:
         raise serializers.ValidationError(TEMPLATES_NOT_FOUND.format(bad_template_names))
     return value
Ejemplo n.º 4
0
def test_template_exists(Environment_m, FileSystemLoader_m):
    loader = FileSystemLoader_m.return_value
    loader.list_templates.return_value = [
        'c/t1/template.j2', 'c/t2/template.j2'
    ]
    manager = TemplateManager('/fake')
    assert manager.template_exists('c/t1')
    assert manager.template_exists('c/t1/template.j2')
    assert not manager.template_exists('none')
Ejemplo n.º 5
0
    def post(self, request):
        self.validate(request)
        template_name = request.data.get('template_name')
        manager = TemplateManager(settings.RAFEE_REPO_DIR)
        if not manager.template_exists(template_name):
            return Response(
                {'detail': TEMPLATE_NOT_FOUND},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )

        task = render.delay(template_name)
        return Response({'task': task.task_id})
Ejemplo n.º 6
0
    def post(self, request):
        self.validate(request)
        template_str = request.data.get('template_str')
        manager = TemplateManager(settings.RAFEE_REPO_DIR)
        data_source = {}
        data_source_url = request.data.get('data_source_url', None)
        if data_source_url is not None and data_source_url != '':
            data_source = requests.get(data_source_url).json()

        preview = manager.render_from_string(
            template_str,
            data_source=data_source,
        )
        return Response({'preview': preview})
Ejemplo n.º 7
0
 def get(self, request):
     manager = TemplateManager(settings.RAFEE_REPO_DIR)
     info = manager.get_templates_info()
     return Response(info)