UPDATE
This commit is contained in:
+84
-28
@@ -3,24 +3,83 @@ import uuid
|
||||
from django.db import models
|
||||
|
||||
|
||||
def build_default_sub_block(block_code: str, *, boundary: dict | None = None) -> dict:
|
||||
normalized_block_code = str(block_code or "block-1").strip() or "block-1"
|
||||
return {
|
||||
"sub_block_code": f"{normalized_block_code}-sub-1",
|
||||
"cluster_label": 0,
|
||||
"source": "default",
|
||||
"boundary": boundary or {},
|
||||
"cluster_uuid": None,
|
||||
}
|
||||
|
||||
|
||||
|
||||
def ensure_block_layout_defaults(layout: dict | None, *, block_count: int | None = None) -> dict:
|
||||
raw_layout = dict(layout or {})
|
||||
raw_blocks = list(raw_layout.get("blocks") or [])
|
||||
normalized_count = len(raw_blocks) if raw_blocks else max(int(block_count or raw_layout.get("input_block_count") or 1), 1)
|
||||
normalized_blocks: list[dict] = []
|
||||
|
||||
for index in range(normalized_count):
|
||||
raw_block = raw_blocks[index] if index < len(raw_blocks) else {}
|
||||
block_code = str(raw_block.get("block_code") or f"block-{index + 1}").strip() or f"block-{index + 1}"
|
||||
boundary = raw_block.get("boundary") or {}
|
||||
sub_blocks = [dict(sub_block) for sub_block in (raw_block.get("sub_blocks") or []) if isinstance(sub_block, dict)]
|
||||
if not sub_blocks:
|
||||
sub_blocks = [build_default_sub_block(block_code, boundary=boundary)]
|
||||
|
||||
normalized_block = {
|
||||
"block_code": block_code,
|
||||
"order": int(raw_block.get("order") or index + 1),
|
||||
"source": raw_block.get("source") or ("input" if raw_blocks or normalized_count > 1 else "default"),
|
||||
"boundary": boundary,
|
||||
"needs_subdivision": raw_block.get("needs_subdivision"),
|
||||
"sub_blocks": sub_blocks,
|
||||
}
|
||||
for extra_key in ("subdivision_summary", "analysis_grid_summary", "aggregated_metrics"):
|
||||
if extra_key in raw_block:
|
||||
normalized_block[extra_key] = raw_block[extra_key]
|
||||
normalized_blocks.append(normalized_block)
|
||||
|
||||
return {
|
||||
"input_block_count": normalized_count,
|
||||
"default_full_farm": raw_layout.get("default_full_farm", normalized_count == 1),
|
||||
"algorithm_status": raw_layout.get("algorithm_status") or "pending",
|
||||
"blocks": normalized_blocks,
|
||||
}
|
||||
|
||||
|
||||
|
||||
def build_block_layout(block_count: int = 1, blocks: list[dict] | None = None) -> dict:
|
||||
normalized_blocks = []
|
||||
if blocks:
|
||||
for index, block in enumerate(blocks):
|
||||
normalized_blocks.append(
|
||||
{
|
||||
"block_code": str(block.get("block_code") or f"block-{index + 1}").strip(),
|
||||
"order": int(block.get("order") or index + 1),
|
||||
"source": "input",
|
||||
"boundary": block.get("boundary") or {},
|
||||
"needs_subdivision": None,
|
||||
"sub_blocks": [],
|
||||
}
|
||||
)
|
||||
else:
|
||||
normalized_count = max(int(block_count or 1), 1)
|
||||
for index in range(normalized_count):
|
||||
normalized_blocks.append(
|
||||
return ensure_block_layout_defaults(
|
||||
{
|
||||
"input_block_count": len(blocks),
|
||||
"default_full_farm": len(blocks) == 1,
|
||||
"algorithm_status": "pending",
|
||||
"blocks": [
|
||||
{
|
||||
"block_code": str(block.get("block_code") or f"block-{index + 1}").strip(),
|
||||
"order": int(block.get("order") or index + 1),
|
||||
"source": "input",
|
||||
"boundary": block.get("boundary") or {},
|
||||
"needs_subdivision": None,
|
||||
"sub_blocks": [dict(sub_block) for sub_block in (block.get("sub_blocks") or [])],
|
||||
}
|
||||
for index, block in enumerate(blocks)
|
||||
],
|
||||
},
|
||||
block_count=len(blocks),
|
||||
)
|
||||
|
||||
normalized_count = max(int(block_count or 1), 1)
|
||||
return ensure_block_layout_defaults(
|
||||
{
|
||||
"input_block_count": normalized_count,
|
||||
"default_full_farm": normalized_count == 1,
|
||||
"algorithm_status": "pending",
|
||||
"blocks": [
|
||||
{
|
||||
"block_code": f"block-{index + 1}",
|
||||
"order": index + 1,
|
||||
@@ -29,16 +88,11 @@ def build_block_layout(block_count: int = 1, blocks: list[dict] | None = None) -
|
||||
"needs_subdivision": None,
|
||||
"sub_blocks": [],
|
||||
}
|
||||
)
|
||||
|
||||
normalized_count = len(normalized_blocks) if normalized_blocks else max(int(block_count or 1), 1)
|
||||
|
||||
return {
|
||||
"input_block_count": normalized_count,
|
||||
"default_full_farm": normalized_count == 1,
|
||||
"algorithm_status": "pending",
|
||||
"blocks": normalized_blocks,
|
||||
}
|
||||
for index in range(normalized_count)
|
||||
],
|
||||
},
|
||||
block_count=normalized_count,
|
||||
)
|
||||
|
||||
|
||||
class SoilLocation(models.Model):
|
||||
@@ -122,8 +176,10 @@ class SoilLocation(models.Model):
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.input_block_count:
|
||||
self.input_block_count = 1
|
||||
if not self.block_layout:
|
||||
self.block_layout = build_block_layout(self.input_block_count)
|
||||
self.block_layout = ensure_block_layout_defaults(
|
||||
self.block_layout,
|
||||
block_count=self.input_block_count,
|
||||
)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user