1743 lines
59 KiB
Bash
1743 lines
59 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[1;31m'
|
|
GREEN='\033[1;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
# 1. Docker check (incl. Arch Linux)
|
|
if ! command -v docker &>/dev/null; then
|
|
echo -e "${RED}❗ Docker is not installed.${NC}"
|
|
read -p "Would you like to install Docker automatically now? [y/N]: " install_docker
|
|
if [[ "$install_docker" =~ ^[YyJj]$ ]]; then
|
|
if [ -f /etc/arch-release ]; then
|
|
echo -e "${GREEN}▶️ Installing Docker for Arch Linux...${NC}"
|
|
sudo pacman -Sy --noconfirm docker
|
|
sudo systemctl enable --now docker.service
|
|
else
|
|
echo -e "${GREEN}▶️ Using generic Docker installation script...${NC}"
|
|
curl -fsSL https://get.docker.com -o get-docker.sh
|
|
sudo sh get-docker.sh
|
|
rm get-docker.sh
|
|
fi
|
|
|
|
# Docker group membership prüfen
|
|
if ! groups | grep -q '\bdocker\b'; then
|
|
echo -e "${YELLOW}⚠️ Your user is not in the docker group. Adding now...${NC}"
|
|
sudo usermod -aG docker $USER
|
|
newgrp docker
|
|
fi
|
|
|
|
echo -e "${GREEN}✔️ Docker has been installed.${NC}"
|
|
else
|
|
echo -e "${YELLOW}❌ Docker is required. Exiting script.${NC}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# 2. Check Docker compose (V1 und V2 Plugin, incl. Arch Support)
|
|
DOCKER_COMPOSE_CMD=""
|
|
if command -v docker-compose &>/dev/null; then
|
|
DOCKER_COMPOSE_CMD="docker-compose"
|
|
elif docker compose version &>/dev/null; then
|
|
DOCKER_COMPOSE_CMD="docker compose"
|
|
else
|
|
echo -e "${RED}❗ Neither docker-compose nor docker compose plugin is installed!${NC}"
|
|
read -p "Would you like to install Docker Compose automatically now? [y/N]: " install_compose
|
|
if [[ "$install_compose" =~ ^[YyJj]$ ]]; then
|
|
if [ -f /etc/arch-release ]; then
|
|
sudo pacman -Sy --noconfirm docker-compose
|
|
elif command -v apt-get &>/dev/null; then
|
|
sudo apt-get update
|
|
sudo apt-get install -y docker-compose-plugin
|
|
sudo apt-get install -y docker-compose
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo dnf install -y docker-compose
|
|
elif command -v yum &>/dev/null; then
|
|
sudo yum install -y docker-compose-plugin
|
|
else
|
|
echo -e "${RED}❌ Unsupported package manager! Please install Docker Compose manually.${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Final check
|
|
if ! command -v docker-compose &>/dev/null && ! docker compose version &>/dev/null; then
|
|
echo -e "${RED}❌ Docker Compose installation failed!${NC}"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}✔️ Docker Compose has been installed successfully.${NC}"
|
|
else
|
|
echo -e "${YELLOW}❌ Docker Compose is required. Exiting script.${NC}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Configuration
|
|
PROJECT_DIR="steam-gift-manager"
|
|
TRANSLATIONS_DIR="$PWD/translations"
|
|
DATA_DIR="$PWD/data"
|
|
|
|
# 1. Create folders
|
|
mkdir -p "$PROJECT_DIR"/{templates,static}
|
|
mkdir -p "$TRANSLATIONS_DIR"/de/LC_MESSAGES
|
|
mkdir -p "$TRANSLATIONS_DIR"/en/LC_MESSAGES
|
|
mkdir -p "$DATA_DIR"
|
|
|
|
chmod -R a+rwX "$TRANSLATIONS_DIR" "$DATA_DIR"
|
|
|
|
cd $PROJECT_DIR
|
|
|
|
# 2. requirements.txt
|
|
cat <<EOL > requirements.txt
|
|
flask
|
|
flask-login
|
|
flask-wtf
|
|
flask-migrate
|
|
werkzeug
|
|
python-dotenv
|
|
flask-sqlalchemy
|
|
flask-babel
|
|
jinja2<3.1.0
|
|
itsdangerous
|
|
sqlalchemy
|
|
apscheduler
|
|
reportlab
|
|
requests
|
|
pillow
|
|
gunicorn
|
|
apprise
|
|
EOL
|
|
|
|
# 3. .env Datei in Parent-Folder
|
|
cd ..
|
|
SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_hex(24))')
|
|
REDEEM_SECRET=$(python3 -c 'import secrets; print(secrets.token_hex(16))')
|
|
REDEEM_CSRF=$(python3 -c 'import secrets; print(secrets.token_hex(16))')
|
|
|
|
cat <<EOL > .env
|
|
# Flask-Configuration
|
|
SECRET_KEY="$SECRET_KEY"
|
|
REDEEM_SECRET="$REDEEM_SECRET"
|
|
WTF_CSRF_SECRET_KEY="$REDEEM_CSRF"
|
|
|
|
# locales
|
|
BABEL_DEFAULT_LOCALE="en"
|
|
BABEL_SUPPORTED_LOCALES="de,en"
|
|
BABEL_TRANSLATION_DIRECTORIES="translations"
|
|
|
|
# Timezone
|
|
TZ=Europe/Berlin
|
|
|
|
# Security
|
|
SESSION_COOKIE_SECURE="False"
|
|
CSRF_ENABLED="True"
|
|
# Account registration
|
|
REGISTRATION_ENABLED="True"
|
|
|
|
# checking interval if keys have to be redeemed before a specific date
|
|
CHECK_EXPIRING_KEYS_INTERVAL_HOURS=6
|
|
|
|
# Apprise URLs (separate several with a line break, comma or space)
|
|
APPRISE_URLS=""
|
|
|
|
### example for multiple notifications
|
|
#APPRISE_URLS="pover://USER_KEY@APP_TOKEN
|
|
#gotify://gotify.example.com/TOKEN
|
|
#matrixs://TOKEN@matrix.org/!ROOM_ID"
|
|
EOL
|
|
|
|
cd $PROJECT_DIR
|
|
|
|
# 4. app.py (the main app)
|
|
cat <<'PYTHON_END' > app.py
|
|
import os
|
|
import warnings
|
|
from sqlalchemy.exc import LegacyAPIWarning
|
|
warnings.simplefilter("ignore", category=LegacyAPIWarning)
|
|
from flask import Flask, render_template, request, redirect, url_for, flash, make_response, session, abort, send_file, jsonify
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
|
from flask_babel import Babel, _
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
from datetime import datetime, timedelta
|
|
from flask_wtf import CSRFProtect
|
|
from flask import abort
|
|
import io
|
|
import warnings
|
|
import re
|
|
import io
|
|
import csv
|
|
import secrets
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
load_dotenv(override=True)
|
|
from sqlalchemy.exc import IntegrityError
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
import atexit
|
|
from flask_migrate import Migrate
|
|
from sqlalchemy import MetaData
|
|
from reportlab.pdfgen import canvas
|
|
from reportlab.lib.pagesizes import A4, landscape, letter
|
|
from reportlab.platypus import (
|
|
SimpleDocTemplate,
|
|
Table,
|
|
TableStyle,
|
|
Paragraph,
|
|
Image,
|
|
Spacer
|
|
)
|
|
from reportlab.lib import colors
|
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
from reportlab.lib.utils import ImageReader
|
|
from reportlab.lib.units import cm, inch, mm
|
|
from io import BytesIO
|
|
import reportlab.lib
|
|
import logging
|
|
logging.basicConfig()
|
|
logging.getLogger('babel').setLevel(logging.DEBUG)
|
|
app = Flask(__name__)
|
|
|
|
csrf = CSRFProtect(app)
|
|
|
|
convention = {
|
|
"ix": "ix_%(column_0_label)s",
|
|
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
|
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
|
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
|
"pk": "pk_%(table_name)s"
|
|
}
|
|
|
|
metadata = MetaData(naming_convention=convention)
|
|
load_dotenv(override=True)
|
|
|
|
# Lade Umgebungsvariablen aus .env mit override
|
|
load_dotenv(override=True)
|
|
|
|
# App-Configuration
|
|
app.config.update(
|
|
SECRET_KEY=os.getenv('SECRET_KEY'),
|
|
SQLALCHEMY_DATABASE_URI='sqlite:////app/data/games.db',
|
|
SQLALCHEMY_TRACK_MODIFICATIONS=False,
|
|
BABEL_DEFAULT_LOCALE=os.getenv('BABEL_DEFAULT_LOCALE', 'en'),
|
|
BABEL_SUPPORTED_LOCALES=os.getenv('BABEL_SUPPORTED_LOCALES', 'de,en').split(','),
|
|
BABEL_TRANSLATION_DIRECTORIES=os.path.join(app.root_path, 'translations'),
|
|
SESSION_COOKIE_SECURE=os.getenv('SESSION_COOKIE_SECURE', 'False') == 'True',
|
|
SESSION_COOKIE_SAMESITE='Lax',
|
|
PERMANENT_SESSION_LIFETIME=timedelta(days=30),
|
|
SESSION_REFRESH_EACH_REQUEST=False,
|
|
WTF_CSRF_ENABLED=os.getenv('CSRF_ENABLED', 'True') == 'True',
|
|
REGISTRATION_ENABLED=os.getenv('REGISTRATION_ENABLED', 'True').lower() == 'true',
|
|
SEND_FILE_MAX_AGE_DEFAULT=int(os.getenv('SEND_FILE_MAX_AGE_DEFAULT', 0)),
|
|
TEMPLATES_AUTO_RELOAD=os.getenv('TEMPLATES_AUTO_RELOAD', 'True') == 'True'
|
|
)
|
|
|
|
interval_hours = int(os.getenv('CHECK_EXPIRING_KEYS_INTERVAL_HOURS', 12))
|
|
|
|
# Initialisation
|
|
db = SQLAlchemy(app, metadata=metadata)
|
|
migrate = Migrate(app, db)
|
|
login_manager = LoginManager(app)
|
|
login_manager.login_view = 'login'
|
|
babel = Babel(app)
|
|
|
|
# Logging
|
|
app.logger.addHandler(logging.StreamHandler())
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
@babel.localeselector
|
|
def get_locale():
|
|
if 'lang' in session and session['lang'] in app.config['BABEL_SUPPORTED_LOCALES']:
|
|
return session['lang']
|
|
return request.accept_languages.best_match(app.config['BABEL_SUPPORTED_LOCALES'])
|
|
|
|
@app.before_request
|
|
def reload_translations():
|
|
if app.config['DEBUG']:
|
|
babel.reload()
|
|
|
|
@app.context_processor
|
|
def inject_template_vars():
|
|
return dict(
|
|
get_locale=get_locale,
|
|
theme='dark' if request.cookies.get('dark_mode') == 'true' else 'light'
|
|
)
|
|
|
|
# DB Models
|
|
class User(db.Model, UserMixin):
|
|
__tablename__ = 'users'
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
password = db.Column(db.String(256), nullable=False)
|
|
games = db.relationship('Game', back_populates='owner', lazy=True)
|
|
|
|
class Game(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
owner = db.relationship('User', back_populates='games')
|
|
name = db.Column(db.String(100), nullable=False)
|
|
steam_key = db.Column(db.String(100), nullable=False, unique=True)
|
|
status = db.Column(db.String(50), nullable=False)
|
|
recipient = db.Column(db.String(100))
|
|
notes = db.Column(db.Text)
|
|
url = db.Column(db.String(200))
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
redeem_date = db.Column(db.DateTime)
|
|
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
|
steam_appid = db.Column(db.String(20))
|
|
|
|
class RedeemToken(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
token = db.Column(db.String(17), unique=True, nullable=False)
|
|
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
|
|
expires = db.Column(db.DateTime, nullable=False)
|
|
used = db.Column(db.Boolean, default=False)
|
|
total_hours = db.Column(db.Integer, nullable=False)
|
|
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
return db.session.get(User, int(user_id))
|
|
|
|
def extract_steam_appid(url):
|
|
match = re.search(r'store\.steampowered\.com/app/(\d+)', url or '')
|
|
return match.group(1) if match else ''
|
|
|
|
# 404
|
|
def get_or_404(model, id):
|
|
instance = db.session.get(model, id)
|
|
if not instance:
|
|
abort(404)
|
|
return instance
|
|
|
|
@app.route('/')
|
|
@login_required
|
|
def index():
|
|
search_query = request.args.get('q', '')
|
|
query = Game.query.filter_by(user_id=current_user.id)
|
|
|
|
if search_query:
|
|
query = query.filter(Game.name.ilike(f'%{search_query}%'))
|
|
|
|
games = query.order_by(Game.created_at.desc()).all()
|
|
return render_template('index.html',
|
|
games=games,
|
|
format_date=lambda dt: dt.strftime('%d.%m.%Y') if dt else '',
|
|
search_query=search_query)
|
|
|
|
@app.route('/set-lang/<lang>')
|
|
def set_lang(lang):
|
|
if lang in app.config['BABEL_SUPPORTED_LOCALES']:
|
|
session['lang'] = lang
|
|
return redirect(request.referrer or url_for('index'))
|
|
|
|
@app.route('/set-theme/<theme>')
|
|
def set_theme(theme):
|
|
resp = make_response('', 204)
|
|
resp.set_cookie('dark_mode', 'true' if theme == 'dark' else 'false', max_age=60*60*24*365)
|
|
return resp
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
password = request.form['password']
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user and check_password_hash(user.password, password):
|
|
login_user(user)
|
|
return redirect(url_for('index'))
|
|
|
|
flash(_('Invalid credentials'), 'danger')
|
|
return render_template('login.html')
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if not app.config['REGISTRATION_ENABLED']:
|
|
flash(_('No new registrations. They are deactivated!'), 'danger')
|
|
return redirect(url_for('login'))
|
|
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
password = generate_password_hash(request.form['password'])
|
|
|
|
if User.query.filter_by(username=username).first():
|
|
flash(_('Username already exists'), 'danger')
|
|
return redirect(url_for('register'))
|
|
|
|
new_user = User(username=username, password=password)
|
|
db.session.add(new_user)
|
|
db.session.commit()
|
|
login_user(new_user)
|
|
return redirect(url_for('index'))
|
|
|
|
return render_template('register.html')
|
|
|
|
@app.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
return redirect(url_for('login'))
|
|
|
|
@app.route('/change-password', methods=['GET', 'POST'])
|
|
@login_required
|
|
def change_password():
|
|
if request.method == 'POST':
|
|
current_password = request.form['current_password']
|
|
new_password = request.form['new_password']
|
|
confirm_password = request.form['confirm_password']
|
|
|
|
if not check_password_hash(current_user.password, current_password):
|
|
flash(_('Current passwort is wrong'), 'danger')
|
|
return redirect(url_for('change_password'))
|
|
|
|
if new_password != confirm_password:
|
|
flash(_('New Passwords are not matching'), 'danger')
|
|
return redirect(url_for('change_password'))
|
|
|
|
current_user.password = generate_password_hash(new_password)
|
|
db.session.commit()
|
|
flash(_('Password changed successfully'), 'success')
|
|
return redirect(url_for('index'))
|
|
|
|
return render_template('change_password.html')
|
|
|
|
@app.route('/add', methods=['GET', 'POST'])
|
|
@login_required
|
|
def add_game():
|
|
if request.method == 'POST':
|
|
try:
|
|
url = request.form.get('url', '')
|
|
steam_appid = request.form.get('steam_appid', '').strip()
|
|
|
|
if not steam_appid:
|
|
steam_appid = extract_steam_appid(url)
|
|
|
|
new_game = Game(
|
|
name=request.form['name'],
|
|
steam_key=request.form['steam_key'],
|
|
status=request.form['status'],
|
|
recipient=request.form.get('recipient', ''),
|
|
notes=request.form.get('notes', ''),
|
|
url=url,
|
|
steam_appid=steam_appid,
|
|
redeem_date=datetime.strptime(request.form['redeem_date'], '%Y-%m-%d') if request.form['redeem_date'] else None,
|
|
user_id=current_user.id
|
|
)
|
|
|
|
db.session.add(new_game)
|
|
db.session.commit()
|
|
flash(_('Game added successfully!'), 'success')
|
|
return redirect(url_for('index'))
|
|
|
|
except IntegrityError:
|
|
db.session.rollback()
|
|
flash(_('Steam Key already exists!'), 'danger')
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
flash(_('Error: ') + str(e), 'danger')
|
|
|
|
return render_template('add_game.html')
|
|
|
|
@app.route('/edit/<int:game_id>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def edit_game(game_id):
|
|
game = db.session.get(Game, game_id)
|
|
if not game or game.owner != current_user:
|
|
abort(404)
|
|
|
|
if not game or game.owner != current_user:
|
|
abort(403)
|
|
|
|
active_redeem = RedeemToken.query.filter(
|
|
RedeemToken.game_id == game_id,
|
|
RedeemToken.expires > datetime.utcnow()
|
|
).first()
|
|
|
|
redeem_url = url_for('redeem_page', token=active_redeem.token, _external=True) if active_redeem else None
|
|
|
|
if request.method == 'POST':
|
|
try:
|
|
url = request.form.get('url', '')
|
|
steam_appid = request.form.get('steam_appid', '').strip()
|
|
|
|
if not steam_appid:
|
|
steam_appid = extract_steam_appid(url)
|
|
|
|
game.name = request.form['name']
|
|
game.steam_key = request.form['steam_key']
|
|
game.status = request.form['status']
|
|
game.recipient = request.form.get('recipient', '')
|
|
game.notes = request.form.get('notes', '')
|
|
game.url = url
|
|
game.steam_appid = steam_appid
|
|
game.redeem_date = datetime.strptime(request.form['redeem_date'], '%Y-%m-%d') if request.form['redeem_date'] else None
|
|
|
|
db.session.commit()
|
|
flash(_('Changes saved!'), 'success')
|
|
return redirect(url_for('index'))
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
flash(_('Error: ') + str(e), 'danger')
|
|
|
|
return render_template('edit_game.html',
|
|
game=game,
|
|
redeem_url=redeem_url,
|
|
active_redeem=active_redeem,
|
|
redeem_date=game.redeem_date.strftime('%Y-%m-%d') if game.redeem_date else '')
|
|
|
|
|
|
@app.route('/delete/<int:game_id>', methods=['POST'])
|
|
@login_required
|
|
def delete_game(game_id):
|
|
game = db.session.get(Game, game_id)
|
|
if not game or game.owner != current_user:
|
|
abort(404)
|
|
|
|
if game.owner != current_user:
|
|
abort(403)
|
|
|
|
try:
|
|
db.session.delete(game)
|
|
db.session.commit()
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
@app.route('/export', methods=['GET'])
|
|
@login_required
|
|
def export_games():
|
|
games = Game.query.filter_by(user_id=current_user.id).all()
|
|
output = io.StringIO()
|
|
writer = csv.writer(output)
|
|
|
|
writer.writerow(['Name', 'Steam Key', 'Status', 'Recipient', 'Notes', 'URL', 'Created', 'Redeem by', 'Steam AppID'])
|
|
|
|
for game in games:
|
|
writer.writerow([
|
|
game.name,
|
|
game.steam_key,
|
|
game.status,
|
|
game.recipient,
|
|
game.notes,
|
|
game.url,
|
|
game.created_at.strftime('%Y-%m-%d %H:%M:%S') if game.created_at else '',
|
|
game.redeem_date.strftime('%Y-%m-%d') if game.redeem_date else '',
|
|
game.steam_appid
|
|
])
|
|
|
|
output.seek(0)
|
|
return send_file(
|
|
io.BytesIO(output.getvalue().encode('utf-8')),
|
|
mimetype='text/csv',
|
|
as_attachment=True,
|
|
download_name='games_export.csv'
|
|
)
|
|
|
|
|
|
@app.route('/export_pdf')
|
|
@login_required
|
|
def export_pdf():
|
|
excluded_statuses = ['eingelöst', 'verschenkt']
|
|
|
|
games = Game.query.filter(
|
|
Game.user_id == current_user.id,
|
|
Game.status.notin_(excluded_statuses)
|
|
).order_by(Game.created_at.desc()).all()
|
|
|
|
buffer = io.BytesIO()
|
|
doc = SimpleDocTemplate(buffer,
|
|
pagesize=landscape(A4),
|
|
leftMargin=40,
|
|
rightMargin=40,
|
|
topMargin=40,
|
|
bottomMargin=40
|
|
)
|
|
|
|
styles = getSampleStyleSheet()
|
|
elements = []
|
|
img_height = 2*cm
|
|
|
|
# Titel
|
|
elements.append(Paragraph(_("Game List (without Keys)"), styles['Title']))
|
|
elements.append(Spacer(1, 12))
|
|
|
|
# Tabellenkopf
|
|
col_widths = [
|
|
5*cm, 10*cm, 6*cm, 3*cm
|
|
]
|
|
data = [[
|
|
Paragraph('<b>Cover</b>', styles['Normal']),
|
|
Paragraph('<b>Name</b>', styles['Normal']),
|
|
Paragraph('<b>Shop-Link</b>', styles['Normal']),
|
|
Paragraph('<b>Einlösen bis</b>', styles['Normal'])
|
|
]]
|
|
|
|
for game in games:
|
|
img = None
|
|
if game.steam_appid:
|
|
try:
|
|
img_url = f"https://cdn.cloudflare.steamstatic.com/steam/apps/{game.steam_appid}/header.jpg"
|
|
img_data = io.BytesIO(requests.get(img_url, timeout=5).content)
|
|
img = Image(img_data, width=3*cm, height=img_height)
|
|
except Exception:
|
|
img = Paragraph('', styles['Normal'])
|
|
elif game.url and 'gog.com' in game.url:
|
|
try:
|
|
img_path = os.path.join(app.root_path, 'static', 'gog_logo.webp')
|
|
img = Image(img_path, width=3*cm, height=img_height)
|
|
except Exception:
|
|
img = Paragraph('', styles['Normal'])
|
|
|
|
data.append([
|
|
img or '',
|
|
Paragraph(game.name, styles['Normal']),
|
|
Paragraph(game.url or '', styles['Normal']),
|
|
game.redeem_date.strftime('%d.%m.%y') if game.redeem_date else ''
|
|
])
|
|
|
|
# Table format (korrekte Einrückung)
|
|
table = Table(data, colWidths=col_widths, repeatRows=1)
|
|
table.setStyle(TableStyle([
|
|
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
|
|
('FONTSIZE', (0,0), (-1,0), 8),
|
|
('FONTSIZE', (0,1), (-1,-1), 8),
|
|
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
|
|
('ALIGN', (0,0), (-1,-1), 'LEFT'),
|
|
('GRID', (0,0), (-1,-1), 0.5, colors.lightgrey),
|
|
('WORDWRAP', (1,1), (1,-1), 'CJK'),
|
|
]))
|
|
|
|
elements.append(table)
|
|
doc.build(elements)
|
|
|
|
buffer.seek(0)
|
|
return send_file(
|
|
buffer,
|
|
mimetype='application/pdf',
|
|
as_attachment=True,
|
|
download_name=f'game_export_{datetime.now().strftime("%Y%m%d")}.pdf'
|
|
)
|
|
|
|
|
|
@app.route('/import', methods=['GET', 'POST'])
|
|
@login_required
|
|
def import_games():
|
|
if request.method == 'POST':
|
|
file = request.files.get('file')
|
|
|
|
if file and file.filename.endswith('.csv'):
|
|
stream = io.StringIO(file.stream.read().decode("UTF8"))
|
|
reader = csv.DictReader(stream)
|
|
new_games = 0
|
|
duplicates = 0
|
|
|
|
try:
|
|
with db.session.begin_nested():
|
|
for row in reader:
|
|
steam_key = row['Steam Key'].strip()
|
|
|
|
if Game.query.filter_by(steam_key=steam_key).first():
|
|
duplicates += 1
|
|
continue
|
|
|
|
game = Game(
|
|
name=row['Name'],
|
|
steam_key=steam_key,
|
|
status=row['Status'],
|
|
recipient=row.get('Recipient', ''),
|
|
notes=row.get('Notes', ''),
|
|
url=row.get('URL', ''),
|
|
created_at=datetime.strptime(row['Created'], '%Y-%m-%d %H:%M:%S') if row.get('Created') else datetime.utcnow(),
|
|
redeem_date=datetime.strptime(row['Redeem by'], '%Y-%m-%d') if row.get('Redeem by') else None,
|
|
steam_appid=row.get('Steam AppID', ''),
|
|
user_id=current_user.id
|
|
)
|
|
|
|
db.session.add(game)
|
|
new_games += 1
|
|
|
|
db.session.commit()
|
|
|
|
flash(_('%(new)d new games imported, %(dup)d skipped duplicates', new=new_games, dup=duplicates), 'success')
|
|
|
|
except Exception as e:
|
|
db.session.rollback()
|
|
flash(_('Import error: %(error)s', error=str(e)), 'danger')
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
flash(_('Please upload a valid CSV file.'), 'danger')
|
|
|
|
return render_template('import.html')
|
|
|
|
@app.route('/generate_redeem/<int:game_id>', methods=['POST'])
|
|
@login_required
|
|
def generate_redeem(game_id):
|
|
game = db.session.get(Game, game_id)
|
|
if not game or game.owner != current_user:
|
|
abort(403)
|
|
|
|
if game.owner != current_user or game.status != 'verschenkt':
|
|
abort(403)
|
|
|
|
try:
|
|
token = secrets.token_urlsafe(12)[:17]
|
|
expires = datetime.utcnow() + timedelta(hours=24)
|
|
total_hours = 24
|
|
|
|
RedeemToken.query.filter_by(game_id=game_id).delete()
|
|
|
|
new_token = RedeemToken(
|
|
token=token,
|
|
game_id=game_id,
|
|
expires=expires,
|
|
total_hours=24
|
|
)
|
|
|
|
db.session.add(new_token)
|
|
db.session.commit()
|
|
|
|
redeem_url = url_for('redeem_page', token=token, _external=True)
|
|
return jsonify({'url': redeem_url})
|
|
|
|
except Exception as e:
|
|
app.logger.error(f"Redeem error: {str(e)}")
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/redeem/<token>')
|
|
def redeem_page(token):
|
|
redeem_token = RedeemToken.query.filter_by(token=token).first()
|
|
|
|
if not redeem_token:
|
|
abort(404)
|
|
if redeem_token.expires < datetime.utcnow():
|
|
db.session.delete(redeem_token)
|
|
db.session.commit()
|
|
abort(404)
|
|
|
|
game = Game.query.get(redeem_token.game_id)
|
|
redeem_token.used = True
|
|
db.session.commit()
|
|
|
|
return render_template('redeem.html',
|
|
game=game,
|
|
redeem_token=redeem_token,
|
|
platform_link='https://store.steampowered.com/account/registerkey?key=' if game.steam_appid else 'https://www.gog.com/redeem')
|
|
|
|
@app.route('/debug-session')
|
|
def debug_session():
|
|
return jsonify({
|
|
'session_lang': session.get('lang'),
|
|
'config_locales': app.config['BABEL_SUPPORTED_LOCALES']
|
|
})
|
|
|
|
# Apprise Notifications
|
|
import apprise
|
|
|
|
def send_apprise_notification(user, game):
|
|
apprise_urls = os.getenv('APPRISE_URLS', '').strip()
|
|
if not apprise_urls:
|
|
app.logger.error("No APPRISE_URLS configured")
|
|
return False
|
|
|
|
apobj = apprise.Apprise()
|
|
for url in apprise_urls.replace(',', '\n').splitlines():
|
|
if url.strip():
|
|
apobj.add(url.strip())
|
|
|
|
edit_url = url_for('edit_game', game_id=game.id, _external=True)
|
|
result = apobj.notify(
|
|
title="Steam-Key läuft ab!",
|
|
body=f"Dein Key für '{game.name}' läuft in weniger als 48 Stunden ab!\n\nLink: {edit_url}",
|
|
)
|
|
return result
|
|
|
|
def send_notification(user, game):
|
|
return send_apprise_notification(user, game)
|
|
|
|
def check_expiring_keys():
|
|
with app.app_context():
|
|
now = datetime.utcnow()
|
|
expiry_threshold = now + timedelta(hours=48)
|
|
|
|
# Moderner Select-Aufruf
|
|
stmt = select(Game).where(
|
|
Game.status != 'eingelöst',
|
|
Game.redeem_date <= expiry_threshold,
|
|
Game.redeem_date > now
|
|
)
|
|
|
|
expiring_games = db.session.execute(stmt).scalars().all()
|
|
|
|
for game in expiring_games:
|
|
user = User.query.get(game.user_id)
|
|
if user.notification_service and user.notification_service != 'none':
|
|
send_notification(user, game)
|
|
|
|
|
|
# Optional: cleaning up old tokens
|
|
def cleanup_expired_tokens():
|
|
now = datetime.utcnow()
|
|
expired = RedeemToken.query.filter(RedeemToken.expires < now).all()
|
|
for token in expired:
|
|
db.session.delete(token)
|
|
db.session.commit()
|
|
|
|
|
|
# Scheduler start
|
|
scheduler = BackgroundScheduler()
|
|
scheduler.add_job(func=check_expiring_keys, trigger="interval", hours=interval_hours)
|
|
scheduler.add_job(func=cleanup_expired_tokens, trigger="interval", hours=1)
|
|
scheduler.start()
|
|
|
|
# Shutdown of the Schedulers when stopping the app
|
|
atexit.register(lambda: scheduler.shutdown())
|
|
|
|
if __name__ == '__main__':
|
|
with app.app_context():
|
|
db.create_all()
|
|
app.run(host='0.0.0.0', port=5000)
|
|
|
|
PYTHON_END
|
|
|
|
# Create Babel configuration
|
|
cat <<EOL > babel.cfg
|
|
[python: **.py]
|
|
[jinja2: **/templates/**.html]
|
|
extensions=jinja2.ext.autoescape,jinja2.ext.with_
|
|
EOL
|
|
|
|
# 5. Dockerfile
|
|
cat <<DOCKER_END > Dockerfile
|
|
FROM python:3.10-slim
|
|
|
|
SHELL ["/bin/bash", "-c"]
|
|
|
|
RUN apt-get update && apt-get install -y --no-install-recommends wget \
|
|
&& mkdir -p /app/static \
|
|
&& wget -O /app/static/logo.webp "https://drop.nocadmin.net/logo.webp" \
|
|
&& wget -O /app/static/logo_small.webp "https://drop.nocadmin.net/logo_small.webp" \
|
|
&& wget -O /app/static/forgejo.webp "https://drop.nocadmin.net/forgejo.webp" \
|
|
&& wget -O /app/static/gog_logo.webp "https://drop.nocadmin.net/gog_logo.webp" \
|
|
&& wget -O /app/static/logo_small_maskable.webp "https://drop.nocadmin.net/logo_small_maskable.webp" \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
RUN mkdir -p /app/data && \
|
|
chown -R 1000:1000 /app/data
|
|
|
|
ENV TZ=${TZ}
|
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
|
|
|
|
|
WORKDIR /app
|
|
COPY requirements.txt .
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
COPY . .
|
|
|
|
ARG UID=1000
|
|
ARG GID=1000
|
|
RUN groupadd -g \$GID appuser && \
|
|
useradd -u \$UID -g \$GID -m appuser && \
|
|
chown -R appuser:appuser /app
|
|
|
|
USER appuser
|
|
|
|
EXPOSE 5000
|
|
|
|
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]
|
|
DOCKER_END
|
|
|
|
# 6. docker-compose.yml
|
|
cat <<COMPOSE_END > docker-compose.yml
|
|
services:
|
|
steam-manager:
|
|
build: .
|
|
ports:
|
|
- "5000:5000"
|
|
environment:
|
|
- REGISTRATION_ENABLED=${REGISTRATION_ENABLED:-True}
|
|
- TZ=${TZ}
|
|
volumes:
|
|
- ../data:/app/data
|
|
- ../translations:/app/translations:rw
|
|
- ../.env:/app/.env
|
|
user: "${UID}:${GID}"
|
|
restart: unless-stopped
|
|
|
|
COMPOSE_END
|
|
|
|
# 7. Directories and permissions
|
|
mkdir -p ../data ../translations
|
|
chmod -R a+rwX ../data ../translations
|
|
find ../data ../translations -type d -exec chmod 775 {} \;
|
|
find ../data ../translations -type f -exec chmod 664 {} \;
|
|
|
|
# 8. Translation and upgrade scripts
|
|
cat <<'SCRIPT_END' > ../translate.sh
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
cd "$(dirname "$0")/steam-gift-manager"
|
|
|
|
declare -a locales=("de" "en")
|
|
|
|
docker-compose run --rm steam-manager pybabel extract -F babel.cfg -o translations/messages.pot .
|
|
|
|
for lang in "${locales[@]}"; do
|
|
po_path="translations/${lang}/LC_MESSAGES/messages.po"
|
|
|
|
if [ -f "$po_path" ]; then
|
|
encoding=$(file -i "$po_path" | grep -Po 'charset=\K.+')
|
|
|
|
if [[ "$encoding" != "utf-8" ]]; then
|
|
echo "Konvertiere $po_path von $encoding nach UTF-8"
|
|
iconv -f "$encoding" -t UTF-8 "$po_path" > "$po_path.utf8"
|
|
mv "$po_path.utf8" "$po_path"
|
|
fi
|
|
fi
|
|
|
|
docker-compose run --rm steam-manager pybabel update \
|
|
-i translations/messages.pot \
|
|
-d translations \
|
|
-l "$lang" \
|
|
--previous
|
|
done
|
|
|
|
docker-compose run --rm steam-manager pybabel compile -d translations
|
|
|
|
echo "✅ Setup and/or update translations done! YOLO"
|
|
|
|
SCRIPT_END
|
|
chmod +x ../translate.sh
|
|
|
|
cat <<'SCRIPT_END' > ../upgrade.sh
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
# Set the working directory to the project directory
|
|
cd "$(dirname "$0")/steam-gift-manager"
|
|
|
|
# Setze FLASK_APP, falls nötig
|
|
export FLASK_APP=app.py
|
|
|
|
# Initialize migrations, if not yet available
|
|
if [ ! -d migrations ]; then
|
|
echo "Starting Flask-Migrate..."
|
|
docker-compose exec steam-manager flask db init
|
|
fi
|
|
|
|
# Create migration (only if models have changed)
|
|
docker-compose exec steam-manager flask db migrate -m "Automatic Migration"
|
|
|
|
# Apply migration
|
|
docker-compose exec steam-manager flask db upgrade
|
|
|
|
echo "✅ Database migration completed!"
|
|
SCRIPT_END
|
|
chmod +x ../upgrade.sh
|
|
|
|
# Manifest for PWA
|
|
cat <<MANIFEST_END > static/manifest.json
|
|
{
|
|
"id": "/",
|
|
"name": "Game Key Manager",
|
|
"short_name": "GameKeys",
|
|
"start_url": "/",
|
|
"display": "standalone",
|
|
"background_color": "#212529",
|
|
"theme_color": "#212529",
|
|
"description": "Manage Steam/GOG keys easily!",
|
|
"orientation": "any",
|
|
"launch_handler": {
|
|
"client_mode": "navigate-existing"
|
|
},
|
|
"icons": [
|
|
{
|
|
"src": "/static/logo_small.webp",
|
|
"sizes": "192x192",
|
|
"type": "image/webp",
|
|
"purpose": "any"
|
|
},
|
|
{
|
|
"src": "/static/logo_small_maskable.webp",
|
|
"sizes": "192x192",
|
|
"type": "image/webp",
|
|
"purpose": "maskable"
|
|
},
|
|
{
|
|
"src": "/static/logo.webp",
|
|
"sizes": "512x512",
|
|
"type": "image/webp",
|
|
"purpose": "any maskable"
|
|
}
|
|
]
|
|
}
|
|
MANIFEST_END
|
|
|
|
|
|
# Service Worker
|
|
cat <<SW_END > static/serviceworker.js
|
|
const CACHE_NAME = 'game-key-manager-v1';
|
|
const ASSETS = [
|
|
'/',
|
|
'/static/style.css',
|
|
'/static/logo.webp',
|
|
'/static/logo_small.webp',
|
|
'/static/gog_logo.webp',
|
|
'/static/forgejo.webp'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.addAll(ASSETS))
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(cachedResponse => cachedResponse || fetch(event.request))
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys => Promise.all(
|
|
keys.filter(key => key !== CACHE_NAME)
|
|
.map(key => caches.delete(key))
|
|
))
|
|
);
|
|
});
|
|
SW_END
|
|
|
|
|
|
# 9. Templates
|
|
mkdir -p templates static
|
|
|
|
# Base Template
|
|
cat <<HTML_END > templates/base.html
|
|
<!DOCTYPE html>
|
|
<html lang="{{ get_locale() if get_locale() in ['en', 'de'] else 'en' }}" data-bs-theme="{{ theme }}">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="csrf-token" content="{{ csrf_token() }}">
|
|
<meta name="description" content="Manage your Steam and GOG keys efficiently. Track redemption dates, share games, and export lists.">
|
|
<meta name="theme-color" content="#212529">
|
|
<title>{{ _('Game Key Manager') }}</title>
|
|
<!-- PWA Manifest -->
|
|
<link rel="manifest" href="{{ url_for('static', filename='manifest.json') }}">
|
|
<!-- Preload Bootstrap CSS for better LCP -->
|
|
<link rel="preload" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
|
|
<noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"></noscript>
|
|
<!-- Critical CSS (Above-the-Fold) kann hier inline ergänzt werden -->
|
|
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
|
</head>
|
|
<body>
|
|
<nav class="navbar navbar-expand-lg bg-body-tertiary">
|
|
<div class="container">
|
|
<a class="navbar-brand d-flex align-items-center gap-2" href="/">
|
|
<img src="{{ url_for('static', filename='logo_small.webp') }}" alt="Logo" width="150" height="116" style="object-fit:contain; border-radius:8px;">
|
|
<span>Game Key Manager</span>
|
|
</a>
|
|
<div class="d-flex align-items-center gap-3">
|
|
<form class="d-flex" action="{{ url_for('index') }}" method="GET">
|
|
<label for="searchInput" class="visually-hidden">{{ _('Search') }}</label>
|
|
<input class="form-control me-2"
|
|
type="search"
|
|
name="q"
|
|
id="searchInput"
|
|
placeholder="{{ _('Search') }}"
|
|
value="{{ search_query }}">
|
|
<button class="btn btn-outline-success" type="submit">🔍</button>
|
|
</form>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input"
|
|
type="checkbox"
|
|
id="darkModeSwitch" {% if theme == 'dark' %}checked{% endif %}>
|
|
<label class="form-check-label" for="darkModeSwitch">{{ _('Dark Mode') }}</label>
|
|
</div>
|
|
<div class="dropdown ms-3">
|
|
<div hidden id="locale-debug" data-locale="{{ get_locale() }}"></div>
|
|
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
|
{% if get_locale() == 'de' %} Deutsch {% elif get_locale() == 'en' %} English {% else %} Sprache {% endif %}
|
|
</button>
|
|
<ul class="dropdown-menu">
|
|
<li><a class="dropdown-item {% if get_locale() == 'de' %}active{% endif %}" href="{{ url_for('set_lang', lang='de') }}">Deutsch</a></li>
|
|
<li><a class="dropdown-item {% if get_locale() == 'en' %}active{% endif %}" href="{{ url_for('set_lang', lang='en') }}">English</a></li>
|
|
</ul>
|
|
</div>
|
|
{% if current_user.is_authenticated %}
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="{{ url_for('change_password') }}">🔒 {{ _('Password') }}</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="{{ url_for('logout') }}">🚪 {{ _('Logout') }}</a>
|
|
</li>
|
|
{% endif %}
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
<div class="container mt-4">
|
|
{% with messages = get_flashed_messages(with_categories=true) %}
|
|
{% if messages %}
|
|
{% for category, message in messages %}
|
|
<div class="alert alert-{{ category }} alert-dismissible fade show">
|
|
{{ message }}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
{% endfor %}
|
|
{% endif %}
|
|
{% endwith %}
|
|
{% block content %}{% endblock %}
|
|
</div>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
// Service Worker Registration for PWA
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker.register('{{ url_for("static", filename="serviceworker.js") }}', {scope: '/'})
|
|
.then(registration => {
|
|
console.log('ServiceWorker registered:', registration.scope);
|
|
})
|
|
.catch(error => {
|
|
console.log('ServiceWorker registration failed:', error);
|
|
});
|
|
});
|
|
}
|
|
// Dark Mode Switch
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const toggle = document.getElementById('darkModeSwitch')
|
|
const html = document.documentElement
|
|
if (toggle) {
|
|
toggle.addEventListener('change', function() {
|
|
const theme = this.checked ? 'dark' : 'light'
|
|
fetch('/set-theme/' + theme)
|
|
.then(() => html.setAttribute('data-bs-theme', theme))
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
{% include "footer.html" %}
|
|
</body>
|
|
</html>
|
|
HTML_END
|
|
|
|
# Index Template
|
|
cat <<HTML_END > templates/index.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>{{ _('My Games') }}</h1>
|
|
<div>
|
|
<a href="{{ url_for('export_games') }}" class="btn btn-outline-secondary">⬇️ {{ _('Export CSV') }}</a>
|
|
<a href="{{ url_for('export_pdf') }}" class="btn btn-outline-secondary">⬇️ Export PDF (for sharing)</a>
|
|
<a href="{{ url_for('import_games') }}" class="btn btn-outline-secondary">⬆️ {{ _('Import CSV') }}</a>
|
|
<a href="{{ url_for('add_game') }}" class="btn btn-primary">+ {{ _('Add New Game') }}</a>
|
|
</div>
|
|
</div>
|
|
|
|
{% if games %}
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>{{ _('Cover') }}</th>
|
|
<th>{{ _('Name') }}</th>
|
|
<th>{{ _('Key') }}</th>
|
|
<th>{{ _('Status') }}</th>
|
|
<th>{{ _('Created') }}</th>
|
|
<th>{{ _('Redeem by') }}</th>
|
|
<th>{{ _('Shop') }}</th>
|
|
<th>{{ _('Actions') }}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{% for game in games %}
|
|
<tr>
|
|
<td>
|
|
{% if game.steam_appid %}
|
|
<img src="https://cdn.cloudflare.steamstatic.com/steam/apps/{{ game.steam_appid }}/header.jpg"
|
|
alt="Steam Header"
|
|
class="game-cover"
|
|
{% if loop.first %}fetchpriority="high"{% endif %}
|
|
width="368"
|
|
height="172">
|
|
{% elif game.url and 'gog.com' in game.url %}
|
|
<img src="{{ url_for('static', filename='gog_logo.webp') }}"
|
|
alt="GOG Logo"
|
|
class="game-cover"
|
|
width="368"
|
|
height="172">
|
|
{% endif %}
|
|
</td>
|
|
<td>{{ game.name }}</td>
|
|
<td class="font-monospace">{{ game.steam_key }}</td>
|
|
<td>
|
|
{% if game.status == 'nicht eingelöst' %}
|
|
<span class="badge bg-warning text-dark">{{ _('Not redeemed') }}</span>
|
|
{% elif game.status == 'verschenkt' %}
|
|
<span class="badge bg-success">{{ _('Gifted') }}</span>
|
|
{% elif game.status == 'eingelöst' %}
|
|
<span class="badge bg-secondary">{{ _('Redeemed') }}</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>{{ format_date(game.created_at) }}</td>
|
|
<td>
|
|
{% if game.redeem_date %}
|
|
<span class="badge bg-danger">{{ format_date(game.redeem_date) }}</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
{% if game.url %}
|
|
<a href="{{ game.url }}" target="_blank" class="btn btn-sm btn-outline-info">🔗 {{ _('Shop') }}</a>
|
|
{% endif %}
|
|
</td>
|
|
<td class="text-nowrap">
|
|
{% if game.status == 'verschenkt' %}
|
|
<button class="btn btn-sm btn-success generate-redeem"
|
|
data-game-id="{{ game.id }}"
|
|
title="{{ _('Generate redeem link') }}">
|
|
🔗
|
|
</button>
|
|
{% endif %}
|
|
<a href="{{ url_for('edit_game', game_id=game.id) }}" class="btn btn-sm btn-warning">✏️</a>
|
|
<form method="POST" action="{{ url_for('delete_game', game_id=game.id) }}" class="d-inline">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('{{ _('Really delete?') }}')">🗑️</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<script>
|
|
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
|
|
|
document.querySelectorAll('.generate-redeem').forEach(btn => {
|
|
btn.addEventListener('click', async function() {
|
|
const gameId = this.dataset.gameId;
|
|
try {
|
|
const response = await fetch('/generate_redeem/' + gameId, {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-CSRFToken': csrfToken
|
|
}
|
|
});
|
|
if (!response.ok) throw new Error('Network error');
|
|
const data = await response.json();
|
|
if(data.url) {
|
|
await navigator.clipboard.writeText(data.url);
|
|
alert('{{ _("Redeem link copied to clipboard!") }}');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
alert('{{ _("Error generating link") }}');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
{% else %}
|
|
<div class="alert alert-info">{{ _('No games yet') }}</div>
|
|
{% endif %}
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
# Login Template
|
|
cat <<HTML_END > templates/login.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="row justify-content-center mt-5">
|
|
<div class="col-md-6">
|
|
<div class="card shadow-sm">
|
|
<div class="card-body text-center">
|
|
<img src="{{ url_for('static', filename='logo.webp') }}" alt="Logo" width="266" height="206" class="mb-4" style="object-fit:contain;">
|
|
<h2 class="card-title mb-4">{{ _('Login') }}</h2>
|
|
<form method="POST">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Username') }}</label>
|
|
<input type="text" name="username" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Password') }}</label>
|
|
<input type="password" name="password" class="form-control" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">{{ _('Login') }}</button>
|
|
</form>
|
|
<div class="mt-3 text-center">
|
|
<a href="{{ url_for('register') }}">{{ _('No account yet? Register') }}</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
HTML_END
|
|
|
|
# Register Template
|
|
cat <<HTML_END > templates/register.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="row justify-content-center mt-5">
|
|
<div class="col-md-6">
|
|
<div class="card shadow-sm">
|
|
<div class="card-body">
|
|
<h2 class="card-title mb-4">{{ _('Register') }}</h2>
|
|
<form method="POST">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Username') }}</label>
|
|
<input type="text" name="username" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Password') }}</label>
|
|
<input type="password" name="password" class="form-control" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">{{ _('Register') }}</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
# Change Password Template
|
|
cat <<HTML_END > templates/change_password.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="card p-4 shadow-sm">
|
|
<h2 class="mb-4">{{ _('Change Password') }}</h2>
|
|
<form method="POST">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Current Password') }}</label>
|
|
<input type="password" name="current_password" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('New Password') }}</label>
|
|
<input type="password" name="new_password" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Confirm New Password') }}</label>
|
|
<input type="password" name="confirm_password" class="form-control" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">{{ _('Change Password') }}</button>
|
|
</form>
|
|
</div>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
|
|
# Edit Game Template
|
|
cat <<HTML_END > templates/edit_game.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="card p-4 shadow-sm">
|
|
<h2 class="mb-4">{{ _('Edit Game') }}</h2>
|
|
<form method="POST">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="row g-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label">{{ _('Name') }} *</label>
|
|
<input type="text" name="name" class="form-control" value="{{ game.name }}" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">{{ _('Game Key') }} *</label>
|
|
<input type="text" name="steam_key" class="form-control" value="{{ game.steam_key }}" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">{{ _('Steam AppID (optional)') }}</label>
|
|
<input type="text" name="steam_appid" class="form-control" value="{{ game.steam_appid or '' }}">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Status') }} *</label>
|
|
<select name="status" class="form-select" required>
|
|
<option value="nicht eingelöst" {% if game.status == 'nicht eingelöst' %}selected{% endif %}>{{ _('Not redeemed') }}</option>
|
|
<option value="verschenkt" {% if game.status == 'verschenkt' %}selected{% endif %}>{{ _('Gifted') }}</option>
|
|
<option value="eingelöst" {% if game.status == 'eingelöst' %}selected{% endif %}>{{ _('Redeemed') }}</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Redeem by') }}</label>
|
|
<input type="date" name="redeem_date" class="form-control" value="{{ redeem_date }}">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Recipient') }}</label>
|
|
<input type="text" name="recipient" class="form-control" value="{{ game.recipient }}">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">{{ _('Shop URL') }}</label>
|
|
<input type="url" name="url" class="form-control" value="{{ game.url }}">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">{{ _('Notes') }}</label>
|
|
<textarea name="notes" class="form-control" rows="3">{{ game.notes }}</textarea>
|
|
</div>
|
|
<div class="col-12">
|
|
{% if redeem_url and active_redeem %}
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Active Redeem Link') }}</label>
|
|
<input type="text"
|
|
class="form-control"
|
|
value="{{ redeem_url }}"
|
|
readonly
|
|
onclick="this.select()">
|
|
<small class="text-muted">
|
|
{{ _('Expires at') }}: {{ active_redeem.expires.strftime('%d.%m.%Y %H:%M') }}
|
|
</small>
|
|
</div>
|
|
{% endif %}
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-primary">{{ _('Save') }}</button>
|
|
<a href="{{ url_for('index') }}" class="btn btn-outline-secondary">{{ _('Cancel') }}</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
cat <<HTML_END > templates/add_game.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="card p-4 shadow-sm">
|
|
<h2 class="mb-4">{{ _('Add New Game') }}</h2>
|
|
<form method="POST">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="row g-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label">{{ _('Name') }} *</label>
|
|
<input type="text" name="name" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">{{ _('Game Key') }} *</label>
|
|
<input type="text" name="steam_key" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Status') }} *</label>
|
|
<select name="status" class="form-select" required>
|
|
<option value="nicht eingelöst">{{ _('Not redeemed') }}</option>
|
|
<option value="verschenkt">{{ _('Gifted') }}</option>
|
|
<option value="eingelöst">{{ _('Redeemed') }}</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Redeem by') }}</label>
|
|
<input type="date" name="redeem_date" class="form-control">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label">{{ _('Recipient') }}</label>
|
|
<input type="text" name="recipient" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">{{ _('Shop URL') }}</label>
|
|
<input type="url" name="url" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">{{ _('Notes') }}</label>
|
|
<textarea name="notes" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
<div class="col-12">
|
|
<button type="submit" class="btn btn-success">{{ _('Save') }}</button>
|
|
<a href="{{ url_for('index') }}" class="btn btn-outline-secondary">{{ _('Cancel') }}</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
# Import/Export Templates
|
|
cat <<HTML_END > templates/import.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="card p-4 shadow-sm">
|
|
<h2 class="mb-4">{{ _('Import Games') }}</h2>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
|
<div class="mb-3">
|
|
<label class="form-label">{{ _('Select CSV file') }}</label>
|
|
<input type="file" name="file" class="form-control" accept=".csv" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-success">{{ _('Import') }}</button>
|
|
<a href="{{ url_for('index') }}" class="btn btn-outline-secondary">{{ _('Cancel') }}</a>
|
|
</form>
|
|
</div>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
# Redeem Template
|
|
cat <<'HTML_END' > templates/redeem.html
|
|
{% extends "base.html" %}
|
|
{% block content %}
|
|
<div class="container mt-5">
|
|
<div class="card shadow-lg">
|
|
<div class="row g-0">
|
|
{% if game.steam_appid %}
|
|
<div class="col-md-4">
|
|
<img src="https://cdn.cloudflare.steamstatic.com/steam/apps/{{ game.steam_appid }}/header.jpg"
|
|
class="img-fluid rounded-start" alt="Game Cover">
|
|
</div>
|
|
{% endif %}
|
|
<div class="col-md-8">
|
|
<div class="card-body">
|
|
<h1 class="card-title mb-4">{{ game.name }}</h1>
|
|
<div class="alert alert-success">
|
|
<h4>{{ _('Your Key:') }}</h4>
|
|
<code class="fs-3">{{ game.steam_key }}</code>
|
|
</div>
|
|
<a href="{{ platform_link }}{{ game.steam_key }}"
|
|
class="btn btn-primary btn-lg mb-3"
|
|
target="_blank">
|
|
{{ _('Redeem now on') }} {% if game.steam_appid %}Steam{% else %}GOG{% endif %}
|
|
</a>
|
|
<div class="mt-4 text-muted">
|
|
<small>
|
|
{{ _('This page will expire in') }}
|
|
<span id="expiry-countdown" class="fw-bold"></span>
|
|
</small>
|
|
<div class="progress mt-2" style="height: 8px;">
|
|
<div id="expiry-bar"
|
|
class="progress-bar bg-danger"
|
|
role="progressbar"
|
|
style="width: 100%">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
const totalDuration = {{ redeem_token.total_hours * 3600 * 1000 }}; // Gesamtdauer in Millisekunden
|
|
const expires = {{ (redeem_token.expires.timestamp() * 1000) | int }};
|
|
const countdownEl = document.getElementById('expiry-countdown');
|
|
const progressBar = document.getElementById('expiry-bar');
|
|
|
|
function formatTime(unit) {
|
|
return unit < 10 ? `0${unit}` : unit;
|
|
}
|
|
|
|
function updateProgressBar(percentage) {
|
|
// Alle Farbklassen entfernen
|
|
progressBar.classList.remove('bg-success', 'bg-warning', 'bg-danger');
|
|
|
|
if (percentage > 75) {
|
|
progressBar.classList.add('bg-success');
|
|
} else if (percentage > 25) {
|
|
progressBar.classList.add('bg-warning');
|
|
} else {
|
|
progressBar.classList.add('bg-danger');
|
|
}
|
|
}
|
|
|
|
function updateCountdown() {
|
|
const now = Date.now();
|
|
const remaining = expires - now;
|
|
const percent = (remaining / totalDuration) * 100;
|
|
|
|
if (remaining < 0) {
|
|
countdownEl.innerHTML = "EXPIRED";
|
|
progressBar.style.width = "0%";
|
|
clearInterval(timer);
|
|
setTimeout(() => window.location.reload(), 5000);
|
|
return;
|
|
}
|
|
|
|
const hours = Math.floor(remaining / (1000 * 60 * 60));
|
|
const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60));
|
|
const seconds = Math.floor((remaining % (1000 * 60)) / 1000);
|
|
|
|
countdownEl.innerHTML = `${formatTime(hours)}h ${formatTime(minutes)}m ${formatTime(seconds)}s`;
|
|
progressBar.style.width = `${percent}%`;
|
|
updateProgressBar(percent);
|
|
}
|
|
|
|
// run countdown
|
|
updateCountdown();
|
|
const timer = setInterval(updateCountdown, 1000);
|
|
</script>
|
|
{% endblock %}
|
|
HTML_END
|
|
|
|
# Footer Template
|
|
cat <<HTML_END > templates/footer.html
|
|
<footer class="mt-5 py-4 bg-body-tertiary border-top">
|
|
<div class="container text-center small text-muted">
|
|
<div class="mb-2">
|
|
<strong>Game Key Manager</strong> — is done by nocci
|
|
</div>
|
|
<div class="mb-2">
|
|
<a href="https://git.nocci.it/nocci/GiftGamesDB" target="_blank" rel="noopener">
|
|
<img src="{{ url_for('static', filename='forgejo.webp') }}" alt="forgejo" width="20" style="vertical-align:middle;margin-right:4px;">
|
|
find the source code on my Forgejo
|
|
</a>
|
|
</div>
|
|
<div>
|
|
<span>feel free to donate - if you can affort it:</span>
|
|
<a href="https://ko-fi.com/nocci" target="_blank" rel="noopener">Ko-fi</a> ·
|
|
<a href="https://liberapay.com/nocci" target="_blank" rel="noopener">Liberapay</a>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
HTML_END
|
|
|
|
|
|
# CSS
|
|
cat <<CSS_END > static/style.css
|
|
:root {
|
|
--bs-body-bg: #ffffff;
|
|
--bs-body-color: #212529;
|
|
}
|
|
[data-bs-theme="dark"] {
|
|
--bs-body-bg: #1a1a1a;
|
|
--bs-body-color: #f8f9fa;
|
|
--bs-border-color: #495057;
|
|
}
|
|
[data-bs-theme="dark"] .table {
|
|
--bs-table-bg: #212529;
|
|
--bs-table-color: #fff;
|
|
--bs-table-border-color: #495057;
|
|
}
|
|
[data-bs-theme="dark"] .card {
|
|
background-color: #2b3035;
|
|
border-color: var(--bs-border-color);
|
|
}
|
|
[data-bs-theme="dark"] .navbar {
|
|
background-color: #212529 !important;
|
|
}
|
|
body {
|
|
background-color: var(--bs-body-bg);
|
|
color: var(--bs-body-color);
|
|
transition: all 0.3s ease;
|
|
}
|
|
.font-monospace {
|
|
font-family: Monaco, Consolas, "Courier New", monospace;
|
|
}
|
|
.badge {
|
|
font-size: 0.9em;
|
|
font-weight: 500;
|
|
}
|
|
#expiry-countdown {
|
|
font-weight: 600;
|
|
letter-spacing: 0.05em;
|
|
color: #dc3545;
|
|
transition: color 0.3s ease;
|
|
}
|
|
|
|
[data-bs-theme="dark"] #expiry-countdown {
|
|
color: #ff6b6b;
|
|
}
|
|
|
|
/* Progressbar-Animations */
|
|
#expiry-bar {
|
|
transition: width 1s linear, background-color 0.5s ease;
|
|
}
|
|
|
|
.bg-success { background-color: #198754 !important; }
|
|
.bg-warning { background-color: #ffc107 !important; }
|
|
.bg-danger { background-color: #dc3545 !important; }
|
|
|
|
.progress-bar {
|
|
transition: width 1s linear, background-color 0.3s ease;
|
|
}
|
|
.table-pdf {
|
|
font-size: 0.8em;
|
|
}
|
|
.table-pdf td, .table-pdf th {
|
|
padding: 4px 8px;
|
|
}
|
|
|
|
.game-cover {
|
|
width: 368px;
|
|
height: 172px;
|
|
max-width: 100%;
|
|
max-height: 35vw;
|
|
object-fit: contain;
|
|
background: #222;
|
|
border-radius: 8px;
|
|
display: block;
|
|
margin: 0 auto;
|
|
transition: width 0.2s, height 0.2s;
|
|
}
|
|
|
|
/* Responsive Cover Images */
|
|
.game-cover {
|
|
width: 368px;
|
|
height: 172px;
|
|
object-fit: contain;
|
|
background: #222;
|
|
border-radius: 6px;
|
|
}
|
|
|
|
@media (max-width: 1200px) {
|
|
.game-cover {
|
|
width: 260px;
|
|
height: 122px;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 992px) {
|
|
.game-cover {
|
|
width: 180px;
|
|
height: 84px;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.game-cover {
|
|
width: 120px;
|
|
height: 56px;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 576px) {
|
|
.game-cover {
|
|
width: 90px;
|
|
height: 42px;
|
|
}
|
|
}
|
|
|
|
/* Accessibility Improvements */
|
|
.visually-hidden {
|
|
position: absolute;
|
|
width: 1px;
|
|
height: 1px;
|
|
padding: 0;
|
|
margin: -1px;
|
|
overflow: hidden;
|
|
clip: rect(0, 0, 0, 0);
|
|
border: 0;
|
|
}
|
|
|
|
CSS_END
|
|
|
|
echo -e "\n\033[1;32m✅ Setup done! Seems to be okay!\033[0m"
|
|
echo -e "Have a look in your .env"
|
|
echo -e "nano .env"
|
|
echo -e "\n\033[1;32m✅ After you are done start the system with:\033[0m"
|
|
echo -e "cd steam-gift-manager"
|
|
echo -e "docker-compose build --no-cache && docker-compose up -d"
|
|
echo -e "\nGenerate translations: ./translate.sh"
|
|
echo -e "You can edit them in translations/en/LC_MESSAGES/messages.po"
|
|
echo -e "Enter your Apprise URLs in .env at APPRISE_URLS (e.g. for Pushover, Gotify, Matrix etc.)"
|
|
echo -e "\nAfter any change in you configuration, .env or even translations:"
|
|
echo -e "cd steam-gift-manager"
|
|
echo -e "docker-compose down && docker-compose up -d --build"
|
|
echo -e "\n\033[1;32m❗ Great - if you are updating/upgrading - visit my git for more details ❗\033[0m"
|