Skip to content

Commit

Permalink
fix: add outline_centroid to task_schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 29, 2023
1 parent b404da5 commit 079617e
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
"""Pydantic schemas for FMTM task areas."""


import base64
Expand All @@ -26,48 +27,44 @@
from pydantic import BaseModel, ConfigDict, Field, ValidationInfo
from pydantic.functional_validators import field_validator

from app.db.postgis_utils import geometry_to_geojson
from app.db.postgis_utils import geometry_to_geojson, get_centroid
from app.models.enums import TaskStatus


class TaskHistoryBase(BaseModel):
"""Task mapping history."""

id: int
action_text: str
action_date: datetime
obj: Optional[Any] = None


class TaskHistoryOut(TaskHistoryBase):
pass

"""Task mapping history display."""

class TaskBasicInfo(BaseModel):
id: int
project_id: int
project_task_index: int
task_status: TaskStatus
locked_by_uid: int = None
locked_by_username: str = None
task_history: List[TaskHistoryBase]
pass


class TaskBase(BaseModel):
"""Core fields for a Task."""

model_config = ConfigDict(
use_enum_values=True,
validate_default=True,
)

# Excluded
lock_holder: Any = Field(exclude=True)
outline: Any = Field(exclude=True)
qr_code: Any = Field(exclude=True)

id: int
project_id: int
project_task_index: int
project_task_name: str
outline_geojson: Optional[Feature] = None
# outline_centroid: Feature
# initial_feature_count: int
outline_centroid: Optional[Feature] = None
initial_feature_count: Optional[int] = None
task_status: TaskStatus
locked_by_uid: Optional[int] = None
locked_by_username: Optional[str] = None
Expand All @@ -87,6 +84,20 @@ def get_geojson_from_outline(cls, v: Any, info: ValidationInfo) -> str:
return geometry_to_geojson(outline, properties, info.data.get("id"))
return None

@field_validator("outline_centroid", mode="before")
@classmethod
def get_centroid_from_outline(cls, v: Any, info: ValidationInfo) -> str:
"""Get outline_centroid from Shapely geom."""
if outline := info.data.get("outline"):
properties = {
"fid": info.data.get("project_task_index"),
"uid": info.data.get("id"),
"name": info.data.get("project_task_name"),
}
log.debug("Converting task outline to geojson")
return get_centroid(outline, properties, info.data.get("id"))
return None

@field_validator("locked_by_uid", mode="before")
@classmethod
def get_lock_uid(cls, v: int, info: ValidationInfo) -> str:
Expand All @@ -105,6 +116,8 @@ def get_lock_username(cls, v: str, info: ValidationInfo) -> str:


class Task(TaskBase):
"""Task details plus base64 QR codes."""

qr_code_base64: Optional[str] = None

@field_validator("qr_code_base64", mode="before")
Expand All @@ -118,9 +131,5 @@ def get_qrcode_base64(cls, v: Any, info: ValidationInfo) -> str:
)
return base64.b64encode(qr_code.image)
else:
log.warning(f"No QR code found for task ID {db_task.id}")
log.warning(f"No QR code found for task ID {info.data.get('id')}")
return ""


class TaskDetails(TaskBase):
pass

0 comments on commit 079617e

Please sign in to comment.