#31 API basic implementation
* adds new app to project * adds relation between User model and new APIUserToken model * adds first implementation for GET of intervention * adds basic code layout for future extension by having new versions
This commit is contained in:
8
api/views/__init__.py
Normal file
8
api/views/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from .v1 import *
|
||||
7
api/views/v1/__init__.py
Normal file
7
api/views/v1/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
77
api/views/v1/general.py
Normal file
77
api/views/v1/general.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from api.views.views import AbstractModelAPIView
|
||||
from codelist.models import KonovaCode
|
||||
from intervention.models import Responsibility, Legal
|
||||
|
||||
|
||||
class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
""" Holds general serialization functions for API v1
|
||||
|
||||
"""
|
||||
|
||||
def konova_code_to_json(self, konova_code: KonovaCode):
|
||||
return {
|
||||
"atom_id": konova_code.atom_id,
|
||||
"long_name": konova_code.long_name,
|
||||
"short_name": konova_code.short_name,
|
||||
}
|
||||
|
||||
def responsible_to_json(self, responsible: Responsibility):
|
||||
return {
|
||||
"registration_office": self.konova_code_to_json(responsible.registration_office),
|
||||
"registration_file_number": responsible.registration_file_number,
|
||||
"conservation_office": self.konova_code_to_json(responsible.conservation_office),
|
||||
"conservation_file_number": responsible.conservation_file_number,
|
||||
"handler": responsible.handler,
|
||||
}
|
||||
|
||||
def legal_to_json(self, legal: Legal):
|
||||
return {
|
||||
"registration_date": legal.registration_date,
|
||||
"binding_date": legal.binding_date,
|
||||
"process_type": self.konova_code_to_json(legal.process_type),
|
||||
"laws": [self.konova_code_to_json(law) for law in legal.laws.all()],
|
||||
}
|
||||
|
||||
def payments_to_json(self, qs: QuerySet):
|
||||
""" Serializes payments into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of Payment entries
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"amount": entry.amount,
|
||||
"due_on": entry.due_on,
|
||||
"comment": entry.comment,
|
||||
}
|
||||
for entry in qs
|
||||
]
|
||||
|
||||
def deductions_to_json(self, qs: QuerySet):
|
||||
""" Serializes eco account deductions into json
|
||||
|
||||
Args:
|
||||
qs (QuerySet): A queryset of EcoAccountDeduction entries
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"eco_account": entry.account.pk,
|
||||
"surface": entry.surface,
|
||||
}
|
||||
for entry in qs
|
||||
]
|
||||
40
api/views/v1/intervention.py
Normal file
40
api/views/v1/intervention.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
import json
|
||||
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
|
||||
from api.views.v1.general import AbstractModelAPIViewV1
|
||||
from intervention.models import Intervention
|
||||
|
||||
|
||||
class APIInterventionView(AbstractModelAPIViewV1):
|
||||
model = Intervention
|
||||
fields_to_serialize = {
|
||||
"identifier",
|
||||
"title",
|
||||
}
|
||||
|
||||
def get(self, request: HttpRequest, identifier):
|
||||
data = self.fetch_and_serialize("identifier", identifier)
|
||||
return JsonResponse(data)
|
||||
|
||||
def model_to_json(self, entry: Intervention):
|
||||
entry_json = {
|
||||
"identifier": entry.identifier,
|
||||
"title": entry.title,
|
||||
"responsible": self.responsible_to_json(entry.responsible),
|
||||
"legal": self.legal_to_json(entry.legal),
|
||||
"compensations": list(entry.compensations.all().values_list("pk", flat=True)),
|
||||
"payments": self.payments_to_json(entry.payments.all()),
|
||||
"deductions": self.deductions_to_json(entry.deductions.all()),
|
||||
}
|
||||
geom = entry.geometry.geom.geojson
|
||||
geo_json = json.loads(geom)
|
||||
geo_json["properties"] = entry_json
|
||||
return geo_json
|
||||
55
api/views/views.py
Normal file
55
api/views/views.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 21.01.22
|
||||
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
|
||||
from django.views import View
|
||||
|
||||
|
||||
class AbstractModelAPIView(View):
|
||||
""" Base class for API views
|
||||
|
||||
The API must follow the GeoJSON Specification RFC 7946
|
||||
https://geojson.org/
|
||||
https://datatracker.ietf.org/doc/html/rfc7946
|
||||
|
||||
"""
|
||||
model = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@abstractmethod
|
||||
def model_to_json(self, entry):
|
||||
""" Defines the returned json values of the model
|
||||
|
||||
Args:
|
||||
entry (): The found entry from the database
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
raise NotImplementedError("Must be implemented in subclasses")
|
||||
|
||||
def fetch_and_serialize(self, lookup_field, lookup_val):
|
||||
""" Serializes the model entry according to the given lookup data
|
||||
|
||||
Args:
|
||||
lookup_field (): Which field used for lookup
|
||||
lookup_val (): Value for lookup
|
||||
|
||||
Returns:
|
||||
serialized_data (dict)
|
||||
"""
|
||||
_filters = {
|
||||
lookup_field: lookup_val
|
||||
}
|
||||
qs = self.model.objects.filter(**_filters)
|
||||
serialized_data = {}
|
||||
for entry in qs:
|
||||
serialized_data[str(entry.pk)] = self.model_to_json(entry)
|
||||
return serialized_data
|
||||
Reference in New Issue
Block a user