Skip to content

Commit

Permalink
Improved API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Crow committed Dec 1, 2021
1 parent 378688f commit 2ec19b3
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 7 deletions.
151 changes: 149 additions & 2 deletions francedata/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ninja import Router, Schema
from ninja import Router
from unidecode import unidecode
from typing import List
from django.shortcuts import get_object_or_404
Expand All @@ -14,9 +14,18 @@
Commune,
DataYear,
)
from francedata.models.collectivity import (
CommuneData,
DepartementData,
EpciData,
RegionData,
)

from francedata.schemas import (
DataYearSchema,
CommuneDataSchema,
DepartementDataSchema,
EpciDataSchema,
RegionDataSchema,
RegionSchema,
DepartementSchema,
EpciSchema,
Expand Down Expand Up @@ -289,3 +298,141 @@ def get_commune_by_siren(request, siren_id):
def get_commune_by_insee(request, insee_id):
item = get_object_or_404(Commune, insee=insee_id)
return item


# Region data
@router.get("/regiondata/{siren_id}", response=List[RegionDataSchema], tags=["data"])
def get_region_data(request, siren_id):
"""
Return all data for the given region
"""
queryset = RegionData.objects.filter(region__siren=siren_id)
return queryset


@router.get(
"/regiondata/{siren_id}/latest", response=List[RegionDataSchema], tags=["data"]
)
def get_latest_region_data(request, siren_id):
"""
Return the latest data for the given region
"""
max_year = RegionData.objects.latest("year__year").year
queryset = RegionData.objects.filter(region__siren=siren_id, year=max_year)
return queryset


@router.get(
"/regiondata/{siren_id}/{year}", response=List[RegionDataSchema], tags=["data"]
)
def get_region_data_by_year(request, siren_id, year):
"""
Return data for the given region and the given year
"""
queryset = RegionData.objects.filter(region__siren=siren_id, year__year=year)
return queryset


# Departement data
@router.get(
"/departementdata/{siren_id}", response=List[DepartementDataSchema], tags=["data"]
)
def get_departement_data(request, siren_id):
"""
Return all data for the given département
"""
queryset = DepartementData.objects.filter(departement__siren=siren_id)
return queryset


@router.get(
"/departementdata/{siren_id}/latest",
response=List[DepartementDataSchema],
tags=["data"],
)
def get_latest_departement_data(request, siren_id):
"""
Return the latest data for the given département
"""
max_year = DepartementData.objects.latest("year__year").year
queryset = DepartementData.objects.filter(
departement__siren=siren_id, year=max_year
)
return queryset


@router.get(
"/departementdata/{siren_id}/{year}",
response=List[DepartementDataSchema],
tags=["data"],
)
def get_departement_data_by_year(request, siren_id, year):
"""
Return data for the given département and the given year
"""
queryset = DepartementData.objects.filter(
departement__siren=siren_id, year__year=year
)
return queryset


# EPCI data
@router.get("/epcidata/{siren_id}", response=List[EpciDataSchema], tags=["data"])
def get_epci_data(request, siren_id):
"""
Return all data for the given EPCI
"""
queryset = EpciData.objects.filter(epci__siren=siren_id)
return queryset


@router.get("/epcidata/{siren_id}/latest", response=List[EpciDataSchema], tags=["data"])
def get_latest_epci_data(request, siren_id):
"""
Return latest data for the given EPCI
"""
max_year = EpciData.objects.latest("year__year").year
queryset = EpciData.objects.filter(epci__siren=siren_id, year=max_year)
return queryset


@router.get("/epcidata/{siren_id}/{year}", response=List[EpciDataSchema], tags=["data"])
def get_epci_data_by_year(request, siren_id, year):
"""
Return data for the given EPCI and the given year
"""
queryset = EpciData.objects.filter(epci__siren=siren_id, year__year=year)
return queryset


# Commune
@router.get("/communedata/{siren_id}", response=List[CommuneDataSchema], tags=["data"])
def get_commune_data(request, siren_id):
"""
Return all data for the given commune
"""
queryset = CommuneData.objects.filter(commune__siren=siren_id)
return queryset


@router.get(
"/communedata/{siren_id}/latest", response=List[CommuneDataSchema], tags=["data"]
)
def get_latest_commune_data(request, siren_id):
"""
Return the latest data for the given commune
"""
max_year = CommuneData.objects.latest("year__year").year
queryset = CommuneData.objects.filter(commune__siren=siren_id, year=max_year)
return queryset


@router.get(
"/communedata/{siren_id}/{year}", response=List[CommuneDataSchema], tags=["data"]
)
def get_commune_data_by_year(request, siren_id, year):
"""
Return data for the given commune and the given year
"""
queryset = CommuneData.objects.filter(commune__siren=siren_id, year__year=year)
return queryset
5 changes: 1 addition & 4 deletions francedata/models/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ def __str__(self):

class Meta:
verbose_name = "millésime"

@classmethod
def get_latest(cls):
return cls.objects.order_by("-year")[0]
get_latest_by = "year"
51 changes: 51 additions & 0 deletions francedata/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class DataYearSchema(Schema):
year: int


class DataSourceSchema(Schema):
title: str = None
url: str = None
year: DataYearSchema = None
public_label: str = None


class RegionSchema(Schema):
id: int
name: str = None
Expand Down Expand Up @@ -39,3 +46,47 @@ class CommuneSchema(Schema):
departement: DepartementSchema = None
population: int = None
years: List[DataYearSchema] = None


class RegionDataSchema(Schema):
id: int
year: DataYearSchema = None
datacode: str = None
value: str = None
label: str = None
datatype: str = None
source: DataSourceSchema = None
region: RegionSchema = None


class DepartementDataSchema(Schema):
id: int
year: DataYearSchema = None
datacode: str = None
value: str = None
label: str = None
datatype: str = None
source: DataSourceSchema = None
departement: DepartementSchema = None


class EpciDataSchema(Schema):
id: int
year: DataYearSchema = None
datacode: str = None
value: str = None
label: str = None
datatype: str = None
source: DataSourceSchema = None
epci: EpciSchema = None


class CommuneDataSchema(Schema):
id: int
year: DataYearSchema = None
datacode: str = None
value: str = None
label: str = None
datatype: str = None
source: DataSourceSchema = None
commune: CommuneSchema = None
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Sylvain Boissel <sylvain.boissel@dgcl.gouv.fr>"]
description = "A Django app to provide a database structure, API and import scripts to manage French communes, intercommunalités, départements and régions, with their structure and data from Insee and the DGCL."
license = "AGPL-3"
name = "django-francedata"
version = "0.14.2"
version = "0.14.3"

classifiers = [
"Development Status :: 3 - Alpha",
Expand Down

0 comments on commit 2ec19b3

Please sign in to comment.