Beispiel #1
0
class ProductCrossSellsPlugin(TemplatedPlugin):
    identifier = "classic_gray.product_cross_sells"
    name = _("Product Cross Sells")
    template_name = "classic_gray/cross_sells_plugin.jinja"
    required_context_variables = ["product"]
    fields = [
        ("title",
         TranslatableField(label=_("Title"), required=False, initial="")),
        ("type", ProductCrossSell.type.field.formfield()),
        ("count", forms.IntegerField(label=_("Count"), min_value=1,
                                     initial=4)),
        ("orderable_only",
         forms.BooleanField(label=_("Only show in-stock and orderable items"),
                            initial=True,
                            required=False))
    ]

    def get_context_data(self, context):
        count = self.config.get("count", 4)
        product = context.get("product", None)
        orderable_only = self.config.get("orderable_only", True)
        type = self.config.get("type")
        if not isinstance(type, ProductCrossSellType):
            type = ProductCrossSellType.RELATED
        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "product": product,
            "type": type,
            "count": count,
            "orderable_only": orderable_only,
        }
Beispiel #2
0
class ProductHighlightPlugin(TemplatedPlugin):
    identifier = "classic_gray.product_highlight"
    name = _("Product Highlights")
    template_name = "classic_gray/highlight_plugin.jinja"
    fields = [("title",
               TranslatableField(label=_("Title"), required=False,
                                 initial="")),
              ("type",
               forms.ChoiceField(label=_("Type"),
                                 choices=[
                                     ("newest", "Newest"),
                                     ("best_selling", "Best Selling"),
                                     ("random", "Random"),
                                 ],
                                 initial="newest")),
              ("count",
               forms.IntegerField(label=_("Count"), min_value=1, initial=4))]

    def get_context_data(self, context):
        type = self.config.get("type", "newest")
        count = self.config.get("count", 4)
        if type == "newest":
            products = get_newest_products(context, count)
        elif type == "best_selling":
            products = get_best_selling_products(context, count)
        elif type == "random":
            products = get_random_products(context, count)
        else:
            products = []

        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
Beispiel #3
0
class ProductCrossSellsPlugin(TemplatedPlugin):
    identifier = "classic_gray.product_cross_sells"
    name = _("Product Cross Sells")
    template_name = "classic_gray/cross_sells_plugin.jinja"
    required_context_variables = ["product"]
    fields = [
        ("title",
         TranslatableField(label=_("Title"), required=False, initial="")),
        ("type", ProductCrossSell.type.field.formfield()),
        ("count", forms.IntegerField(label=_("Count"), min_value=1,
                                     initial=4)),
        ("orderable_only",
         forms.BooleanField(label=_("Only show in-stock and orderable items"),
                            initial=True,
                            required=False))
    ]

    def __init__(self, config):
        relation_type = config.get("type", None)
        if relation_type:
            # Map initial config string to enum type
            try:
                type = map_relation_type(relation_type)
            except AttributeError:
                type = ProductCrossSellType.RELATED
            config["type"] = type
        super(ProductCrossSellsPlugin, self).__init__(config)

    def get_context_data(self, context):
        count = self.config.get("count", 4)
        product = context.get("product", None)
        orderable_only = self.config.get("orderable_only", True)
        relation_type = self.config.get("type")
        try:
            type = map_relation_type(relation_type)
        except AttributeError:
            type = ProductCrossSellType.RELATED
        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "product": product,
            "type": type,
            "count": count,
            "orderable_only": orderable_only,
        }
Beispiel #4
0
class ImagePlugin(TemplatedPlugin):
    """
    A linkable image plugin
    """
    identifier = "images"
    name = _("Image")
    template_name = "shoop/xtheme/plugins/image.jinja"
    fields = [
        ("title", TranslatableField(label=_("Title"), required=False)),
        ("image_id", ImageIDField(label=_("Image"), required=False)),
        ("url", forms.URLField(label=_("Image Link URL"), required=False)),
        ("full_width", forms.BooleanField(
            label=_("Full width"),
            required=False,
            initial=True,
            help_text=_("Set image to full width of cell")
        )),
        ("width", forms.IntegerField(
            label=_("Width (px)"),
            required=False,
            help_text=_("Leave blank for default width"),
        )),
        ("height", forms.IntegerField(
            label=_("Height (px)"),
            required=False,
            help_text=_("Leave blank for default width"),
        )),
    ]

    def get_context_data(self, context):
        """
        A custom get_context_data that returns the matching filer File
        """
        image = None
        image_id = self.config.get("image_id", None)
        if image_id:
            image = File.objects.filter(pk=image_id).first()
        return {
            "title": self.get_translated_value("title", ""),
            "image": image,
            "url": self.config.get("url", None),
            "full_width": self.config.get("full_width", None),
            "width": self.config.get("width", None),
            "height": self.config.get("height", None),
        }
Beispiel #5
0
class TextPlugin(Plugin):
    """
    Very basic Markdown rendering plugin.
    """
    identifier = "text"
    name = "Text"
    fields = [("text",
               TranslatableField(label=_("text"),
                                 required=False,
                                 widget=forms.Textarea))]

    def render(self, context):  # doccov: ignore
        text = self.get_translated_value("text")
        try:
            markup = markdown.markdown(text)
        except:  # Markdown parsing error? Well, just escape then...
            markup = escape(text)
        return mark_safe(markup)
