Source code for viiapackage.reporting.helper_functions.viia_get_bevindingen_report_data

### ===================================================================================================================
###   FUNCTION: Get data for the Bevindingen report
### ===================================================================================================================
# Copyright ©VIIA 2024

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

# General imports
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, List
from datetime import datetime

# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
    from viiapackage.viiaStatus import ViiaProject
from viiapackage.database import myviia_get_object_deel


### ===================================================================================================================
###   2. Function to collect the data required for the Bevindingen report
### ===================================================================================================================

[docs]def get_bevindingen_report_data(project: ViiaProject, extra_object_parts: Optional[List[str]] = None) -> dict: """ This function will generate dictionary keys for Bevindingen report data that can later be used as jinja tags for templating. Input - project (obj): VIIA project object containing collections of fem objects and project variables. - extra_object_parts (list of str): List of names of extra object parts to include in the report. Default value is None. Output - Dictionary of jinja tags assigned to corresponding info. """ inspection_info = None if 'test-' != project.name[:5]: # Obtain object part opname objectpart_id = None for objectpart in project.project_information['objectdelen']: if objectpart['naam'] == project.project_information['objectdeel']: objectpart_id = objectpart['id'] break if not objectpart_id: raise ValueError("ERROR: Could not find the correct objectpart.") object_part_details = myviia_get_object_deel(object_deel_id=objectpart_id, token=project.token) project.project_information['object_deel_bevindingen'] = object_part_details['object_deel_bevindingen'] project.project_information['bevindingen_images_folder'] = 'bevindingen_images' bevindingen_info = project.project_information.get('object_deel_bevindingen', None) if not bevindingen_info: project.write_log( f"WARNING: No bevindingen information is retrieved from database for this object. Bevindingen information is " f"not replaced in the bevindingen report.") context = {} if bevindingen_info: # Remove the ID of the database key_list = [x for x in bevindingen_info.keys() if x != 'id'] # All keys will have an entry (might be an empty string) for key in key_list: val = bevindingen_info.get(key) or '' # If the data on MYVIIA is a list it will be converted to a string comma separated, except for the last # element which will be added after an 'en'. All input is lowercase except for first letter of list. if isinstance(val, list): if len(val) > 1: val = ', '.join([v.lower() for v in val]).capitalize() k = val.rfind(', ') val = f'{val[:k]} en {val[k + 2:]}.' else: val = val[0].capitalize() + '.' # Other cases the first letter only will be capitalised elif isinstance(val, str) and val != '': if len(val) == 1: val = val[0].upper() else: val = val[0].upper() + val[1:] context[key] = val # Group info from a set of questions together grouped_questions = [1, 2, 3, 4, 6] for question in grouped_questions: context[f'vraag_{question}_sub'] = [[key.split('_')[-1], context[f'{key}_toelichting'], f'{key}', context[f'{key}_bestand']] for key in context.keys() if f'vraag_{question}_' in key and 'toelichting' not in key and 'bestand' not in key] return context
### =================================================================================================================== ### 3. End of script ### ===================================================================================================================