#31 API PUT/POST EcoAccount
* adds support for PUT and POST of EcoAccount API
This commit is contained in:
		
							parent
							
								
									047e4fb5cd
								
							
						
					
					
						commit
						096688fad5
					
				@ -5,10 +5,15 @@ Contact: michel.peltriaux@sgdnord.rlp.de
 | 
			
		||||
Created on: 24.01.22
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
from django.db import transaction
 | 
			
		||||
 | 
			
		||||
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, \
 | 
			
		||||
    LegalAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin, DeductableAPISerializerV1Mixin
 | 
			
		||||
from compensation.models import EcoAccount
 | 
			
		||||
from intervention.models import Legal, Responsibility
 | 
			
		||||
from konova.models import Geometry
 | 
			
		||||
from konova.tasks import celery_update_parcels
 | 
			
		||||
from user.models import UserActionLogEntry
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
 | 
			
		||||
@ -39,4 +44,115 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
 | 
			
		||||
            "conservation_office": self.konova_code_to_json(responsible.conservation_office),
 | 
			
		||||
            "conservation_file_number": responsible.conservation_file_number,
 | 
			
		||||
            "handler": responsible.handler,
 | 
			
		||||
        }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def set_legal(self, obj, legal_data):
 | 
			
		||||
        obj.legal.registration_date = legal_data.get("agreement_date", None)
 | 
			
		||||
        return obj
 | 
			
		||||
 | 
			
		||||
    def initialize_objects(self, json_model, user):
 | 
			
		||||
        """ Initializes all needed objects from the json_model data
 | 
			
		||||
 | 
			
		||||
        Does not persist data to the DB!
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            json_model (dict): The json data
 | 
			
		||||
            user (User): The API user
 | 
			
		||||
 | 
			
		||||
        Returns:
 | 
			
		||||
            obj (Compensation)
 | 
			
		||||
        """
 | 
			
		||||
        create_action = UserActionLogEntry.get_created_action(user, comment="API Import")
 | 
			
		||||
        # Create geometry
 | 
			
		||||
        json_geom = self.create_geometry_from_json(json_model)
 | 
			
		||||
        geometry = Geometry()
 | 
			
		||||
        geometry.geom = json_geom
 | 
			
		||||
        geometry.created = create_action
 | 
			
		||||
 | 
			
		||||
        # Create linked objects
 | 
			
		||||
        obj = EcoAccount()
 | 
			
		||||
        obj.responsible = Responsibility()
 | 
			
		||||
        obj.legal = Legal()
 | 
			
		||||
        created = create_action
 | 
			
		||||
        obj.created = created
 | 
			
		||||
        obj.geometry = geometry
 | 
			
		||||
        return obj
 | 
			
		||||
 | 
			
		||||
    def create_model_from_json(self, json_model, user):
 | 
			
		||||
        """ Creates a new entry for the model based on the contents of json_model
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            json_model (dict): The json containing data
 | 
			
		||||
            user (User): The API user
 | 
			
		||||
 | 
			
		||||
        Returns:
 | 
			
		||||
            created_id (str): The id of the newly created EcoAccount entry
 | 
			
		||||
        """
 | 
			
		||||
        with transaction.atomic():
 | 
			
		||||
            obj = self.initialize_objects(json_model, user)
 | 
			
		||||
 | 
			
		||||
            # Fill in data to objects
 | 
			
		||||
            properties = json_model["properties"]
 | 
			
		||||
            obj.identifier = obj.generate_new_identifier()
 | 
			
		||||
            obj.title = properties["title"]
 | 
			
		||||
            obj.deductable_surface = float(properties["deductable_surface"])
 | 
			
		||||
            obj = self.set_responsibility(obj, properties["responsible"])
 | 
			
		||||
            obj = self.set_legal(obj, properties["legal"])
 | 
			
		||||
 | 
			
		||||
            obj.geometry.save()
 | 
			
		||||
            obj.responsible.save()
 | 
			
		||||
            obj.legal.save()
 | 
			
		||||
            obj.save()
 | 
			
		||||
 | 
			
		||||
            obj = self.set_compensation_actions(obj, properties["actions"])
 | 
			
		||||
            obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states)
 | 
			
		||||
            obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states)
 | 
			
		||||
            obj = self.set_deadlines(obj, properties["deadlines"])
 | 
			
		||||
 | 
			
		||||
            obj.log.add(obj.created)
 | 
			
		||||
            obj.users.add(user)
 | 
			
		||||
 | 
			
		||||
            celery_update_parcels.delay(obj.geometry.id)
 | 
			
		||||
 | 
			
		||||
            return obj.id
 | 
			
		||||
 | 
			
		||||
    def update_model_from_json(self, id, json_model, user):
 | 
			
		||||
        """ Updates an entry for the model based on the contents of json_model
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            id (str): The object's id
 | 
			
		||||
            json_model (dict): The json containing data
 | 
			
		||||
            user (User): The API user
 | 
			
		||||
 | 
			
		||||
        Returns:
 | 
			
		||||
            created_id (str): The id of the newly created EcoAccount entry
 | 
			
		||||
        """
 | 
			
		||||
        with transaction.atomic():
 | 
			
		||||
            update_action = UserActionLogEntry.get_edited_action(user, "API update")
 | 
			
		||||
            obj = self.get_obj_from_db(id, user)
 | 
			
		||||
 | 
			
		||||
            # Fill in data to objects
 | 
			
		||||
            properties = json_model["properties"]
 | 
			
		||||
            obj.title = properties["title"]
 | 
			
		||||
            obj.deductable_surface = float(properties["deductable_surface"])
 | 
			
		||||
            obj.modified = update_action
 | 
			
		||||
            obj.geometry.geom = self.create_geometry_from_json(json_model)
 | 
			
		||||
            obj.geometry.modified = update_action
 | 
			
		||||
            obj = self.set_responsibility(obj, properties["responsible"])
 | 
			
		||||
            obj = self.set_legal(obj, properties["legal"])
 | 
			
		||||
 | 
			
		||||
            obj.geometry.save()
 | 
			
		||||
            obj.responsible.save()
 | 
			
		||||
            obj.legal.save()
 | 
			
		||||
            obj.save()
 | 
			
		||||
 | 
			
		||||
            obj = self.set_compensation_actions(obj, properties["actions"])
 | 
			
		||||
            obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states)
 | 
			
		||||
            obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states)
 | 
			
		||||
            obj = self.set_deadlines(obj, properties["deadlines"])
 | 
			
		||||
 | 
			
		||||
            obj.log.add(update_action)
 | 
			
		||||
 | 
			
		||||
            celery_update_parcels.delay(obj.geometry.id)
 | 
			
		||||
 | 
			
		||||
            return obj.id
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user