-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (92 loc) · 3.83 KB
/
Copy pathapp.py
File metadata and controls
114 lines (92 loc) · 3.83 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import logging
from datetime import timedelta
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_wtf.csrf import CSRFProtect
from werkzeug.middleware.proxy_fix import ProxyFix
from sqlalchemy.orm import DeclarativeBase
from flask_migrate import Migrate
# Configure logging
logging.basicConfig(level=logging.DEBUG)
class Base(DeclarativeBase):
pass
# Initialize extensions
db = SQLAlchemy(model_class=Base)
login_manager = LoginManager()
mail = Mail()
csrf = CSRFProtect()
migrate = None
def create_app():
app = Flask(__name__)
# Configuration
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)
# Flask-WTF/CSRF Configuration - Temporarily disabled for debugging
app.config['WTF_CSRF_ENABLED'] = False
app.config['WTF_CSRF_TIME_LIMIT'] = None
# Database configuration
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL", "sqlite:///tradesos.db")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
"pool_recycle": 300,
"pool_pre_ping": True,
}
# Mail configuration
app.config['MAIL_SERVER'] = os.environ.get('MAIL_SERVER', 'localhost')
app.config['MAIL_PORT'] = int(os.environ.get('MAIL_PORT', 587))
app.config['MAIL_USE_TLS'] = os.environ.get('MAIL_USE_TLS', 'true').lower() in ['true', 'on', '1']
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
app.config['MAIL_DEFAULT_SENDER'] = os.environ.get('MAIL_DEFAULT_SENDER', 'noreply@tradesos.co.uk')
# File upload configuration
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
# Stripe configuration
app.config['STRIPE_SECRET_KEY'] = os.environ.get('STRIPE_SECRET_KEY')
app.config['STRIPE_WEBHOOK_SECRET'] = os.environ.get('STRIPE_WEBHOOK_SECRET')
# TradeSOS specific configuration
app.config['PREMIUM_FIRST_ACCESS_MINUTES'] = int(os.environ.get('PREMIUM_FIRST_ACCESS_MINUTES', 3))
app.config['STANDARD_PLAN_PRICE'] = 4000 # £40.00 in pence
app.config['PREMIUM_PLAN_PRICE'] = 8000 # £80.00 in pence
# Initialize extensions
db.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
csrf.init_app(app)
# Login manager configuration - BASIC
login_manager.login_view = 'login'
login_manager.login_message = 'Please log in to access this page.'
login_manager.login_message_category = 'info'
# User loader function - FIXED
@login_manager.user_loader
def load_user(user_id):
from models import User
try:
return User.query.get(int(user_id))
except:
return None
# Initialize migrations
global migrate
migrate = Migrate(app, db)
# Create upload directory
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
with app.app_context():
# Import models to ensure they're registered
import models
# For development convenience, create tables if they don't exist.
# In production use Flask-Migrate (alembic) commands: `flask db init/migrate/upgrade`.
try:
db.create_all()
except Exception:
# If migrations are in use, creation may be handled by Alembic; ignore errors here.
logging.info('db.create_all() skipped (migrations may manage schema)')
logging.info("TradeSOS application initialized successfully")
return app
# Create app instance
app = create_app()
# Import routes after app creation to avoid circular imports
with app.app_context():
import routes