def test_get_resources_should_return_resources_matching_given_ext(self): self._assertExpectedResources(self.EXPECTED_RESOURCES, get_resources('testing', ext='css')) self._assertExpectedResources( self.EXPECTED_PRINT_RESOURCES, get_resources('testing', ext='css', css_media='print'))
def handle(self, *args, **options): """ Run command. """ self.verbose('Processing svgicons...Please Wait...') identifier = options.get('identifier') if not identifier: identifier = load_resource_version_identifier() for target in get_resource_targets(): # build filename filename = get_svgicons_filename(target, identifier) # build icon sheet resources = get_resources(target, 'svg') resources_with_style = get_resources(target, 'svg', 'with-style') if len(resources) > 0 or len(resources_with_style) > 0: self.verbose('> %s' % filename) svg = get_combined_svg(resources, resources_with_style) # folder exists? if not os.path.isdir(settings.STATIC_ROOT): os.makedirs(settings.STATIC_ROOT) # write file path = os.path.join(settings.STATIC_ROOT, filename) with codecs.open(path, 'w', encoding='utf-8') as f: f.write(svg) self.verbose('Complete.')
def media_api_svgicons(request, target, name=None): """ Serve an SVG icon set file for given target in debug mode. """ if not settings.DEBUG: raise PermissionDenied( 'SVG icon sets via the media api are only available in DEBUG mode.' ) # get list of SVG resource files resources = get_resources(target, 'svg', name=name) styled_resources = get_resources(target, 'svg', 'with-style', name=name) # raise 404 if we cannot find an icon if not resources and not styled_resources: raise Http404('No icons found for the given target and/or icon name.') # construct svg icon set file content content = get_combined_svg(resources, styled_resources) # construct http response response = HttpResponse(content, content_type='image/svg+xml') response['Content-Length'] = len(content) return response
def get_used_font_declarations(self): """ Return a unique list of font declarations that are currently used by the system and need to be cached. """ font_declarations = {} targets = get_resource_targets() for target in targets: font_resources = get_resources( target, css_media='font', data_only=True ) for font_resource in font_resources: font_declaration = FontCache.get_font_declaration(font_resource) font_name = font_declaration.font_name if font_name in font_declarations: # font already listed. Join varients font_declarations[font_name].join_with(font_declaration) else: # new font font_declarations[font_name] = font_declaration return sorted( font_declarations.values(), key=lambda decl: decl.font_name )
def inline_svgicons(target): """ Renders inline markup for defining an SVG icon sheet for all SVG icon assets defined for the given bucket name (target). SVG assets are using the standard resource system that is also used for CSS and Javascript assets in combination with resources and inline_resources template tags. """ if target not in get_resource_target_definition(): raise AttributeError( 'Expected valid \'target\' argument for template ' + 'tag \'inline_svgicons\'.') if settings.DEBUG: return mark_safe( get_combined_svg(get_resources(target, 'svg'), get_resources(target, 'svg', 'with-style'))) else: identifier = load_resource_version_identifier() filename = get_svgicons_filename(target, identifier) path = os.path.join(settings.STATIC_ROOT, filename) return mark_safe(file_get_contents(path))
def test_get_resources_should_filter_by_name_after_glob_if_argument_is_given( self): self.assertEqual(['/cubane/testapp/css/glob/b.css'], get_resources('testing', ext='css', name='b'))
def test_get_resources_should_filter_by_name(self): self.assertEqual(['cubane/testapp/css/style.templating.css'], get_resources('testing', ext='css', name='style.templating'))
def test_get_resources_should_apply_glob_for_resources_of_sub_app(self): self._assertExpectedResources(self.EXPECTED_SUBAPP_GLOB_RESOURCES, get_resources('subapp', ext='svg'))
def test_get_resources_should_apply_glob_sorted_alphabetically(self): self._assertExpectedResources(self.EXPECTED_GLOB_RESOURCES, get_resources('testing', ext='css'))
def test_get_resources_should_ignore_apps_that_do_not_exist(self): settings.RESOURCES = {'testing': ['fakeapp']} settings.INSTALLED_APPS = ['fakeapp'] self.assertEqual([], get_resources('testing', 'css'))
def test_get_resources_should_return_resources_matching_given_media_prefix( self): self.assertEqual(1, len(get_resources('testing', css_media='print')))
def test_get_resources_should_return_resources_matching_screen_media_without_media_prefix( self): resources = get_resources('testing', 'css') self._assertExpectedResources(self.EXPECTED_RESOURCES, resources)
def test_get_resources_should_return_list(self): self.assertIsInstance(get_resources('testing'), list)
def render(self, context): """ Render resource tag. """ target = value_or_literal(self.target, context) css_media = value_or_literal(self.css_media, context) # async if self.is_async is not None: is_async = value_or_literal(self.is_async, context) elif self.ext == 'js': is_async = True else: is_async = False # additonal_data if self.additional_data is not None: additional_data = value_or_literal(self.additional_data, context) else: additional_data = None if css_media and css_media not in settings.CSS_MEDIA: raise template.TemplateSyntaxError( ("'resources' only accepts the following values for css_media argument: " + "%s." ) % settings.CSS_MEDIA ) # if target is not defined, just ignore it. if target not in get_resource_target_definition(): return '' if self.inline: if not settings.MINIFY_RESOURCES: resources = get_resources(target, self.ext) content = '\n'.join([get_resource(url) for url in resources]) else: content = get_resource( get_minified_filename( target, self.ext, 'screen' if self.ext == 'css' else None, self.get_identifier() ) ) return self.inline_include(content, additional_data, css_media) else: if not settings.MINIFY_RESOURCES: resources = get_resources(target, self.ext, css_media) return ''.join([ self.include(url, css_media, is_async, additional_data) for url in resources ]) else: return self.include( get_minified_filename( target, self.ext, css_media, self.get_identifier() ), css_media, is_async, additional_data )