From b624e333e31443091e89aa779596684a8dcdb50a Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 3 Nov 2021 21:52:11 +0700 Subject: [PATCH] [ADD] pricelist_cache_rest --- pricelist_cache_rest/README.rst | 84 ++++ pricelist_cache_rest/__init__.py | 3 + pricelist_cache_rest/__manifest__.py | 22 + pricelist_cache_rest/controllers/__init__.py | 1 + pricelist_cache_rest/controllers/main.py | 57 +++ pricelist_cache_rest/data/ir_exports_data.xml | 21 + .../demo/auth_api_key_demo.xml | 12 + pricelist_cache_rest/models/__init__.py | 1 + pricelist_cache_rest/models/res_company.py | 14 + pricelist_cache_rest/readme/CONTRIBUTORS.rst | 4 + pricelist_cache_rest/readme/CREDITS.rst | 4 + pricelist_cache_rest/readme/DESCRIPTION.rst | 1 + .../static/description/index.html | 437 ++++++++++++++++++ pricelist_cache_rest/tests/__init__.py | 1 + .../tests/test_pricelist_cache_rest.py | 98 ++++ pricelist_cache_rest/wizards/__init__.py | 1 + .../wizards/res_config_settings.py | 17 + .../wizards/res_config_settings.xml | 26 ++ .../odoo/addons/pricelist_cache_rest | 1 + setup/pricelist_cache_rest/setup.py | 6 + 20 files changed, 811 insertions(+) create mode 100644 pricelist_cache_rest/README.rst create mode 100644 pricelist_cache_rest/__init__.py create mode 100644 pricelist_cache_rest/__manifest__.py create mode 100644 pricelist_cache_rest/controllers/__init__.py create mode 100644 pricelist_cache_rest/controllers/main.py create mode 100644 pricelist_cache_rest/data/ir_exports_data.xml create mode 100644 pricelist_cache_rest/demo/auth_api_key_demo.xml create mode 100644 pricelist_cache_rest/models/__init__.py create mode 100644 pricelist_cache_rest/models/res_company.py create mode 100644 pricelist_cache_rest/readme/CONTRIBUTORS.rst create mode 100644 pricelist_cache_rest/readme/CREDITS.rst create mode 100644 pricelist_cache_rest/readme/DESCRIPTION.rst create mode 100644 pricelist_cache_rest/static/description/index.html create mode 100644 pricelist_cache_rest/tests/__init__.py create mode 100644 pricelist_cache_rest/tests/test_pricelist_cache_rest.py create mode 100644 pricelist_cache_rest/wizards/__init__.py create mode 100644 pricelist_cache_rest/wizards/res_config_settings.py create mode 100644 pricelist_cache_rest/wizards/res_config_settings.xml create mode 120000 setup/pricelist_cache_rest/odoo/addons/pricelist_cache_rest create mode 100644 setup/pricelist_cache_rest/setup.py diff --git a/pricelist_cache_rest/README.rst b/pricelist_cache_rest/README.rst new file mode 100644 index 00000000000..092f5345320 --- /dev/null +++ b/pricelist_cache_rest/README.rst @@ -0,0 +1,84 @@ +==================== +Pricelist Cache Rest +==================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/sale-workflow/tree/14.0/pricelist_cache_rest + :alt: OCA/sale-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-workflow-14-0/sale-workflow-14-0-pricelist_cache_rest + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/167/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Provides an endpoint to get product prices for a given customer + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp + +Contributors +~~~~~~~~~~~~ + +* Simone Orsi +* Matthieu Méquignon +* `Trobz `_: + * Khoi Vo + +Other credits +~~~~~~~~~~~~~ + +**Financial support** + +* Cosanum +* Camptocamp R&D + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/sale-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/pricelist_cache_rest/__init__.py b/pricelist_cache_rest/__init__.py new file mode 100644 index 00000000000..ada0b87be6b --- /dev/null +++ b/pricelist_cache_rest/__init__.py @@ -0,0 +1,3 @@ +from . import controllers +from . import models +from . import wizards diff --git a/pricelist_cache_rest/__manifest__.py b/pricelist_cache_rest/__manifest__.py new file mode 100644 index 00000000000..c0475dba3a4 --- /dev/null +++ b/pricelist_cache_rest/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Pricelist Cache Rest", + "summary": "Provides an endpoint to get product prices for a given customer", + "version": "14.0.1.0.0", + "category": "Hidden", + "author": "Camptocamp", + "license": "AGPL-3", + "depends": [ + "auth_api_key", + "base_jsonify", + "pricelist_cache", + ], + "website": "https://github.com/OCA/sale-workflow", + "installable": True, + "data": [ + "data/ir_exports_data.xml", + "wizards/res_config_settings.xml", + ], + "demo": ["demo/auth_api_key_demo.xml"], +} diff --git a/pricelist_cache_rest/controllers/__init__.py b/pricelist_cache_rest/controllers/__init__.py new file mode 100644 index 00000000000..12a7e529b67 --- /dev/null +++ b/pricelist_cache_rest/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/pricelist_cache_rest/controllers/main.py b/pricelist_cache_rest/controllers/main.py new file mode 100644 index 00000000000..4c75c8e2891 --- /dev/null +++ b/pricelist_cache_rest/controllers/main.py @@ -0,0 +1,57 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import json + +from odoo import http +from werkzeug.exceptions import Unauthorized + + +class PricelistController(http.Controller): + """Expose prices for pricelists. + """ + + @http.route( + "/pricelist/", + type="http", + auth="api_key", + methods=["GET"], + csrf=False, + ) + def partner_pricelist(self, partner): + """Retrive all prices for given partner. + + :return: TODO + """ + env = http.request.env + auth_api_key_id = getattr(http.request, "auth_api_key_id", None) + self._validate_api_key(env, auth_api_key_id) + + cache_items = self._get_cache_items(partner) + return self._make_json_response(self._cache_to_json(cache_items)) + + def _validate_api_key(self, env, api_key_id): + if api_key_id is None: + raise Unauthorized("API key missing") + allowed_keys = self._get_authorized_api_keys(env) + if api_key_id not in allowed_keys: + raise Unauthorized("API key not valid") + return True + + def _get_authorized_api_keys(self, env): + # TODO: what about multi company support? + return env.company.pricelist_cache_auhorize_apikey_ids.ids + + def _get_cache_items(self, partner): + return partner._pricelist_cache_get_prices() + + def _make_json_response(self, data): + headers = {} + headers["Content-Type"] = "application/json" + return http.request.make_response(json.dumps(data), headers=headers) + + def _cache_to_json(self, cache_items): + exporter = cache_items.env.ref( + "pricelist_cache_rest.ir_exp_cache_item" + ) + return cache_items.jsonify(exporter.get_json_parser()) diff --git a/pricelist_cache_rest/data/ir_exports_data.xml b/pricelist_cache_rest/data/ir_exports_data.xml new file mode 100644 index 00000000000..3d3092b52ee --- /dev/null +++ b/pricelist_cache_rest/data/ir_exports_data.xml @@ -0,0 +1,21 @@ + + + + + Pricelist Cache Parser + pricelist.cache + + + + product_id + product_id:id + _jsonify_m2o_to_id + + + + + price + + + + diff --git a/pricelist_cache_rest/demo/auth_api_key_demo.xml b/pricelist_cache_rest/demo/auth_api_key_demo.xml new file mode 100644 index 00000000000..af3d286c6d5 --- /dev/null +++ b/pricelist_cache_rest/demo/auth_api_key_demo.xml @@ -0,0 +1,12 @@ + + + Pricelist Cache demo key + + 8E57rAPnZY6iS20w4XUQ + + + Pricelist Cache demo key 2 + + 8E57rAPnZY6iS20w4XUQ-2 + + diff --git a/pricelist_cache_rest/models/__init__.py b/pricelist_cache_rest/models/__init__.py new file mode 100644 index 00000000000..aff44f335ae --- /dev/null +++ b/pricelist_cache_rest/models/__init__.py @@ -0,0 +1 @@ +from . import res_company diff --git a/pricelist_cache_rest/models/res_company.py b/pricelist_cache_rest/models/res_company.py new file mode 100644 index 00000000000..75cdef52c8b --- /dev/null +++ b/pricelist_cache_rest/models/res_company.py @@ -0,0 +1,14 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import fields, models + + +class ResCompany(models.Model): + + _inherit = "res.company" + + pricelist_cache_auhorize_apikey_ids = fields.Many2many( + "auth.api.key", + help="API keys that can retrieve pricelist data via REST endpoints.", + ) diff --git a/pricelist_cache_rest/readme/CONTRIBUTORS.rst b/pricelist_cache_rest/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..efddf5ef630 --- /dev/null +++ b/pricelist_cache_rest/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Simone Orsi +* Matthieu Méquignon +* `Trobz `_: + * Khoi Vo diff --git a/pricelist_cache_rest/readme/CREDITS.rst b/pricelist_cache_rest/readme/CREDITS.rst new file mode 100644 index 00000000000..4641e106617 --- /dev/null +++ b/pricelist_cache_rest/readme/CREDITS.rst @@ -0,0 +1,4 @@ +**Financial support** + +* Cosanum +* Camptocamp R&D diff --git a/pricelist_cache_rest/readme/DESCRIPTION.rst b/pricelist_cache_rest/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..5474b6eae3f --- /dev/null +++ b/pricelist_cache_rest/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Provides an endpoint to get product prices for a given customer diff --git a/pricelist_cache_rest/static/description/index.html b/pricelist_cache_rest/static/description/index.html new file mode 100644 index 00000000000..26d5aea0546 --- /dev/null +++ b/pricelist_cache_rest/static/description/index.html @@ -0,0 +1,437 @@ + + + + + + +Pricelist Cache Rest + + + +
+

