def __init__(self, apps=None, *args, **kwargs): # List of locations with static files self.locations = get_directories_in_tethys(('static', 'public'), with_app_name=True) # Maps dir paths to an appropriate storage instance self.storages = SortedDict() for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super().__init__(*args, **kwargs)
def test_get_directories_in_tethys_templates_apps_import_error(self, mock_harvester): # Mock the extension_modules variable with bad data, to throw an ImportError mock_harvester().app_modules = {'foo_invalid_foo': 'tethys_app.foo_invalid_foo'} result = utilities.get_directories_in_tethys(('templates',)) self.assertGreaterEqual(len(result), 0) test_app = False for r in result: if '/tethysapp/foo_invalid_foo/templates' in r: test_app = True self.assertFalse(test_app)
def __init__(self, apps=None, *args, **kwargs): # List of locations with static files self.locations = get_directories_in_tethys(('static', 'public'), with_app_name=True) # Maps dir paths to an appropriate storage instance self.storages = SortedDict() for prefix, root in self.locations: filesystem_storage = FileSystemStorage(location=root) filesystem_storage.prefix = prefix self.storages[root] = filesystem_storage super().__init__(*args, **kwargs)
def test_get_directories_in_tethys_templates(self): # Get the templates directories for the test_app and test_extension result = utilities.get_directories_in_tethys(('templates',)) self.assertGreaterEqual(len(result), 2) test_app = False test_ext = False for r in result: if '/tethysapp/test_app/templates' in r: test_app = True if '/tethysext-test_extension/tethysext/test_extension/templates' in r: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def test_get_directories_in_tethys_templates(self): # Get the templates directories for the test_app and test_extension result = utilities.get_directories_in_tethys(('templates',)) self.assertGreaterEqual(len(result), 2) test_app = False test_ext = False for r in result: if '/tethysapp/test_app/templates' in r: test_app = True if '/tethysext-test_extension/tethysext/test_extension/templates' in r: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def test_get_directories_in_tethys_foo_public(self): # Get the foo and public directories for the test_app and test_extension # foo doesn't exist, but public will result = utilities.get_directories_in_tethys(('foo', 'public')) self.assertGreaterEqual(len(result), 2) test_app = False test_ext = False for r in result: if '/tethysapp/test_app/public' in r: test_app = True if '/tethysext-test_extension/tethysext/test_extension/public' in r: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def test_get_directories_in_tethys_foo_public(self): # Get the foo and public directories for the test_app and test_extension # foo doesn't exist, but public will result = utilities.get_directories_in_tethys(('foo', 'public')) self.assertGreaterEqual(len(result), 2) test_app = False test_ext = False for r in result: if '/tethysapp/test_app/public' in r: test_app = True if '/tethysext-test_extension/tethysext/test_extension/public' in r: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def test_get_directories_in_tethys_templates_extension_import_error(self, mock_harvester): # Mock the extension_modules variable with bad data, to throw an ImportError mock_harvester().extension_modules = {'foo_invalid_foo': 'tethysext.foo_invalid_foo'} result = utilities.get_directories_in_tethys(('templates',)) self.assertGreaterEqual(len(result), 1) test_app = False test_ext = False for r in result: if '/tethysapp/test_app/templates' in r: test_app = True if '/tethysext-test_extension/tethysext/test_extension/templates' in r: test_ext = True self.assertTrue(test_app) self.assertFalse(test_ext)
def test_get_directories_in_tethys_templates_with_app_name(self): # Get the templates directories for the test_app and test_extension # Use the with_app_name argument, so that the app and extension names appear in the result result = utilities.get_directories_in_tethys(('templates',), with_app_name=True) self.assertGreaterEqual(len(result), 2) self.assertEqual(2, len(result[0])) self.assertEqual(2, len(result[1])) test_app = False test_ext = False for r in result: if 'test_app' in r and '/tethysapp/test_app/templates' in r[1]: test_app = True if 'test_extension' in r and '/tethysext-test_extension/tethysext/test_extension/templates' in r[1]: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def test_get_directories_in_tethys_templates_with_app_name(self): # Get the templates directories for the test_app and test_extension # Use the with_app_name argument, so that the app and extension names appear in the result result = utilities.get_directories_in_tethys(('templates',), with_app_name=True) self.assertGreaterEqual(len(result), 2) self.assertEqual(2, len(result[0])) self.assertEqual(2, len(result[1])) test_app = False test_ext = False for r in result: if 'test_app' in r and '/tethysapp/test_app/templates' in r[1]: test_app = True if 'test_extension' in r and '/tethysext-test_extension/tethysext/test_extension/templates' in r[1]: test_ext = True self.assertTrue(test_app) self.assertTrue(test_ext)
def get_template_sources(self, template_name, template_dirs=None): """ Return an Origin object pointing to an absolute path in each directory in template_dirs. For security reasons, if a path doesn't lie inside one of the template_dirs it is excluded from the result set. """ if not template_dirs: template_dirs = get_directories_in_tethys(('templates', )) for template_dir in template_dirs: try: name = safe_join(template_dir, template_name) except SuspiciousFileOperation: # The joined path was located outside of this template_dir # (it might be inside another one, so this isn't fatal). continue yield Origin( name=name, template_name=template_name, loader=self, )
def get_template_sources(self, template_name, template_dirs=None): """ Return an Origin object pointing to an absolute path in each directory in template_dirs. For security reasons, if a path doesn't lie inside one of the template_dirs it is excluded from the result set. """ if not template_dirs: template_dirs = get_directories_in_tethys(('templates',)) for template_dir in template_dirs: try: name = safe_join(template_dir, template_name) except SuspiciousFileOperation: # The joined path was located outside of this template_dir # (it might be inside another one, so this isn't fatal). continue yield Origin( name=name, template_name=template_name, loader=self, )
def test_get_directories_in_tethys_foo(self): # Get the foo directories for the test_app and test_extension # foo doesn't exist result = utilities.get_directories_in_tethys(('foo',)) self.assertEqual(0, len(result))
def test_get_directories_in_tethys_foo(self): # Get the foo directories for the test_app and test_extension # foo doesn't exist result = utilities.get_directories_in_tethys(('foo',)) self.assertEqual(0, len(result))