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

17.0: Widget map and new map field #2953

Open
wants to merge 16 commits into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ addon | version | maintainers | summary
[web_widget_domain_editor_dialog](web_widget_domain_editor_dialog/) | 17.0.1.0.0 | | Recovers the Domain Editor Dialog functionality
[web_widget_dropdown_dynamic](web_widget_dropdown_dynamic/) | 17.0.1.0.0 | | This module adds support for dynamic dropdown widget
[web_widget_image_download](web_widget_image_download/) | 17.0.1.0.0 | | Allows to download any image from its widget
[web_widget_map](web_widget_map/) | 17.0.1.0.0 | [![drkpkg](https://github.com/drkpkg.png?size=30px)](https://github.com/drkpkg) | This module adds support for map widget
[web_widget_numeric_step](web_widget_numeric_step/) | 17.0.1.0.0 | [![rafaelbn](https://github.com/rafaelbn.png?size=30px)](https://github.com/rafaelbn) [![yajo](https://github.com/yajo.png?size=30px)](https://github.com/yajo) | Web Widget Numeric Step
[web_widget_open_tab](web_widget_open_tab/) | 17.0.1.0.0 | | Allow to open record from trees on new tab from tree views
[web_widget_plotly_chart](web_widget_plotly_chart/) | 17.0.1.0.0 | [![robyf70](https://github.com/robyf70.png?size=30px)](https://github.com/robyf70) | Allow to draw plotly charts.
Expand Down
65 changes: 65 additions & 0 deletions web_widget_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Map field and widget

This new widget is a combination of the `map` field and the `map` widget. It allows you
to display a map in the form and to select a location by dragging a marker on the map.

## Configuration

## Field instance

The python instance of the field is `fields.Map`.

```python

from odoo import models, fields, api, _

_logger = logging.getLogger(__name__)

class MyModel(models.Model):
_name = 'my.model'

name = fields.Char(string='Name')
location = fields.Map(string=_('Location'))

@api.onchange('location')
def _onchange_location(self):
"""
This will be called when the location is changed.
"""
if self.location:
_logger.info('Location: %s', self.location)
```

Now lets implement the widget.

```xml
<odoo>
<data>
<record id="view_my_model_form" model="ir.ui.view">
<field name="name">my.model.form</field>
<field name="model">my.model</field>
<field name="arch" type="xml">
<form string="My Model">
<sheet>
<group>
<field name="name" />
<field name="location" widget="map" />
</group>
</sheet>
</form>
</field>
</record>
</data>
</odoo>
```

After that you will get this result:

![Map widget](static/img/map.png)

## Thanks

I wanna thank Cybrosys Technologies for the documentation they maintain. I have used
their documentation as a reference to create this widget.

https://www.cybrosys.com/blog/how-to-create-a-widget-in-odoo-17
1 change: 1 addition & 0 deletions web_widget_map/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import fields
15 changes: 15 additions & 0 deletions web_widget_map/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Map field widget",
"summary": "A map field widget for Odoo.",
"author": "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/web",
"category": "Customizations",
"version": "17.0.1.0.0",
"depends": ["web"],
"assets": {
"web.assets_backend": [
"web_widget_map/static/src/components/*",
],
},
"license": "AGPL-3",
}
1 change: 1 addition & 0 deletions web_widget_map/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import map
61 changes: 61 additions & 0 deletions web_widget_map/fields/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from odoo import _, fields
from odoo.exceptions import ValidationError


class MapField(fields.Field):
"""
Custom field to store the location of a record.

The field stores the location as a string in the format 'lat,lng'.
"""

type = "char"
column_type = ("varchar", "varchar")

def convert_to_record(self, value, record, validate=True):
"""
Converts the value to a valid value for the record.
"""
try:
float(value.split(",")[0])
float(value.split(",")[1])
except ValueError as e:
raise ValidationError(_("Coords must be numbers.")) from e
return value

Check warning on line 24 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L19-L24

Added lines #L19 - L24 were not covered by tests

def convert_to_export(self, value, record):
"""
Converts a value from the database to a format suitable for export.
"""
return value or None

Check warning on line 30 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L30

Added line #L30 was not covered by tests

def convert_to_cache(self, value, record, validate=True):
"""
Converts a value from the database to a format suitable for the cache.
"""
return value or None

Check warning on line 36 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L36

Added line #L36 was not covered by tests

def convert_to_column(self, value, record, validate=True):
"""
Converts a value to a format suitable for the column.
"""
return value or None

Check warning on line 42 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L42

Added line #L42 was not covered by tests

def _get_attrs(self, model_class, name):
"""
Get the attributes of the field.
"""
res = super()._get_attrs(model_class, name)
res["type"] = "text"
return res

Check warning on line 50 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L48-L50

Added lines #L48 - L50 were not covered by tests

def get_location(self, field_name, model, id):
"""
Get the location of the record.
"""
record = self.env[model].browse(id)
return record[field_name]

Check warning on line 57 in web_widget_map/fields/map.py

View check run for this annotation

Codecov / codecov/patch

web_widget_map/fields/map.py#L56-L57

Added lines #L56 - L57 were not covered by tests


# Monkey patch the fields module to add the Map field.
fields.Map = MapField
37 changes: 37 additions & 0 deletions web_widget_map/i18n/es_BO.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * map_field
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-18 18:52+0000\n"
"PO-Revision-Date: 2024-09-18 18:52+0000\n"
"Last-Translator: Felix Daniel Coca Calvimontes <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Get Location"
msgstr "Obtener Ubicación"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Latitude:"
msgstr "Latitud:"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Longitude:"
msgstr "Longitud:"
37 changes: 37 additions & 0 deletions web_widget_map/i18n/es_CR.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * map_field
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-18 18:52+0000\n"
"PO-Revision-Date: 2024-09-18 18:52+0000\n"
"Last-Translator: Felix Daniel Coca Calvimontes <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Get Location"
msgstr "Obtener Ubicación"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Latitude:"
msgstr "Latitud:"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Longitude:"
msgstr "Longitud:"
37 changes: 37 additions & 0 deletions web_widget_map/i18n/map_field.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * map_field
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-18 18:51+0000\n"
"PO-Revision-Date: 2024-09-18 18:51+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Get Location"
msgstr ""

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Latitude:"
msgstr ""

#. module: map_field
#. odoo-javascript
#: code:addons/map_field/static/src/components/location_map.xml:0
#, python-format
msgid "Longitude:"
msgstr ""
3 changes: 3 additions & 0 deletions web_widget_map/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
Binary file added web_widget_map/static/img/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web_widget_map/static/lib/leaflet/images/layers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading