266 lines
8.8 KiB
Python
266 lines
8.8 KiB
Python
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"""
|
|
games = Game.query.all()
|
|
return render_template('index.html', games=games)
|
|
|
|
@main_bp.route('/search', methods=['GET', 'POST'])
|
|
def search_games():
|
|
"""Search for games using IGDB API"""
|
|
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"Oopsie! Couldn't search IGDB: {str(e)}", 'error')
|
|
|
|
return render_template('search.html', results=results, query=query)
|
|
|
|
@main_bp.route('/add_game/<int:igdb_id>', methods=['GET', 'POST'])
|
|
def add_game(igdb_id):
|
|
"""Add a game to tracking list"""
|
|
# Check if we already have this game
|
|
existing_game = Game.query.filter_by(igdb_id=igdb_id).first()
|
|
|
|
if existing_game:
|
|
flash(f"You'we alweady twacking {existing_game.name}!", 'info')
|
|
return redirect(url_for('main.index'))
|
|
|
|
# Check if we're at the 5 game limit
|
|
if Game.query.count() >= 5:
|
|
flash("You can onwy twack up to 5 games at once! Pwease wemove a game fiwst~", 'error')
|
|
return redirect(url_for('main.index'))
|
|
|
|
# Get game details from IGDB
|
|
try:
|
|
game_data = igdb.get_game_by_id(igdb_id)
|
|
|
|
# Create new Game object
|
|
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 youw twacking wist! ^w^", 'success')
|
|
return redirect(url_for('main.index'))
|
|
|
|
except Exception as e:
|
|
flash(f"Faiwed to add game: {str(e)}", 'error')
|
|
return redirect(url_for('main.search_games'))
|
|
|
|
@main_bp.route('/update_game/<int:game_id>', methods=['POST'])
|
|
def update_game(game_id):
|
|
"""Update game progress and status"""
|
|
game = Game.query.get_or_404(game_id)
|
|
|
|
if request.method == 'POST':
|
|
# Update status if provided
|
|
if 'status' in request.form:
|
|
game.status = request.form['status']
|
|
|
|
# Update progress if provided
|
|
if 'progress' in request.form:
|
|
try:
|
|
progress = int(request.form['progress'])
|
|
if 0 <= progress <= 100:
|
|
game.progress = progress
|
|
except ValueError:
|
|
flash("Pwogwess must be a numbew between 0 and 100!", 'error')
|
|
|
|
# Update notes if provided
|
|
if 'notes' in request.form:
|
|
game.notes = request.form['notes']
|
|
|
|
db.session.commit()
|
|
flash(f"Updated {game.name}! (。・ω・。)", 'success')
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
@main_bp.route('/remove_game/<int:game_id>', methods=['POST'])
|
|
def remove_game(game_id):
|
|
"""Remove a game from tracking list"""
|
|
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.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'))
|