slight changes

This commit is contained in:
2026-07-19 21:00:56 +02:00
parent adc484cc26
commit 3cffa4f601
+27 -76
View File
@@ -10397,102 +10397,53 @@ def notifications_unread_status():
client.close()
@app.route('/admin/damaged_items')
def admin_damaged_items():
"""Dedicated admin management window for damaged items."""
@app.route('/admin/damaged_items')
def damaged_items():
"""Admin-Übersicht aller aktiven und vergangenen Ausleihen."""
if 'username' not in session:
flash('Administratorrechte erforderlich.', 'error')
return redirect(url_for('login'))
# Import the decryption handler if it's not already at the top of the file
from modules.inventarsystem.data_protection import decrypt_text
from bson.objectid import ObjectId
client = None
try:
client = MongoClient(MONGODB_HOST, MONGODB_PORT)
db = client[MONGODB_DB]
items_col = db['items']
ausleihungen_col = db['ausleihungen']
items_col = db['items']
items = list(items_col.find(
{
'Deleted': {'$ne': True},
'$or': [
{'HasDamage': True},
{'Condition': 'destroyed'},
{'DamageReports.0': {'$exists': True}},
]
},
{
'Name': 1,
'Code_4': 1,
'ItemType': 1,
'Author': 1,
'ISBN': 1,
'Condition': 1,
'DamageReports': 1,
'DamageRepairs': 1,
'Verfuegbar': 1,
'User': 1,
'LastUpdated': 1,
}
).sort('LastUpdated', -1))
ausleihungen = list(ausleihungen_col.find().sort('Start', -1))
damaged_rows = []
for item_doc in items:
item_id = str(item_doc.get('_id'))
active_borrow = ausleihungen_col.find_one(
{'Item': item_id, 'Status': {'$in': ['active', 'planned']}},
{'_id': 1, 'User': 1, 'Status': 1, 'End': 1}
)
# Decrypt the borrower inside the active loan tracking object
if active_borrow and active_borrow.get('User'):
active_borrow['User'] = decrypt_text(active_borrow['User'])
for record in ausleihungen:
raw_user = record.get('User', '')
if raw_user:
record['User'] = decrypt_text(raw_user)
app.logger.debug(f"Active borrow for item {item_id}: {active_borrow}")
print(f"Active borrow for item {item_id}: {active_borrow}")
reports = item_doc.get('DamageReports', []) or []
latest_report = reports[0] if reports else {}
# Decrypt the static snapshot borrower field on the item itself
raw_user = item_doc.get('User', '')
decrypted_user = decrypt_text(raw_user) if raw_user else ''
app.logger.debug(f"Processing damaged item {item_id}: {item_doc.get('Name', '')}, Reports: {len(reports)}, Latest Report: {latest_report}, Decryptete user: {decrypted_user}, Active Borrow: {active_borrow}")
print(f"Processing damaged item {item_id}: {item_doc.get('Name', '')}, Reports: {len(reports)}, Latest Report: {latest_report}, Decrypted user: {decrypted_user}, Active Borrow: {active_borrow}")
damaged_rows.append({
'id': item_id,
'name': item_doc.get('Name', ''),
'code': item_doc.get('Code_4', ''),
'item_type': item_doc.get('ItemType', ''),
'author': item_doc.get('Author', ''),
'isbn': item_doc.get('ISBN', ''),
'condition': item_doc.get('Condition', ''),
'available': bool(item_doc.get('Verfuegbar', False)),
'borrow_user': decrypted_user,
'damage_count': len(reports),
'damage_reports': reports,
'latest_damage_description': latest_report.get('description', ''),
'latest_damage_by': latest_report.get('reported_by', ''),
'latest_damage_at': latest_report.get('reported_at'),
'active_borrow': active_borrow,
'last_updated': item_doc.get('LastUpdated'),
})
item_id = record.get('Item')
if item_id:
try:
item_doc = items_col.find_one({'_id': ObjectId(item_id)})
if item_doc:
if item_doc.get('User'):
item_doc['User'] = decrypt_text(item_doc['User'])
record['ItemDetails'] = item_doc
except Exception as e:
app.logger.warning(f"Konnte Item {item_id} für Ausleihe {record.get('_id')} nicht laden: {e}")
return render_template(
'admin_damaged_items.html',
damaged_items=damaged_rows,
'admin_damaged_items.html',
ausleihungen=ausleihungen,
library_module_enabled=cfg.MODULES.is_enabled('library'),
student_cards_module_enabled=cfg.MODULES.is_enabled('student_cards'),
mail_module_enabled=cfg.MODULES.is_enabled('mail')
)
except Exception as exc:
app.logger.error(f"Error loading damaged-items admin view: {exc}")
flash('Fehler beim Laden der Defekte-Items-Verwaltung.', 'error')
app.logger.error(f"Fehler beim Laden der Ausleihen-Verwaltung: {exc}")
flash('Fehler beim Laden der Ausleihen-Übersicht.', 'error')
return redirect(url_for('home_admin'))
finally:
if client: