def get_jinja_context(path="/", user=None, **vars): env = Environment() ctx = Context(environment=env, parent=None, name="FauxContext", blocks={}) if "request" not in vars: vars["request"] = get_request_with_basket(path, user=user) ctx.vars.update(vars) return ctx
def _filter_select_dateds(context: Context, dateds: Iterable[Dated], date: Optional[Datey]) -> Iterator[Dated]: if date is None: date = context.resolve_or_missing('today') return filter( lambda dated: dated.date is None or dated.date.comparable and dated. date in date, dateds)
def test_is_menu_category_active(rf, admin_user): url = reverse('shuup_admin:shop_product.list') menu_request = apply_request_middleware(rf.get(url), user=admin_user) menu_categories = get_menu_entry_categories(menu_request) env = Environment() context = Context(environment=env, parent=None, name="FauxContext", blocks={}) context.vars.update({"request": menu_request}) product_category = [ category for category in menu_categories if category.identifier == PRODUCTS_MENU_CATEGORY ][0] assert is_menu_category_active(context, product_category, menu_request.path) assert is_menu_item_active(context, url, menu_request.path) shop_category = [ category for category in menu_categories if category.identifier == SETTINGS_MENU_CATEGORY ][0] assert not is_menu_category_active(context, shop_category, menu_request.path) with override_settings(SHUUP_ALWAYS_ACTIVE_MENU_CATEGORY_IDENTIFIERS=[ SETTINGS_MENU_CATEGORY ]): assert is_menu_category_active(context, shop_category, menu_request.path)
def _filter_url(context: Context, resource: Any, media_type: Optional[str] = None, locale: Optional[str] = None, **kwargs) -> str: media_type = 'text/html' if media_type is None else media_type locale = locale if locale else context.resolve_or_missing('locale') return context.environment.app.localized_url_generator.generate( resource, media_type, locale=locale, **kwargs)
def test_0010_access_anonymous(self): # Login is OK self.assert_response_code(url_for('login.login'), 200) ctx = Context(self.app.jinja_env, None, "pseudo", {}) # Fetching static content is OK self.assert_response_code(require(ctx, 'main.css'), 200) # Access to other pages/blueprints is NOT OK self.assert_response_code(url_for('finance.bank_accounts_list'), 302) self.assert_response_code(url_for('infrastructure.switches'), 302)
def _filter_json(context: Context, data: Any, indent: Optional[int] = None) -> str: """ Converts a value to a JSON string. """ return stdjson.dumps(data, indent=indent, cls=JSONEncoder.get_factory( context.environment.app, context.resolve_or_missing('locale')))
def _filter_select_localizeds( context: Context, localizeds: Iterable[Localized], include_unspecified: bool = False) -> Iterable[Localized]: locale = context.resolve_or_missing('locale') for localized in localizeds: if include_unspecified and localized.locale in { None, 'mis', 'mul', 'und', 'zxx' }: yield localized if localized.locale is not None and negotiate_locale( locale, [localized.locale]) is not None: yield localized
def _filter_sort_localizeds(context: Context, localizeds: Iterable[Localized], localized_attribute: str, sort_attribute: str) -> Iterable[Localized]: locale = context.resolve_or_missing('locale') get_localized_attr = make_attrgetter(context.environment, localized_attribute) get_sort_attr = make_attrgetter(context.environment, sort_attribute) def _get_sort_key(x): return get_sort_attr( negotiate_localizeds(locale, get_localized_attr(x))) return sorted(localizeds, key=_get_sort_key)
def new_context(self, vars=None, shared=False): """Create a new :class:`Context` for this template. The vars provided will be passed to the template. Per default the globals are added to the context, if shared is set to `True` the data provided is used as parent namespace. This is used to share the same globals in multiple contexts without consuming more memory. (This works because the context does not modify the parent dict) """ if vars is None: vars = {} if shared: parent = vars else: parent = dict(self.globals, **vars) return Context(self.environment, parent, self.name, self.blocks)
def new_context(self, vars=None, shared=False, locals=None): """Create a new :class:`Context` for this template. The vars provided will be passed to the template. Per default the globals are added to the context. If shared is set to `True` the data is passed as it to the context without adding the globals. `locals` can be a dict of local variables for internal usage. """ if vars is None: vars = {} if shared: parent = vars else: parent = dict(self.globals, **vars) if locals: # if the parent is shared a copy should be created because # we don't want to modify the dict passed if shared: parent = dict(parent) for key, value in locals.iteritems(): if key[:2] == 'l_' and value is not missing: parent[key[2:]] = value return Context(self.environment, parent, self.name, self.blocks)
def __init__(self, placeholder, *args, **kwargs): Context.__init__(self, *args, **kwargs) self.placeholder = placeholder
def _filter_format_date(context: Context, date: Datey) -> str: locale = context.resolve_or_missing('locale') return format_datey(date, locale)
def _filter_negotiate_localizeds( context: Context, localizeds: Iterable[Localized]) -> Optional[Localized]: locale = context.resolve_or_missing('locale') return negotiate_localizeds(locale, list(localizeds))