54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from app.core.extensions import db
|
|
from app.core.models import Subnet, Server, App, Port
|
|
from app.core.auth import User # Import User from auth module
|
|
import json
|
|
|
|
|
|
def seed_database():
|
|
"""Add sample data to the database"""
|
|
# Create a default subnet if none exists
|
|
if Subnet.query.count() == 0:
|
|
subnet = Subnet(
|
|
cidr="192.168.1.0/24",
|
|
location="Office",
|
|
auto_scan=True,
|
|
active_hosts=json.dumps([]),
|
|
)
|
|
db.session.add(subnet)
|
|
|
|
# Create a sample server
|
|
server = Server(
|
|
hostname="server1",
|
|
ip_address="192.168.1.10",
|
|
subnet=subnet,
|
|
documentation="# Server 1\n\nThis is a sample server.",
|
|
)
|
|
db.session.add(server)
|
|
|
|
# Create a sample app
|
|
app = App(
|
|
name="Web App",
|
|
server=server,
|
|
documentation="# Welcome to Web App\n\nThis is a sample application.",
|
|
)
|
|
db.session.add(app)
|
|
|
|
# Add some ports
|
|
ports = [
|
|
Port(app=app, port_number=80, protocol="TCP", description="HTTP"),
|
|
Port(app=app, port_number=443, protocol="TCP", description="HTTPS"),
|
|
]
|
|
db.session.add_all(ports)
|
|
|
|
# Create a default user if none exists
|
|
if User.query.count() == 0:
|
|
admin = User(username="admin", email="admin@example.com", is_admin=True)
|
|
admin.set_password("admin")
|
|
db.session.add(admin)
|
|
|
|
try:
|
|
db.session.commit()
|
|
print("Database seeded successfully")
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
print(f"Error seeding database: {e}")
|