mpeltriaux
beb67ba307
* outsources json creation of modified_on an created_on to superclass * adds API support for fetching ecoaccount data
115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
"""
|
|
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 list(qs.values("amount", "due_on", "comment"))
|
|
|
|
def deductions_to_json(self, qs: QuerySet):
|
|
""" Serializes eco account deductions into json
|
|
|
|
Args:
|
|
qs (QuerySet): A queryset of EcoAccountDeduction entries
|
|
|
|
Returns:
|
|
|
|
"""
|
|
return [
|
|
{
|
|
"id": entry.pk,
|
|
"eco_account": {
|
|
"id": entry.account.pk,
|
|
"identifier": entry.account.identifier,
|
|
"title": entry.account.title,
|
|
},
|
|
"surface": entry.surface,
|
|
"intervention": {
|
|
"id": entry.intervention.pk,
|
|
"identifier": entry.intervention.identifier,
|
|
"title": entry.intervention.title,
|
|
}
|
|
}
|
|
for entry in qs
|
|
]
|
|
|
|
def compensation_state_to_json(self, qs: QuerySet):
|
|
return [
|
|
{
|
|
"biotope": self.konova_code_to_json(entry.biotope_type),
|
|
"surface": entry.surface,
|
|
}
|
|
for entry in qs
|
|
]
|
|
|
|
def compensation_actions_to_json(self, qs: QuerySet):
|
|
return [
|
|
{
|
|
"action": self.konova_code_to_json(entry.action_type),
|
|
"amount": entry.amount,
|
|
"unit": entry.unit,
|
|
"comment": entry.comment,
|
|
}
|
|
for entry in qs
|
|
]
|
|
|
|
def deadlines_to_json(self, qs: QuerySet):
|
|
return list(qs.values(
|
|
"type",
|
|
"date",
|
|
"comment",
|
|
))
|
|
|
|
def created_on_to_json(self, entry):
|
|
return entry.created.timestamp
|
|
|
|
def modified_on_to_json(self, entry):
|
|
modified_on = entry.modified or entry.created
|
|
modified_on = modified_on.timestamp
|
|
return modified_on |