import os import json import uuid from datetime import datetime, timedelta from flask import Blueprint, request, render_template, redirect, url_for bp_tray = Blueprint('forked_tray', __name__) PAIRING_FILE = os.path.join(os.path.dirname(__file__), '../instance/pairing_keys.json') # Utility to load pairing keys from file def load_pairing_keys(): if not os.path.exists(PAIRING_FILE): return [] with open(PAIRING_FILE, 'r') as f: return json.load(f) # Utility to save pairing keys to file def save_pairing_keys(keys): with open(PAIRING_FILE, 'w') as f: json.dump(keys, f, indent=2) @bp_tray.route('/download_extension', methods=['GET']) def download_extension(): pairing_keys = load_pairing_keys() return render_template('download_extension.html', pairing_keys=pairing_keys) @bp_tray.route('/generate_pairing_key', methods=['POST']) def generate_pairing_key(): printer_name = request.form.get('printer_name') if not printer_name: return redirect(url_for('forked_tray.download_extension')) # Generate key and warranty pairing_key = str(uuid.uuid4()) warranty_days = 30 warranty_until = (datetime.utcnow() + timedelta(days=warranty_days)).strftime('%Y-%m-%d') # Load, append, and save keys = load_pairing_keys() keys.append({ 'printer_name': printer_name, 'pairing_key': pairing_key, 'warranty_until': warranty_until }) save_pairing_keys(keys) return render_template('download_extension.html', pairing_key=pairing_key, printer_name=printer_name, warranty_until=warranty_until, pairing_keys=keys)