Simplify FSM zones and switch polygon preview to Mapbox
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user