|
34 | 34 |
|
35 | 35 | _logger = logging.getLogger(__name__) |
36 | 36 |
|
| 37 | +DEBUG_HH_POINTS_LAYER_NAME = "HH Points (Debug)" |
| 38 | +DEBUG_HH_POINTS_LAYER_DOMAIN = ( |
| 39 | + "[('is_registrant', '=', True), " |
| 40 | + "('is_group', '=', True), " |
| 41 | + "('coordinates', '!=', False), " |
| 42 | + "('active', '=', True)]" |
| 43 | +) |
| 44 | + |
37 | 45 | # Standard variables from spp_studio to activate (module.xml_id format) |
38 | 46 | STANDARD_VARIABLES = [ |
39 | 47 | # Demographics (computed) |
@@ -128,6 +136,93 @@ def _activate_variables(env, xml_ids, source_name): |
128 | 136 | return activated, skipped, errors |
129 | 137 |
|
130 | 138 |
|
| 139 | +def _find_debug_layer_view(env): |
| 140 | + """Find the most useful GIS view for debug household points.""" |
| 141 | + view_model = env["ir.ui.view"] |
| 142 | + |
| 143 | + # Prefer geofence map when available (best fit for geofence query debugging) |
| 144 | + geofence_view = view_model.search( |
| 145 | + [ |
| 146 | + ("model", "=", "spp.gis.geofence"), |
| 147 | + ("type", "=", "gis"), |
| 148 | + ], |
| 149 | + limit=1, |
| 150 | + ) |
| 151 | + if geofence_view: |
| 152 | + return geofence_view |
| 153 | + |
| 154 | + # Fallback to the standard area map |
| 155 | + return view_model.search( |
| 156 | + [ |
| 157 | + ("model", "=", "spp.area"), |
| 158 | + ("type", "=", "gis"), |
| 159 | + ], |
| 160 | + limit=1, |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def _ensure_household_points_debug_layer(env): |
| 165 | + """Create/update the HH points debug GIS layer (hidden on startup).""" |
| 166 | + coordinates_field = env["ir.model.fields"].search( |
| 167 | + [ |
| 168 | + ("model", "=", "res.partner"), |
| 169 | + ("name", "=", "coordinates"), |
| 170 | + ], |
| 171 | + limit=1, |
| 172 | + ) |
| 173 | + if not coordinates_field: |
| 174 | + _logger.info( |
| 175 | + "[spp.mis.demo] Skipping HH debug layer: res.partner.coordinates not available" |
| 176 | + ) |
| 177 | + return False |
| 178 | + |
| 179 | + view = _find_debug_layer_view(env) |
| 180 | + if not view: |
| 181 | + _logger.info( |
| 182 | + "[spp.mis.demo] Skipping HH debug layer: no GIS view found for geofence/area" |
| 183 | + ) |
| 184 | + return False |
| 185 | + |
| 186 | + layer_model = env["spp.gis.data.layer"] |
| 187 | + layer = layer_model.search( |
| 188 | + [ |
| 189 | + ("name", "=", DEBUG_HH_POINTS_LAYER_NAME), |
| 190 | + ("geo_field_id", "=", coordinates_field.id), |
| 191 | + ("view_id", "=", view.id), |
| 192 | + ], |
| 193 | + limit=1, |
| 194 | + ) |
| 195 | + |
| 196 | + vals = { |
| 197 | + "name": DEBUG_HH_POINTS_LAYER_NAME, |
| 198 | + "geo_field_id": coordinates_field.id, |
| 199 | + "view_id": view.id, |
| 200 | + "geo_repr": "basic", |
| 201 | + "active_on_startup": False, |
| 202 | + "layer_opacity": 0.9, |
| 203 | + "begin_color": "#0057B8", |
| 204 | + "sequence": 25, |
| 205 | + "domain": DEBUG_HH_POINTS_LAYER_DOMAIN, |
| 206 | + } |
| 207 | + |
| 208 | + if layer: |
| 209 | + layer.write(vals) |
| 210 | + _logger.info( |
| 211 | + "[spp.mis.demo] Updated HH debug layer '%s' on view %s", |
| 212 | + layer.name, |
| 213 | + view.display_name, |
| 214 | + ) |
| 215 | + return layer |
| 216 | + |
| 217 | + layer = layer_model.create(vals) |
| 218 | + _logger.info( |
| 219 | + "[spp.mis.demo] Created HH debug layer '%s' on view %s", |
| 220 | + layer.name, |
| 221 | + view.display_name, |
| 222 | + ) |
| 223 | + return layer |
| 224 | + |
| 225 | + |
131 | 226 | def post_init_hook(env_or_cr, registry=None): |
132 | 227 | """Post-initialization hook for demo module. |
133 | 228 |
|
@@ -162,3 +257,5 @@ def post_init_hook(env_or_cr, registry=None): |
162 | 257 | total_skipped, |
163 | 258 | total_errors, |
164 | 259 | ) |
| 260 | + |
| 261 | + _ensure_household_points_debug_layer(env) |
0 commit comments