From b2b225049d9652ad65ed3616415978e8bda81478 Mon Sep 17 00:00:00 2001 From: Scheianu Ionut Date: Wed, 1 Oct 2025 20:46:46 +0300 Subject: [PATCH] Fix .gitignore, add secure pairing key route, and fix template errors --- .gitignore | 7 ++ py_app/app/routes.py | 71 ++++++++++++++++++- py_app/app/templates/download_extension.html | 73 ++++++++++---------- 3 files changed, 114 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 42b08bd..6eb7e1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,11 @@ + recticel/ .venv/ __pycache__/ app/__pycache__/ +tray/ +jdk-11.0.20-full/ +# Ignore generated or local files +py_app/instance/pairing_keys.json +py_app/app/forked_tray.py +backup/print_module.html diff --git a/py_app/app/routes.py b/py_app/app/routes.py index d540f2c..e212815 100644 --- a/py_app/app/routes.py +++ b/py_app/app/routes.py @@ -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/') def extension_files(filename): diff --git a/py_app/app/templates/download_extension.html b/py_app/app/templates/download_extension.html index 9f1e05f..462c17b 100644 --- a/py_app/app/templates/download_extension.html +++ b/py_app/app/templates/download_extension.html @@ -1,43 +1,44 @@ {% extends "base.html" %} - -{% block title %}Quality Recticel Print Service Downloads{% endblock %} - {% block content %} -
- -
-
-
-

🖨️ Quality Recticel Print Solutions

-

Choose Your Printing Method: Chrome Extension or Windows Service

-
- 🆕 TWO POWERFUL OPTIONS: Simple browser-based Chrome extension or enterprise-grade Windows service for advanced printing needs. -
-
+
+

QZ Tray Pairing Key Management

+
+ + + +
+
+ {% if pairing_key %} +
+ Pairing Key: {{ pairing_key }}
+ Printer Name: {{ printer_name }}
+ Valid Until: {{ warranty_until }}
+ {% endif %}
- - -
-
-
-
-

🚀 Two Printing Solutions Available

-
-
-
-
-
� Chrome Extension (Recommended)
-
    -
  • Easy Setup - 2 minutes to install
  • -
  • Cross-Platform - Windows, Mac, Linux
  • -
  • User Control - Print dialog for printer selection
  • -
  • Zero Configuration - Works immediately
  • -
  • Secure - No external services needed
  • -
-
-
-
🔧 Windows Service (Enterprise)
+

Active Pairing Keys

+ + + + + + + + + + {% for key in pairing_keys %} + + + + + + {% endfor %} + +
Printer NamePairing KeyValid Until
{{ key.printer_name }}{{ key.pairing_key }}{{ key.warranty_until }}
+
+
  • Silent Printing - No user interaction needed
  • �️ Direct Printer Access - System-level printing