Пример #1
0
def render_component(path_to_source,
                     props=None,
                     to_static_markup=False,
                     json_encoder=None):
    if not os.path.isabs(path_to_source):
        # If its using the manifest staticfiles storage, to the hashed name.
        # eg. js/hello.js -> js/hello.d0bf07ff5f07.js
        if isinstance(staticfiles_storage, HashedFilesMixin):
            try:
                path_to_source = staticfiles_storage.stored_name(
                    path_to_source)
            except ValueError:
                # Couldn't find it.
                pass
        # Now resolve it to the absolute path using the finders
        path_to_source = find_static(path_to_source) or path_to_source

    if json_encoder is None:
        json_encoder = DjangoJSONEncoder().encode

    try:
        html = render_core(path_to_source,
                           props,
                           to_static_markup,
                           json_encoder,
                           service_url=SERVICE_URL)
    except:
        if not FAIL_SAFE:
            raise
        log.exception('Error while rendering %s', path_to_source)
        html = ''

    return RenderedComponent(html, path_to_source, props, json_encoder)
Пример #2
0
def render_component(path_to_source, props=None, to_static_markup=False, json_encoder=None):
    if not os.path.isabs(path_to_source):
        # If its using the manifest staticfiles storage, to the hashed name.
        # eg. js/hello.js -> js/hello.d0bf07ff5f07.js
        if isinstance(staticfiles_storage, HashedFilesMixin):
            try:
                path_to_source = staticfiles_storage.stored_name(path_to_source)
            except ValueError:
                # Couldn't find it.
                pass
        # Now resolve it to the absolute path using the finders
        path_to_source = find_static(path_to_source) or path_to_source

    if json_encoder is None:
        json_encoder = DjangoJSONEncoder().encode

    try:
        html = render_core(path_to_source, props, to_static_markup, json_encoder, service_url=SERVICE_URL)
    except:
        if not FAIL_SAFE:
            raise
        log.exception('Error while rendering %s', path_to_source)
        html = ''

    return RenderedComponent(html, path_to_source, props, json_encoder)
Пример #3
0
def parse_source(source):
    """
    Parse and lookup the file system path for a given source.
    The function understands both media names and static names
    (if prefixed with "static:")
    """
    if source.startswith('static:'):
        source = source[7:]

        # Don't hash if in debug mode. This will also fail hard if the
        # staticfiles storage doesn't support hashing of filenames.
        if not settings.DEBUG:
            # We always should have used stored_name because hashed_name
            # calculates the name on the fly. For backwards compatibility we
            # try to use stored_name but fall back to hashed_name.
            if hasattr(staticfiles_storage, 'stored_name'):
                source = staticfiles_storage.stored_name(source)
            else:
                source = staticfiles_storage.hashed_name(source)
            source = staticfiles_storage.path(source)
        else:
            source = find(source)
    else:
        if not source.startswith('/'):
            source = default_storage.path(source)

    return source
def cloudinary_static(context, image, options_dict={}, **options):
    options = dict(options_dict, **options)
    try:
        if context['request'].is_secure() and 'secure' not in options:
            options['secure'] = True
    except KeyError:
        pass
    if not isinstance(image, CloudinaryResource):
        image = staticfiles_storage.stored_name(image)
        image = CloudinaryResource(image)
    return mark_safe(image.image(**options))
def cloudinary_static(context, image, options_dict={}, **options):
    options = dict(options_dict, **options)
    try:
        if context['request'].is_secure() and 'secure' not in options:
            options['secure'] = True
    except KeyError:
        pass
    if not isinstance(image, CloudinaryResource):
        image = staticfiles_storage.stored_name(image)
        image = CloudinaryResource(image)
    return mark_safe(image.image(**options))
Пример #6
0
def load_staticfile(name, postprocessor=None, fail_silently=False):
    if postprocessor:
        cache_key = '{0}:{1}.{2}'.format(name, postprocessor.__module__,
                                         postprocessor.__name__)
    else:
        cache_key = name

    if cache_key in load_staticfile._cache:
        return load_staticfile._cache[cache_key]

    if settings.DEBUG:
        # Dont access file via staticfile storage in debug mode. Not available
        # without collectstatic management command.
        path = find(name)
    else:
        # Ensure that we include the hashed version of the static file if
        # staticfiles storage uses the HashedFilesMixin.
        if hasattr(staticfiles_storage, 'stored_name'):
            name = staticfiles_storage.stored_name(name)

        if staticfiles_storage.exists(name):
            # get path if target file exists.
            path = staticfiles_storage.path(name)
        else:
            path = None

    if not path:
        if not fail_silently:
            raise ValueError(
                'Staticfile not found for inlining: {0}'.format(name))
        return ''

    with open(path, 'r') as staticfile:
        content = staticfile.read()

    if postprocessor:
        content = postprocessor(name, path, content)

    if not settings.DEBUG:
        load_staticfile._cache[cache_key] = content

    return content
Пример #7
0
def render_component(path_to_source, props=None, to_static_markup=False, json_encoder=None, timeout=TIMEOUT):
    if not os.path.isabs(path_to_source):
        # If its using the manifest staticfiles storage, to the hashed name.
        # eg. js/hello.js -> js/hello.d0bf07ff5f07.js
        if isinstance(staticfiles_storage, HashedFilesMixin):
            try:
                path_to_source = staticfiles_storage.stored_name(path_to_source)
            except ValueError:
                # Couldn't find it.
                pass

        # first, attempt to resolve at STATIC_ROOT if the file was collected
        abs_path = os.path.join(settings.STATIC_ROOT or '', path_to_source)
        if os.path.exists(abs_path):
            path_to_source = abs_path
        else:
            # Otherwise, resolve it using finders
            path_to_source = find_static(path_to_source) or path_to_source

    if json_encoder is None:
        json_encoder = DjangoJSONEncoder().encode

    try:
        html = render_core(
            path_to_source,
            props,
            to_static_markup,
            json_encoder,
            service_url=SERVICE_URL,
            timeout=timeout,
        )
    except Exception:
        if not FAIL_SAFE:
            raise
        log.exception('Error while rendering %s', path_to_source)
        html = ''

    return RenderedComponent(html, path_to_source, props, json_encoder)