admin
This commit is contained in:
157
app/routes.py
157
app/routes.py
@@ -1,12 +1,23 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, current_app
|
||||
from .models import Game
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify, current_app, session
|
||||
from .models import Game, AdminConfig
|
||||
from . import db
|
||||
from .igdb_helpers import IGDBHelper
|
||||
import os
|
||||
import functools
|
||||
|
||||
main_bp = Blueprint('main', __name__)
|
||||
igdb = IGDBHelper()
|
||||
|
||||
# Admin authentication decorator
|
||||
def admin_required(view):
|
||||
@functools.wraps(view)
|
||||
def wrapped_view(**kwargs):
|
||||
if not session.get('admin_authenticated'):
|
||||
flash('Please log in to access the admin area', 'error')
|
||||
return redirect(url_for('main.admin_login'))
|
||||
return view(**kwargs)
|
||||
return wrapped_view
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
"""Home page showing currently tracked games"""
|
||||
@@ -110,5 +121,145 @@ def remove_game(game_id):
|
||||
db.session.delete(game)
|
||||
db.session.commit()
|
||||
|
||||
flash(f"Removed {name} from youw twacking wist! OwO", 'success')
|
||||
flash(f"Removed {name} from tracking list!", 'success')
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
# Admin Routes
|
||||
@main_bp.route('/admin/login', methods=['GET', 'POST'])
|
||||
def admin_login():
|
||||
"""Admin login page"""
|
||||
if request.method == 'POST':
|
||||
password = request.form.get('password')
|
||||
|
||||
# Check if this is initial setup
|
||||
admin_exists = AdminConfig.query.first()
|
||||
|
||||
if not admin_exists:
|
||||
# Initial setup - set password
|
||||
if password and len(password) >= 6:
|
||||
AdminConfig.set_password(password)
|
||||
session['admin_authenticated'] = True
|
||||
flash('Admin account created successfully!', 'success')
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
else:
|
||||
flash('Password must be at least 6 characters long', 'error')
|
||||
else:
|
||||
# Regular login
|
||||
if AdminConfig.check_password(password):
|
||||
session['admin_authenticated'] = True
|
||||
flash('Logged in successfully!', 'success')
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
else:
|
||||
flash('Invalid password', 'error')
|
||||
|
||||
# Check if admin exists to show proper message
|
||||
admin_exists = AdminConfig.query.first()
|
||||
return render_template('admin/login.html', admin_exists=admin_exists)
|
||||
|
||||
@main_bp.route('/admin/dashboard')
|
||||
@admin_required
|
||||
def admin_dashboard():
|
||||
"""Admin dashboard"""
|
||||
games = Game.query.all()
|
||||
return render_template('admin/dashboard.html', games=games)
|
||||
|
||||
@main_bp.route('/admin/search', methods=['GET', 'POST'])
|
||||
@admin_required
|
||||
def admin_search():
|
||||
"""Admin game search"""
|
||||
results = []
|
||||
query = ''
|
||||
|
||||
if request.method == 'POST':
|
||||
query = request.form.get('query', '')
|
||||
|
||||
if query:
|
||||
try:
|
||||
results = igdb.search_games(query)
|
||||
except Exception as e:
|
||||
flash(f"Couldn't search IGDB: {str(e)}", 'error')
|
||||
|
||||
return render_template('admin/search.html', results=results, query=query)
|
||||
|
||||
@main_bp.route('/admin/add_game/<int:igdb_id>', methods=['GET', 'POST'])
|
||||
@admin_required
|
||||
def admin_add_game(igdb_id):
|
||||
"""Admin add game"""
|
||||
existing_game = Game.query.filter_by(igdb_id=igdb_id).first()
|
||||
|
||||
if existing_game:
|
||||
flash(f"Game {existing_game.name} is already being tracked", 'info')
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
|
||||
try:
|
||||
game_data = igdb.get_game_by_id(igdb_id)
|
||||
|
||||
new_game = Game(
|
||||
igdb_id=game_data['id'],
|
||||
name=game_data['name'],
|
||||
cover_url=game_data['cover_url'],
|
||||
description=game_data['description'],
|
||||
release_date=game_data['release_date'],
|
||||
platform=', '.join(game_data['platforms']) if game_data['platforms'] else None,
|
||||
developer=game_data['developer'],
|
||||
publisher=game_data['publisher'],
|
||||
status="Playing",
|
||||
progress=0
|
||||
)
|
||||
|
||||
db.session.add(new_game)
|
||||
db.session.commit()
|
||||
|
||||
flash(f"Added {new_game.name} to tracking list", 'success')
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
|
||||
except Exception as e:
|
||||
flash(f"Failed to add game: {str(e)}", 'error')
|
||||
return redirect(url_for('main.admin_search'))
|
||||
|
||||
@main_bp.route('/admin/update_game/<int:game_id>', methods=['POST'])
|
||||
@admin_required
|
||||
def admin_update_game(game_id):
|
||||
"""Admin update game"""
|
||||
game = Game.query.get_or_404(game_id)
|
||||
|
||||
if request.method == 'POST':
|
||||
if 'status' in request.form:
|
||||
game.status = request.form['status']
|
||||
|
||||
if 'progress' in request.form:
|
||||
try:
|
||||
progress = int(request.form['progress'])
|
||||
if 0 <= progress <= 100:
|
||||
game.progress = progress
|
||||
except ValueError:
|
||||
flash("Progress must be a number between 0 and 100", 'error')
|
||||
|
||||
if 'notes' in request.form:
|
||||
game.notes = request.form['notes']
|
||||
|
||||
db.session.commit()
|
||||
flash(f"Updated {game.name}", 'success')
|
||||
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
|
||||
@main_bp.route('/admin/remove_game/<int:game_id>', methods=['POST'])
|
||||
@admin_required
|
||||
def admin_remove_game(game_id):
|
||||
"""Admin remove game"""
|
||||
game = Game.query.get_or_404(game_id)
|
||||
name = game.name
|
||||
|
||||
db.session.delete(game)
|
||||
db.session.commit()
|
||||
|
||||
flash(f"Removed {name} from tracking list", 'success')
|
||||
return redirect(url_for('main.admin_dashboard'))
|
||||
|
||||
@main_bp.route('/admin/logout')
|
||||
@admin_required
|
||||
def admin_logout():
|
||||
"""Admin logout"""
|
||||
session.pop('admin_authenticated', None)
|
||||
flash('Logged out successfully', 'success')
|
||||
return redirect(url_for('main.index'))
|
||||
|
||||
Reference in New Issue
Block a user