Fix .gitignore, add secure pairing key route, and fix template errors

This commit is contained in:
2025-10-01 20:46:46 +03:00
parent ea6364eb7b
commit b2b225049d
3 changed files with 114 additions and 37 deletions

View File

@@ -1,3 +1,17 @@
import json
from flask import Blueprint
bp = Blueprint('main', __name__)
@bp.route('/get_pairing_keys')
def get_pairing_keys():
"""Return all pairing keys as JSON for client selection."""
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
with open(keys_path, 'r') as f:
keys = json.load(f)
except Exception as e:
print(f"Error loading pairing keys: {e}")
return jsonify({'success': False, 'error': str(e), 'pairing_keys': []}), 500
return jsonify({'success': True, 'pairing_keys': keys})
import os
import mariadb
from datetime import datetime, timedelta
@@ -932,10 +946,65 @@ def upload_data():
def print_module():
return render_template('print_module.html')
import secrets
from datetime import datetime, timedelta
@bp.route('/generate_pairing_key', methods=['POST'])
def generate_pairing_key():
"""Generate a secure pairing key for a printer and store it."""
printer_name = request.form.get('printer_name', '').strip()
if not printer_name:
flash('Printer name is required.', 'danger')
return redirect(url_for('main.download_extension'))
# Generate a secure random key
pairing_key = secrets.token_urlsafe(32)
warranty_until = (datetime.utcnow() + timedelta(days=365)).strftime('%Y-%m-%d')
# Load existing keys
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
if os.path.exists(keys_path):
with open(keys_path, 'r') as f:
keys = json.load(f)
else:
keys = []
except Exception as e:
keys = []
# Add new key
keys.append({
'printer_name': printer_name,
'pairing_key': pairing_key,
'warranty_until': warranty_until
})
# Save updated keys
with open(keys_path, 'w') as f:
json.dump(keys, f, indent=2)
# Pass new key and all keys to template
return render_template('download_extension.html',
pairing_key=pairing_key,
printer_name=printer_name,
warranty_until=warranty_until,
pairing_keys=keys)
@bp.route('/download_extension')
def download_extension():
"""Route for downloading the Chrome extension"""
return render_template('download_extension.html')
# Load all pairing keys for display
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
if os.path.exists(keys_path):
with open(keys_path, 'r') as f:
keys = json.load(f)
else:
keys = []
except Exception as e:
keys = []
return render_template('download_extension.html', pairing_keys=keys)
@bp.route('/extension_files/<path:filename>')
def extension_files(filename):

View File

@@ -1,43 +1,44 @@
{% extends "base.html" %}
{% block title %}Quality Recticel Print Service Downloads{% endblock %}
{% block content %}
<div class="container-fluid">
<!-- Header Section -->
<div class="row justify-content-center mb-4">
<div class="col-md-12">
<div class="text-center">
<h1 class="display-4">🖨️ Quality Recticel Print Solutions</h1>
<p class="lead">Choose Your Printing Method: Chrome Extension or Windows Service</p>
<div class="alert alert-info mx-auto" style="max-width: 800px;">
<strong>🆕 TWO POWERFUL OPTIONS:</strong> Simple browser-based Chrome extension or enterprise-grade Windows service for advanced printing needs.
</div>
</div>
<div style="max-width: 600px; margin: 40px auto; padding: 32px; background: #fff; border-radius: 12px; box-shadow: 0 2px 12px #0002;">
<h2>QZ Tray Pairing Key Management</h2>
<form id="pairing-form" method="POST" action="/generate_pairing_key" style="margin-bottom: 32px;">
<label for="printer_name">Printer Name:</label>
<input type="text" id="printer_name" name="printer_name" required style="margin: 0 8px 0 8px;">
<button type="submit">Generate Pairing Key</button>
</form>
<div id="pairing-result">
{% if pairing_key %}
<div style="margin-bottom: 16px;">
<strong>Pairing Key:</strong> <span style="font-family: monospace;">{{ pairing_key }}</span><br>
<strong>Printer Name:</strong> {{ printer_name }}<br>
<strong>Valid Until:</strong> {{ warranty_until }}
</div>
{% endif %}
</div>
<!-- Overview Card -->
<div class="row justify-content-center mb-4">
<div class="col-md-10">
<div class="card border-primary">
<div class="card-header bg-primary text-white">
<h3 class="mb-0">🚀 Two Printing Solutions Available</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<h5><EFBFBD> Chrome Extension (Recommended)</h5>
<ul>
<li><strong>Easy Setup</strong> - 2 minutes to install</li>
<li><strong>Cross-Platform</strong> - Windows, Mac, Linux</li>
<li><strong>User Control</strong> - Print dialog for printer selection</li>
<li><strong>Zero Configuration</strong> - Works immediately</li>
<li><strong>Secure</strong> - No external services needed</li>
</ul>
</div>
<div class="col-md-6">
<h5>🔧 Windows Service (Enterprise)</h5>
<h3>Active Pairing Keys</h3>
<table style="width:100%; border-collapse:collapse;">
<thead>
<tr style="background:#f0f0f0;">
<th style="padding:8px; border:1px solid #ccc;">Printer Name</th>
<th style="padding:8px; border:1px solid #ccc;">Pairing Key</th>
<th style="padding:8px; border:1px solid #ccc;">Valid Until</th>
</tr>
</thead>
<tbody>
{% for key in pairing_keys %}
<tr>
<td style="padding:8px; border:1px solid #ccc;">{{ key.printer_name }}</td>
<td style="padding:8px; border:1px solid #ccc; font-family:monospace;">{{ key.pairing_key }}</td>
<td style="padding:8px; border:1px solid #ccc;">{{ key.warranty_until }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<script>
// Optionally add AJAX for key generation if you want dynamic updates
</script>
<ul>
<li><strong>Silent Printing</strong> - No user interaction needed</li>
<li><EFBFBD> <strong>Direct Printer Access</strong> - System-level printing</li>