from app import app, db from app.models.user import User from app.models.document import Document, Category, Tag from werkzeug.security import generate_password_hash import os def setup_database(): """Set up the database tables and create a demo user if none exists.""" with app.app_context(): # Create all tables db.create_all() # Check if any users exist if User.query.count() == 0: print('Creating demo user...') # Create demo user demo_user = User(username='demo') demo_user.set_password('password') db.session.add(demo_user) db.session.flush() # To get the user ID # Create a root category for the demo user root_category = Category( name='My Documents', icon='mdi-folder', description='Default document category', user_id=demo_user.id, is_root=True ) db.session.add(root_category) # Create some sample categories categories = [ Category( name='Vim Commands', icon='mdi-vim', user_id=demo_user.id, description='Essential Vim commands and shortcuts' ), Category( name='Flask Development', icon='mdi-flask', user_id=demo_user.id, description='Flask web development notes' ), Category( name='Python Snippets', icon='mdi-language-python', user_id=demo_user.id, description='Useful Python code snippets' ) ] for category in categories: db.session.add(category) # Create a sample document sample_doc = Document( title='Getting Started with Vim', content="""# Getting Started with Vim ## Basic Commands ### Movement - `h` - move left - `j` - move down - `k` - move up - `l` - move right ### Modes - `i` - enter insert mode - `Esc` - return to normal mode - `v` - enter visual mode - `:` - enter command mode > Vim has a steep learning curve, but it's worth it! > [!TIP] > Use `vimtutor` to learn Vim basics interactively. > [!NOTE] > Vim is available on almost all Unix-like systems. """, user_id=demo_user.id, category_id=categories[0].id ) db.session.add(sample_doc) # Create some tags tags = [ Tag(name='vim', user_id=demo_user.id, color='#50fa7b'), Tag(name='editor', user_id=demo_user.id, color='#bd93f9'), Tag(name='tutorial', user_id=demo_user.id, color='#ff79c6') ] for tag in tags: db.session.add(tag) # Associate tags with the document sample_doc.tags = tags # Commit all changes db.session.commit() print('Demo user and sample data created successfully!') else: print('Database already contains users, skipping demo data creation.') if __name__ == '__main__': # Create database file if it doesn't exist db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'app', 'docs.db') if not os.path.exists(db_path): print(f'Creating database file at {db_path}') setup_database() print('Database setup complete!')