def __init__(self, context, placeholder_name, default_layout=None, template_name=None, global_type=False): """ :param context: Rendering context :type context: jinja2.runtime.Context :param placeholder_name: Placeholder name :type placeholder_name: str :param default_layout: Layout or serialized layout (from template configuration) :type default_layout: Layout|dict|None :param template_name: The actual template this node was in. Used to figure out whether the placeholder lives in an `extends` parent, or in a child. :type template_name: str|None :param global_type: Boolean indicating whether this is a global placeholder :type global_type: bool|False """ self.context = context self.view_config = get_view_config(context, global_type=global_type) self.placeholder_name = placeholder_name self.template_name = ("_xtheme_global_template_name" if global_type else template_name) self.default_layout = default_layout self.layout = self.view_config.get_placeholder_layout(placeholder_name, self.default_layout) self.global_type = global_type # For non-global placeholders, editing is only available for placeholders in the "base" template, i.e. # one that is not an `extend` parent. Declaring placeholders in `include`d templates is fine, # but their configuration will not be shared among different uses of the same include. if global_type: self.edit = is_edit_mode(context["request"]) else: is_base = (self.template_name == self.context.name) self.edit = (is_base and is_edit_mode(context["request"]))
def test_edit_can_be_set_via_view(rf): request = rf.get("/") request.user = SuperUser() request.session = {} request.POST = {"command": "edit_on"} xtheme_dispatch(request) assert is_edit_mode(request) request.POST = {"command": "edit_off"} xtheme_dispatch(request) assert not is_edit_mode(request)
def test_edit_can_be_set_via_view(rf): request = rf.get("/") request.user = SuperUser() request.session = {} request.POST = {"command": "edit_on"} command_dispatch(request) assert is_edit_mode(request) request.POST = {"command": "edit_off"} command_dispatch(request) assert not is_edit_mode(request)
def test_edit_priv(rf): request = rf.get("/") request.user = SuperUser() request.session = {} assert could_edit(request) assert not is_edit_mode(request) set_edit_mode(request, True) assert is_edit_mode(request) set_edit_mode(request, False) assert not is_edit_mode(request)
def get_view_config(context, global_type=False): """ Get a view configuration object for a Jinja2 rendering context. :param context: Rendering context :type context: jinja2.runtime.Context :param global_type: Boolean indicating whether this is a global type :type global_type: bool|False :return: View config :rtype: shoop.xtheme.view_config.ViewConfig """ # This uses the Jinja context's technically-immutable vars dict # to cache the view configuration. This is fine in our case, I'd say. request = context.get("request") config_key = "_xtheme_global_view_config" if global_type else "_xtheme_view_config" config = context.vars.get(config_key) if (config is None): view_object = context.get("view") if view_object: view_class = view_object.__class__ view_name = view_class.__name__ else: view_name = "UnknownView" config = ViewConfig( theme=get_current_theme(request), view_name=view_name, draft=is_edit_mode(request), global_type=global_type, ) context.vars[config_key] = config return config
def get_view_config(context): """ Get a view configuration object for a Jinja2 rendering context. :param context: Rendering context :type context: jinja2.runtime.Context :return: View config :rtype: shoop.xtheme.view_config.ViewConfig """ # This uses the Jinja context's technically-immutable vars dict # to cache the view configuration. This is fine in our case, I'd say. request = context.get("request") config = context.vars.get("_xtheme_view_config") if config is None: view_object = context.get("view") if view_object: view_class = view_object.__class__ view_name = view_class.__name__ else: view_name = "UnknownView" config = ViewConfig( theme=get_current_theme(request), view_name=view_name, draft=is_edit_mode(request) ) context.vars["_xtheme_view_config"] = config return config
def get_request(edit=False): request = RequestFactory().get("/") request.session = {} if edit: request.user = SuperUser() set_edit_mode(request, True) assert is_edit_mode(request) else: request.user = AnonymousUser() return request
def is_edit_mode(self, context): """ Get the current edit mode status. :param context: Implicit Jinja2 context :type context: jinja2.runtime.Context :return: Edit mode enable flag :rtype: bool """ return is_edit_mode(context["request"])
def __init__(self, context, placeholder_name, default_layout=None, template_name=None, global_type=False): """ :param context: Rendering context :type context: jinja2.runtime.Context :param placeholder_name: Placeholder name :type placeholder_name: str :param default_layout: Layout or serialized layout (from template configuration) :type default_layout: Layout|dict|None :param template_name: The actual template this node was in. Used to figure out whether the placeholder lives in an `extends` parent, or in a child. :type template_name: str|None :param global_type: Boolean indicating whether this is a global placeholder :type global_type: bool|False """ self.context = context self.view_config = get_view_config(context, global_type=global_type) self.placeholder_name = placeholder_name self.template_name = ("_xtheme_global_template_name" if global_type else template_name) self.default_layout = default_layout self.layout = self.view_config.get_placeholder_layout( placeholder_name, self.default_layout) self.global_type = global_type # For non-global placeholders, editing is only available for placeholders in the "base" template, i.e. # one that is not an `extend` parent. Declaring placeholders in `include`d templates is fine, # but their configuration will not be shared among different uses of the same include. if global_type: self.edit = is_edit_mode(context["request"]) else: is_base = (self.template_name == self.context.name) self.edit = (is_base and is_edit_mode(context["request"]))