from flask_sqlalchemy import SQLAlchemy from config import item_attributes # Import the configuration from sqlalchemy import Enum, Integer, String, Date, Column from config import sql_conf db = SQLAlchemy() # Dynamically create the Asset model def create_asset_model(): """ Dynamically creates the Asset model based on the configuration in item_attributes. """ attrs = { '__tablename__': sql_conf.SQL_TABLE, # Table name '__init__': lambda self, **kwargs: self.__dict__.update(kwargs), # Constructor '__repr__': lambda self: f"" # Representation } for attrib in item_attributes: # Determine the column type based on the configuration if attrib.html_input_type == "text": column_type = String(50) elif attrib.html_input_type == "date": column_type = Date elif attrib.html_input_type == "select": column_type = Enum(*attrib.options) elif attrib.html_input_type == "number": column_type = Integer # Define the column with additional properties attrs[attrib.attrib_name] = Column( column_type, primary_key=attrib.primary, unique=attrib.unique, nullable=not attrib.required, default=attrib.default_val, #title=attrib.title ) # Create the Asset class dynamically Asset = type('Asset', (db.Model,), attrs) return Asset # Create the Asset model Asset = create_asset_model()