added scan page

This commit is contained in:
2025-04-17 15:02:08 +03:00
parent fc355333cb
commit 9244ff90f6
8 changed files with 243 additions and 24 deletions

View File

@@ -1,10 +1,34 @@
import os
import pyodbc
from flask import Blueprint, render_template, redirect, url_for, request, flash, session, current_app
from .models import User
from . import db
bp = Blueprint('main', __name__)
def get_db_connection():
"""Reads the external_server.conf file and returns a database connection."""
settings_file = os.path.join(current_app.instance_path, 'external_server.conf')
if not os.path.exists(settings_file):
raise FileNotFoundError("The external_server.conf file is missing in the instance folder.")
# Read settings from the configuration file
settings = {}
with open(settings_file, 'r') as f:
for line in f:
key, value = line.strip().split('=', 1)
settings[key] = value
# Create a database connection string
connection_string = (
f"DRIVER={{ODBC Driver 17 for SQL Server}};"
f"SERVER={settings['server_domain']},{settings['port']};"
f"DATABASE={settings['database_name']};"
f"UID={settings['username']};"
f"PWD={settings['password']};"
)
return pyodbc.connect(connection_string)
@bp.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
@@ -59,12 +83,48 @@ def warehouse():
return redirect(url_for('main.dashboard'))
return render_template('warehouse.html')
@bp.route('/scan')
@bp.route('/scan', methods=['GET', 'POST'])
def scan():
if 'role' not in session or session['role'] not in ['superadmin', 'scan']:
flash('Access denied: Scan users only.')
return redirect(url_for('main.dashboard'))
return render_template('scan.html')
if request.method == 'POST':
# Handle form submission
operator_code = request.form.get('operator_code')
cp_code = request.form.get('cp_code')
oc1_code = request.form.get('oc1_code')
oc2_code = request.form.get('oc2_code')
defect_code = request.form.get('defect_code')
date = request.form.get('date')
time = request.form.get('time')
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"INSERT INTO scanare (operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, quantity) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
operator_code, cp_code, oc1_code, oc2_code, defect_code, date, time, 1
)
conn.commit()
conn.close()
flash('Scan data saved successfully.')
except Exception as e:
flash(f"Error saving scan data: {e}")
# Fetch the latest scan data for display
scan_data = []
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT TOP 14 * FROM scanare ORDER BY id DESC")
scan_data = cursor.fetchall()
conn.close()
except Exception as e:
flash(f"Error fetching scan data: {e}")
return render_template('scan.html', scan_data=scan_data)
@bp.route('/logout')
def logout():