diff --git a/engine/core/admin.py b/engine/core/admin.py index 93be8b04..9fbf6ebc 100644 --- a/engine/core/admin.py +++ b/engine/core/admin.py @@ -69,6 +69,24 @@ from engine.core.models import ( ) +class StorefrontLinkMixin: + """Adds a 'See on site' link button to the change form submit row.""" + + change_form_template = "admin/core/change_form_with_storefront_link.html" + storefront_path_prefix: str = "" + + def changeform_view(self, request, object_id=None, form_url="", extra_context=None): + extra_context = extra_context or {} + if object_id: + obj = self.get_object(request, object_id) + if obj and hasattr(obj, "slug"): + extra_context["storefront_url"] = ( + f"https://{settings.STOREFRONT_DOMAIN}" + f"/{self.storefront_path_prefix}/{obj.slug}" + ) + return super().changeform_view(request, object_id, form_url, extra_context) + + class FieldsetsMixin: general_fields: list[str] | None = [] relation_fields: list[str] | None = [] @@ -361,12 +379,14 @@ class AttributeValueAdmin(FieldsetsMixin, ActivationActionsMixin, ModelAdmin): @register(Category) class CategoryAdmin( + StorefrontLinkMixin, DjangoQLSearchMixin, FieldsetsMixin, ActivationActionsMixin, DraggableMPTTAdmin, ModelAdmin, ): + storefront_path_prefix = "catalog" # noinspection PyClassVar model = Category formfield_overrides = {TextField: {"widget": MarkdownWidget}} @@ -417,8 +437,13 @@ class CategoryAdmin( @register(Brand) class BrandAdmin( - DjangoQLSearchMixin, FieldsetsMixin, ActivationActionsMixin, ModelAdmin + StorefrontLinkMixin, + DjangoQLSearchMixin, + FieldsetsMixin, + ActivationActionsMixin, + ModelAdmin, ): + storefront_path_prefix = "brand" # noinspection PyClassVar model = Brand formfield_overrides = {TextField: {"widget": MarkdownWidget}} @@ -449,12 +474,14 @@ class BrandAdmin( @register(Product) class ProductAdmin( + StorefrontLinkMixin, DjangoQLSearchMixin, FieldsetsMixin, ActivationActionsMixin, ModelAdmin, ImportExportModelAdmin, ): + storefront_path_prefix = "product" # noinspection PyClassVar model = Product formfield_overrides = {TextField: {"widget": MarkdownWidget}} diff --git a/engine/core/templates/admin/core/change_form_with_storefront_link.html b/engine/core/templates/admin/core/change_form_with_storefront_link.html new file mode 100644 index 00000000..c89434e6 --- /dev/null +++ b/engine/core/templates/admin/core/change_form_with_storefront_link.html @@ -0,0 +1,18 @@ +{% extends "admin/change_form.html" %} +{% load i18n admin_modify %} + +{% block submit_buttons_bottom %} + {% submit_row %} + + {% if original and storefront_url %} +
+ {% endif %} +{% endblock %} diff --git a/engine/core/templates/unfold/helpers/language_switch.html b/engine/core/templates/unfold/helpers/language_switch.html new file mode 100644 index 00000000..05f12b43 --- /dev/null +++ b/engine/core/templates/unfold/helpers/language_switch.html @@ -0,0 +1,21 @@ +{% load i18n %} + +{% get_current_language as LANGUAGE_CODE %} +{% get_available_languages as LANGUAGES %} +{% get_language_info_list for LANGUAGES as languages %} + +{% if show_languages %} +