baserow-ifto/matrix_builder.py

80 lines
4.2 KiB
Python
Raw Normal View History

2023-09-05 00:57:29 +02:00
from data_processor import DataProcessor
2023-09-05 02:31:01 +02:00
2023-09-05 00:57:29 +02:00
class MatrixBuilder:
2023-09-05 02:36:14 +02:00
def __init__(self, data_processor, tables_data, verbose=False):
2023-09-05 00:57:29 +02:00
self.data_processor = data_processor
2023-09-05 02:36:14 +02:00
self.tables_data = tables_data
2023-09-05 02:44:23 +02:00
self.lazy_loaded_tables_data = {} # Neue Arbeitsvariable
2023-09-05 00:57:29 +02:00
self.verbose = verbose
2023-09-05 02:44:23 +02:00
self.matrix = {} # Klassenvariable
2023-09-05 00:57:29 +02:00
2023-09-05 02:36:14 +02:00
def build_multitable_matrix(self):
for table_name, table_rows in self.tables_data.items():
2023-09-05 02:31:01 +02:00
self.print_verbose_message(f"Building matrix for table: {table_name}")
2023-09-05 02:44:23 +02:00
self.matrix[table_name] = self._build_matrix_for_table(table_name, table_rows.copy())
return self.matrix
2023-09-05 02:24:11 +02:00
2023-09-05 02:36:14 +02:00
def _build_matrix_for_table(self, table_name, table_rows, reference_map={}):
2023-09-05 02:31:01 +02:00
self.print_verbose_message(f"Starting matrix build for table: {table_name}")
2023-09-05 02:44:23 +02:00
self.matrix[table_name] = table_rows
2023-09-05 02:31:01 +02:00
reference_map = reference_map.copy()
2023-09-05 02:36:14 +02:00
self._process_link_fields(table_name, reference_map)
2023-09-05 02:44:23 +02:00
self._populate_matrix_with_related_content(table_name, table_rows, reference_map)
return self.matrix[table_name]
2023-09-05 00:57:29 +02:00
2023-09-05 02:44:23 +02:00
def _populate_matrix_with_related_content(self, table_name, table_rows, reference_map):
2023-09-05 00:57:29 +02:00
for table_row in table_rows:
2023-09-05 02:31:01 +02:00
for column_name, cell_content in table_row.items():
if column_name in reference_map:
self.print_verbose_message(f"Handling linked content for column: {column_name}")
2023-09-05 02:44:23 +02:00
self._handle_linked_content(table_row, column_name, cell_content, reference_map)
2023-09-05 02:31:01 +02:00
2023-09-05 02:44:23 +02:00
def _handle_linked_content(self, table_row, column_name, cell_content, reference_map):
2023-09-05 02:31:01 +02:00
link_table_id = reference_map[column_name]["link_row_table_id"]
link_field_id = reference_map[column_name]["link_row_related_field_id"]
if not link_field_id:
raise Exception("link_row_related_field_id has to be a positive number")
self.print_verbose_message(f"Fetching related content for table ID: {link_table_id} and field ID: {link_field_id}")
2023-09-05 02:44:23 +02:00
new_content = self._fetch_related_content(cell_content, link_table_id, link_field_id, reference_map)
2023-09-05 02:31:01 +02:00
table_row[column_name] = new_content
2023-09-05 02:44:23 +02:00
def _fetch_related_content(self, cell_content, link_table_id, link_field_id, reference_map):
2023-09-05 02:31:01 +02:00
new_content = []
for entry in cell_content:
related_cell_id = self._generate_related_cell_identifier(link_table_id, link_field_id, entry["id"])
self.print_verbose_message(f"Generated related cell identifier: {related_cell_id}")
if related_cell_id not in reference_map.get("embeded", []):
reference_map.setdefault("embeded", []).append(related_cell_id)
2023-09-05 02:44:23 +02:00
self.matrix[link_table_id] = self._build_matrix_for_table(link_table_id, self.lazy_loaded_tables_data.get(link_table_id, []).copy(), reference_map)
2023-09-05 02:31:01 +02:00
new_content.append(entry)
return new_content
def _generate_related_cell_identifier(self, table_id, column_id, row_id):
return f"table_{table_id}_field_{column_id}_row_{row_id}"
2023-09-05 02:36:14 +02:00
def _process_link_fields(self, table_name, reference_map):
2023-09-05 00:57:29 +02:00
link_fields = self.data_processor.get_link_fields_for_table(table_name)
for link_field in link_fields:
2023-09-05 02:31:01 +02:00
self.print_verbose_message(f"Processing link fields for table: {table_name}")
2023-09-05 02:36:14 +02:00
self._load_table_data_if_missing(link_field)
2023-09-05 02:31:01 +02:00
self._update_reference_map(link_field, reference_map)
2023-09-05 02:36:14 +02:00
def _load_table_data_if_missing(self, link_field):
2023-09-05 02:31:01 +02:00
link_table_id = link_field["link_row_table_id"]
2023-09-05 02:44:23 +02:00
if link_table_id not in self.lazy_loaded_tables_data:
2023-09-05 02:31:01 +02:00
self.print_verbose_message(f"Loading data for missing table ID: {link_table_id}")
2023-09-05 02:44:23 +02:00
self.lazy_loaded_tables_data[link_table_id] = self.data_processor.get_all_rows_from_table(link_table_id)
2023-09-05 02:31:01 +02:00
def _update_reference_map(self, link_field, reference_map):
field_name = f"field_{link_field['id']}"
if field_name not in reference_map:
self.print_verbose_message(f"Updating reference map for field: {field_name}")
reference_map[field_name] = link_field
reference_map[field_name]["embeded"] = []
2023-09-05 00:57:29 +02:00
2023-09-05 02:31:01 +02:00
def print_verbose_message(self, message):
if self.verbose:
print(message)