Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] shopinvader_base_address_city: module for localization of cities #1394

Open
wants to merge 5 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/shopinvader_base_address_city/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
80 changes: 80 additions & 0 deletions shopinvader_base_address_city/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
=============================
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 cities.
By default, this module returns a list of cities in a given country by state code.


**Table of contents**

.. contents::
:local:

Usage
=====

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 <https://github.com/shopinvader/odoo-shopinvader/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 <https://github.com/shopinvader/odoo-shopinvader/issues/new?body=module:%20shopinvader_lead%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* KMEE INFORMATICA LTDA

Contributors
~~~~~~~~~~~~

* Cristiano Rodrigues <[email protected]>

Other credits
~~~~~~~~~~~~~

* KMEE INFORMATICA LTDA (http://www.kmee.com.br).


Maintainers
~~~~~~~~~~~

This module is part of the `shopinvader/odoo-shopinvader <https://github.com/shopinvader/odoo-shopinvader/tree/14.0/shopinvader_list_cities>`_ project on GitHub.

You are welcome to contribute.
4 changes: 4 additions & 0 deletions shopinvader_base_address_city/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2022 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import services
18 changes: 18 additions & 0 deletions shopinvader_base_address_city/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br).
# @author Cristiano Rodrigues <[email protected]>
# 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"],
}
1 change: 1 addition & 0 deletions shopinvader_base_address_city/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Cristiano Rodrigues <[email protected]>
1 change: 1 addition & 0 deletions shopinvader_base_address_city/readme/CREDITS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* KMEE INFORMATICA LTDA (http://www.kmee.com.br).
2 changes: 2 additions & 0 deletions shopinvader_base_address_city/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REST Services for To list cities.
By default, this module returns a list of cities in a given country by state code.
7 changes: 7 additions & 0 deletions shopinvader_base_address_city/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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.
7 changes: 7 additions & 0 deletions shopinvader_base_address_city/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br).
# @author Cristiano Rodrigues <[email protected]>
# 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
50 changes: 50 additions & 0 deletions shopinvader_base_address_city/services/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2022 KMEE (http://www.kmee.com.br).
# @author Cristiano Rodrigues <[email protected]>
# 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()
if "city" in res:
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)
102 changes: 102 additions & 0 deletions shopinvader_base_address_city/services/res_city_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br).
# @author Cristiano Rodrigues <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, http
from odoo.exceptions import 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):
country_code = params.get("country_code")
state_code = params.get("state_code")
try:
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_code)]
)
city_ids = self.env["res.city"].search(
[("country_id", "=", country_id.id), ("state_id", "=", state_id.id)]
)

res = []
if not city_ids:
res.append({"id": state_id.id, "name": state_id.name})

for x in city_ids:
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": res}

def _validator_search(self):
return {
"country_code": {"type": "string", "required": True},
"state_code": {"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},
},
},
},
}
14 changes: 14 additions & 0 deletions shopinvader_base_address_city/services/res_city_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 KMEE INFORMATICA LTDA (http://www.kmee.com.br).
# @author Cristiano Rodrigues <[email protected]>
# 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"
Loading