def add_resources(context, content): view_class = getattr(context["view"], "__class__", None) if context.get("view") else None if not view_class: return view_name = getattr(view_class, "__name__", "") if view_name == "ProductDetailView": add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce/recently_viewed_products/lib.js"))
def add_init_fields_resource(context, country_code_field, region_code_field, region_field=None, placement="body_end"): add_resource(context, placement, InlineScriptResource( INITIALIZE_FIELDS_FUNCTION % { "country_code_field": country_code_field, "region_code_field": region_code_field, "region_field": region_field if region_field else "" }) )
def render(self, context): for location, __ in self.fields: if location in resources.KNOWN_LOCATIONS: resource = self.config.get(location, "") add_resource(context, location, JinjaMarkupResource(resource, context)) in_place = self.config.get("in_place", "") if in_place: return JinjaMarkupResource(in_place, context).render()
def add_edit_resources(context): """ Possibly inject Xtheme editor injection resources into the given context's resources. :param context: Jinja rendering context :type context: jinja2.runtime.Context """ request = context.get("request") if not can_edit(context): return try: command_url = reverse("E-Commerce:xtheme") edit_url = reverse("E-Commerce:xtheme_editor") inject_snipper = reverse("E-Commerce_admin:xtheme_snippet.list") except NoReverseMatch: # No URLs no resources return from .rendering import get_view_config # avoid circular import view_config = get_view_config(context) theme = get_current_theme(request.shop) add_resource(context, "body_end", InlineScriptResource.from_vars("XthemeEditorConfig", { "commandUrl": command_url, "editUrl": edit_url, "injectSnipperUrl": inject_snipper, "themeIdentifier": theme.identifier, "viewName": view_config.view_name, "edit": is_edit_mode(request), "csrfToken": get_token(request), })) add_resource(context, "head_end", get_E-Commerce_static_url("xtheme/editor-injection.css")) add_resource(context, "body_end", get_E-Commerce_static_url("xtheme/editor-injection.js"))
def add_gdpr_consent_resources(context, content): if not valid_view(context): return request = context["request"] shop = get_shop(request) gdpr_settings = GDPRSettings.get_for_shop(shop) # GDPR not enabled, nothing to do if not gdpr_settings.enabled: return # always add styles add_resource(context, "head_end", get_E-Commerce_static_url("E-Commerce-gdpr.css")) user = request.user if not user.is_anonymous() and should_reconsent_privacy_policy(shop, user): consent_page = get_privacy_policy_page(shop) render_context = { "request": request, "csrf_token": context["csrf_token"], "url": "/%s" % consent_page.url, "accept_url": reverse("E-Commerce:gdpr_policy_consent", kwargs=dict(page_id=consent_page.id)) } update_resource = InlineMarkupResource( loader.render_to_string("E-Commerce/gdpr/privacy_policy_update.jinja", context=render_context) ) add_resource(context, "body_end", update_resource) # consent already added if settings.E-Commerce_GDPR_CONSENT_COOKIE_NAME in request.COOKIES: return gdpr_documents = [] if has_installed("E-Commerce.simple_cms"): gdpr_documents = get_active_consent_pages(shop) render_context = { "request": request, "csrf_token": context["csrf_token"], "gdpr_settings": gdpr_settings, "gdpr_documents": gdpr_documents, "gdpr_cookie_categories": GDPRCookieCategory.objects.filter(shop=shop) } html_resource = InlineMarkupResource( loader.render_to_string("E-Commerce/gdpr/gdpr_consent.jinja", context=render_context) ) add_resource(context, "body_end", html_resource) add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce-gdpr.js"))
def add_front_resources(context, content): view_class = getattr(context["view"], "__class__", None) if context.get("view") else None if not view_class: return view_name = getattr(view_class, "__name__", "") # For front if view_name in ["CheckoutMethodPhase", "CompanyRegistrationView", "CustomerEditView", "CompanyEditView"]: add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce-regions.js")) add_init_fields_resource(context, "#id_billing-country", "#id_billing-region_code", "#id_billing-region") add_init_fields_resource(context, "#id_shipping-country", "#id_shipping-region_code", "#id_shipping-region") elif view_name in ["AddressesPhase"]: # the address phase can be requested through ajax # when ajax request, we should append the scripts at the end of the content # and not at the body end, as there is no body element in the ajax response placement = "body_end" request = context.get("request") if request and request.is_ajax(): placement = "content_end" add_resource(context, placement, get_E-Commerce_static_url("E-Commerce-regions.js")) add_init_fields_resource( context, "#id_billing-country", "#id_billing-region_code", "#id_billing-region", placement ) add_init_fields_resource( context, "#id_shipping-country", "#id_shipping-region_code", "#id_shipping-region", placement ) # For admin views elif view_name in ["ContactEditView", "OrderAddressEditView"]: add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce-regions.js")) add_init_fields_resource( context, "#id_billing_address-country", "#id_billing_address-region_code", "#id_billing_address-region" ) add_init_fields_resource( context, "#id_shipping_address-country", "#id_shipping_address-region_code", "#id_shipping_address-region" ) # For admin order editor only regions is enough elif view_name == "OrderEditView": add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce-regions.js")) elif view_name in ["AddressBookEditView", "WizardView", "ShopEditView", "SupplierEditView"]: add_resource(context, "body_end", get_E-Commerce_static_url("E-Commerce-regions.js")) add_init_fields_resource(context, "#id_address-country", "#id_address-region_code", "#id_address-region")
def add_test_injection(context, content): add_resource(context, "body_end", InlineScriptResource("window.injectedFromAddon=true;"))