update
This commit is contained in:
parent
0b92875c00
commit
2631c549a5
@ -102,6 +102,7 @@ except Exception as e:
|
|||||||
|
|
||||||
skipped_models_cache = set() # Cache to store skipped models
|
skipped_models_cache = set() # Cache to store skipped models
|
||||||
|
|
||||||
|
|
||||||
def fetch_related_fields_for_model(model, base_model):
|
def fetch_related_fields_for_model(model, base_model):
|
||||||
"""Fetch related fields for a single model with caching for skipped models."""
|
"""Fetch related fields for a single model with caching for skipped models."""
|
||||||
if model in skipped_models_cache:
|
if model in skipped_models_cache:
|
||||||
@ -117,11 +118,16 @@ def fetch_related_fields_for_model(model, base_model):
|
|||||||
and field_info.get("relation") == base_model
|
and field_info.get("relation") == base_model
|
||||||
):
|
):
|
||||||
related_fields.setdefault(model, []).append(field_name)
|
related_fields.setdefault(model, []).append(field_name)
|
||||||
|
except odoorpc.error.RPCError as e:
|
||||||
|
color_log.Show(FAIL, f"Access denied for model {model}: {e}")
|
||||||
|
skipped_models_cache.add(model) # Add to cache if access is denied
|
||||||
|
return {}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
color_log.Show(NOTICE, f"Skipping model {model}: {e}")
|
color_log.Show(NOTICE, f"Skipping model {model}: {e}")
|
||||||
skipped_models_cache.add(model) # Add to cache if an error occurs
|
skipped_models_cache.add(model) # Add to cache for any other errors
|
||||||
return related_fields
|
return related_fields
|
||||||
|
|
||||||
|
|
||||||
# Function to fetch related models
|
# Function to fetch related models
|
||||||
def get_related_fields(db_name, base_model):
|
def get_related_fields(db_name, base_model):
|
||||||
"""Fetch related fields for a given model using multiprocessing."""
|
"""Fetch related fields for a given model using multiprocessing."""
|
||||||
@ -166,7 +172,7 @@ def get_related_fields(db_name, base_model):
|
|||||||
def delete_records_cascade(
|
def delete_records_cascade(
|
||||||
record_ids, model_name, process_count, related_models, db_name
|
record_ids, model_name, process_count, related_models, db_name
|
||||||
):
|
):
|
||||||
"""Iteratively delete related records before deleting the base records"""
|
"""Iteratively delete related records before deleting the base records."""
|
||||||
# Stack for traversal: list of (model_name, record_ids) tuples
|
# Stack for traversal: list of (model_name, record_ids) tuples
|
||||||
stack = [(model_name, record_ids)]
|
stack = [(model_name, record_ids)]
|
||||||
# Track visited records to avoid cycles: set of (model_name, record_id) tuples
|
# Track visited records to avoid cycles: set of (model_name, record_id) tuples
|
||||||
@ -186,6 +192,10 @@ def delete_records_cascade(
|
|||||||
for related_model, fields in get_related_fields(
|
for related_model, fields in get_related_fields(
|
||||||
db_name, current_model
|
db_name, current_model
|
||||||
).items():
|
).items():
|
||||||
|
if related_model in skipped_models_cache:
|
||||||
|
color_log.Show(NOTICE, f"Skipping cached model {related_model}")
|
||||||
|
continue
|
||||||
|
|
||||||
related_model_obj = odoo.env[related_model]
|
related_model_obj = odoo.env[related_model]
|
||||||
for field in fields:
|
for field in fields:
|
||||||
try:
|
try:
|
||||||
@ -196,15 +206,22 @@ def delete_records_cascade(
|
|||||||
stack.append((related_model, related_ids))
|
stack.append((related_model, related_ids))
|
||||||
has_dependents = True
|
has_dependents = True
|
||||||
color_log.Show(
|
color_log.Show(
|
||||||
3,
|
NOTICE,
|
||||||
f"{process_count}: Found {len(related_ids)} related records in {related_model} ({field})",
|
f"{process_count}: Found {len(related_ids)} related records in {related_model} ({field})",
|
||||||
)
|
)
|
||||||
process_count += 1
|
process_count += 1
|
||||||
except odoorpc.error.RPCError as e:
|
except odoorpc.error.RPCError as e:
|
||||||
color_log.Show(
|
color_log.Show(
|
||||||
1, f"Access denied for model {related_model}: {e}"
|
FAIL, f"Access denied for model {related_model}: {e}"
|
||||||
)
|
)
|
||||||
continue
|
skipped_models_cache.add(related_model) # Add to cache
|
||||||
|
break # Skip further processing for this model
|
||||||
|
except Exception as e:
|
||||||
|
color_log.Show(
|
||||||
|
NOTICE, f"Skipping model {related_model} due to error: {e}"
|
||||||
|
)
|
||||||
|
skipped_models_cache.add(related_model) # Add to cache
|
||||||
|
break # Skip further processing for this model
|
||||||
|
|
||||||
if not has_dependents:
|
if not has_dependents:
|
||||||
deletion_order.append((current_model, current_ids))
|
deletion_order.append((current_model, current_ids))
|
||||||
@ -223,7 +240,7 @@ def delete_records_cascade(
|
|||||||
# Now delete the main records after all dependencies are gone
|
# Now delete the main records after all dependencies are gone
|
||||||
try:
|
try:
|
||||||
if record_ids:
|
if record_ids:
|
||||||
odoo.env.browse(record_ids).unlink()
|
odoo.env[model_name].browse(record_ids).unlink()
|
||||||
color_log.Show(OK, f"Deleted {len(record_ids)} records from {model_name}")
|
color_log.Show(OK, f"Deleted {len(record_ids)} records from {model_name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
color_log.Show(FAIL, f"Error deleting {model_name} records: {e}")
|
color_log.Show(FAIL, f"Error deleting {model_name} records: {e}")
|
||||||
|
Loading…
Reference in New Issue
Block a user