""" 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 JsonResponse, HttpRequest from api.views.v1.general import AbstractModelAPIViewV1 from compensation.models import EcoAccount from intervention.models import Legal, Responsibility class APIEcoAccountViewV1(AbstractModelAPIViewV1): model = EcoAccount def get(self, request: HttpRequest, id): self.lookup["id"] = id self.lookup["users__in"] = [self.user] data = self.fetch_and_serialize() return JsonResponse(data) def model_to_json(self, entry): entry_json = { "identifier": entry.identifier, "title": entry.title, "deductable_surface": entry.deductable_surface, "deductable_surface_available": entry.deductable_surface - entry.get_deductions_surface(), "responsible": self.responsible_to_json(entry.responsible), "legal": self.legal_to_json(entry.legal), "before_states": self.compensation_state_to_json(entry.before_states.all()), "after_states": self.compensation_state_to_json(entry.after_states.all()), "actions": self.compensation_actions_to_json(entry.actions.all()), "deadlines": self.deadlines_to_json(entry.deadlines.all()), "deductions": self.deductions_to_json(entry.deductions.all()), "modified_on": self.modified_on_to_json(entry), "created_on": self.created_on_to_json(entry), } geom = entry.geometry.geom.geojson geo_json = json.loads(geom) geo_json["properties"] = entry_json return geo_json def legal_to_json(self, legal: Legal): return { "agreement_date": legal.registration_date, } def responsible_to_json(self, responsible: Responsibility): return { "conservation_office": self.konova_code_to_json(responsible.conservation_office), "conservation_file_number": responsible.conservation_file_number, "handler": responsible.handler, }