- Added environment variable loading with python-dotenv - Fixed Docker session permissions by using /tmp directory - Updated .dockerignore to include .env file properly - Enhanced docker-compose.yml with env_file directive - Fixed Gunicorn configuration for Docker compatibility - Updated README.md with comprehensive deployment docs - Cleaned up debug logging from API routes - Added DOMAIN_SETUP.md for reverse proxy guidance - All production issues resolved and tested working - Application now accessible at qr.moto-adv.com
97 lines
3.1 KiB
Python
Executable File
97 lines
3.1 KiB
Python
Executable File
"""
|
|
Data storage utilities for QR codes
|
|
"""
|
|
|
|
import uuid
|
|
import json
|
|
import os
|
|
from datetime import datetime
|
|
|
|
# Data storage directory
|
|
DATA_DIR = 'data'
|
|
QR_CODES_FILE = os.path.join(DATA_DIR, 'qr_codes.json')
|
|
|
|
# Ensure data directory exists
|
|
os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
|
class QRDataManager:
|
|
def __init__(self):
|
|
self.qr_codes_db = self._load_qr_codes()
|
|
|
|
def _load_qr_codes(self):
|
|
"""Load QR codes from JSON file"""
|
|
try:
|
|
if os.path.exists(QR_CODES_FILE):
|
|
with open(QR_CODES_FILE, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
return {}
|
|
except Exception as e:
|
|
print(f"Error loading QR codes: {e}")
|
|
return {}
|
|
|
|
def _save_qr_codes(self):
|
|
"""Save QR codes to JSON file"""
|
|
try:
|
|
with open(QR_CODES_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(self.qr_codes_db, f, indent=2, ensure_ascii=False)
|
|
except Exception as e:
|
|
print(f"Error saving QR codes: {e}")
|
|
|
|
def save_qr_record(self, qr_type, content, settings, image_data, page_id=None):
|
|
"""Save QR code record to database"""
|
|
qr_id = str(uuid.uuid4())
|
|
qr_record = {
|
|
'id': qr_id,
|
|
'type': qr_type,
|
|
'content': content,
|
|
'settings': settings,
|
|
'created_at': datetime.now().isoformat(),
|
|
'image_data': image_data
|
|
}
|
|
|
|
if page_id:
|
|
qr_record['page_id'] = page_id
|
|
|
|
self.qr_codes_db[qr_id] = qr_record
|
|
self._save_qr_codes() # Persist to file
|
|
return qr_id
|
|
|
|
def get_qr_record(self, qr_id):
|
|
"""Get QR code record"""
|
|
# Reload data from file to ensure we have the latest data
|
|
self.qr_codes_db = self._load_qr_codes()
|
|
return self.qr_codes_db.get(qr_id)
|
|
|
|
def get_qr_code(self, qr_id):
|
|
"""Get QR code record (alias for compatibility)"""
|
|
return self.get_qr_record(qr_id)
|
|
|
|
def delete_qr_record(self, qr_id):
|
|
"""Delete QR code record"""
|
|
if qr_id in self.qr_codes_db:
|
|
del self.qr_codes_db[qr_id]
|
|
self._save_qr_codes() # Persist to file
|
|
return True
|
|
return False
|
|
|
|
def list_qr_codes(self):
|
|
"""List all QR codes"""
|
|
# Reload data from file to ensure we have the latest data
|
|
self.qr_codes_db = self._load_qr_codes()
|
|
qr_list = []
|
|
for qr_id, qr_data in self.qr_codes_db.items():
|
|
qr_list.append({
|
|
'id': qr_id,
|
|
'type': qr_data['type'],
|
|
'created_at': qr_data['created_at'],
|
|
'preview': f'data:image/png;base64,{qr_data["image_data"]}',
|
|
'page_id': qr_data.get('page_id') # Include page_id if it exists
|
|
})
|
|
return qr_list
|
|
|
|
def qr_exists(self, qr_id):
|
|
"""Check if QR code exists"""
|
|
# Reload data from file to ensure we have the latest data
|
|
self.qr_codes_db = self._load_qr_codes()
|
|
return qr_id in self.qr_codes_db
|