161 lines
No EOL
4.9 KiB
Python
161 lines
No EOL
4.9 KiB
Python
# App package initialization
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import LoginManager
|
|
from flask_migrate import Migrate
|
|
from flask_wtf.csrf import CSRFProtect
|
|
import os
|
|
from datetime import timedelta
|
|
|
|
# Initialize SQLAlchemy outside of create_app
|
|
db = SQLAlchemy()
|
|
login_manager = LoginManager()
|
|
csrf = CSRFProtect()
|
|
migrate = Migrate()
|
|
|
|
# App configuration
|
|
class Config:
|
|
SECRET_KEY = os.environ.get('SECRET_KEY') or os.urandom(24)
|
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///docs.db'
|
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
|
PERMANENT_SESSION_LIFETIME = timedelta(hours=12)
|
|
SESSION_TYPE = 'filesystem'
|
|
|
|
def init_db(app):
|
|
"""Initialize the database and create tables if they don't exist."""
|
|
with app.app_context():
|
|
db.create_all()
|
|
from app.models.user import User
|
|
# Create a demo user if no users exist
|
|
if User.query.count() == 0:
|
|
from app.models.document import Document, Category, Tag
|
|
from werkzeug.security import generate_password_hash
|
|
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!')
|
|
|
|
def create_app(config_class=Config):
|
|
app = Flask(__name__)
|
|
app.config.from_object(config_class)
|
|
|
|
# Initialize extensions
|
|
db.init_app(app)
|
|
login_manager.init_app(app)
|
|
csrf.init_app(app)
|
|
migrate.init_app(app, db)
|
|
|
|
# Configure login manager
|
|
login_manager.login_view = 'auth.login'
|
|
login_manager.login_message = 'Please log in to access this page.'
|
|
login_manager.login_message_category = 'info'
|
|
|
|
# Configure session
|
|
app.config['SESSION_PERMANENT'] = True
|
|
|
|
# Register blueprints
|
|
from app.routes import main as main_bp
|
|
app.register_blueprint(main_bp)
|
|
|
|
from app.auth import bp as auth_bp
|
|
app.register_blueprint(auth_bp, url_prefix='/auth')
|
|
|
|
return app
|
|
|
|
# Create app instance
|
|
app = create_app()
|
|
|
|
# Initialize database
|
|
init_db(app)
|
|
|
|
# Import models after db initialization to avoid circular imports
|
|
from app.models.document import Document, Category, Tag
|
|
from app.models.user import User |