Files
eveAI/common/utils/view_assistants.py
2024-06-28 14:40:13 +02:00

50 lines
1.7 KiB
Python

from flask import flash
def prepare_table(model_objects, column_names):
"""
Converts a list of SQLAlchemy model objects into a list of dictionaries based on specified column names.
Args:
model_objects (list): List of SQLAlchemy model instances.
column_names (list): List of strings representing the column names to be included in the dictionaries.
Returns:
list: List of dictionaries where each dictionary represents a record with keys as column names.
"""
table_data = [
{col: getattr(obj, col) for col in column_names}
for obj in model_objects
]
return table_data
def prepare_table_for_macro(model_objects, column_attrs):
"""
Prepare data for rendering in a macro that expects each cell as a dictionary with class, type, and value.
Args:
model_objects (list): List of model instances or dictionaries.
column_attrs (list of tuples): Each tuple contains the attribute name and additional properties like class.
Returns:
list: A list of rows, where each row is a list of cell dictionaries.
"""
return [
[
{'value': getattr(obj, attr), 'class': cls, 'type': 'text'} # Adjust 'type' as needed
for attr, cls in column_attrs
]
for obj in model_objects
]
def form_validation_failed(request, form):
if request.method == 'POST':
for fieldName, errorMessages in form.errors.items():
for err in errorMessages:
flash(f"Error in {fieldName}: {err}", 'danger')
def form_to_dict(form):
return {field.name: field.data for field in form if field.name != 'csrf_token' and hasattr(field, 'data')}