Beispiel #1
0
        class LoggedService(Component):
            _inherit = "base.rest.service"
            _name = "test.log.service"
            _usage = "logmycalls"
            _collection = class_or_instance._collection_name
            _description = "Test log my calls"
            _log_calls_in_db = True

            @restapi.method(
                [(["/<int:id>/get", "/<int:id>"], "GET")],
                output_param=restapi.CerberusValidator("_get_out_schema"),
                auth="public",
            )
            def get(self, _id):
                """Get some information"""
                return {"name": "Mr Logger"}

            def _get_out_schema(self):
                return {"name": {"type": "string", "required": True}}

            @restapi.method([(["/fail/<string:how>"], "GET")], auth="public")
            def fail(self, how):
                """Test a failure"""
                exc = {
                    "value": ValueError,
                    "validation": exceptions.ValidationError,
                    "user": exceptions.UserError,
                }
                raise exc[how]("Failed as you wanted!")
Beispiel #2
0
        class LoggedService(Component):
            _inherit = "base.rest.service"
            _name = "test.log.service"
            _usage = "logmycalls"
            _collection = class_or_instance._collection_name
            _description = "Test log my calls"
            _log_calls_in_db = True

            @restapi.method(
                [(["/<int:id>/get", "/<int:id>"], "GET")],
                output_param=restapi.CerberusValidator("_get_out_schema"),
                auth="public",
            )
            def get(self, _id):
                """Get some information"""
                return {"name": "Mr Logger"}

            def _get_out_schema(self):
                return {"name": {"type": "string", "required": True}}
Beispiel #3
0
class ExportSettingsService(Component):
    """Shopinvader service to expose allowed settings"""

    _inherit = [
        "base.shopinvader.service",
    ]
    _name = "shopinvader.settings.service"
    _usage = "settings"
    _description = __doc__

    def _get_all_schema(self):
        return {
            "countries": {
                "type": "list",
                "required": True,
                "nullable": False,
                "schema": {
                    "type": "dict",
                    "schema": self._get_countries_schema(),
                },
            },
            "titles": {
                "type": "list",
                "required": True,
                "nullable": False,
                "schema": {
                    "type": "dict",
                    "schema": self._get_titles_schema(),
                },
            },
            "industries": {
                "type": "list",
                "required": True,
                "nullable": False,
                "schema": {
                    "type": "dict",
                    "schema": self._get_industries_schema(),
                },
            },
            "currencies": {
                "type": "list",
                "required": True,
                "nullable": False,
                "schema": {
                    "type": "dict",
                    "schema": self._get_currencies_schema(),
                },
            },
            "languages": {
                "type": "list",
                "required": True,
                "nullable": False,
                "schema": {
                    "type": "dict",
                    "schema": self._get_languages_schema(),
                },
            },
        }

    @restapi.method(
        [(["/", "/all"], "GET")],
        output_param=restapi.CerberusValidator("_get_all_schema"),
        auth="public_or_default",
    )
    def get_all(self):
        return self._get_all()

    def _get_all(self):
        return {
            "countries": self._get_countries(),
            "titles": self._get_titles(),
            "industries": self._get_industries(),
            "currencies": self._get_currencies(),
            "languages": self._get_languages(),
        }

    def _jsonify_fields_country(self):
        return [
            "name",
            "code",
            "id",
        ]

    def _get_countries(self):
        return self.shopinvader_backend.allowed_country_ids.jsonify(
            self._jsonify_fields_country()
        )

    def _get_countries_schema(self):
        return {
            "name": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "code": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "id": {
                "type": "integer",
                "required": True,
                "nullable": False,
            },
        }

    @restapi.method(
        [(["/countries"], "GET")],
        output_param=restapi.CerberusListValidator(
            "_get_countries_schema", unique_items=True
        ),
        auth="public_or_default",
    )
    def countries(self):
        return self._get_countries()

    def _jsonify_fields_title(self):
        return [
            "id",
            "name",
        ]

    def _get_titles(self):
        return self.shopinvader_backend.partner_title_ids.jsonify(
            self._jsonify_fields_title()
        )

    def _get_titles_schema(self):
        return {
            "name": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "id": {
                "type": "integer",
                "required": True,
                "nullable": False,
            },
        }

    @restapi.method(
        [(["/titles"], "GET")],
        output_param=restapi.CerberusListValidator(
            "_get_titles_schema", unique_items=True
        ),
        auth="public_or_default",
    )
    def titles(self):
        return self._get_titles()

    def _jsonify_fields_industry(self):
        return [
            "id",
            "name",
        ]

    def _get_industries(self):
        return self.shopinvader_backend.partner_industry_ids.jsonify(
            self._jsonify_fields_industry()
        )

    def _get_industries_schema(self):
        return {
            "name": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "id": {
                "type": "integer",
                "required": True,
                "nullable": False,
            },
        }

    @restapi.method(
        [(["/industries"], "GET")],
        output_param=restapi.CerberusListValidator(
            "_get_industries_schema", unique_items=True
        ),
        auth="public_or_default",
    )
    def industries(self):
        return self._get_industries()

    def _jsonify_fields_currency(self):
        return [
            "id",
            "name",
        ]

    def _get_currencies(self):
        return self.shopinvader_backend.currency_ids.jsonify(
            self._jsonify_fields_currency()
        )

    def _get_currencies_schema(self):
        return {
            "name": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "id": {
                "type": "integer",
                "required": True,
                "nullable": False,
            },
        }

    @restapi.method(
        [(["/currencies"], "GET")],
        output_param=restapi.CerberusListValidator(
            "_get_currencies_schema", unique_items=True
        ),
        auth="public_or_default",
    )
    def currencies(self):
        return self._get_currencies()

    def _jsonify_fields_lang(self):
        return [
            "id",
            "name",
            "iso_code",
        ]

    def _get_languages(self):
        return self.shopinvader_backend.lang_ids.jsonify(self._jsonify_fields_lang())

    def _get_languages_schema(self):
        return {
            "name": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
            "id": {
                "type": "integer",
                "required": True,
                "nullable": False,
            },
            "iso_code": {
                "type": "string",
                "required": True,
                "nullable": False,
            },
        }

    @restapi.method(
        [(["/languages"], "GET")],
        output_param=restapi.CerberusListValidator(
            "_get_languages_schema", unique_items=True
        ),
        auth="public_or_default",
    )
    def languages(self):
        return self._get_languages()
