|
1 | 1 | from enum import StrEnum |
2 | | -from typing import Any, Generic, Literal, Optional, TypeVar |
| 2 | +from typing import Any, Dict, Generic, Iterator, Literal, Optional, TypeVar, Union |
3 | 3 |
|
4 | | -from geojson_pydantic import Feature, FeatureCollection |
| 4 | +from geojson_pydantic.base import _GeoJsonBase |
5 | 5 | from geojson_pydantic.geometries import Geometry |
6 | 6 | from pydantic import ( |
7 | 7 | AwareDatetime, |
8 | 8 | BaseModel, |
9 | 9 | ConfigDict, |
10 | 10 | Field, |
11 | 11 | StrictStr, |
| 12 | + field_validator, |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | from stapi_fastapi.models.opportunity import OpportunityProperties |
15 | 16 | from stapi_fastapi.models.shared import Link |
16 | 17 | from stapi_fastapi.types.datetime_interval import DatetimeInterval |
17 | 18 | from stapi_fastapi.types.filter import CQL2Filter |
18 | 19 |
|
| 20 | +Props = TypeVar("Props", bound=Union[Dict[str, Any], BaseModel]) |
| 21 | +Geom = TypeVar("Geom", bound=Geometry) |
| 22 | + |
19 | 23 |
|
20 | 24 | class OrderParameters(BaseModel): |
21 | 25 | model_config = ConfigDict(extra="forbid") |
@@ -71,14 +75,43 @@ class OrderProperties(BaseModel): |
71 | 75 | model_config = ConfigDict(extra="allow") |
72 | 76 |
|
73 | 77 |
|
74 | | -class Order(Feature[Geometry, OrderProperties]): |
| 78 | +# derived from geojson_pydantic.Feature |
| 79 | +class Order(_GeoJsonBase): |
75 | 80 | # We need to enforce that orders have an id defined, as that is required to |
76 | 81 | # retrieve them via the API |
77 | 82 | id: StrictStr |
78 | 83 | type: Literal["Feature"] = "Feature" |
| 84 | + |
| 85 | + geometry: Geometry = Field(...) |
| 86 | + properties: OrderProperties = Field(...) |
| 87 | + |
79 | 88 | links: list[Link] = Field(default_factory=list) |
80 | 89 |
|
| 90 | + __geojson_exclude_if_none__ = {"bbox", "id"} |
| 91 | + |
| 92 | + @field_validator("geometry", mode="before") |
| 93 | + def set_geometry(cls, geometry: Any) -> Any: |
| 94 | + """set geometry from geo interface or input""" |
| 95 | + if hasattr(geometry, "__geo_interface__"): |
| 96 | + return geometry.__geo_interface__ |
| 97 | + |
| 98 | + return geometry |
81 | 99 |
|
82 | | -class OrderCollection(FeatureCollection[Order]): |
| 100 | + |
| 101 | +# derived from geojson_pydantic.FeatureCollection |
| 102 | +class OrderCollection(_GeoJsonBase): |
83 | 103 | type: Literal["FeatureCollection"] = "FeatureCollection" |
| 104 | + features: list[Order] |
84 | 105 | links: list[Link] = Field(default_factory=list) |
| 106 | + |
| 107 | + def __iter__(self) -> Iterator[Order]: # type: ignore [override] |
| 108 | + """iterate over features""" |
| 109 | + return iter(self.features) |
| 110 | + |
| 111 | + def __len__(self) -> int: |
| 112 | + """return features length""" |
| 113 | + return len(self.features) |
| 114 | + |
| 115 | + def __getitem__(self, index: int) -> Order: |
| 116 | + """get feature at a given index""" |
| 117 | + return self.features[index] |
0 commit comments