From 908fc8116ec7a874e733547276990206668ebc7a Mon Sep 17 00:00:00 2001 From: Cristiano Rodrigues Date: Thu, 10 Aug 2023 11:00:38 -0300 Subject: [PATCH 1/5] [ADD] shopinvader_base_address_city: module for localization of cities by state --- shopinvader_base_address_city/README.rst | 72 +++ shopinvader_base_address_city/__init__.py | 4 + shopinvader_base_address_city/__manifest__.py | 18 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/CREDITS.rst | 1 + .../readme/DESCRIPTION.rst | 1 + .../readme/USAGE.rst | 1 + .../services/__init__.py | 6 + .../services/res_city_abstract.py | 64 +++ .../services/res_city_component.py | 14 + .../static/description/index.html | 433 ++++++++++++++++++ .../tests/__init__.py | 5 + .../tests/test_city_abstract.py | 30 ++ 13 files changed, 650 insertions(+) create mode 100644 shopinvader_base_address_city/README.rst create mode 100644 shopinvader_base_address_city/__init__.py create mode 100644 shopinvader_base_address_city/__manifest__.py create mode 100644 shopinvader_base_address_city/readme/CONTRIBUTORS.rst create mode 100644 shopinvader_base_address_city/readme/CREDITS.rst create mode 100644 shopinvader_base_address_city/readme/DESCRIPTION.rst create mode 100644 shopinvader_base_address_city/readme/USAGE.rst create mode 100644 shopinvader_base_address_city/services/__init__.py create mode 100644 shopinvader_base_address_city/services/res_city_abstract.py create mode 100644 shopinvader_base_address_city/services/res_city_component.py create mode 100644 shopinvader_base_address_city/static/description/index.html create mode 100644 shopinvader_base_address_city/tests/__init__.py create mode 100644 shopinvader_base_address_city/tests/test_city_abstract.py diff --git a/shopinvader_base_address_city/README.rst b/shopinvader_base_address_city/README.rst new file mode 100644 index 0000000000..3023300fd9 --- /dev/null +++ b/shopinvader_base_address_city/README.rst @@ -0,0 +1,72 @@ +============================= +Shopinvader Base Address City +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:df08260bb029cf91a4dddf225b7ccffeb710a7bc21b57ceb0790ccbc6f400b8a + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-shopinvader%2Fodoo--shopinvader-lightgray.png?logo=github + :target: https://github.com/shopinvader/odoo-shopinvader/tree/14.0/shopinvader_list_cities + :alt: shopinvader/odoo-shopinvader + + +|badge1| |badge2| |badge3| + +REST Services for To list Brazilian cities. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Call this endpoint every time you need to fetch Brazilian cities from a state. + +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 +~~~~~~~ + +* KMEE INFORMATICA LTDA + +Contributors +~~~~~~~~~~~~ + +* Cristiano Rodrigues + +Other credits +~~~~~~~~~~~~~ + +* KMEE INFORMATICA LTDA (http://www.kmee.com.br). + + +Maintainers +~~~~~~~~~~~ + +This module is part of the `shopinvader/odoo-shopinvader `_ project on GitHub. + +You are welcome to contribute. diff --git a/shopinvader_base_address_city/__init__.py b/shopinvader_base_address_city/__init__.py new file mode 100644 index 0000000000..45a63b85b4 --- /dev/null +++ b/shopinvader_base_address_city/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2022 KMEE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import services diff --git a/shopinvader_base_address_city/__manifest__.py b/shopinvader_base_address_city/__manifest__.py new file mode 100644 index 0000000000..f5251f3950 --- /dev/null +++ b/shopinvader_base_address_city/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Shopinvader Base Address City", + "summary": ( + "Shopinvader, call this endpoint every time you " + "need to fetch cities from a state" + ), + "version": "14.0.0.0.0", + "category": "e-commerce", + "website": "https://github.com/shopinvader/odoo-shopinvader", + "author": "KMEE INFORMATICA LTDA", + "license": "AGPL-3", + "external_dependencies": {"python": ["cerberus"]}, + "depends": ["shopinvader", "base_address_city"], +} diff --git a/shopinvader_base_address_city/readme/CONTRIBUTORS.rst b/shopinvader_base_address_city/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..2b95fa3ca9 --- /dev/null +++ b/shopinvader_base_address_city/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Cristiano Rodrigues diff --git a/shopinvader_base_address_city/readme/CREDITS.rst b/shopinvader_base_address_city/readme/CREDITS.rst new file mode 100644 index 0000000000..00611148d4 --- /dev/null +++ b/shopinvader_base_address_city/readme/CREDITS.rst @@ -0,0 +1 @@ +* KMEE INFORMATICA LTDA (http://www.kmee.com.br). diff --git a/shopinvader_base_address_city/readme/DESCRIPTION.rst b/shopinvader_base_address_city/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..4f7b9493d9 --- /dev/null +++ b/shopinvader_base_address_city/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +REST Services for To list Brazilian cities. diff --git a/shopinvader_base_address_city/readme/USAGE.rst b/shopinvader_base_address_city/readme/USAGE.rst new file mode 100644 index 0000000000..944430f622 --- /dev/null +++ b/shopinvader_base_address_city/readme/USAGE.rst @@ -0,0 +1 @@ +Call this endpoint every time you need to fetch Brazilian cities from a state. diff --git a/shopinvader_base_address_city/services/__init__.py b/shopinvader_base_address_city/services/__init__.py new file mode 100644 index 0000000000..298325dd86 --- /dev/null +++ b/shopinvader_base_address_city/services/__init__.py @@ -0,0 +1,6 @@ +# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import res_city_abstract +from . import res_city_component diff --git a/shopinvader_base_address_city/services/res_city_abstract.py b/shopinvader_base_address_city/services/res_city_abstract.py new file mode 100644 index 0000000000..05576f2d5d --- /dev/null +++ b/shopinvader_base_address_city/services/res_city_abstract.py @@ -0,0 +1,64 @@ +# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import UserError, ValidationError + +from odoo.addons.base_rest import restapi +from odoo.addons.component.core import AbstractComponent + + +class SearchServiceCities(AbstractComponent): + _name = "search.service.cities" + _inherit = "base.rest.service" + _usage = "city" + _description = "REST Services for Search cities across the state" + + @restapi.method( + [(["/"], "GET")], + input_param=restapi.CerberusValidator("_validator_search"), + output_param=restapi.CerberusValidator("_validator_return_search"), + cors="*", + ) + def search(self, **params): + state = params.get("state") + try: + country_id = self.shopinvader_backend.company_id.country_id + if country_id: + state_id = self.env["res.country.state"].search( + [("country_id", "=", country_id.id), ("code", "=", state)] + ) + city_ids = self.env["res.city"].search( + [("country_id", "=", country_id.id), ("state_id", "=", state_id.id)] + ) + cities = [] + for x in city_ids: + cities.append({"name": x.name, "id": x.id}) + except (UserError, ValidationError) as e: + return {"result": False, "error": str(e)} + + return {"result": cities} + + def _validator_search(self): + return { + "state": {"type": "string", "required": True}, + } + + def _validator_return_search(self): + return { + "result": { + "type": "list", + "required": True, + "schema": { + "type": "dict", + "schema": { + "name": { + "type": "string", + "required": True, + "nullable": True, + }, + "id": {"type": "integer", "required": True}, + }, + }, + }, + } diff --git a/shopinvader_base_address_city/services/res_city_component.py b/shopinvader_base_address_city/services/res_city_component.py new file mode 100644 index 0000000000..416eba4e50 --- /dev/null +++ b/shopinvader_base_address_city/services/res_city_component.py @@ -0,0 +1,14 @@ +# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.component.core import Component + + +class SearchServiceCitiesShopinvader(Component): + """Expose Search Cities across the state service under /shopinvader endpoint""" + + _name = "search.service.cities.shopinvader" + _inherit = ["search.service.cities", "base.shopinvader.service"] + _usage = "city" + _collection = "shopinvader.backend" diff --git a/shopinvader_base_address_city/static/description/index.html b/shopinvader_base_address_city/static/description/index.html new file mode 100644 index 0000000000..39d6da2155 --- /dev/null +++ b/shopinvader_base_address_city/static/description/index.html @@ -0,0 +1,433 @@ + + + + + + +Shopinvader Search Cities Brazilian + + + +
+

