Moved SQL config to config file
This commit is contained in:
		
							parent
							
								
									871b455d5e
								
							
						
					
					
						commit
						487e0fc720
					
				
							
								
								
									
										4
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								app.py
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | |||||||
| # Validate configuration before starting the app | # Validate configuration before starting the app | ||||||
| from functions.validate_config import validate_config | from functions.validate_config import validate_config | ||||||
| from config import item_attributes | from config import item_attributes, sql_conf | ||||||
| 
 | 
 | ||||||
| config_status = validate_config(item_attributes) | config_status = validate_config(item_attributes) | ||||||
| if config_status != "Ok": | if config_status != "Ok": | ||||||
| @ -23,7 +23,7 @@ from routes.confirm_save import confirm_save_bp | |||||||
| 
 | 
 | ||||||
| app = Flask(__name__) | app = Flask(__name__) | ||||||
| app.secret_key = 'your_secret_key'  # Required for flashing messages and session | app.secret_key = 'your_secret_key'  # Required for flashing messages and session | ||||||
| app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://assetadmin:1234@localhost/asset_test_db' | app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql://{sql_conf.SQL_USER}:{sql_conf.SQL_PASSWORD}@{sql_conf.SQL_HOST}/{sql_conf.SQL_DB}' | ||||||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||||||
| db.init_app(app) | db.init_app(app) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,13 @@ | |||||||
| from definitions.attribute import Attribute | from definitions.attribute import Attribute | ||||||
| 
 | 
 | ||||||
|  | # MySQL information | ||||||
|  | class sql_conf: | ||||||
|  |     SQL_USER = "assetadmin" | ||||||
|  |     SQL_PASSWORD = "1234" | ||||||
|  |     SQL_HOST = "localhost" | ||||||
|  |     SQL_DB = "asset_test_db" | ||||||
|  |     SQL_TABLE = "asset_test" | ||||||
|  | 
 | ||||||
| item_attributes = { | item_attributes = { | ||||||
|     "assettag": Attribute( |     "assettag": Attribute( | ||||||
|         display_name="Asset Tag", |         display_name="Asset Tag", | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| from flask_sqlalchemy import SQLAlchemy | from flask_sqlalchemy import SQLAlchemy | ||||||
| from config import item_attributes  # Import the configuration | from config import item_attributes  # Import the configuration | ||||||
| from sqlalchemy import Enum, Integer, String, Date, Column | from sqlalchemy import Enum, Integer, String, Date, Column | ||||||
|  | from config import sql_conf | ||||||
| 
 | 
 | ||||||
| db = SQLAlchemy() | db = SQLAlchemy() | ||||||
| 
 | 
 | ||||||
| @ -10,7 +11,7 @@ def create_asset_model(): | |||||||
|     Dynamically creates the Asset model based on the configuration in item_attributes. |     Dynamically creates the Asset model based on the configuration in item_attributes. | ||||||
|     """ |     """ | ||||||
|     attrs = { |     attrs = { | ||||||
|         '__tablename__': 'asset_test',  # Table name |         '__tablename__': sql_conf.SQL_TABLE,  # Table name | ||||||
|         '__init__': lambda self, **kwargs: self.__dict__.update(kwargs),  # Constructor |         '__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 |         '__repr__': lambda self: f"<Asset {getattr(self, next(attrib for attrib, config in item_attributes.items() if config.primary))}>"  # Representation | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user