Replace zone preview with JS Mapbox widget and simplify zone UI
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
from urllib.parse import quote
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
@@ -14,40 +13,25 @@ class RepairFsmZone(models.Model):
|
||||
name = fields.Char(required=True, tracking=True)
|
||||
polygon_geojson = fields.Text(
|
||||
string="Polygon (GeoJSON)",
|
||||
required=True,
|
||||
tracking=True,
|
||||
help="GeoJSON Polygon geometry. Coordinates order: [longitude, latitude].",
|
||||
)
|
||||
polygon_map_preview = fields.Html(
|
||||
string="Polygon Map Preview",
|
||||
compute="_compute_polygon_map_preview",
|
||||
sanitize=False,
|
||||
)
|
||||
state = fields.Selection(
|
||||
selection=[
|
||||
("draft", "Draft"),
|
||||
("active", "Active"),
|
||||
("archived", "Archived"),
|
||||
],
|
||||
default="draft",
|
||||
default="active",
|
||||
tracking=True,
|
||||
group_expand="_group_expand_states",
|
||||
)
|
||||
mapbox_token = fields.Char(compute="_compute_mapbox_token")
|
||||
|
||||
def _group_expand_states(self, states, domain, order):
|
||||
return [key for key, _label in self._fields["state"].selection]
|
||||
|
||||
def action_set_active(self):
|
||||
@api.depends_context("uid")
|
||||
def _compute_mapbox_token(self):
|
||||
token = self.env["ir.config_parameter"].sudo().get_param("dsrpt_repair_config.mapbox_token") or ""
|
||||
for rec in self:
|
||||
if not rec.polygon_geojson:
|
||||
raise ValidationError("Polygon is required before activating the FSM Zone.")
|
||||
rec._extract_polygon_points()
|
||||
self.write({"state": "active"})
|
||||
|
||||
def action_archive(self):
|
||||
self.write({"state": "archived"})
|
||||
|
||||
def action_reset_draft(self):
|
||||
self.write({"state": "draft"})
|
||||
rec.mapbox_token = token
|
||||
|
||||
@staticmethod
|
||||
def _point_in_polygon(longitude, latitude, points):
|
||||
@@ -108,51 +92,7 @@ class RepairFsmZone(models.Model):
|
||||
points = self._extract_polygon_points()
|
||||
return self._point_in_polygon(float(longitude), float(latitude), points)
|
||||
|
||||
def action_open_polygon_in_map(self):
|
||||
self.ensure_one()
|
||||
encoded = quote(self.polygon_geojson or '{"type":"Polygon","coordinates":[[]]}')
|
||||
return {
|
||||
"type": "ir.actions.act_url",
|
||||
"url": f"https://geojson.io/#data=data:application/json,{encoded}",
|
||||
"target": "new",
|
||||
}
|
||||
|
||||
def action_open_point_picker_map(self):
|
||||
return {
|
||||
"type": "ir.actions.act_url",
|
||||
"url": "https://www.openstreetmap.org",
|
||||
"target": "new",
|
||||
}
|
||||
|
||||
@api.depends("polygon_geojson")
|
||||
def _compute_polygon_map_preview(self):
|
||||
token = self.env["ir.config_parameter"].sudo().get_param("dsrpt_repair_config.mapbox_token")
|
||||
for rec in self:
|
||||
if not rec.polygon_geojson:
|
||||
rec.polygon_map_preview = "<div>No polygon yet.</div>"
|
||||
continue
|
||||
if not token:
|
||||
rec.polygon_map_preview = "<div>Set Mapbox token in system parameter dsrpt_repair_config.mapbox_token.</div>"
|
||||
continue
|
||||
try:
|
||||
data = json.loads(rec.polygon_geojson)
|
||||
except Exception:
|
||||
rec.polygon_map_preview = "<div>Invalid polygon JSON.</div>"
|
||||
continue
|
||||
feature = {"type": "Feature", "geometry": data, "properties": {"name": rec.name or "Zone"}}
|
||||
overlay = quote(json.dumps(feature, separators=(",", ":")))
|
||||
token_encoded = quote(token)
|
||||
static_url = (
|
||||
"https://api.mapbox.com/styles/v1/mapbox/streets-v12/static/"
|
||||
f"geojson({overlay})/auto/1100x420?padding=40&access_token={token_encoded}"
|
||||
)
|
||||
rec.polygon_map_preview = (
|
||||
f'<img src="{static_url}" alt="Zone polygon map preview" '
|
||||
'style="width:100%;height:auto;max-height:420px;object-fit:contain;border:1px solid #d9d9d9;border-radius:6px;"/>'
|
||||
)
|
||||
|
||||
@api.constrains("polygon_geojson")
|
||||
def _check_polygon_geojson(self):
|
||||
for rec in self:
|
||||
if rec.polygon_geojson:
|
||||
rec._extract_polygon_points()
|
||||
rec._extract_polygon_points()
|
||||
|
||||
Reference in New Issue
Block a user