Beispiel #4
0
class CartService(Component):
    _inherit = "shopinvader.cart.service"

    def set_carrier(self, **params):
        """
            This service will set the given delivery method to the current
            cart
        :param params: The carrier_id to set
        :return:
        """
        cart = self._get()
        if not cart:
            raise UserError(_("There is not cart"))
        else:
            self._set_carrier(cart, params["carrier_id"])
            return self._to_json(cart)

    # DEPRECATED METHODS #
    @restapi.method(
        routes=[(["/get_delivery_methods"], "GET")],
        output_param=restapi.CerberusValidator(
            "_validator_get_delivery_methods",
        ),
    )
    @skip_secure_response
    def get_delivery_methods(self):
        """
            !!!!DEPRECATED!!!!! Uses delivery_carrier.search

            This service will return all possible delivery methods for the
            current cart

        :return:
        """
        _logger.warning(
            "DEPRECATED: You should use %s in service %s",
            "search",
            "delivery_carrier",
        )
        return self.component("delivery_carrier").search(target="current_cart")["rows"]

    def apply_delivery_method(self, **params):
        """
            !!!!DEPRECATED!!!!! Uses set_carrier

            This service will apply the given delivery method to the current
            cart
        :param params: Dict containing delivery method to apply
        :return:
        """
        return self.set_carrier(carrier_id=params["carrier"]["id"])

    # Validator
    def _validator_apply_delivery_method(self):
        return {
            "carrier": {
                "type": "dict",
                "schema": {
                    "id": {
                        "coerce": int,
                        "nullable": True,
                        "required": True,
                        "type": "integer",
                    }
                },
            }
        }

    def _validator_get_delivery_methods(self):
        return {
            "country_id": {
                "coerce": to_int,
                "required": False,
                "type": "integer",
            },
            "zip_code": {"required": False, "type": "string"},
        }

    def _validator_set_carrier(self):
        return {
            "carrier_id": {
                "coerce": int,
                "nullable": True,
                "required": True,
                "type": "integer",
            }
        }

    # internal methods

    def _add_item(self, cart, params):
        res = super()._add_item(cart, params)
        self._unset_carrier(cart)
        return res

    def _update_item(self, cart, params, item=False):
        res = super()._update_item(cart, params, item)
        self._unset_carrier(cart)
        return res

    def _delete_item(self, cart, params):
        res = super()._delete_item(cart, params)
        self._unset_carrier(cart)
        return res

    def _set_carrier(self, cart, carrier_id):
        if carrier_id not in cart.shopinvader_available_carrier_ids.ids:
            raise UserError(_("This delivery method is not available for you order"))
        carrier = self.env["delivery.carrier"].browse(carrier_id)
        cart.set_delivery_line(carrier, carrier.rate_shipment(cart).get("price", 0.0))

    def _unset_carrier(self, cart):
        cart._remove_delivery_line()

    def _get_lines_to_copy(self, cart):
        # Override. Don't copy delivery lines.
        res = super()._get_lines_to_copy(cart)
        return res.filtered(lambda l: not l.is_delivery)
class ExportSettingsService(Component):
    _inherit = [
        "shopinvader.settings.service",
    ]

    def _get_all_schema(self):
        schema = super()._get_all_schema()
        schema.update(**self._get_es_settings_schema())
        return schema

    def _get_all(self):
        res = super()._get_all()
        res.update(**self._get_es_settings())
        return res

    def _get_es_settings_schema(self):
        return {
            "elasticsearch": {
                "required": True,
                "nullable": True,
                "type": "dict",
                "schema": {
                    "host": {
                        "type": "string",
                        "required": True,
                        "nullable": False,
                    },
                    "indexes": {
                        "meta": {
                            "description":
                            "A key/value mapping where Key is the "
                            "model name used to fill the index "
                            "and value is the index name",
                            "example": {
                                "shopinvader.category":
                                "demo_elasticsearch_backend"
                                "_shopinvader_category_en_US",
                                "shopinvader.variant":
                                "demo_elasticsearch_backend"
                                "_shopinvader_variant_en_US",
                            },
                        },
                        "type": "dict",
                        "required": True,
                        "nullable": True,
                        "keysrules": {
                            "type": "string"
                        },
                        "valuesrules": {
                            "type": "string",
                            "required": True,
                            "nullable": False,
                        },
                    },
                },
            }
        }

    @restapi.method(
        [(["/elasticsearch"], "GET")],
        output_param=restapi.CerberusValidator("_get_es_settings_schema"),
        auth="public_or_default",
    )
    def get_es_settings(self):
        return self._get_es_settings()

    def _get_es_settings(self):
        se_backend = self.shopinvader_backend.se_backend_id
        settings = None
        if se_backend.search_engine_name == "elasticsearch":
            es_se_backend = se_backend.specific_backend
            indexes = {}
            for se_index in se_backend.index_ids.filtered(
                    lambda idx: idx.lang_id.code == idx.env.context.get("lang"
                                                                        )):
                indexes[se_index.model_id.model] = se_index.name
            settings = {
                "host": es_se_backend.es_server_host,
                "indexes": indexes if indexes else None,
            }
        return {"elasticsearch": settings}