mirror of
				https://github.com/StefBuwalda/dashboard_test.git
				synced 2025-10-30 19:29:58 +00:00 
			
		
		
		
	Add services to db if they don't exist
This commit is contained in:
		
							
								
								
									
										14
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								app.py
									
									
									
									
									
								
							| @@ -5,6 +5,7 @@ from mem import services, app, db | |||||||
| import threading | import threading | ||||||
| from flask_migrate import upgrade, stamp | from flask_migrate import upgrade, stamp | ||||||
| from pathlib import Path | from pathlib import Path | ||||||
|  | from models import service | ||||||
|  |  | ||||||
|  |  | ||||||
| # Init and upgrade | # Init and upgrade | ||||||
| @@ -17,6 +18,19 @@ with app.app_context(): | |||||||
|     # Upgrade db if any new migrations exist |     # Upgrade db if any new migrations exist | ||||||
|     upgrade() |     upgrade() | ||||||
|  |  | ||||||
|  | with app.app_context(): | ||||||
|  |     if not db.session.query(service).first(): | ||||||
|  |         for s in services: | ||||||
|  |             db.session.add( | ||||||
|  |                 service( | ||||||
|  |                     url=s.url, | ||||||
|  |                     label=s.label, | ||||||
|  |                     public_access=s.public, | ||||||
|  |                     ping_method=s.ping_type, | ||||||
|  |                 ) | ||||||
|  |             ) | ||||||
|  |         db.session.commit() | ||||||
|  |  | ||||||
|  |  | ||||||
| @app.route("/") | @app.route("/") | ||||||
| def homepage(): | def homepage(): | ||||||
|   | |||||||
| @@ -1,5 +0,0 @@ | |||||||
| from app import app |  | ||||||
| from flask_migrate import migrate |  | ||||||
|  |  | ||||||
| with app.app_context(): |  | ||||||
|     migrate() |  | ||||||
							
								
								
									
										35
									
								
								migrations/versions/f04407e8e466_.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								migrations/versions/f04407e8e466_.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | |||||||
|  | """empty message | ||||||
|  |  | ||||||
|  | Revision ID: f04407e8e466 | ||||||
|  | Revises: d7d380435347 | ||||||
|  | Create Date: 2025-09-03 15:40:30.413166 | ||||||
|  |  | ||||||
|  | """ | ||||||
|  | from alembic import op | ||||||
|  | import sqlalchemy as sa | ||||||
|  |  | ||||||
|  |  | ||||||
|  | # revision identifiers, used by Alembic. | ||||||
|  | revision = 'f04407e8e466' | ||||||
|  | down_revision = 'd7d380435347' | ||||||
|  | branch_labels = None | ||||||
|  | depends_on = None | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def upgrade(): | ||||||
|  |     # ### commands auto generated by Alembic - please adjust! ### | ||||||
|  |     op.create_table('service', | ||||||
|  |     sa.Column('id', sa.Integer(), nullable=False), | ||||||
|  |     sa.Column('url', sa.String(), nullable=False), | ||||||
|  |     sa.Column('label', sa.String(length=15), nullable=False), | ||||||
|  |     sa.Column('public_access', sa.Boolean(), nullable=False), | ||||||
|  |     sa.Column('ping_method', sa.Integer(), nullable=False), | ||||||
|  |     sa.PrimaryKeyConstraint('id') | ||||||
|  |     ) | ||||||
|  |     # ### end Alembic commands ### | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def downgrade(): | ||||||
|  |     # ### commands auto generated by Alembic - please adjust! ### | ||||||
|  |     op.drop_table('service') | ||||||
|  |     # ### end Alembic commands ### | ||||||
							
								
								
									
										11
									
								
								models.py
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								models.py
									
									
									
									
									
								
							| @@ -1,5 +1,6 @@ | |||||||
| from mem import db | from mem import db | ||||||
| from datetime import datetime, timezone | from datetime import datetime, timezone | ||||||
|  | from validators import url as is_url | ||||||
|  |  | ||||||
|  |  | ||||||
| class log(db.Model): | class log(db.Model): | ||||||
| @@ -19,5 +20,13 @@ class service(db.Model): | |||||||
|     public_access: bool = db.Column(db.Boolean, nullable=False) |     public_access: bool = db.Column(db.Boolean, nullable=False) | ||||||
|     ping_method: int = db.Column(db.Integer, nullable=False) |     ping_method: int = db.Column(db.Integer, nullable=False) | ||||||
|  |  | ||||||
|     def __init__(self): |     def __init__( | ||||||
|  |         self, url: str, label: str, public_access: bool, ping_method: int | ||||||
|  |     ): | ||||||
|  |         if not is_url(url): | ||||||
|  |             raise Exception("URL IS NOT A VALID URL") | ||||||
|         super().__init__() |         super().__init__() | ||||||
|  |         self.url = url | ||||||
|  |         self.label = label | ||||||
|  |         self.public_access = public_access | ||||||
|  |         self.ping_method = ping_method | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user