|
|
|
@@ -2177,7 +2177,7 @@ def _upload_student_cards_excel():
|
|
|
|
|
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
|
|
|
|
try:
|
|
|
|
|
db = client[cfg.MONGODB_DB]
|
|
|
|
|
student_cards = db['student_cards']
|
|
|
|
|
student_cards = list(db['student_cards'].find())
|
|
|
|
|
existing_ids.update(
|
|
|
|
|
str(card.get('AusweisId', '')).strip().upper()
|
|
|
|
|
for card in student_cards.find({}, {'AusweisId': 1})
|
|
|
|
@@ -4108,20 +4108,21 @@ def student_cards_admin():
|
|
|
|
|
app.logger.error(f"Error adding student card: {e}")
|
|
|
|
|
flash('Fehler beim Hinzufügen des Ausweises.', 'error')
|
|
|
|
|
|
|
|
|
|
# Get all student cards
|
|
|
|
|
# Get all student cards and decrypt them
|
|
|
|
|
all_cards = list(student_cards.find().sort('AusweisId', 1))
|
|
|
|
|
all_cards = [_decrypt_student_card_doc(card) for card in all_cards]
|
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
|
|
raw_classes = set(card.get('Klasse', '').strip() for card in all_cards if card.get('Klasse'))
|
|
|
|
|
available_classes = sorted(list(raw_classes))
|
|
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
|
'student_cards_admin.html',
|
|
|
|
|
username=session['username'],
|
|
|
|
|
student_cards=all_cards,
|
|
|
|
|
student_cards=all_cards, # Hier all_cards übergeben
|
|
|
|
|
edit_mode=edit_mode,
|
|
|
|
|
form_data=form_data,
|
|
|
|
|
config={'default': cfg.STUDENT_DEFAULT_BORROW_DAYS},
|
|
|
|
|
library_module_enabled=cfg.MODULES.is_enabled('library'),
|
|
|
|
|
student_cards_module_enabled=cfg.MODULES.is_enabled('student_cards')
|
|
|
|
|
available_classes=available_classes, # Enthält nun die lesbaren Klassen
|
|
|
|
|
config=cfg.get_school_info()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -4215,6 +4216,7 @@ def student_card_barcode_download():
|
|
|
|
|
from reportlab.pdfgen import canvas
|
|
|
|
|
from reportlab.lib.units import mm
|
|
|
|
|
from reportlab.lib.colors import HexColor, white, black
|
|
|
|
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
import barcode
|
|
|
|
|
from barcode.writer import ImageWriter
|
|
|
|
@@ -4246,10 +4248,10 @@ def student_card_barcode_download():
|
|
|
|
|
# Professional color palette
|
|
|
|
|
header_color = HexColor("#0F172A") # Sehr dunkles blau
|
|
|
|
|
accent_color = HexColor("#2563EB") # Helles blau
|
|
|
|
|
light_bg = HexColor("#F8FAFC") # Sehr heller grau
|
|
|
|
|
card_bg_color = HexColor("#FFFFFF") # Weiß
|
|
|
|
|
text_dark = HexColor("#1E293B") # Dunkler text
|
|
|
|
|
text_gray = HexColor("#64748B") # Grauer text
|
|
|
|
|
light_bg = HexColor("#F8FAFC") # Sehr heller grau
|
|
|
|
|
card_bg_color = HexColor("#FFFFFF") # Weiß
|
|
|
|
|
text_dark = HexColor("#1E293B") # Dunkler text
|
|
|
|
|
text_gray = HexColor("#64748B") # Grauer text
|
|
|
|
|
|
|
|
|
|
for i, card in enumerate(all_cards):
|
|
|
|
|
if col_idx == 0 and i > 0:
|
|
|
|
@@ -4265,7 +4267,7 @@ def student_card_barcode_download():
|
|
|
|
|
|
|
|
|
|
# Card shadow effect (unter border)
|
|
|
|
|
c.setFillColor(HexColor("#E2E8F0"))
|
|
|
|
|
c.rect(x_pos + 0.5*mm, y_pos - card_height - 0.5*mm, card_width, card_height, fill=1, stroke=0)
|
|
|
|
|
c.rect(x_pos + 0.5 * mm, y_pos - card_height - 0.5 * mm, card_width, card_height, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Card background
|
|
|
|
|
c.setLineWidth(1)
|
|
|
|
@@ -4280,84 +4282,98 @@ def student_card_barcode_download():
|
|
|
|
|
|
|
|
|
|
# Header bar
|
|
|
|
|
c.setFillColor(header_color)
|
|
|
|
|
c.rect(x_pos, y_pos - 10*mm, card_width, 10*mm, fill=1, stroke=0)
|
|
|
|
|
c.rect(x_pos, y_pos - 10 * mm, card_width, 10 * mm, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Header accent line
|
|
|
|
|
c.setStrokeColor(accent_color)
|
|
|
|
|
c.setLineWidth(2)
|
|
|
|
|
c.line(x_pos, y_pos - 10*mm, x_pos + card_width, y_pos - 10*mm)
|
|
|
|
|
|
|
|
|
|
c.line(x_pos, y_pos - 10 * mm, x_pos + card_width, y_pos - 10 * mm)
|
|
|
|
|
|
|
|
|
|
school_name = cfg.get_school_info()
|
|
|
|
|
school_name = school_name["name"]
|
|
|
|
|
# "SCHÜLERAUSWEIS" text in header
|
|
|
|
|
c.setFont("Helvetica-Bold", 9)
|
|
|
|
|
school_name_str = str(school_name["name"])
|
|
|
|
|
|
|
|
|
|
# Zwei-Zeilen-Header: 1. BIBLIOTHEKSAUSWEIS, 2. Schulname (dynamisch)
|
|
|
|
|
c.setFont("Helvetica-Bold", 8)
|
|
|
|
|
c.setFillColor(white)
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 6.5*mm, f"BIBLIOTHEKSAUSWEIS - {str(school_name)}")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 4 * mm, "BIBLIOTHEKSAUSWEIS")
|
|
|
|
|
|
|
|
|
|
# Dynamische Anpassung des Schulnamens an die Breite
|
|
|
|
|
font_size = 8
|
|
|
|
|
while stringWidth(school_name_str, "Helvetica-Bold", font_size) > (card_width - 6 * mm) and font_size > 4:
|
|
|
|
|
font_size -= 0.5
|
|
|
|
|
|
|
|
|
|
c.setFont("Helvetica-Bold", font_size)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 8 * mm, school_name_str)
|
|
|
|
|
|
|
|
|
|
# Student name - large and bold
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 10)
|
|
|
|
|
name = card['SchülerName'][:20]
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 14*mm, name)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 16 * mm, name)
|
|
|
|
|
|
|
|
|
|
# ID with label
|
|
|
|
|
c.setFont("Helvetica", 8)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 18*mm, "Ausweis ID:")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 20 * mm, "Ausweis ID:")
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 9)
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 21*mm, str(card['AusweisId']))
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 23 * mm, str(card['AusweisId']))
|
|
|
|
|
|
|
|
|
|
# Class with label
|
|
|
|
|
if card.get('Klasse'):
|
|
|
|
|
c.setFont("Helvetica", 8)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 25*mm, "Klasse:")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 27 * mm, "Klasse:")
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 9)
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 28*mm, card['Klasse'])
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 31*mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 34*mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 37*mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3*mm, y_pos - 40*mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 30 * mm, card['Klasse'])
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 33 * mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 36 * mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 39 * mm, "-")
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 42 * mm, "-")
|
|
|
|
|
|
|
|
|
|
# Right barcode section with border highlight
|
|
|
|
|
barcode_x_start = x_pos + info_width + 1*mm
|
|
|
|
|
barcode_x_start = x_pos + info_width + 1 * mm
|
|
|
|
|
c.setFillColor(accent_color)
|
|
|
|
|
c.setLineWidth(0)
|
|
|
|
|
c.rect(barcode_x_start - 1*mm, y_pos - card_height,
|
|
|
|
|
card_width - info_width + 1*mm, 2*mm, fill=1, stroke=0)
|
|
|
|
|
c.rect(barcode_x_start - 1 * mm, y_pos - card_height,
|
|
|
|
|
card_width - info_width + 1 * mm, 2 * mm, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Generate and add larger barcode
|
|
|
|
|
# Generate and add dynamically sized barcode
|
|
|
|
|
try:
|
|
|
|
|
temp_dir = tempfile.gettempdir()
|
|
|
|
|
barcode_path = os.path.join(temp_dir, f"barcode_{card['AusweisId']}")
|
|
|
|
|
|
|
|
|
|
# Generate barcode with higher module width for better scanning
|
|
|
|
|
# ImageWriter Settings für maximalen Platz und Lesbarkeit
|
|
|
|
|
options = {
|
|
|
|
|
'write_text': False, # Text entfernen (spart Platz)
|
|
|
|
|
'quiet_zone': 1.0, # Minimaler Rand
|
|
|
|
|
'dpi': 300 # Hohe Auflösung
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
barcode_obj = barcode.get('code128', str(card['AusweisId']), writer=ImageWriter())
|
|
|
|
|
barcode_obj.save(barcode_path)
|
|
|
|
|
barcode_obj.save(barcode_path, options=options)
|
|
|
|
|
barcode_file = f"{barcode_path}.png"
|
|
|
|
|
|
|
|
|
|
if os.path.exists(barcode_file):
|
|
|
|
|
# Larger barcode taking up most of right section
|
|
|
|
|
barcode_width = (card_width - info_width - 4*mm)
|
|
|
|
|
barcode_height = 16*mm
|
|
|
|
|
barcode_y = y_pos - card_height + (card_height - barcode_height) / 2 + 2*mm
|
|
|
|
|
# Barcode über den fast kompletten restlichen Bereich strecken
|
|
|
|
|
barcode_width = (card_width - info_width - 4 * mm)
|
|
|
|
|
barcode_height = card_height - 15 * mm
|
|
|
|
|
barcode_y = y_pos - card_height + 2.5 * mm
|
|
|
|
|
|
|
|
|
|
c.drawImage(barcode_file,
|
|
|
|
|
barcode_x_start + 1*mm,
|
|
|
|
|
barcode_y,
|
|
|
|
|
width=barcode_width,
|
|
|
|
|
height=barcode_height,
|
|
|
|
|
preserveAspectRatio=True)
|
|
|
|
|
barcode_x_start + 1 * mm,
|
|
|
|
|
barcode_y,
|
|
|
|
|
width=barcode_width,
|
|
|
|
|
height=barcode_height,
|
|
|
|
|
preserveAspectRatio=False) # Strecken erlauben für maximalen Scanbereich
|
|
|
|
|
os.remove(barcode_file)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Barcode file not created")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
c.setFont("Helvetica", 7)
|
|
|
|
|
c.setFillColor(HexColor("#DC2626"))
|
|
|
|
|
c.drawString(barcode_x_start + 2*mm, y_pos - card_height + 20*mm, "⚠️ Barcode Error")
|
|
|
|
|
c.drawString(barcode_x_start + 2 * mm, y_pos - card_height + 20 * mm, "⚠️ Barcode Error")
|
|
|
|
|
|
|
|
|
|
col_idx += 1
|
|
|
|
|
if col_idx >= cols:
|
|
|
|
@@ -4378,6 +4394,187 @@ def student_card_barcode_download():
|
|
|
|
|
return redirect(url_for('student_cards_admin'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/student_card_class_barcode_download', methods=['GET'])
|
|
|
|
|
def student_card_class_barcode_download():
|
|
|
|
|
"""
|
|
|
|
|
Download PDF with student card barcodes filtered by a specific class from dropdown.
|
|
|
|
|
"""
|
|
|
|
|
from flask import request, session, redirect, url_for, send_file, flash
|
|
|
|
|
|
|
|
|
|
if 'username' not in session:
|
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
|
|
|
|
|
|
current_permissions = us.get_effective_permissions(session['username'])
|
|
|
|
|
if not current_permissions['actions'].get('can_manage_users', False):
|
|
|
|
|
flash('Ihnen fehlen die nötigen Berechtigungen, um diese Aktion auszuführen.', 'error')
|
|
|
|
|
return redirect(url_for('library_view'))
|
|
|
|
|
|
|
|
|
|
class_name = request.args.get('class_name', '').strip()
|
|
|
|
|
if not class_name:
|
|
|
|
|
flash('Bitte wählen Sie eine Klasse aus.', 'error')
|
|
|
|
|
return redirect(url_for('student_cards_admin'))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from reportlab.lib.pagesizes import A4
|
|
|
|
|
from reportlab.pdfgen import canvas
|
|
|
|
|
from reportlab.lib.units import mm
|
|
|
|
|
from reportlab.lib.colors import HexColor, white
|
|
|
|
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
import barcode
|
|
|
|
|
from barcode.writer import ImageWriter
|
|
|
|
|
import tempfile
|
|
|
|
|
import os
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
|
|
|
|
db = client[cfg.MONGODB_DB]
|
|
|
|
|
student_cards = db['student_cards']
|
|
|
|
|
|
|
|
|
|
class_cards = list(student_cards.find({'Klasse': class_name}).sort('AusweisId', 1))
|
|
|
|
|
class_cards = [_decrypt_student_card_doc(card) for card in class_cards]
|
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
|
|
if not class_cards:
|
|
|
|
|
flash(f'Keine Ausweise für die Klasse "{class_name}" gefunden.', 'error')
|
|
|
|
|
return redirect(url_for('student_cards_admin'))
|
|
|
|
|
|
|
|
|
|
pdf_buffer = BytesIO()
|
|
|
|
|
c = canvas.Canvas(pdf_buffer, pagesize=A4)
|
|
|
|
|
|
|
|
|
|
page_width, page_height = A4
|
|
|
|
|
margin = 10 * mm
|
|
|
|
|
card_width = 88 * mm
|
|
|
|
|
card_height = 56 * mm
|
|
|
|
|
cols = 2
|
|
|
|
|
gap_x = 8 * mm
|
|
|
|
|
gap_y = 8 * mm
|
|
|
|
|
x_positions = [margin, margin + card_width + gap_x]
|
|
|
|
|
y_start = page_height - margin
|
|
|
|
|
y_pos = y_start
|
|
|
|
|
col_idx = 0
|
|
|
|
|
|
|
|
|
|
header_color = HexColor("#0F172A")
|
|
|
|
|
accent_color = HexColor("#2563EB")
|
|
|
|
|
light_bg = HexColor("#F8FAFC")
|
|
|
|
|
card_bg_color = HexColor("#FFFFFF")
|
|
|
|
|
text_dark = HexColor("#1E293B")
|
|
|
|
|
text_gray = HexColor("#64748B")
|
|
|
|
|
|
|
|
|
|
for i, card in enumerate(class_cards):
|
|
|
|
|
if col_idx == 0 and i > 0:
|
|
|
|
|
y_pos -= (card_height + gap_y)
|
|
|
|
|
|
|
|
|
|
if y_pos - card_height < margin:
|
|
|
|
|
c.showPage()
|
|
|
|
|
y_pos = y_start
|
|
|
|
|
col_idx = 0
|
|
|
|
|
|
|
|
|
|
x_pos = x_positions[col_idx]
|
|
|
|
|
|
|
|
|
|
c.setFillColor(HexColor("#E2E8F0"))
|
|
|
|
|
c.rect(x_pos + 0.5 * mm, y_pos - card_height - 0.5 * mm, card_width, card_height, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
c.setLineWidth(1)
|
|
|
|
|
c.setFillColor(card_bg_color)
|
|
|
|
|
c.setStrokeColor(HexColor("#CBD5E1"))
|
|
|
|
|
c.rect(x_pos, y_pos - card_height, card_width, card_height, fill=1, stroke=1)
|
|
|
|
|
|
|
|
|
|
info_width = 38 * mm
|
|
|
|
|
c.setFillColor(light_bg)
|
|
|
|
|
c.rect(x_pos, y_pos - card_height, info_width, card_height, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
c.setFillColor(header_color)
|
|
|
|
|
c.rect(x_pos, y_pos - 10 * mm, card_width, 10 * mm, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
c.setStrokeColor(accent_color)
|
|
|
|
|
c.setLineWidth(2)
|
|
|
|
|
c.line(x_pos, y_pos - 10 * mm, x_pos + card_width, y_pos - 10 * mm)
|
|
|
|
|
|
|
|
|
|
school_name = cfg.get_school_info()
|
|
|
|
|
school_name_str = str(school_name["name"])
|
|
|
|
|
|
|
|
|
|
c.setFont("Helvetica-Bold", 8)
|
|
|
|
|
c.setFillColor(white)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 4 * mm, "BIBLIOTHEKSAUSWEIS")
|
|
|
|
|
|
|
|
|
|
font_size = 8
|
|
|
|
|
while stringWidth(school_name_str, "Helvetica-Bold", font_size) > (card_width - 6 * mm) and font_size > 4:
|
|
|
|
|
font_size -= 0.5
|
|
|
|
|
|
|
|
|
|
c.setFont("Helvetica-Bold", font_size)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 8 * mm, school_name_str)
|
|
|
|
|
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 10)
|
|
|
|
|
name = card.get('SchülerName', '')[:20]
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 16 * mm, name)
|
|
|
|
|
|
|
|
|
|
c.setFont("Helvetica", 8)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 20 * mm, "Ausweis ID:")
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 9)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 23 * mm, str(card.get('AusweisId', '')))
|
|
|
|
|
|
|
|
|
|
if card.get('Klasse'):
|
|
|
|
|
c.setFont("Helvetica", 8)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 27 * mm, "Klasse:")
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 9)
|
|
|
|
|
c.drawString(x_pos + 3 * mm, y_pos - 30 * mm, card['Klasse'])
|
|
|
|
|
|
|
|
|
|
barcode_x_start = x_pos + info_width + 1 * mm
|
|
|
|
|
c.setFillColor(accent_color)
|
|
|
|
|
c.setLineWidth(0)
|
|
|
|
|
c.rect(barcode_x_start - 1 * mm, y_pos - card_height, card_width - info_width + 1 * mm, 2 * mm, fill=1,
|
|
|
|
|
stroke=0)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
temp_dir = tempfile.gettempdir()
|
|
|
|
|
barcode_path = os.path.join(temp_dir, f"barcode_{card.get('AusweisId', i)}")
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
'write_text': False,
|
|
|
|
|
'quiet_zone': 1.0,
|
|
|
|
|
'dpi': 300
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
barcode_obj = barcode.get('code128', str(card.get('AusweisId', '')), writer=ImageWriter())
|
|
|
|
|
barcode_obj.save(barcode_path, options=options)
|
|
|
|
|
barcode_file = f"{barcode_path}.png"
|
|
|
|
|
|
|
|
|
|
if os.path.exists(barcode_file):
|
|
|
|
|
barcode_width = (card_width - info_width - 4 * mm)
|
|
|
|
|
barcode_height = card_height - 15 * mm
|
|
|
|
|
barcode_y = y_pos - card_height + 2.5 * mm
|
|
|
|
|
|
|
|
|
|
c.drawImage(barcode_file, barcode_x_start + 1 * mm, barcode_y,
|
|
|
|
|
width=barcode_width, height=barcode_height, preserveAspectRatio=False)
|
|
|
|
|
os.remove(barcode_file)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
col_idx += 1
|
|
|
|
|
if col_idx >= cols:
|
|
|
|
|
col_idx = 0
|
|
|
|
|
|
|
|
|
|
c.save()
|
|
|
|
|
pdf_buffer.seek(0)
|
|
|
|
|
|
|
|
|
|
filename = f'ausweise_klasse_{class_name.replace(" ", "_")}_{datetime.datetime.now().strftime("%Y%m%d")}.pdf'
|
|
|
|
|
|
|
|
|
|
return send_file(
|
|
|
|
|
pdf_buffer,
|
|
|
|
|
mimetype='application/pdf',
|
|
|
|
|
as_attachment=True,
|
|
|
|
|
download_name=filename
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
flash('Fehler beim PDF-Download', 'error')
|
|
|
|
|
return redirect(url_for('student_cards_admin'))
|
|
|
|
|
|
|
|
|
|
@app.route('/student_card_single_barcode_download/<card_id>', methods=['GET'])
|
|
|
|
|
def student_card_single_barcode_download(card_id):
|
|
|
|
|
"""
|
|
|
|
@@ -4399,6 +4596,7 @@ def student_card_single_barcode_download(card_id):
|
|
|
|
|
from reportlab.pdfgen import canvas
|
|
|
|
|
from reportlab.lib.units import mm
|
|
|
|
|
from reportlab.lib.colors import HexColor, white, black
|
|
|
|
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
from bson.objectid import ObjectId
|
|
|
|
|
import barcode
|
|
|
|
@@ -4421,29 +4619,30 @@ def student_card_single_barcode_download(card_id):
|
|
|
|
|
c = canvas.Canvas(pdf_buffer, pagesize=A4)
|
|
|
|
|
|
|
|
|
|
page_width, page_height = A4
|
|
|
|
|
margin = 20 * mm
|
|
|
|
|
card_width = 105 * mm
|
|
|
|
|
card_height = 70 * mm
|
|
|
|
|
x_pos = (page_width - card_width) / 2
|
|
|
|
|
y_pos = (page_height - card_height) / 2
|
|
|
|
|
|
|
|
|
|
# Korrektur des y_pos auf die OBERKANTE, damit die nachfolgenden Rechnungen bündig sind
|
|
|
|
|
y_pos = (page_height + card_height) / 2
|
|
|
|
|
|
|
|
|
|
# Professional color palette
|
|
|
|
|
header_color = HexColor("#0F172A") # Sehr dunkles blau
|
|
|
|
|
accent_color = HexColor("#2563EB") # Helles blau
|
|
|
|
|
light_bg = HexColor("#F8FAFC") # Sehr heller grau
|
|
|
|
|
card_bg_color = HexColor("#FFFFFF") # Weiß
|
|
|
|
|
text_dark = HexColor("#1E293B") # Dunkler text
|
|
|
|
|
text_gray = HexColor("#64748B") # Grauer text
|
|
|
|
|
header_color = HexColor("#0F172A")
|
|
|
|
|
accent_color = HexColor("#2563EB")
|
|
|
|
|
light_bg = HexColor("#F8FAFC")
|
|
|
|
|
card_bg_color = HexColor("#FFFFFF")
|
|
|
|
|
text_dark = HexColor("#1E293B")
|
|
|
|
|
text_gray = HexColor("#64748B")
|
|
|
|
|
|
|
|
|
|
# Card shadow effect
|
|
|
|
|
c.setFillColor(HexColor("#E2E8F0"))
|
|
|
|
|
c.rect(x_pos + 1*mm, y_pos - card_height - 1*mm, card_width, card_height, fill=1, stroke=0)
|
|
|
|
|
c.rect(x_pos + 1 * mm, y_pos - card_height - 1 * mm, card_width, card_height, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Card background
|
|
|
|
|
c.setLineWidth(1.5)
|
|
|
|
|
c.setFillColor(card_bg_color)
|
|
|
|
|
c.setStrokeColor(HexColor("#CBD5E1"))
|
|
|
|
|
c.rect(x_pos, y_pos, card_width, card_height, fill=1, stroke=1)
|
|
|
|
|
c.rect(x_pos, y_pos - card_height, card_width, card_height, fill=1, stroke=1)
|
|
|
|
|
|
|
|
|
|
# Left info section (50mm)
|
|
|
|
|
info_width = 50 * mm
|
|
|
|
@@ -4457,86 +4656,101 @@ def student_card_single_barcode_download(card_id):
|
|
|
|
|
|
|
|
|
|
# Header bar
|
|
|
|
|
c.setFillColor(header_color)
|
|
|
|
|
c.rect(x_pos, y_pos - 10*mm, card_width, 10*mm, fill=1, stroke=0)
|
|
|
|
|
c.rect(x_pos, y_pos - 10 * mm, card_width, 10 * mm, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Header accent line
|
|
|
|
|
c.setStrokeColor(accent_color)
|
|
|
|
|
c.setLineWidth(2.5)
|
|
|
|
|
c.line(x_pos, y_pos - 10*mm, x_pos + card_width, y_pos - 10*mm)
|
|
|
|
|
c.line(x_pos, y_pos - 10 * mm, x_pos + card_width, y_pos - 10 * mm)
|
|
|
|
|
|
|
|
|
|
school_name = cfg.get_school_info()
|
|
|
|
|
school_name = school_name["name"]
|
|
|
|
|
# "SCHÜLERAUSWEIS" text in header
|
|
|
|
|
c.setFont("Helvetica-Bold", 11)
|
|
|
|
|
school_name_str = str(school_name["name"])
|
|
|
|
|
|
|
|
|
|
# Zwei-Zeilen-Header
|
|
|
|
|
c.setFont("Helvetica-Bold", 10)
|
|
|
|
|
c.setFillColor(white)
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 6.5*mm, f"BIBLIOTHEKSAUSWEIS - {str(school_name)}")
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 4.5 * mm, "BIBLIOTHEKSAUSWEIS")
|
|
|
|
|
|
|
|
|
|
# Dynamische Größe für den Schulnamen
|
|
|
|
|
font_size = 10
|
|
|
|
|
while stringWidth(school_name_str, "Helvetica-Bold", font_size) > (card_width - 8 * mm) and font_size > 4:
|
|
|
|
|
font_size -= 0.5
|
|
|
|
|
|
|
|
|
|
c.setFont("Helvetica-Bold", font_size)
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 8.5 * mm, school_name_str)
|
|
|
|
|
|
|
|
|
|
# Student name - large and prominent
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 12)
|
|
|
|
|
name = card['SchülerName'][:25]
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 16*mm, name)
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 18 * mm, name)
|
|
|
|
|
|
|
|
|
|
# ID section with label
|
|
|
|
|
c.setFont("Helvetica", 9)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 21*mm, "Ausweis-ID:")
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 24 * mm, "Ausweis-ID:")
|
|
|
|
|
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 11)
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 25*mm, str(card['AusweisId']))
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 28 * mm, str(card['AusweisId']))
|
|
|
|
|
|
|
|
|
|
# Class section
|
|
|
|
|
if card.get('Klasse'):
|
|
|
|
|
c.setFont("Helvetica", 9)
|
|
|
|
|
c.setFillColor(text_gray)
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 30*mm, "Klasse:")
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 34 * mm, "Klasse:")
|
|
|
|
|
|
|
|
|
|
c.setFillColor(text_dark)
|
|
|
|
|
c.setFont("Helvetica-Bold", 11)
|
|
|
|
|
c.drawString(x_pos + 4*mm, y_pos - 34*mm, card['Klasse'])
|
|
|
|
|
c.drawString(x_pos + 4 * mm, y_pos - 38 * mm, card['Klasse'])
|
|
|
|
|
|
|
|
|
|
# Barcode section - right side with blue accent
|
|
|
|
|
barcode_x_start = x_pos + info_width + 3*mm
|
|
|
|
|
barcode_x_start = x_pos + info_width + 3 * mm
|
|
|
|
|
c.setFillColor(accent_color)
|
|
|
|
|
c.setLineWidth(0)
|
|
|
|
|
c.rect(x_pos + info_width, y_pos - card_height,
|
|
|
|
|
card_width - info_width, 3*mm, fill=1, stroke=0)
|
|
|
|
|
card_width - info_width, 3 * mm, fill=1, stroke=0)
|
|
|
|
|
|
|
|
|
|
# Generate and add large barcode
|
|
|
|
|
# Generate and add large dynamically sized barcode
|
|
|
|
|
try:
|
|
|
|
|
temp_dir = tempfile.gettempdir()
|
|
|
|
|
barcode_path = os.path.join(temp_dir, f"barcode_{card['AusweisId']}")
|
|
|
|
|
|
|
|
|
|
# Generate barcode with better sizing
|
|
|
|
|
# Options anpassen für Scanbarkeit
|
|
|
|
|
options = {
|
|
|
|
|
'write_text': False,
|
|
|
|
|
'quiet_zone': 1.0,
|
|
|
|
|
'dpi': 300
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
barcode_obj = barcode.get('code128', str(card['AusweisId']), writer=ImageWriter())
|
|
|
|
|
barcode_obj.save(barcode_path)
|
|
|
|
|
barcode_obj.save(barcode_path, options=options)
|
|
|
|
|
barcode_file = f"{barcode_path}.png"
|
|
|
|
|
|
|
|
|
|
if os.path.exists(barcode_file):
|
|
|
|
|
# Large barcode on right side
|
|
|
|
|
barcode_width = (card_width - info_width - 8*mm)
|
|
|
|
|
barcode_height = 20*mm
|
|
|
|
|
barcode_y_offset = (card_height - 10*mm - barcode_height) / 2
|
|
|
|
|
# Den gesamten Platz auf der rechten Seite ausnutzen
|
|
|
|
|
barcode_width = (card_width - info_width - 8 * mm)
|
|
|
|
|
barcode_height = card_height - 18 * mm
|
|
|
|
|
barcode_y = y_pos - card_height + 5 * mm
|
|
|
|
|
|
|
|
|
|
c.drawImage(barcode_file,
|
|
|
|
|
barcode_x_start,
|
|
|
|
|
y_pos - card_height + barcode_y_offset + 5*mm,
|
|
|
|
|
width=barcode_width,
|
|
|
|
|
height=barcode_height,
|
|
|
|
|
preserveAspectRatio=True)
|
|
|
|
|
barcode_x_start + 1 * mm,
|
|
|
|
|
barcode_y,
|
|
|
|
|
width=barcode_width,
|
|
|
|
|
height=barcode_height,
|
|
|
|
|
preserveAspectRatio=False) # Ausdehnung in alle Richtungen
|
|
|
|
|
os.remove(barcode_file)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Barcode file not created")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
c.setFont("Helvetica", 9)
|
|
|
|
|
c.setFillColor(HexColor("#DC2626"))
|
|
|
|
|
c.drawString(barcode_x_start + 2*mm, y_pos - card_height + 25*mm, "⚠️ Barcode Error")
|
|
|
|
|
c.drawString(barcode_x_start + 2 * mm, y_pos - card_height + 25 * mm, "⚠️ Barcode Error")
|
|
|
|
|
|
|
|
|
|
# Bottom info line
|
|
|
|
|
c.setStrokeColor(HexColor("#CBD5E1"))
|
|
|
|
|
c.setLineWidth(0.5)
|
|
|
|
|
c.line(x_pos, y_pos - card_height + 3*mm, x_pos + card_width, y_pos - card_height + 3*mm)
|
|
|
|
|
c.line(x_pos, y_pos - card_height + 3 * mm, x_pos + card_width, y_pos - card_height + 3 * mm)
|
|
|
|
|
|
|
|
|
|
c.save()
|
|
|
|
|
pdf_buffer.seek(0)
|
|
|
|
@@ -4552,7 +4766,6 @@ def student_card_single_barcode_download(card_id):
|
|
|
|
|
flash('Fehler beim PDF-Download', 'error')
|
|
|
|
|
return redirect(url_for('student_cards_admin'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
|
def login():
|
|
|
|
|
"""
|
|
|
|
@@ -5239,7 +5452,7 @@ def debug_favorites():
|
|
|
|
|
def upload_item():
|
|
|
|
|
"""
|
|
|
|
|
Route for adding new items to the inventory.
|
|
|
|
|
Handles file uploads and creates QR codes.
|
|
|
|
|
Handles image file uploads and creates QR codes.
|
|
|
|
|
Enhanced for mobile browser compatibility.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
@@ -5266,9 +5479,9 @@ def upload_item():
|
|
|
|
|
# Detect if request is from mobile device
|
|
|
|
|
is_mobile = 'Mobile' in request.headers.get('User-Agent', '')
|
|
|
|
|
|
|
|
|
|
# Log mobile request for debugging
|
|
|
|
|
if is_mobile:
|
|
|
|
|
app.logger.info(f"Mobile upload from {request.headers.get('User-Agent', 'unknown')} by {encrypt_text(username)}")
|
|
|
|
|
app.logger.info(
|
|
|
|
|
f"Mobile upload from {request.headers.get('User-Agent', 'unknown')} by {encrypt_text(username)}")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Strip whitespace from all text fields
|
|
|
|
@@ -5301,46 +5514,30 @@ def upload_item():
|
|
|
|
|
item_count = 1
|
|
|
|
|
item_count = max(1, min(item_count, 100))
|
|
|
|
|
|
|
|
|
|
# Optional list of per-item codes (one code per line)
|
|
|
|
|
# Optional list of per-item codes
|
|
|
|
|
individual_codes = []
|
|
|
|
|
if individual_codes_raw:
|
|
|
|
|
individual_codes = [c.strip() for c in str(individual_codes_raw).replace(',', '\n').splitlines() if c.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Check if this is a duplication
|
|
|
|
|
is_duplicating = request.form.get('is_duplicating') == 'true'
|
|
|
|
|
|
|
|
|
|
# Get duplicate_images if duplicating
|
|
|
|
|
duplicate_images = request.form.getlist('duplicate_images') if is_duplicating else []
|
|
|
|
|
|
|
|
|
|
# Make sure duplicate_images is always a list, even if there's only one
|
|
|
|
|
if is_duplicating and duplicate_images and not isinstance(duplicate_images, list):
|
|
|
|
|
duplicate_images = [duplicate_images]
|
|
|
|
|
|
|
|
|
|
# Log details about each image
|
|
|
|
|
if is_duplicating and duplicate_images:
|
|
|
|
|
for i, img in enumerate(duplicate_images):
|
|
|
|
|
print(f"DEBUG: Duplicate image {i+1}/{len(duplicate_images)}: {img}")
|
|
|
|
|
individual_codes = [c.strip() for c in str(individual_codes_raw).replace(',', '\n').splitlines() if
|
|
|
|
|
c.strip()]
|
|
|
|
|
|
|
|
|
|
# Get book cover image if downloaded
|
|
|
|
|
book_cover_image = request.form.get('book_cover_image')
|
|
|
|
|
|
|
|
|
|
# Special handling for mobile browsers that might send data differently
|
|
|
|
|
# Special handling for mobile browsers
|
|
|
|
|
if is_mobile and 'mobile_data' in request.form:
|
|
|
|
|
try:
|
|
|
|
|
mobile_data = json.loads(request.form['mobile_data'])
|
|
|
|
|
# Override values with mobile data if available
|
|
|
|
|
if 'filters' in mobile_data:
|
|
|
|
|
filter_upload = mobile_data.get('filters', [])
|
|
|
|
|
if 'filters2' in mobile_data:
|
|
|
|
|
filter_upload2 = mobile_data.get('filters2', [])
|
|
|
|
|
if 'filters3' in mobile_data:
|
|
|
|
|
filter_upload3 = mobile_data.get('filters3', [])
|
|
|
|
|
if 'duplicate_images' in mobile_data and mobile_data['duplicate_images']:
|
|
|
|
|
duplicate_images = mobile_data.get('duplicate_images', [])
|
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
|
app.logger.error(f"Error parsing mobile data: {str(e)}")
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
app.logger.error(f"Upload Form Data Parsing Error: {str(e)}")
|
|
|
|
|
flash('Fehler beim Verarbeiten der Formulardaten. Bitte versuchen Sie es erneut.', 'error')
|
|
|
|
|
return redirect(url_for(success_redirect_endpoint))
|
|
|
|
|
|
|
|
|
@@ -5348,7 +5545,7 @@ def upload_item():
|
|
|
|
|
filter_upload = expand_filter_selection(filter_upload, 1)
|
|
|
|
|
filter_upload2 = expand_filter_selection(filter_upload2, 2)
|
|
|
|
|
|
|
|
|
|
# Validation
|
|
|
|
|
# Base Validation
|
|
|
|
|
if not name or not ort or not beschreibung:
|
|
|
|
|
error_msg = 'Bitte füllen Sie alle erforderlichen Felder aus'
|
|
|
|
|
if is_mobile:
|
|
|
|
@@ -5384,9 +5581,8 @@ def upload_item():
|
|
|
|
|
flash(error_msg, 'error')
|
|
|
|
|
return redirect(url_for('library_admin'))
|
|
|
|
|
|
|
|
|
|
# Only check for images if not duplicating and no duplicate images provided and no book cover
|
|
|
|
|
# For library mode, skip this check as images come only from ISBN fetch
|
|
|
|
|
if upload_mode != 'library' and not is_duplicating and not images and not duplicate_images and not book_cover_image:
|
|
|
|
|
# Strict Image Validation (Since duplication is removed)
|
|
|
|
|
if upload_mode != 'library' and not images and not book_cover_image:
|
|
|
|
|
error_msg = 'Bitte laden Sie mindestens ein Bild hoch'
|
|
|
|
|
if is_mobile:
|
|
|
|
|
return jsonify({'success': False, 'message': error_msg}), 400
|
|
|
|
@@ -5395,36 +5591,20 @@ def upload_item():
|
|
|
|
|
return redirect(url_for(success_redirect_endpoint))
|
|
|
|
|
|
|
|
|
|
primary_code_raw = request.form.get('code_4', '').strip()
|
|
|
|
|
individual_codes_raw = request.form.get('individual_codes', '').strip()
|
|
|
|
|
|
|
|
|
|
all_item_codes = []
|
|
|
|
|
|
|
|
|
|
if primary_code_raw:
|
|
|
|
|
all_item_codes.append(primary_code_raw)
|
|
|
|
|
|
|
|
|
|
if individual_codes_raw:
|
|
|
|
|
extra_codes = [c.strip() for c in individual_codes_raw.replace('\r', '').split('\n') if c.strip()]
|
|
|
|
|
|
|
|
|
|
for c in extra_codes:
|
|
|
|
|
if individual_codes:
|
|
|
|
|
for c in individual_codes:
|
|
|
|
|
if c not in all_item_codes:
|
|
|
|
|
all_item_codes.append(c)
|
|
|
|
|
|
|
|
|
|
if len(all_item_codes) > 0:
|
|
|
|
|
item_count = len(all_item_codes)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
item_count = int(request.form.get('item_count', 1))
|
|
|
|
|
except ValueError:
|
|
|
|
|
item_count = 1
|
|
|
|
|
|
|
|
|
|
if upload_mode != 'library' and not is_duplicating and not images and not duplicate_images and not book_cover_image:
|
|
|
|
|
error_msg = 'Bitte laden Sie mindestens ein Bild hoch'
|
|
|
|
|
if is_mobile:
|
|
|
|
|
return jsonify({'success': False, 'message': error_msg}), 400
|
|
|
|
|
else:
|
|
|
|
|
flash(error_msg, 'error')
|
|
|
|
|
return redirect(url_for(success_redirect_endpoint))
|
|
|
|
|
|
|
|
|
|
# Check unique codes
|
|
|
|
|
for code in all_item_codes:
|
|
|
|
|
if not it.is_code_unique(code):
|
|
|
|
|
app.logger.info(f"DEBUG: Code '{code}' is not unique.")
|
|
|
|
@@ -5451,14 +5631,11 @@ def upload_item():
|
|
|
|
|
return redirect(url_for(success_redirect_endpoint))
|
|
|
|
|
|
|
|
|
|
def generate_unique_batch_code(base_code, position):
|
|
|
|
|
"""Generate a unique code for every item in a batch if no specific code is provided."""
|
|
|
|
|
if not base_code:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
candidate = base_code if position == 1 else f"{base_code}"
|
|
|
|
|
if it.is_code_unique(candidate):
|
|
|
|
|
return candidate
|
|
|
|
|
|
|
|
|
|
suffix = 1
|
|
|
|
|
while suffix <= 1000:
|
|
|
|
|
alternative = f"{candidate}"
|
|
|
|
@@ -5473,14 +5650,11 @@ def upload_item():
|
|
|
|
|
skipped_count = 0
|
|
|
|
|
|
|
|
|
|
upload_session_id = str(uuid.uuid4())[:8]
|
|
|
|
|
app.logger.info(f"Starting image upload session {upload_session_id} - Files: {len(images)}, User: {encrypt_text(username)}")
|
|
|
|
|
app.logger.info(
|
|
|
|
|
f"Starting image upload session {upload_session_id} - Files: {len(images)}, User: {encrypt_text(username)}")
|
|
|
|
|
|
|
|
|
|
# 1. Process Manual Image Uploads
|
|
|
|
|
for index, image in enumerate(images):
|
|
|
|
|
#if upload_mode == 'library':
|
|
|
|
|
# app.logger.info(f"[Upload {upload_session_id}] Skipping manual upload (Library Mode)")
|
|
|
|
|
# skipped_count += 1
|
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
if not image or not image.filename:
|
|
|
|
|
skipped_count += 1
|
|
|
|
|
continue
|
|
|
|
@@ -5498,9 +5672,7 @@ def upload_item():
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
secure_name = secure_filename(image.filename)
|
|
|
|
|
|
|
|
|
|
image.seek(0)
|
|
|
|
|
|
|
|
|
|
image_bytes = image.read()
|
|
|
|
|
|
|
|
|
|
if not image_bytes:
|
|
|
|
@@ -5535,7 +5707,6 @@ def upload_item():
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
image_filenames.append(new_filename)
|
|
|
|
|
|
|
|
|
|
processed_count += 1
|
|
|
|
|
final_size_kb = len(optimized_io.getvalue()) / 1024
|
|
|
|
|
app.logger.info(
|
|
|
|
@@ -5545,79 +5716,13 @@ def upload_item():
|
|
|
|
|
app.logger.error(f"{image_log_prefix} Processing failed: {str(e)}")
|
|
|
|
|
error_count += 1
|
|
|
|
|
|
|
|
|
|
app.logger.info(f"Upload session {upload_session_id} completed: {processed_count} processed, {error_count} errors, {skipped_count} skipped")
|
|
|
|
|
|
|
|
|
|
# 1. Duplikate verarbeiten (Direkt via MongoDB / GridFS)
|
|
|
|
|
if duplicate_images:
|
|
|
|
|
app.logger.info(f"Processing {len(duplicate_images)} duplicate images from GridFS: {duplicate_images}")
|
|
|
|
|
duplicate_image_copies = []
|
|
|
|
|
placeholder_used = False
|
|
|
|
|
|
|
|
|
|
for i, dup_img in enumerate(duplicate_images):
|
|
|
|
|
try:
|
|
|
|
|
# Suche das Originalbild direkt in der GridFS-Datenbank
|
|
|
|
|
existing_file = fs.find_one({"filename": dup_img})
|
|
|
|
|
|
|
|
|
|
# Fallback: Falls der exakte Name nicht gefunden wird, suche über Regex nach dem Basisnamen
|
|
|
|
|
if not existing_file:
|
|
|
|
|
base_name = re.sub(r'_\d+(?=\.[^.]+$)', '', dup_img) # Entfernt Suffixe wie _800
|
|
|
|
|
base_name_no_ext = os.path.splitext(base_name)[0]
|
|
|
|
|
|
|
|
|
|
if len(base_name_no_ext) > 5:
|
|
|
|
|
app.logger.info(f"Trying regex search for base name: {base_name_no_ext}")
|
|
|
|
|
existing_file = fs.find_one({"filename": {"$regex": f"^{re.escape(base_name_no_ext)}"}})
|
|
|
|
|
|
|
|
|
|
new_filename = f"{uuid.uuid4().hex}_{int(time.time())}.webp"
|
|
|
|
|
|
|
|
|
|
if existing_file:
|
|
|
|
|
app.logger.info(f"Found image in GridFS for {dup_img}. Duplicating...")
|
|
|
|
|
# Bilddaten in den Speicher laden und direkt als neue Datei in GridFS ablegen
|
|
|
|
|
image_data = existing_file.read()
|
|
|
|
|
|
|
|
|
|
fs.put(
|
|
|
|
|
image_data,
|
|
|
|
|
filename=new_filename,
|
|
|
|
|
content_type=existing_file.content_type,
|
|
|
|
|
metadata={'original_duplicate_of': dup_img}
|
|
|
|
|
)
|
|
|
|
|
duplicate_image_copies.append(new_filename)
|
|
|
|
|
processed_count += 1
|
|
|
|
|
else:
|
|
|
|
|
app.logger.warning(f"Could not find {dup_img} in GridFS. Using placeholder.")
|
|
|
|
|
placeholder_path = os.path.join(app.static_folder, 'img', 'no-image.png')
|
|
|
|
|
|
|
|
|
|
if os.path.exists(placeholder_path):
|
|
|
|
|
with open(placeholder_path, 'rb') as pf:
|
|
|
|
|
placeholder_data = pf.read()
|
|
|
|
|
|
|
|
|
|
fs.put(
|
|
|
|
|
placeholder_data,
|
|
|
|
|
filename=new_filename,
|
|
|
|
|
content_type='image/png',
|
|
|
|
|
metadata={'is_placeholder': True}
|
|
|
|
|
)
|
|
|
|
|
duplicate_image_copies.append(new_filename)
|
|
|
|
|
placeholder_used = True
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
app.logger.error(f"Error duplicating image {dup_img}: {str(e)}")
|
|
|
|
|
error_count += 1
|
|
|
|
|
|
|
|
|
|
if placeholder_used:
|
|
|
|
|
app.logger.warning("Used placeholders for some missing images during duplication")
|
|
|
|
|
|
|
|
|
|
image_filenames.extend(duplicate_image_copies)
|
|
|
|
|
|
|
|
|
|
# 2. Buchcover verarbeiten
|
|
|
|
|
# 2. Process Book Cover
|
|
|
|
|
if book_cover_image:
|
|
|
|
|
new_filename = f"{uuid.uuid4().hex}_{int(time.time())}_book_cover.webp"
|
|
|
|
|
|
|
|
|
|
# Prüfen, ob das Cover noch als lokale temporäre Datei existiert (z.B. von einer API geladen)
|
|
|
|
|
local_path = os.path.join(app.config.get('UPLOAD_FOLDER', ''), book_cover_image)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if os.path.exists(local_path):
|
|
|
|
|
# Lokales Bild einlesen, zu WebP optimieren und in GridFS schieben
|
|
|
|
|
with Image.open(local_path) as img:
|
|
|
|
|
if img.mode not in ('RGB', 'RGBA'):
|
|
|
|
|
img = img.convert('RGBA')
|
|
|
|
@@ -5635,7 +5740,6 @@ def upload_item():
|
|
|
|
|
image_filenames.append(new_filename)
|
|
|
|
|
app.logger.info(f"Processed local book cover and saved to GridFS: {new_filename}")
|
|
|
|
|
else:
|
|
|
|
|
# Falls es bereits in GridFS liegt, einfach duplizieren
|
|
|
|
|
existing_cover = fs.find_one({"filename": book_cover_image})
|
|
|
|
|
if existing_cover:
|
|
|
|
|
fs.put(
|
|
|
|
@@ -5651,7 +5755,10 @@ def upload_item():
|
|
|
|
|
except Exception as e:
|
|
|
|
|
app.logger.error(f"Error processing book cover {book_cover_image}: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# 3. Item-Erstellung (Logik bleibt identisch, da it.add_item nun die GridFS-Filenames erhält)
|
|
|
|
|
app.logger.info(
|
|
|
|
|
f"Upload session {upload_session_id} completed: {processed_count} processed, {error_count} errors, {skipped_count} skipped")
|
|
|
|
|
|
|
|
|
|
# 3. Item Creation
|
|
|
|
|
predefined_locations = it.get_predefined_locations()
|
|
|
|
|
if ort and ort not in predefined_locations:
|
|
|
|
|
it.add_predefined_location(ort)
|
|
|
|
@@ -5677,7 +5784,6 @@ def upload_item():
|
|
|
|
|
|
|
|
|
|
parent_item_id = str(created_item_ids[0]) if created_item_ids else None
|
|
|
|
|
|
|
|
|
|
# image_filenames enthält jetzt ausschließlich GridFS-Filenames (oder ObjectIds, je nach deinem Setup)
|
|
|
|
|
item_id = it.add_item(
|
|
|
|
|
name, ort, beschreibung, image_filenames, filter_upload,
|
|
|
|
|
filter_upload2, filter_upload3,
|
|
|
|
@@ -5723,8 +5829,8 @@ def upload_item():
|
|
|
|
|
'codes': created_item_ids and [it.get_item(cid).get('Code_4') for cid in created_item_ids] or []
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
app.logger.warning('Audit write failed for library_item_created')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
app.logger.warning(f"Audit write failed for library_item_created: {str(e)}")
|
|
|
|
|
|
|
|
|
|
flash(success_msg, 'success')
|
|
|
|
|
return redirect(url_for(success_redirect_endpoint))
|
|
|
|
|