Updated models.py to use the config!
This commit is contained in:
parent
bf0298e342
commit
1e01c4f144
@ -1,24 +1,44 @@
|
|||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from config import item_attributes # Import the configuration
|
||||||
|
from sqlalchemy import Enum, Integer, String, Date, Column
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
|
||||||
class Asset(db.Model):
|
# Dynamically create the Asset model
|
||||||
__tablename__ = "asset_test" # Table in MySQL
|
def create_asset_model():
|
||||||
|
"""
|
||||||
|
Dynamically creates the Asset model based on the configuration in item_attributes.
|
||||||
|
"""
|
||||||
|
attrs = {
|
||||||
|
'__tablename__': 'asset_test', # Table name
|
||||||
|
'__init__': lambda self, **kwargs: self.__dict__.update(kwargs), # Constructor
|
||||||
|
'__repr__': lambda self: f"<Asset {getattr(self, next(attrib for attrib, config in item_attributes.items() if config.primary))}>" # Representation
|
||||||
|
}
|
||||||
|
|
||||||
assettag = db.Column(db.String(50), primary_key=True) # Primary key
|
for attrib, config in item_attributes.items():
|
||||||
hostname = db.Column(db.String(50)) # VARCHAR(50)
|
# Determine the column type based on the configuration
|
||||||
warrantyfrom = db.Column(db.Date) # DATE
|
if config.html_input_type == "text":
|
||||||
status = db.Column(db.Enum('Active', 'Inactive')) # ENUM('Active', 'Inactive')
|
column_type = String(50)
|
||||||
#staffnum = db.Column(db.Integer, unsigned=True) # INT(8) UNSIGNED
|
elif config.html_input_type == "date":
|
||||||
staffnum = db.Column(db.Integer) # INT(8) UNSIGNED
|
column_type = Date
|
||||||
|
elif config.html_input_type == "select":
|
||||||
|
column_type = Enum(*config.options)
|
||||||
|
elif config.html_input_type == "number":
|
||||||
|
column_type = Integer
|
||||||
|
|
||||||
def __init__(self, assettag, hostname=None, warrantyfrom=None, status=None, staffnum=None):
|
# Define the column with additional properties
|
||||||
self.assettag = assettag
|
attrs[attrib] = Column(
|
||||||
self.hostname = hostname
|
column_type,
|
||||||
self.warrantyfrom = warrantyfrom
|
primary_key=config.primary,
|
||||||
self.status = status
|
unique=config.unique,
|
||||||
self.staffnum = staffnum
|
nullable=not config.required,
|
||||||
|
default=config.default_val,
|
||||||
|
comment=config.comment
|
||||||
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
# Create the Asset class dynamically
|
||||||
return f"<AssetTest {self.assettag}: {self.hostname}>"
|
Asset = type('Asset', (db.Model,), attrs)
|
||||||
|
return Asset
|
||||||
|
|
||||||
|
# Create the Asset model
|
||||||
|
Asset = create_asset_model()
|
@ -1,6 +1,7 @@
|
|||||||
from flask import Blueprint, request, render_template, redirect
|
from flask import Blueprint, request, render_template, redirect
|
||||||
from definitions.models import Asset, db
|
from definitions.models import Asset, db
|
||||||
from config import item_attributes
|
from config import item_attributes
|
||||||
|
from sqlalchemy import exc # Import exc for database exceptions
|
||||||
|
|
||||||
addasset_bp = Blueprint('addasset', __name__)
|
addasset_bp = Blueprint('addasset', __name__)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user