Shopinvader Search Cities Brazilian

+ + +

Beta License: AGPL-3 OCA/odoo-shopinvader Translate me on Weblate Try me on Runboat

+

REST Services for To list Brazilian cities.

+

Table of contents

+ +
+

Usage

+

Call this endpoint every time you need to fetch Brazilian cities from a state.

+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • KMEE INFORMATICA LTDA
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+ +
+
+

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/odoo-shopinvader project on GitHub.

+

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

+
+
+
+ + diff --git a/shopinvader_base_address_city/tests/__init__.py b/shopinvader_base_address_city/tests/__init__.py new file mode 100644 index 0000000000..1cfa80c86a --- /dev/null +++ b/shopinvader_base_address_city/tests/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_city_abstract diff --git a/shopinvader_base_address_city/tests/test_city_abstract.py b/shopinvader_base_address_city/tests/test_city_abstract.py new file mode 100644 index 0000000000..bca4465437 --- /dev/null +++ b/shopinvader_base_address_city/tests/test_city_abstract.py @@ -0,0 +1,30 @@ +# Copyright 2022 KMEE INFORMATICA LTDA (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.shopinvader.tests.common import CommonCase + + +class TestShopinvaderControllerCase(CommonCase): + def setUp(self, *args, **kwargs): + super(TestShopinvaderControllerCase, self).setUp(*args, **kwargs) + with self.work_on_services( + partner=None, shopinvader_session=self.shopinvader_session + ) as work: + self.city_service = work.component(usage="city") + + def test_get_list_of_cities_by_state_code(self): + params = {"state": "PR"} + result = self.city_service.dispatch("search", params=params) + country_id = self.env.ref("base.br") + state_id = self.env["res.country.state"].search( + [("country_id", "=", country_id.id), ("code", "=", "PR")] + ) + city_ids = self.env["res.city"].search( + [("country_id", "=", country_id.id), ("state_id", "=", state_id.id)] + ) + cities = [] + for x in city_ids: + cities.append({"name": x.name, "id": x.id}) + + self.assertEqual(result, {"result": cities}) From 917eb262053f6f9abd1d32b993e12376cc16d9f1 Mon Sep 17 00:00:00 2001 From: Daniel Sadamo Date: Mon, 28 Aug 2023 15:36:50 -0300 Subject: [PATCH 2/5] [ADD]shopinvader_base_address_city: use city_id in address service --- .../services/__init__.py | 1 + .../services/address.py | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 shopinvader_base_address_city/services/address.py diff --git a/shopinvader_base_address_city/services/__init__.py b/shopinvader_base_address_city/services/__init__.py index 298325dd86..f0f521f866 100644 --- a/shopinvader_base_address_city/services/__init__.py +++ b/shopinvader_base_address_city/services/__init__.py @@ -2,5 +2,6 @@ # @author Cristiano Rodrigues # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import address from . import res_city_abstract from . import res_city_component diff --git a/shopinvader_base_address_city/services/address.py b/shopinvader_base_address_city/services/address.py new file mode 100644 index 0000000000..0be767a00f --- /dev/null +++ b/shopinvader_base_address_city/services/address.py @@ -0,0 +1,50 @@ +# Copyright 2022 KMEE (http://www.kmee.com.br). +# @author Cristiano Rodrigues +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.base_rest.components.service import to_int +from odoo.addons.component.core import Component + + +class AddressService(Component): + _inherit = "shopinvader.address.service" + + def _json_parser(self): + res = super()._json_parser() + + res.remove("city") + res.append(("city_id", ["id", "name"])) + + return res + + def _validator_create(self): + res = super()._validator_create() + + if "city" in res: + del res["city"] + + res["city_id"] = { + "type": "dict", + "schema": { + "id": { + "coerce": to_int, + "nullable": False, + "type": "integer", + }, + "name": { + "type": "string", + "nullable": True, + }, + }, + } + + return res + + def _prepare_params(self, params, mode="create"): + if params.get("city_id"): + params["city_id"] = ( + params.get("city_id")["id"] + if type(params["city_id"]) == dict + else params.get("city_id") + ) + return super(AddressService, self)._prepare_params(params, mode) From d58f627c0b825d714ba03a56acae47171bde8766 Mon Sep 17 00:00:00 2001 From: Cristiano Rodrigues Date: Wed, 30 Aug 2023 17:48:12 -0300 Subject: [PATCH 3/5] [FIX] shopinvader_base_address_city: Removed field city --- shopinvader_base_address_city/services/address.py | 1 - 1 file changed, 1 deletion(-) diff --git a/shopinvader_base_address_city/services/address.py b/shopinvader_base_address_city/services/address.py index 0be767a00f..47c11396ac 100644 --- a/shopinvader_base_address_city/services/address.py +++ b/shopinvader_base_address_city/services/address.py @@ -12,7 +12,6 @@ class AddressService(Component): def _json_parser(self): res = super()._json_parser() - res.remove("city") res.append(("city_id", ["id", "name"])) return res From 506e6f505ffe23c570056e6296d4bade7f7ac59a Mon Sep 17 00:00:00 2001 From: Cristiano Rodrigues Date: Thu, 31 Aug 2023 11:47:52 -0300 Subject: [PATCH 4/5] [FIX] shopinvader_base_address_city: Condition for city --- shopinvader_base_address_city/services/address.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shopinvader_base_address_city/services/address.py b/shopinvader_base_address_city/services/address.py index 47c11396ac..8f53333a91 100644 --- a/shopinvader_base_address_city/services/address.py +++ b/shopinvader_base_address_city/services/address.py @@ -11,7 +11,8 @@ class AddressService(Component): def _json_parser(self): res = super()._json_parser() - + if "city" in res: + res.remove("city") res.append(("city_id", ["id", "name"])) return res From 0d4a097ce9a051e08e2797611f53d4303a0a44eb Mon Sep 17 00:00:00 2001 From: Cristiano Rodrigues Date: Mon, 25 Sep 2023 10:29:52 -0300 Subject: [PATCH 5/5] [FIX] Adaptation to respond to any country --- .../odoo/addons/shopinvader_base_address_city | 1 + setup/shopinvader_base_address_city/setup.py | 6 ++ shopinvader_base_address_city/README.rst | 14 ++++- .../readme/DESCRIPTION.rst | 3 +- .../readme/USAGE.rst | 8 ++- .../services/res_city_abstract.py | 58 +++++++++++++++---- 6 files changed, 75 insertions(+), 15 deletions(-) create mode 120000 setup/shopinvader_base_address_city/odoo/addons/shopinvader_base_address_city create mode 100644 setup/shopinvader_base_address_city/setup.py diff --git a/setup/shopinvader_base_address_city/odoo/addons/shopinvader_base_address_city b/setup/shopinvader_base_address_city/odoo/addons/shopinvader_base_address_city new file mode 120000 index 0000000000..bd551a9428 --- /dev/null +++ b/setup/shopinvader_base_address_city/odoo/addons/shopinvader_base_address_city @@ -0,0 +1 @@ +../../../../shopinvader_base_address_city \ No newline at end of file diff --git a/setup/shopinvader_base_address_city/setup.py b/setup/shopinvader_base_address_city/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/shopinvader_base_address_city/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/shopinvader_base_address_city/README.rst b/shopinvader_base_address_city/README.rst index 3023300fd9..93d0472aef 100644 --- a/shopinvader_base_address_city/README.rst +++ b/shopinvader_base_address_city/README.rst @@ -23,7 +23,9 @@ Shopinvader Base Address City |badge1| |badge2| |badge3| -REST Services for To list Brazilian cities. +REST Services for To list cities. +By default, this module returns a list of cities in a given country by state code. + **Table of contents** @@ -33,9 +35,15 @@ REST Services for To list Brazilian cities. Usage ===== -Call this endpoint every time you need to fetch Brazilian cities from a state. - +Find cities by country code or state code. Passing estre json:\n +{ + "country_code": "", + "state_code": "" +} +If you don't pass the country code in the json, the Api will\n + search for the country of the company released on Shopinvader. Bug Tracker + =========== Bugs are tracked on `GitHub Issues `_. diff --git a/shopinvader_base_address_city/readme/DESCRIPTION.rst b/shopinvader_base_address_city/readme/DESCRIPTION.rst index 4f7b9493d9..1532524204 100644 --- a/shopinvader_base_address_city/readme/DESCRIPTION.rst +++ b/shopinvader_base_address_city/readme/DESCRIPTION.rst @@ -1 +1,2 @@ -REST Services for To list Brazilian cities. +REST Services for To list cities. +By default, this module returns a list of cities in a given country by state code. diff --git a/shopinvader_base_address_city/readme/USAGE.rst b/shopinvader_base_address_city/readme/USAGE.rst index 944430f622..13562e82ab 100644 --- a/shopinvader_base_address_city/readme/USAGE.rst +++ b/shopinvader_base_address_city/readme/USAGE.rst @@ -1 +1,7 @@ -Call this endpoint every time you need to fetch Brazilian cities from a state. +Find cities by country code or state code. Passing estre json:\n +{ + "country_code": "", + "state_code": "" +} +If you don't pass the country code in the json, the Api will\n + search for the country of the company released on Shopinvader. diff --git a/shopinvader_base_address_city/services/res_city_abstract.py b/shopinvader_base_address_city/services/res_city_abstract.py index 05576f2d5d..3a0d294148 100644 --- a/shopinvader_base_address_city/services/res_city_abstract.py +++ b/shopinvader_base_address_city/services/res_city_abstract.py @@ -2,7 +2,8 @@ # @author Cristiano Rodrigues # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.exceptions import UserError, ValidationError +from odoo import _, http +from odoo.exceptions import ValidationError from odoo.addons.base_rest import restapi from odoo.addons.component.core import AbstractComponent @@ -21,27 +22,64 @@ class SearchServiceCities(AbstractComponent): cors="*", ) def search(self, **params): - state = params.get("state") + country_code = params.get("country_code") + state_code = params.get("state_code") try: - country_id = self.shopinvader_backend.company_id.country_id + country_id = self.env["res.country"].search([("code", "=", country_code)]) + + if not country_id: + country_id = self.shopinvader_backend.company_id.country_id + + if not country_id: + return http.Response( + _( + { + "result": False, + "error": f"country_code: {country_code} not found", + } + ), + status=404, + ) + if country_id: state_id = self.env["res.country.state"].search( - [("country_id", "=", country_id.id), ("code", "=", state)] + [("country_id", "=", country_id.id), ("code", "=", state_code)] ) city_ids = self.env["res.city"].search( [("country_id", "=", country_id.id), ("state_id", "=", state_id.id)] ) - cities = [] + + res = [] + if not city_ids: + res.append({"id": state_id.id, "name": state_id.name}) + for x in city_ids: - cities.append({"name": x.name, "id": x.id}) - except (UserError, ValidationError) as e: - return {"result": False, "error": str(e)} + res.append({"id": x.id, "name": x.name}) + + if not state_id: + return http.Response( + _( + { + "result": False, + "error": f"state_code: {state_code} not found", + } + ), + status=404, + ) + elif not city_ids: + return http.Response( + _({"result": False, "error": "Cities: not found"}), status=404 + ) + + except ValidationError as e: + return http.Response(_({"result": False, "error": str(e)}), status=404) - return {"result": cities} + return {"result": res} def _validator_search(self): return { - "state": {"type": "string", "required": True}, + "country_code": {"type": "string", "required": True}, + "state_code": {"type": "string", "required": True}, } def _validator_return_search(self):