- Created labels module with complete structure in app/modules/labels/ - Implemented print_module.py with database functions: * get_unprinted_orders_data() - Retrieve unprinted orders from database * get_printed_orders_data() - Retrieve printed orders from database * update_order_printed_status() - Mark orders as printed * search_orders_by_cp_code() - Search orders by production code - Created routes.py with Flask Blueprint and API endpoints: * GET /labels/ - Module home page with feature launchers * GET /labels/print-module - Main label printing interface * GET /labels/print-lost-labels - Lost label reprinting interface * GET /labels/api/unprinted-orders - API for unprinted orders * GET /labels/api/printed-orders - API for printed orders * POST /labels/api/search-orders - Search orders API * POST /labels/api/update-printed-status/<id> - Update status API - Migrated HTML templates from legacy app with theme support: * print_module.html - Thermal label printing with QZ Tray integration * print_lost_labels.html - Lost label search and reprint interface * Added comprehensive CSS variables for dark/light mode theming * Preserved all original JavaScript functionality for printing - Created index.html module home page with feature launchers - Registered labels blueprint in app/__init__.py with /labels prefix - Added 'Label Printing' module card to dashboard with print icon All functionality preserved from original implementation: - QZ Tray thermal printer integration - JsBarcode barcode generation (horizontal + vertical) - PDF export fallback - Session-based authentication for all routes - Database integration with proper error handling
132 lines
4.1 KiB
Python
132 lines
4.1 KiB
Python
"""
|
|
Main application routes (Login, Logout, Dashboard)
|
|
"""
|
|
from flask import (
|
|
Blueprint, render_template, request, session, redirect, url_for,
|
|
flash, current_app
|
|
)
|
|
from app.auth import authenticate_user, get_user_by_id
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
main_bp = Blueprint('main', __name__)
|
|
|
|
|
|
@main_bp.route('/', methods=['GET'])
|
|
def index():
|
|
"""Redirect to dashboard if logged in, otherwise to login"""
|
|
if 'user_id' in session:
|
|
return redirect(url_for('main.dashboard'))
|
|
return redirect(url_for('main.login'))
|
|
|
|
|
|
@main_bp.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
"""Login page and authentication"""
|
|
if request.method == 'POST':
|
|
username = request.form.get('username', '').strip()
|
|
password = request.form.get('password', '')
|
|
|
|
if not username or not password:
|
|
flash('Username and password are required', 'error')
|
|
return render_template('login.html')
|
|
|
|
# Authenticate user
|
|
user = authenticate_user(username, password)
|
|
|
|
if user:
|
|
# Store user information in session
|
|
session.permanent = True
|
|
session['user_id'] = user['id']
|
|
session['username'] = user['username']
|
|
session['email'] = user['email']
|
|
session['role'] = user['role']
|
|
session['full_name'] = user['full_name']
|
|
session.modified = True # Force session to be saved
|
|
|
|
logger.info(f"User {username} logged in successfully")
|
|
logger.debug(f"Session data set: user_id={user['id']}, username={username}")
|
|
flash(f'Welcome, {user["full_name"]}!', 'success')
|
|
|
|
return redirect(url_for('main.dashboard'))
|
|
else:
|
|
flash('Invalid username or password', 'error')
|
|
logger.warning(f"Failed login attempt for user: {username}")
|
|
|
|
return render_template('login.html')
|
|
|
|
|
|
@main_bp.route('/dashboard', methods=['GET'])
|
|
def dashboard():
|
|
"""Main dashboard page"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
user_id = session.get('user_id')
|
|
user = get_user_by_id(user_id)
|
|
|
|
if not user:
|
|
session.clear()
|
|
flash('User session invalid', 'error')
|
|
return redirect(url_for('main.login'))
|
|
|
|
modules = [
|
|
{
|
|
'name': 'Quality Module',
|
|
'description': 'Manage quality checks and inspections',
|
|
'icon': 'fa-check-circle',
|
|
'color': 'primary',
|
|
'url': url_for('quality.quality_index')
|
|
},
|
|
{
|
|
'name': 'Warehouse Module',
|
|
'description': 'Manage warehouse operations and inventory',
|
|
'icon': 'fa-warehouse',
|
|
'color': 'info',
|
|
'url': url_for('warehouse.warehouse_index')
|
|
},
|
|
{
|
|
'name': 'Label Printing',
|
|
'description': 'Print and manage thermal labels for orders',
|
|
'icon': 'fa-print',
|
|
'color': 'success',
|
|
'url': url_for('labels.labels_index')
|
|
},
|
|
{
|
|
'name': 'Settings',
|
|
'description': 'Configure application settings',
|
|
'icon': 'fa-cog',
|
|
'color': 'secondary',
|
|
'url': url_for('settings.settings_index')
|
|
}
|
|
]
|
|
|
|
return render_template('dashboard.html', user=user, modules=modules)
|
|
|
|
|
|
@main_bp.route('/logout', methods=['GET', 'POST'])
|
|
def logout():
|
|
"""Logout user"""
|
|
username = session.get('username', 'Unknown')
|
|
session.clear()
|
|
logger.info(f"User {username} logged out")
|
|
flash('You have been logged out successfully', 'success')
|
|
return redirect(url_for('main.login'))
|
|
|
|
|
|
@main_bp.route('/profile', methods=['GET'])
|
|
def profile():
|
|
"""User profile page"""
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('main.login'))
|
|
|
|
user_id = session.get('user_id')
|
|
user = get_user_by_id(user_id)
|
|
|
|
if not user:
|
|
session.clear()
|
|
return redirect(url_for('main.login'))
|
|
|
|
return render_template('profile.html', user=user)
|