Spaces:
Runtime error
Runtime error
File size: 1,650 Bytes
53c8c29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import os
import logging
from flask import Flask, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_babel import Babel, get_locale
from sqlalchemy.orm import DeclarativeBase
from werkzeug.middleware.proxy_fix import ProxyFix
# Set up logging
logging.basicConfig(level=logging.DEBUG)
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
# Create the app
app = Flask(__name__)
app.secret_key = os.environ.get("SESSION_SECRET", "dev-secret-key-change-in-production")
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
# Configure the database
database_url = os.environ.get("DATABASE_URL", "sqlite:///app.db")
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
# Configure Babel for internationalization
app.config['LANGUAGES'] = {
'en': 'English',
'bn': 'বাংলা'
}
app.config['BABEL_DEFAULT_LOCALE'] = 'en'
app.config['BABEL_DEFAULT_TIMEZONE'] = 'UTC'
# Initialize extensions
db.init_app(app)
babel = Babel(app)
def get_locale():
# Check if language is set in session
if 'language' in session:
return session['language']
# Check Accept-Language header
return request.accept_languages.best_match(app.config['LANGUAGES'].keys()) or 'en'
babel.init_app(app, locale_selector=get_locale)
# Make get_locale available in templates
@app.context_processor
def inject_get_locale():
return dict(get_locale=get_locale)
with app.app_context():
# Import models and routes
import models
import routes
# Create all tables
db.create_all()
|