Skip to content

Commit

Permalink
Merge branch 'main' of github.com:openaq/openaq-api-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Parker committed Jun 13, 2023
2 parents 49a976b + cd4901c commit 66fb112
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions openaq_fastapi/openaq_fastapi/v3/models/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
]




def parameter_dependency_from_model(name: str, model_cls):
"""
Takes a pydantic model class as input and creates
Expand Down Expand Up @@ -94,7 +92,6 @@ class TypeParametersMemoizer(type):
_generics_cache = weakref.WeakValueDictionary()

def __getitem__(cls, typeparams):

# prevent duplication of generic types
if typeparams in cls._generics_cache:
return cls._generics_cache[typeparams]
Expand Down Expand Up @@ -296,7 +293,6 @@ def pagination(self) -> str:


class ParametersQuery(QueryBaseModel):

parameters_id: Union[CommaSeparatedList[int], None] = Query(description="")

def where(self) -> Union[str, None]:
Expand All @@ -305,7 +301,6 @@ def where(self) -> Union[str, None]:


class MobileQuery(QueryBaseModel):

mobile: Union[bool, None] = Query(
description="Is the location considered a mobile location?"
)
Expand All @@ -316,7 +311,6 @@ def where(self) -> Union[str, None]:


class MonitorQuery(QueryBaseModel):

monitor: Union[bool, None] = Query(
description="Is the location considered a reference monitor?"
)
Expand All @@ -327,7 +321,6 @@ def where(self) -> Union[str, None]:


class ProviderQuery(QueryBaseModel):

providers_id: Union[CommaSeparatedList[int], None] = Query(
description="Limit the results to a specific provider"
)
Expand Down Expand Up @@ -374,12 +367,11 @@ def where(self) -> Union[str, None]:
return "(country->'id')::int = ANY (:countries_id)"
elif self.iso is not None:
return "country->>'code' = :iso"


class DateFromQuery(QueryBaseModel):
date_from: Optional[Union[datetime, date]] = Query(
"2022-10-01",
description="From when?"
"2022-10-01", description="From when?"
)

def where(self) -> str:
Expand All @@ -389,15 +381,14 @@ def where(self) -> str:
if self.date_from.tzinfo is None:
return "datetime > (:date_from::timestamp AT TIME ZONE tzid)"
else:
return "datetime > :date_from"
return "datetime > :date_from"
elif isinstance(self.date_from, date):
return "datetime > (:date_from::timestamp AT TIME ZONE tzid)"
return "datetime > (:date_from::timestamp AT TIME ZONE tzid)"


class DateToQuery(QueryBaseModel):
date_to: Optional[Union[datetime, date]] = Query(
datetime.utcnow(),
description="To when?"
datetime.utcnow(), description="To when?"
)

def where(self) -> str:
Expand All @@ -407,9 +398,9 @@ def where(self) -> str:
if self.date_to.tzinfo is None:
return "datetime <= (:date_to::timestamp AT TIME ZONE tzid)"
else:
return "datetime <= :date_to"
return "datetime <= :date_to"
elif isinstance(self.date_to, date):
return "datetime <= (:date_to::timestamp AT TIME ZONE tzid)"
return "datetime <= (:date_to::timestamp AT TIME ZONE tzid)"


class PeriodNames(str, Enum):
Expand All @@ -424,12 +415,10 @@ class PeriodNames(str, Enum):

class PeriodNameQuery(QueryBaseModel):
period_name: Union[PeriodNames, None] = Query(
"hour",
description="Period to aggregate. Month, day, hour"
"hour", description="Period to aggregate. Month, day, hour"
)



# Some spatial helper queries
class RadiusQuery(QueryBaseModel):
coordinates: Union[str, None] = Query(
Expand Down Expand Up @@ -480,7 +469,7 @@ def where(self, geometry_field: str = "geom"):
class BboxQuery(QueryBaseModel):
bbox: Union[str, None] = Query(
None,
regex=r"^-?\d{1,2}\.?\d{0,4},-?1?\d{1,2}\.?\d{0,4},-?\d{1,2}\.?\d{0,4},-?\d{1,2}\.?\d{0,4}$",
regex=r"^(-)?(?:180(?:\.0{1,4})?|((?:|[1-9]|1[0-7])[0-9])(?:\.[0-9]{1,4})?)\,(-)?(?:90(?:\.0{1,4})?|((?:|[1-8])[0-9])(?:\.[0-9]{1,4})?)\,(-)?(?:180(?:\.0{1,4})?|((?:|[1-9]|1[0-7])[0-9])(?:\.[0-9]{1,4})?)\,(-)?(?:90(?:\.0{1,4})?|((?:|[1-8])[0-9])(?:\.[0-9]{1,4})?)$",
description="Min X, min Y, max X, max Y, up to 4 decimal points of precision e.g. -77.037,38.907,-77.0,39.910",
example="-77.037,38.907,-77.035,38.910",
)
Expand Down

0 comments on commit 66fb112

Please sign in to comment.