Skip to content

Commit

Permalink
Merge pull request #38 from cloudblue/fix_url_resolver
Browse files Browse the repository at this point in the history
Fix url resolver when dirs have spaces
  • Loading branch information
marcserrat authored Dec 15, 2021
2 parents 8a9f135 + 8680b24 commit 259489d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions connect/reports/renderers/pdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright © 2021 CloudBlue. All rights reserved.

import pathlib
import os
from datetime import datetime
from functools import partial
Expand All @@ -14,14 +14,16 @@


def local_fetcher(url, root_dir=None, template_dir=None, cwd=None):
tpl_dir_path = pathlib.Path(os.path.abspath(root_dir)) / pathlib.Path(template_dir)
tpl_dir_url = tpl_dir_path.as_uri()
if url.startswith(f'file://{cwd}'):
rel_path = os.path.relpath(url[7:], cwd)
new_path = os.path.join(
os.path.abspath(root_dir),
rel_path,
)
url = f'file://{new_path}'
elif url.startswith(f'file://{os.path.abspath(os.path.join(root_dir, template_dir))}'):
elif url.startswith(tpl_dir_url):
count = url.count(template_dir)
if url.count(template_dir) > 1:
url = url.replace(f'{template_dir}/', '', count - 1)
Expand Down
10 changes: 10 additions & 0 deletions tests/reports/renderers/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def test_local_fetcher(mocker, url, expected_url):
def_fetcher.assert_called_once_with(expected_url)


def test_local_fetcher_root_with_spaces(mocker):
url = 'file:///root%20dir/template_dir/template_dir/image.png'
expected_url = 'file:///root%20dir/template_dir/image.png'
def_fetcher = mocker.patch('connect.reports.renderers.pdf.default_url_fetcher')

local_fetcher(url, root_dir='/root dir', template_dir='template_dir', cwd='/tmp/my_temp_dir')

def_fetcher.assert_called_once_with(expected_url)


def test_generate_report(mocker, account_factory, report_factory, report_data):
mocker.patch(
'connect.reports.renderers.pdf.Jinja2Renderer.generate_report',
Expand Down

0 comments on commit 259489d

Please sign in to comment.