Skip to content

Commit

Permalink
fix: typing, exceptions, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Dec 8, 2023
1 parent 52b931d commit 1fe9284
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
14 changes: 10 additions & 4 deletions zonalda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ def __call__(self, latitude: float, longitude: float):
raise MunicipalityError(
"Emplacement %s ne se trouve pas à Sainte-Adèle" % p
)
district, zone, collecte = None, None, None
districts = self.districts.loc[self.districts.contains(p)]
district = districts.iloc[0]
if len(districts) > 1:
LOGGER.warning("Plusieurs districts trouvé pour %s: %s", p, districts)
if districts:
district = districts.iloc[0]
zones = self.zonage.loc[self.zonage.contains(p)]
if len(zones) > 1:
LOGGER.warning("Plusieurs zones trouvé pour %s: %s", p, zones)
zone = zones.iloc[0]
if zones:
zone = zones.iloc[0]
collectes = self.collectes.loc[self.collectes.contains(p)]
if len(collectes) > 1:
LOGGER.warning(
"Plusieurs ones de collectes trouvé pour %s: %s", p, collectes
"Plusieurs zones de collectes trouvé pour %s: %s", p, collectes
)
collecte = collectes.iloc[0]
if collectes:
collecte = collectes.iloc[0]
return district, zone, collecte


Expand Down
30 changes: 16 additions & 14 deletions zonalda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import httpx
import shapely # type: ignore
from fastapi import FastAPI, HTTPException
from fastapi.responses import PlainTextResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, PlainTextResponse
from pydantic import BaseModel

from . import Zonalda, MunicipalityError
from . import MunicipalityError, Zonalda

LOGGER = logging.getLogger("zonalda-api")
zonalda = Zonalda()
Expand Down Expand Up @@ -45,23 +45,29 @@ class Zone(BaseModel):


class Emplacement(BaseModel):
district: District
collecte: Collecte
zone: Zone
district: District | None
collecte: Collecte | None
zone: Zone | None

@classmethod
def from_wgs84(self, latitude: float, longitude: float) -> "Emplacement":
district, zone, collecte = zonalda(latitude, longitude)
return self(
district=District(numero=district["id"], conseiller=district["Conseiller"]),
collecte=Collecte(jour=collecte["jour"], couleur=collecte["couleur"]),
district=District(numero=district["id"], conseiller=district["Conseiller"])
if district
else None,
collecte=Collecte(jour=collecte["jour"], couleur=collecte["couleur"])
if collecte
else None,
zone=Zone(
zone=zone["ZONE"],
milieu=zone["Types"],
description=zone["Descr_Type"],
# FIXME: thoroughly unnecessary JSON parsing
geometry=json.loads(shapely.to_geojson(zone["geometry"])),
),
)
if zone
else None,
)


Expand All @@ -76,7 +82,7 @@ async def ll(latitude: float, longitude: float):


GEOAPIFY_URL = "https://api.geoapify.com/v1/geocode/autocomplete"
ADDRESS_CACHE = {}
ADDRESS_CACHE: dict[str, dict] = {}
client = httpx.AsyncClient()


Expand Down Expand Up @@ -134,8 +140,4 @@ async def geoloc(
os.getenv("ORIGIN", "https://dhdaines.github.io"),
],
)
app.add_middleware(
CORSMiddleware,
allow_methods=["GET", "OPTIONS"],
**middleware_args
)
app.add_middleware(CORSMiddleware, allow_methods=["GET", "OPTIONS"], **middleware_args)

0 comments on commit 1fe9284

Please sign in to comment.