Simplify FSM zones and switch polygon preview to Mapbox

This commit is contained in:
Ruslan Bakiev
2026-02-13 17:31:51 +07:00
parent 80d0b0881c
commit f967429b9d
8 changed files with 30 additions and 19 deletions

View File

@@ -9,6 +9,7 @@
"data": [
"security/groups.xml",
"security/ir.model.access.csv",
"data/mapbox_token.xml",
"data/repair_fsm_zone_data_atlanta.xml",
"views/repair_fsm_zone_view_list.xml",
"views/repair_fsm_zone_view_form.xml",

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="1">
<record id="param_mapbox_token" model="ir.config_parameter">
<field name="key">dsrpt_repair_config.mapbox_token</field>
<field name="value">pk.eyJ1IjoidmVpa2FiIiwiYSI6ImNtaXcxNGxpZTI3bTEza3EyeWpmZTk1MXoifQ.JtuxUckpXUXpQmhcn-uGqQ</field>
</record>
</odoo>

View File

@@ -2,11 +2,9 @@
<odoo>
<record id="repair_fsm_zone_atlanta" model="repair.fsm.zone">
<field name="name">Atlanta Metro</field>
<field name="code">ATL</field>
<field name="polygon_geojson"><![CDATA[
{"type":"Polygon","coordinates":[[[-84.55,33.60],[-84.20,33.60],[-84.20,33.90],[-84.55,33.90],[-84.55,33.60]]]}
]]></field>
<field name="state">active</field>
<field name="active">True</field>
</record>
</odoo>

View File

@@ -12,7 +12,6 @@ class RepairFsmZone(models.Model):
_inherit = ["mail.thread", "mail.activity.mixin"]
name = fields.Char(required=True, tracking=True)
code = fields.Char(tracking=True)
polygon_geojson = fields.Text(
string="Polygon (GeoJSON)",
tracking=True,
@@ -33,7 +32,6 @@ class RepairFsmZone(models.Model):
tracking=True,
group_expand="_group_expand_states",
)
active = fields.Boolean(default=True, tracking=True)
def _group_expand_states(self, states, domain, order):
return [key for key, _label in self._fields["state"].selection]
@@ -43,13 +41,13 @@ class RepairFsmZone(models.Model):
if not rec.polygon_geojson:
raise ValidationError("Polygon is required before activating the FSM Zone.")
rec._extract_polygon_points()
self.write({"state": "active", "active": True})
self.write({"state": "active"})
def action_archive(self):
self.write({"state": "archived", "active": False})
self.write({"state": "archived"})
def action_reset_draft(self):
self.write({"state": "draft", "active": True})
self.write({"state": "draft"})
@staticmethod
def _point_in_polygon(longitude, latitude, points):
@@ -128,14 +126,29 @@ class RepairFsmZone(models.Model):
@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
encoded = quote(rec.polygon_geojson)
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'<iframe src="https://geojson.io/#data=data:application/json,{encoded}" '
'style="width:100%;height:420px;border:1px solid #d9d9d9;border-radius:6px;"></iframe>'
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")

View File

@@ -16,8 +16,6 @@
<sheet>
<group>
<field name="name"/>
<field name="code"/>
<field name="active"/>
</group>
<group>
<field name="polygon_geojson" widget="text" placeholder='{"type":"Polygon","coordinates":[[[-84.55,33.60],[-84.20,33.60],[-84.20,33.90],[-84.55,33.90],[-84.55,33.60]]]}'/>

View File

@@ -6,7 +6,6 @@
<field name="arch" type="xml">
<kanban default_group_by="state">
<field name="name"/>
<field name="code"/>
<field name="state"/>
<templates>
<t t-name="kanban-box">
@@ -14,9 +13,6 @@
<div class="o_kanban_record_title">
<strong><field name="name"/></strong>
</div>
<div>
<field name="code"/>
</div>
</div>
</t>
</templates>

View File

@@ -6,10 +6,8 @@
<field name="arch" type="xml">
<list>
<field name="name"/>
<field name="code" optional="show"/>
<field name="state" widget="badge" optional="show"/>
<field name="polygon_geojson" optional="hide"/>
<field name="active" optional="hide"/>
</list>
</field>
</record>

View File

@@ -66,7 +66,7 @@ class RepairWorkOrder(models.Model):
return self.service_latitude is not False and self.service_longitude is not False
def _find_zone_for_point(self, latitude, longitude):
zones = self.env["repair.fsm.zone"].search([("active", "=", True), ("state", "=", "active")])
zones = self.env["repair.fsm.zone"].search([("state", "=", "active")])
for zone in zones:
if zone.contains_point(latitude, longitude):
return zone