Esempio n. 1
0
def test_provides():
    IDENTIFIED_OBJECT_SPEC = "%s:IdentifiedObject" % __name__
    category = str(uuid.uuid4())
    with override_provides(
            category,
        [
            IDENTIFIED_OBJECT_SPEC,
            "%s:UnidentifiedObject" % __name__,
            "%s:VeryUnidentifiedObject" % __name__,
        ],
    ):
        objects = get_provide_objects(category)
        assert set(objects) == set(
            (IdentifiedObject, UnidentifiedObject, VeryUnidentifiedObject))
        assert get_identifier_to_object_map(
            category)["identifier"] == IdentifiedObject
        assert get_identifier_to_spec_map(
            category)["identifier"] == IDENTIFIED_OBJECT_SPEC
        assert get_provide_specs_and_objects(
            category)[IDENTIFIED_OBJECT_SPEC] == IdentifiedObject

    # Test the context manager clears things correctly
    assert empty_iterable(get_provide_objects(category))
    assert empty_iterable(get_provide_specs_and_objects(category))
    assert empty_iterable(get_identifier_to_object_map(category))
    assert empty_iterable(get_identifier_to_spec_map(category))
Esempio n. 2
0
 def _get_script_template_class(self):
     """
     Get the script template class from script instance
     """
     return get_identifier_to_object_map(
         SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(self.instance.template,
                                                None)
Esempio n. 3
0
    def get_context_data(self, **kwargs):
        context = super(ScriptEditView, self).get_context_data(**kwargs)
        if self.object.pk:
            buttons = []

            edit_button_title = _("Edit Script Contents...")

            # this script was created through a template
            # so show an option to easily edit the template
            if self.object.template:
                template_cls = get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(self.object.template)

                # check whether is possible to edit the script through the template editor
                if template_cls and template_cls(self.object).can_edit_script():
                    # change the editor button title to advanced mode
                    edit_button_title = _("Edit Script (advanced)")

                    buttons.append(
                        URLActionButton(
                            text=_("Edit Template"),
                            icon="fa fa-pencil-square-o",
                            extra_css_class="btn-info",
                            url=reverse("shuup_admin:notify.script-template-edit", kwargs={"pk": self.object.pk})
                        )
                    )

            buttons.insert(0, URLActionButton(
                text=edit_button_title,
                icon="fa fa-pencil",
                extra_css_class="btn-info",
                url=reverse("shuup_admin:notify.script.edit-content", kwargs={"pk": self.object.pk})
            ))
            context["toolbar"] = Toolbar(buttons)
        return context
Esempio n. 4
0
 def _get_script_template_class(self):
     """
     Get the script template class from the request kwargs
     """
     identifier = self.kwargs.get("id", None)
     return get_identifier_to_object_map(
         SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(identifier, None)
Esempio n. 5
0
def _get_current_theme(shop):
    theme = None
    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shuup.xtheme.models import ThemeSettings
        theme_settings = ThemeSettings.objects.filter(active=True, shop=shop).first()

        # no active found, take the first and activate
        if not theme_settings:
            theme_settings = ThemeSettings.objects.filter(shop=shop).first()

            if theme_settings:
                theme_settings.activate()
                theme_settings.refresh_from_db()

    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        theme_settings = None

    if theme_settings:
        theme_cls = get_identifier_to_object_map("xtheme").get(theme_settings.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(theme_settings=theme_settings)
        else:
            log.warn("The active theme %r is not currently installed", theme_settings.theme_identifier)

    return theme
Esempio n. 6
0
    def post(self, request):
        """
        Create the Script from template directly if the template does not have a bound form.

        If the script template has a bound form, redirect to the template configuration view.

        If no script template is found, redirect to the script list.
        """
        identifier = request.POST.get("id", None)
        script_template_class = get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(identifier)

        if script_template_class:
            script_template = script_template_class()

            # the template has a form for configuration.. lets redirect to the correct view
            if script_template.get_form():
                return redirect("shuup_admin:notify.script-template-config", id=identifier)
            else:
                script = script_template.create_script()

                if script:
                    script.template = identifier
                    script.save(update_fields=["template"])
                    messages.success(request, _("Script created from template!"))

                return redirect("shuup_admin:notify.script.list")
        else:
            messages.error(request, _("Template Script not found!"))
            return redirect("shuup_admin:notify.script.list")
Esempio n. 7
0
def _get_current_theme(shop):
    theme = None
    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shuup.xtheme.models import ThemeSettings
        theme_settings = ThemeSettings.objects.filter(active=True,
                                                      shop=shop).first()

        # no active found, take the first and activate
        if not theme_settings:
            theme_settings = ThemeSettings.objects.filter(shop=shop).first()

            if theme_settings:
                theme_settings.activate()
                theme_settings.refresh_from_db()

    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        theme_settings = None

    if theme_settings:
        theme_cls = get_identifier_to_object_map("xtheme").get(
            theme_settings.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(theme_settings=theme_settings)
        else:
            log.warn("The active theme %r is not currently installed",
                     theme_settings.theme_identifier)

    return theme
Esempio n. 8
0
    def post(self, request):
        """
        Create the Script from template directly if the template does not have a bound form.

        If the script template has a bound form, redirect to the template configuration view.

        If no script template is found, redirect to the script list.
        """
        identifier = request.POST.get("id", None)
        script_template_class = get_identifier_to_object_map(
            SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(identifier)

        if script_template_class:
            script_template = script_template_class()

            # the template has a form for configuration.. lets redirect to the correct view
            if script_template.get_form():
                return redirect("shuup_admin:notify.script-template-config",
                                id=identifier)
            else:
                shop = get_shop(request)
                script = script_template.create_script(shop)

                if script:
                    script.template = identifier
                    script.save(update_fields=["template"])
                    messages.success(request,
                                     _("Script created from template!"))

                return redirect("shuup_admin:notify.script.list")
        else:
            messages.error(request, _("Template Script not found!"))
            return redirect("shuup_admin:notify.script.list")
Esempio n. 9
0
 def get_context_data(self, **kwargs):
     """
     Put all the script templates on disposal of the user.
     """
     context = super(ScriptTemplateView, self).get_context_data(**kwargs)
     context["script_templates"] = six.iteritems(get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY))
     context["edit_mode"] = EditScriptMode.CREATE
     return context
Esempio n. 10
0
 def get_context_data(self, **kwargs):
     """
     Put all the script templates on disposal of the user.
     """
     context = super(ScriptTemplateView, self).get_context_data(**kwargs)
     context["script_templates"] = six.iteritems(get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY))
     context["edit_mode"] = EditScriptMode.CREATE
     return context
Esempio n. 11
0
    def get_context_data(self, **kwargs):
        context = super(ScriptEditView, self).get_context_data(**kwargs)
        if self.object.pk:
            buttons = []

            edit_button_title = _("Edit Script Contents")

            # this script was created through a template
            # so show an option to easily edit the template
            if self.object.template:
                template_cls = get_identifier_to_object_map(
                    SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(
                        self.object.template)

                # check whether is possible to edit the script through the template editor
                if template_cls and template_cls(
                        self.object).can_edit_script():
                    # change the editor button title to advanced mode
                    edit_button_title = _("Edit Script (advanced)")

                    buttons.append(
                        URLActionButton(
                            text=_("Edit Template"),
                            icon="fa fa-pencil-square-o",
                            extra_css_class="btn-primary",
                            url=reverse(
                                "shuup_admin:notify.script-template-edit",
                                kwargs={"pk": self.object.pk}),
                        ))

            buttons.insert(
                0,
                URLActionButton(
                    text=edit_button_title,
                    icon="fa fa-pencil",
                    extra_css_class="btn-primary",
                    url=reverse("shuup_admin:notify.script.edit-content",
                                kwargs={"pk": self.object.pk}),
                ),
            )

            buttons.insert(
                1,
                PostActionButton(
                    post_url=reverse("shuup_admin:notify.script.delete",
                                     kwargs={"pk": self.object.pk}),
                    text=_("Delete"),
                    icon="fa fa-trash",
                    extra_css_class="btn-danger",
                    confirm=_(
                        'Are you sure you wish to delete "%s" notification?') %
                    self.object,
                    required_permissions=("notify.script.delete", ),
                ),
            )

            context["toolbar"] = Toolbar(buttons, view=self)
        return context
Esempio n. 12
0
 def get_ui_info_map(cls):
     map = {}
     for identifier, object in six.iteritems(get_identifier_to_object_map(cls.provide_category)):
         map[identifier] = {
             "identifier": str(identifier),
             "name": force_text(object.name),
             "description": force_text(getattr(object, "description", None) or ""),
         }
     return map
Esempio n. 13
0
 def get_ui_info_map(cls):
     map = {}
     for identifier, object in six.iteritems(get_identifier_to_object_map(cls.provide_category)):
         map[identifier] = {
             "identifier": str(identifier),
             "name": force_text(object.name),
             "description": force_text(getattr(object, "description", None) or ""),
         }
     return map
Esempio n. 14
0
def test_provides():
    IDENTIFIED_OBJECT_SPEC = "%s:IdentifiedObject" % __name__
    category = str(uuid.uuid4())
    with override_provides(category, [
        IDENTIFIED_OBJECT_SPEC,
        "%s:UnidentifiedObject" % __name__,
        "%s:VeryUnidentifiedObject" % __name__,
    ]):
        objects = get_provide_objects(category)
        assert set(objects) == set((IdentifiedObject, UnidentifiedObject, VeryUnidentifiedObject))
        assert get_identifier_to_object_map(category)["identifier"] == IdentifiedObject
        assert get_identifier_to_spec_map(category)["identifier"] == IDENTIFIED_OBJECT_SPEC
        assert get_provide_specs_and_objects(category)[IDENTIFIED_OBJECT_SPEC] == IdentifiedObject

    # Test the context manager clears things correctly
    assert empty_iterable(get_provide_objects(category))
    assert empty_iterable(get_provide_specs_and_objects(category))
    assert empty_iterable(get_identifier_to_object_map(category))
    assert empty_iterable(get_identifier_to_spec_map(category))
Esempio n. 15
0
    def load(cls, identifier):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        return get_identifier_to_object_map("xtheme_plugin").get(identifier)
Esempio n. 16
0
    def load(cls, identifier):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        return get_identifier_to_object_map("xtheme_plugin").get(identifier)
Esempio n. 17
0
    def load(cls, identifier, theme=None):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        loaded_plugin = get_identifier_to_object_map("xtheme_plugin").get(identifier)
        if not loaded_plugin and theme is not None:
            for plugin_spec in theme.plugins:
                plugin = load(plugin_spec)
                if plugin.identifier == identifier:
                    return plugin
        return loaded_plugin
Esempio n. 18
0
    def load(cls, identifier, theme=None):
        """
        Get a plugin class based on the identifier from the `xtheme_plugin` provides registry.

        :param identifier: Plugin class identifier
        :type identifier: str
        :return: A plugin class, or None
        :rtype: class[Plugin]|None
        """
        loaded_plugin = get_identifier_to_object_map("xtheme_plugin").get(identifier)
        if not loaded_plugin and theme is not None:
            for plugin_spec in theme.plugins:
                plugin = load(plugin_spec)
                if plugin.identifier == identifier:
                    return plugin
        return loaded_plugin
Esempio n. 19
0
def get_current_theme(request=None):
    """
    Get the currently active theme object.

    :param request: Request, if available
    :type request: HttpRequest|None
    :return: Theme object or None
    :rtype: Theme
    """
    if _current_theme_class is not _not_set:
        if _current_theme_class:
            return _current_theme_class()
        return None  # No theme (usually for testing)

    if request and hasattr(request, "_current_xtheme"):
        return request._current_xtheme
    theme = None

    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shuup.xtheme.models import ThemeSettings
        ts = ThemeSettings.objects.filter(active=True).first()
    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        ts = None

    if ts:
        theme_cls = get_identifier_to_object_map("xtheme").get(
            ts.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(settings_obj=ts)
        else:
            log.warn("The active theme %r is not currently installed",
                     ts.theme_identifier)

    if request:
        request._current_xtheme = theme
    return theme
Esempio n. 20
0
def get_current_theme(request=None):
    """
    Get the currently active theme object.

    :param request: Request, if available
    :type request: HttpRequest|None
    :return: Theme object or None
    :rtype: Theme
    """
    if _current_theme_class is not _not_set:
        if _current_theme_class:
            return _current_theme_class()
        return None  # No theme (usually for testing)

    if request and hasattr(request, "_current_xtheme"):
        return request._current_xtheme
    theme = None

    try:
        # Ensure this module can be imported from anywhere by lazily importing the model
        from shuup.xtheme.models import ThemeSettings
        ts = ThemeSettings.objects.filter(active=True).first()
    except Exception as exc:
        # This is unfortunate and weird, but I don't want other tests to depend
        # on Xtheme's state or require the `djangodb` mark for every test.
        # So we silence exceptions with pytest-django's "Database access not allowed"
        # message here and let everything else pass.
        if "Database access not allowed" not in str(exc):
            raise
        ts = None

    if ts:
        theme_cls = get_identifier_to_object_map("xtheme").get(ts.theme_identifier)
        if theme_cls is not None:
            theme = theme_cls(settings_obj=ts)
        else:
            log.warn("The active theme %r is not currently installed", ts.theme_identifier)

    if request:
        request._current_xtheme = theme
    return theme
Esempio n. 21
0
    def get_context_data(self, **kwargs):
        context = super(ScriptEditView, self).get_context_data(**kwargs)
        if self.object.pk:
            buttons = []

            edit_button_title = _("Edit Script Contents...")

            # this script was created through a template
            # so show an option to easily edit the template
            if self.object.template:
                template_cls = get_identifier_to_object_map(
                    SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(
                        self.object.template)

                # check whether is possible to edit the script through the template editor
                if template_cls and template_cls(
                        self.object).can_edit_script():
                    # change the editor button title to advanced mode
                    edit_button_title = _("Edit Script (advanced)")

                    buttons.append(
                        URLActionButton(
                            text=_("Edit Template"),
                            icon="fa fa-pencil-square-o",
                            extra_css_class="btn-info",
                            url=reverse(
                                "shuup_admin:notify.script-template-edit",
                                kwargs={"pk": self.object.pk})))

            buttons.insert(
                0,
                URLActionButton(text=edit_button_title,
                                icon="fa fa-pencil",
                                extra_css_class="btn-info",
                                url=reverse(
                                    "shuup_admin:notify.script.edit-content",
                                    kwargs={"pk": self.object.pk})))
            context["toolbar"] = Toolbar(buttons)
        return context
Esempio n. 22
0
 def _get_script_template_class(self):
     """
     Get the script template class from script instance
     """
     return get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(self.instance.template, None)
Esempio n. 23
0
 def class_for_identifier(cls, identifier):
     return get_identifier_to_object_map(
         cls.provide_category).get(identifier)
Esempio n. 24
0
 def _get_script_template_class(self):
     """
     Get the script template class from the request kwargs
     """
     identifier = self.kwargs.get("id", None)
     return get_identifier_to_object_map(SCRIPT_TEMPLATES_PROVIDE_CATEGORY).get(identifier, None)
Esempio n. 25
0
 def class_for_identifier(cls, identifier):
     return get_identifier_to_object_map(cls.provide_category).get(identifier)