Source code for viiapackage.reference_approach.viia_extend_data_reference_objects

### ===================================================================================================================
###   Extend object with data from MYVIIA for the reference object
### ===================================================================================================================
# Copyright ©VIIA 2024

### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================

# General imports
from typing import Optional
from pathlib import Path
from PIL.Image import open, Image
from PIL import UnidentifiedImageError

# References for functions and classes in the viiaPackage
from viiapackage.database import myviia_get_eng_measure
from viiapackage.reference_approach.viia_convert_data_eng_database import MYVIIAEngDatabase
from viiapackage.database.viia_myviia_get_info import myviia_get_object_image
from viiapackage.database import myviia_get_cost_estimation


### ===================================================================================================================
###   2. Helper functions
### ===================================================================================================================

def _get_picture(image_type: str, object_deel_id: int, token: str, images_folder: Path = None) -> Optional[Image]:
    """
    Helper function to get object part image.

    Input:
        - image_type (str): Select the image to collect. Currently, API is available for 'building', 'opname' and
          'engineering'.
        - object_deel_id (int): ID of the object-part in the MYVIIA webtool.
        - token (str): Bearer token for authentication. Use myviia_login function to collect in case of multiple
        - images_folder (path): Path where to save the downloaded image. If no path is given the picture will not be
          saved.

    Output:
        - The picture is returned as an object.
        - If a folder path was given the picture is saved in the path.
    """
    return myviia_get_object_image(
        image=image_type, object_deel_id=object_deel_id, name=f'{object_deel_id}_{image_type}', token=token,
        images_folder=images_folder)


### ===================================================================================================================
###   3. Function to extend the reference object with data from MYVIIA
### ===================================================================================================================

[docs]def viia_extend_reference_object( reference_object: MYVIIAEngDatabase, token: Optional[str] = None) -> MYVIIAEngDatabase: """ This function will extend the information of the reference object with additional queries on MYVIIA. Currently the data is extended for the information on the strengthening measures applied. Input: - reference_object (obj): Object reference of the MYVIIAEngDatabase class. Output: - The instance is extended with data from MYVIIA. """ # Collect information from MYVIIA for the applied strengthening measures object_measures = myviia_get_eng_measure(object_deel_id=reference_object.objectpart_id, token=token) # Convert data of strengthening measures to list of measures reference_object.measures = [measures['name'] for measures in object_measures if object_measures != []] # Collect information from MYVIIA on the cost estimation object_cost = myviia_get_cost_estimation(object_deel_id=reference_object.objectpart_id, token=token) # Collect the total based on most recent if object_cost: cost_report = sorted(object_cost, key=lambda d: d['id'])[-1] total_cost = 0 for key in [ 'abk', 'ak_wr', 'bijkomende_werkzaamheden', 'btw', 'car_wa', 'diverse_kosten', 'honoraria', 'installaties', 'inventaris_huisvesting', 'onvoorzien', 'schoonmaken', 'stelposten', 'uitwerking', 'versterkingsmaatregelen']: if cost_report[key] is not None: total_cost += cost_report[key] reference_object.total_cost = total_cost # Collect analysis images from MYVIIA # TEMPORARY NOT AVAILABLE DUE TO MIGRATION IMAGES MYVIIA object_deel_id = reference_object.objectpart_id reference_object.model_image = _get_picture('model', object_deel_id, token) reference_object.wall_image = _get_picture('wanden', object_deel_id, token) reference_object.floor_image = _get_picture('vloeren', object_deel_id, token) reference_object.roof_image = _get_picture('daken', object_deel_id, token) # Return the object with additional data return reference_object
### =================================================================================================================== ### 4. End of script ### ===================================================================================================================