Beispiel #6
0
class PageLinksPlugin(TemplatedPlugin):
    """
    A plugin for displaying links to visible CMS pages in the shop front
    """
    identifier = "simple_cms.page_links"
    name = _("CMS Page Links")
    template_name = "shoop/simple_cms/plugins/page_links.jinja"
    editor_form_class = PageLinksConfigForm
    fields = [
        ("title", TranslatableField(label=_("Title"), required=False, initial="")),
        ("show_all_pages", forms.BooleanField(
            label=_("Show all pages"),
            required=False,
            initial=True,
            help_text=_("All pages are shown, even if not selected"),
        )),
        ("hide_expired", forms.BooleanField(
            label=_("Hide expired pages"),
            initial=False,
            required=False,
        )),
        "pages",
    ]

    def get_context_data(self, context):
        """
        A custom get_context_data method to return pages, possibly filtering expired pages
        based on the plugin's ``hide_expired`` setting
        """
        selected_pages = self.config.get("pages", [])
        show_all_pages = self.config.get("show_all_pages", True)
        hide_expired = self.config.get("hide_expired", False)

        pages_qs = Page.objects.filter(visible_in_menu=True)
        if hide_expired:
            pages_qs = pages_qs.visible()

        if not show_all_pages:
            pages_qs = pages_qs.filter(id__in=selected_pages)

        return {
            "title": self.get_translated_value("title"),
            "pages": pages_qs,
        }
Beispiel #7
0
class CategoryLinksPlugin(TemplatedPlugin):
    """
    A plugin for displaying links to visible categories on the shop front
    """
    identifier = "category_links"
    name = _("Category Links")
    template_name = "shoop/xtheme/plugins/category_links.jinja"
    editor_form_class = CategoryLinksConfigForm
    fields = [
        ("title",
         TranslatableField(label=_("Title"), required=False, initial="")),
        ("show_all_categories",
         forms.BooleanField(
             label=_("Show all categories"),
             required=False,
             initial=True,
             help_text=_("All categories are shown, even if not selected"),
         )),
        "categories",
    ]

    def get_context_data(self, context):
        """
        A custom get_context_data method to return only visible categories
        for request customer.
        """
        selected_categories = self.config.get("categories", [])
        show_all_categories = self.config.get("show_all_categories", True)
        request = context.get("request")
        categories = Category.objects.all_visible(
            customer=getattr(request, "customer"),
            shop=getattr(request, "shop"))
        if not show_all_categories:
            categories = categories.filter(id__in=selected_categories)
        return {
            "title": self.get_translated_value("title"),
            "categories": categories,
        }
Beispiel #8
0
class Form(forms.Form):
    field = TranslatableField(languages=("en", "fi"))
Beispiel #9
0
class SocialMediaLinksPlugin(TemplatedPlugin):
    """
    An xtheme plugin for displaying site links to common social media sites.
    """
    identifier = "social_media_links"
    name = _("Social Media Links")
    template_name = "shoop/xtheme/plugins/social_media_links.jinja"
    editor_form_class = SocialMediaLinksPluginForm
    fields = [
        ("topic", TranslatableField(label=_("Topic"), required=False, initial="")),
        ("text", TranslatableField(label=_("Title"), required=False, initial="")),
        ("icon_size", forms.ChoiceField(label=_("Icon Size"), required=False, choices=[
            ("", "Default"),
            ("lg", "Large"),
            ("2x", "2x"),
            ("3x", "3x"),
            ("4x", "4x"),
            ("5x", "5x"),
        ], initial="")),
        ("alignment", forms.ChoiceField(label=_("Alignment"), required=False, choices=[
            ("", "Default"),
            ("left", "Left"),
            ("center", "Center"),
            ("right", "Right"),
        ], initial="")),
    ]

    icon_classes = {
        "Facebook": "facebook-square",
        "Flickr": "flickr",
        "Google Plus": "google-plus-square",
        "Instagram": "instagram",
        "Linkedin": "linkedin-square",
        "Pinterest": "pinterest",
        "Tumbler": "tumblr",
        "Twitter": "twitter",
        "Vimeo": "vimeo",
        "Vine": "vine",
        "Yelp": "yelp",
        "Youtube": "youtube",
    }

    def get_context_data(self, context):
        """
        Returns plugin settings and a sorted list of social media links

        :return: Plugin context data
        :rtype: dict
        """
        links = self.get_links()

        return {
            "links": links,
            "request": context["request"],
            "topic": self.get_translated_value("topic"),
            "text": self.get_translated_value("text"),
            "icon_size": self.config.get("icon_size", ""),
            "alignment": self.config.get("alignment", ""),
        }

    def get_links(self):
        """
        Returns the list of social media links sorted according to ordering

        :return: List of link tuples (ordering, icon class, url)
        :rtype: [(int, str, str)]
        """
        links = self.config.get("links", {})

        return sorted([
            (v["ordering"] or 0, self.icon_classes[k], v["url"])
            for (k, v)
            in links.items()
        ])
Beispiel #10
0
def test_translatable_field_attrs():
    """
    Make sure attributes are passed to widgets
    """
    field = TranslatableField(attrs={"class": "passable"})
    assert field.widget.attrs.get("class") == "passable"