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