Pricelist Cache Rest

+ + +

Beta License: AGPL-3 OCA/sale-workflow Translate me on Weblate Try me on Runbot

+

Provides an endpoint to get product prices for a given customer

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

Financial support

+
    +
  • Cosanum
  • +
  • Camptocamp R&D
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/sale-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/pricelist_cache_rest/tests/__init__.py b/pricelist_cache_rest/tests/__init__.py new file mode 100644 index 00000000000..3f350532f4e --- /dev/null +++ b/pricelist_cache_rest/tests/__init__.py @@ -0,0 +1 @@ +from . import test_pricelist_cache_rest diff --git a/pricelist_cache_rest/tests/test_pricelist_cache_rest.py b/pricelist_cache_rest/tests/test_pricelist_cache_rest.py new file mode 100644 index 00000000000..212d1d239d7 --- /dev/null +++ b/pricelist_cache_rest/tests/test_pricelist_cache_rest.py @@ -0,0 +1,98 @@ +# Copyright 2021 Camptocamp SA (http://www.camptocamp.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import contextlib +import json + +from odoo.addons.pricelist_cache.tests.common import TestPricelistCacheCommon +from odoo.addons.website.tools import MockRequest +from werkzeug.exceptions import Unauthorized + +from ..controllers.main import PricelistController + +LIST_PRICES_MAPPING = { + "pricelist_cache.list0": [ + {"id": 17, "price": 100.0}, + {"id": 18, "price": 79.0}, + {"id": 19, "price": 100.0}, + {"id": 20, "price": 47.0}, + ], + "pricelist_cache.list1": [ + {"id": 17, "price": 75.0}, + {"id": 18, "price": 79.0}, + {"id": 19, "price": 100.0}, + {"id": 20, "price": 47.0}, + ], + "pricelist_cache.list2": [ + {"id": 17, "price": 75.0}, + {"id": 18, "price": 79.0}, + {"id": 19, "price": 100.0}, + {"id": 20, "price": 47.0}, + ], + "pricelist_cache.list3": [ + {"id": 17, "price": 25.0}, + {"id": 18, "price": 79.0}, + {"id": 19, "price": 100.0}, + {"id": 20, "price": 47.0}, + ], + "pricelist_cache.list4": [ + {"id": 17, "price": 15.0}, + {"id": 18, "price": 50.0}, + {"id": 19, "price": 100.0}, + {"id": 20, "price": 47.0}, + ], + "pricelist_cache.list5": [ + {"id": 17, "price": 45.0}, + {"id": 18, "price": 99.0}, + {"id": 19, "price": 120.0}, + {"id": 20, "price": 67.0}, + ], +} + + +class TestPricelistCache(TestPricelistCacheCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.api_key = cls.env.ref("pricelist_cache_rest.api_key_demo") + cls.api_key2 = cls.env.ref("pricelist_cache_rest.api_key_demo_2") + cls.env.company.pricelist_cache_auhorize_apikey_ids += cls.api_key + cls.ctrl = PricelistController() + cls.partner = cls.env.ref("base.res_partner_12") + cls.partner.property_product_pricelist = cls.list0 + + @contextlib.contextmanager + def _get_mocked_request(self, httprequest=None, extra_headers=None): + with MockRequest(self.env) as mocked_request: + mocked_request.httprequest = ( + httprequest or mocked_request.httprequest + ) + headers = {} + headers.update(extra_headers or {}) + mocked_request.httprequest.headers = headers + mocked_request.auth_api_key_id = self.api_key.id + mocked_request.make_response = lambda data, **kw: data + yield mocked_request + + def test_api_key_validation(self): + with self._get_mocked_request() as req: + req.auth_api_key_id = None + with self.assertRaisesRegex(Unauthorized, "API key missing"): + self.ctrl.partner_pricelist(self.partner) + with self._get_mocked_request() as req: + req.auth_api_key_id = self.api_key2.id + with self.assertRaisesRegex(Unauthorized, "API key not valid"): + self.ctrl.partner_pricelist(self.partner) + + def _resp_data(self, resp): + return json.loads(resp.data.decode()) + + def test_get_prices(self): + partner = self.partner + for pricelist_xmlid, expected_result in LIST_PRICES_MAPPING.items(): + partner.property_product_pricelist = self.env.ref(pricelist_xmlid) + result = [] + with self._get_mocked_request(): + resp = self.ctrl.partner_pricelist(partner) + data = self._resp_data(resp) + [result.append(c) for c in data if c['id'] in self.products.ids] + self.assertEqual(result, expected_result) diff --git a/pricelist_cache_rest/wizards/__init__.py b/pricelist_cache_rest/wizards/__init__.py new file mode 100644 index 00000000000..0deb68c4680 --- /dev/null +++ b/pricelist_cache_rest/wizards/__init__.py @@ -0,0 +1 @@ +from . import res_config_settings diff --git a/pricelist_cache_rest/wizards/res_config_settings.py b/pricelist_cache_rest/wizards/res_config_settings.py new file mode 100644 index 00000000000..4056f5856dc --- /dev/null +++ b/pricelist_cache_rest/wizards/res_config_settings.py @@ -0,0 +1,17 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import fields, models + +# TODO: this should be moved to `../models` + + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + pricelist_cache_auhorize_apikey_ids = fields.Many2many( + "auth.api.key", + related="company_id.pricelist_cache_auhorize_apikey_ids", + readonly=False, + ) diff --git a/pricelist_cache_rest/wizards/res_config_settings.xml b/pricelist_cache_rest/wizards/res_config_settings.xml new file mode 100644 index 00000000000..8494aea1d48 --- /dev/null +++ b/pricelist_cache_rest/wizards/res_config_settings.xml @@ -0,0 +1,26 @@ + + + + + + res.config.settings.form.inherit + res.config.settings + + + +
+
+
+ Authored Pricelist Cache API keys +
Defines the list of api keys authorized to retrieve prices from the api
+
+ +
+
+
+ + + + + diff --git a/setup/pricelist_cache_rest/odoo/addons/pricelist_cache_rest b/setup/pricelist_cache_rest/odoo/addons/pricelist_cache_rest new file mode 120000 index 00000000000..6130a2f1800 --- /dev/null +++ b/setup/pricelist_cache_rest/odoo/addons/pricelist_cache_rest @@ -0,0 +1 @@ +../../../../pricelist_cache_rest \ No newline at end of file diff --git a/setup/pricelist_cache_rest/setup.py b/setup/pricelist_cache_rest/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/pricelist_cache_rest/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)