-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·235 lines (185 loc) · 7.96 KB
/
app.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import logging
from flask import Flask, render_template, redirect, url_for, flash, request, jsonify, make_response
from config import DevelopmentConfig
from models import db, User, Movie, MovieStats, BlogPost
from forms import SignupForm, LoginForm, ChangePlanForm, BlogPostForm
from flask_migrate import Migrate
from flask_login import LoginManager, current_user, login_user, logout_user, login_required
from flask_wtf.csrf import CSRFProtect
from posthog import Posthog
# Configure logging
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
# Initialize CSRF protection
csrf = CSRFProtect(app)
posthog = Posthog(app.config['PH_PROJECT_KEY'], host=app.config['PH_HOST'])
db.init_app(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
@login.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route('/')
def index():
family_movies = Movie.query.filter_by(genre='Family').all()
action_movies = Movie.query.filter_by(genre='Action').all()
response = make_response(render_template(
'index.html',
family_movies=family_movies,
action_movies=action_movies
))
response.delete_cookie('posthog_js')
return response
@app.route('/movie/<int:movie_id>')
def movie(movie_id):
movie = Movie.query.get_or_404(movie_id)
return render_template('movie.html', movie=movie)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = SignupForm()
preselected_plan = request.args.get('plan')
if request.method == 'POST':
app.logger.debug(f"Form data received: {request.form}")
plan = request.form.get('plan')
app.logger.debug(f"Plan value: {plan}")
if form.validate_on_submit() and plan in ['Free', 'Premium', 'Max-imal']:
user = User(
username=form.username.data,
email=form.email.data,
plan=plan,
is_adult=not form.is_adult.data
)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
app.logger.debug(f"New user created: {user.username} with plan: {user.plan}")
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
else:
if 'plan' not in request.form or not request.form['plan']:
flash('Please select a plan.', 'error')
app.logger.debug(f"Form validation failed. Errors: {form.errors}")
return render_template('signup.html', form=form, preselected_plan=preselected_plan)
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password', 'error')
return redirect(url_for('login'))
login_user(user, remember=True)
posthog.identify(
user.id,
{
"email": user.email,
"username": user.username,
"is_adult": "Yes" if user.is_adult else "No"
}
)
posthog.capture(user.id, 'user_logged_in')
flash('Welcome back!', 'success')
next_page = request.args.get('next')
return redirect(next_page or url_for('index'))
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
if current_user.is_authenticated:
posthog.capture(current_user.email, 'user_logged_out')
logout_user()
flash('You have been logged out.')
return redirect(url_for('index', reload='true'))
@app.route('/search', methods=['POST'])
@csrf.exempt # Disable CSRF for this route for debugging.
def search():
query = request.form.get('query')
posthog.capture('search', 'search_performed', {'query': query})
return redirect(url_for('search_results', query=query))
@app.route('/search_results')
def search_results():
query = request.args.get('query')
movies = Movie.query.filter(Movie.title.like(f'%{query}%')).all()
return render_template('search_results.html', query=query, movies=movies)
@app.route('/profile', methods=['GET', 'POST'])
@login_required
def profile():
form = ChangePlanForm()
if request.method == 'POST':
new_plan = request.form.get('plan')
app.logger.debug(f"Plan to update: {new_plan}")
if new_plan in ['Free', 'Premium', 'Max-imal']:
current_user.plan = new_plan
try:
db.session.commit()
app.logger.debug(f"User plan updated to: {new_plan}")
flash('Your subscription plan has been updated!')
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return jsonify({'success': True})
except Exception as e:
db.session.rollback()
app.logger.error(f"Error updating plan: {e}")
flash('An error occurred while updating your plan. Please try again.', 'danger')
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return jsonify({'success': False})
else:
flash('Please select a valid plan.', 'danger')
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return jsonify({'success': False})
if not request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return redirect(url_for('profile'))
stats_week = [stat.to_dict() for stat in MovieStats.query.filter_by(user_id=current_user.id, time_frame='week').all()]
stats_month = [stat.to_dict() for stat in MovieStats.query.filter_by(user_id=current_user.id, time_frame='month').all()]
stats_year = [stat.to_dict() for stat in MovieStats.query.filter_by(user_id=current_user.id, time_frame='year').all()]
return render_template('profile.html', user=current_user, form=form, stats_week=stats_week, stats_month=stats_month, stats_year=stats_year)
@app.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
if request.method == 'POST':
pass
return render_template('settings.html', user=current_user)
@app.route('/plans', methods=['GET'])
def plans():
return render_template('plans.html', user=current_user)
@app.route('/fourohfour', methods=['GET'])
def fourohfour():
return render_template('404.html', user=current_user)
@app.route('/blog')
def blog():
posts = BlogPost.query.order_by(BlogPost.timestamp.desc()).all()
return render_template('blog.html', posts=posts)
@app.route('/delete_post/<int:post_id>', methods=['POST'])
@login_required
def delete_post(post_id):
if not current_user.is_admin:
return jsonify({'success': False, 'message': 'Unauthorized'}), 403
post = BlogPost.query.get_or_404(post_id)
db.session.delete(post)
db.session.commit()
return jsonify({'success': True})
@app.route('/create_post', methods=['GET', 'POST'])
@login_required
def create_post():
form = BlogPostForm()
if form.validate_on_submit():
post = BlogPost(title=form.title.data, content=form.content.data)
db.session.add(post)
db.session.commit()
flash('Blog post created successfully!', 'success')
return redirect(url_for('blog'))
return render_template('create_post.html', form=form)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.route('/toc')
def toc():
return render_template('toc.html')
@app.route('/feature-flags')
@login_required # Optional: restrict to logged-in users only
def feature_flags():
return render_template('feature_flags.html')
if __name__ == '__main__':
app.run(debug=True, host=app.config['APP_HOST'], port=app.config['APP_PORT'])