Implemenmtation of the new encryption into the my borrowed funktion

This commit is contained in:
2026-07-31 21:28:34 +02:00
parent c9fdf41e0b
commit dcc8aae650
+65 -87
View File
@@ -10248,69 +10248,58 @@ def get_period_times(booking_date, period_num):
"""---------------------------------------------------------Borrowing-----------------------------------------------------------------""" """---------------------------------------------------------Borrowing-----------------------------------------------------------------"""
@app.route('/my_borrowed_items') @app.route('/my_borrowed_items')
def my_borrowed_items(): def my_borrowed_items():
""" """
Zeigt alle vom aktuellen Benutzer ausgeliehenen und geplanten Objekte an. Zeigt alle vom aktuellen Benutzer ausgeliehenen und geplanten Objekte an.
Returns:
Response: Gerendertes Template mit den ausgeliehenen und geplanten Objekten des Benutzers
""" """
if 'username' not in session: if 'username' not in session:
flash('Bitte melden Sie sich an, um Ihre ausgeliehenen Objekte anzuzeigen', 'error') flash('Bitte melden Sie sich an, um Ihre ausgeliehenen Objekte anzuzeigen', 'error')
return redirect(url_for('login', next=request.path)) return redirect(url_for('login', next=request.path))
username = session['username'] username = session['username']
client = MongoClient(MONGODB_HOST, MONGODB_PORT) client = MongoClient(MONGODB_HOST, MONGODB_PORT)
db = client[MONGODB_DB] db = client[MONGODB_DB]
items_collection = db.items items_collection = db['items']
ausleihungen_collection = db.ausleihungen ausleihungen_collection = db['ausleihungen']
# Get current time for comparison all_ausleihungen = list(ausleihungen_collection.find({
current_time = datetime.datetime.now() 'Status': {'$in': ['active', 'planned']}
# Get items currently borrowed by the user (where Verfuegbar=false and User=username)
borrowed_items = list(items_collection.find({'Verfuegbar': False, 'User': encrypt_text(username)}))
# Get active and planned ausleihungen for the user
active_ausleihungen = list(ausleihungen_collection.find({
'User': encrypt_text(username),
'Status': 'active'
})) }))
planned_ausleihungen = list(ausleihungen_collection.find({
'User': encrypt_text(username),
'Status': 'planned'
}))
# Process items
active_items = [] active_items = []
planned_items = [] planned_items = []
processed_item_ids = set() # Keep track of processed item IDs to avoid duplicates processed_item_ids = set()
# First, process items that are directly marked as borrowed by the user for appointment in all_ausleihungen:
for item in borrowed_items: raw_user = appointment.get('User', '')
# Convert ObjectId to string for template try:
item['_id'] = str(item['_id']) decrypted_user = decrypt_text(raw_user) if raw_user else ''
active_items.append(item) except Exception as e:
processed_item_ids.add(item['_id']) app.logger.error(f"Entschlüsselungsfehler: {e}")
decrypted_user = ''
# Process active appointments
for appointment in active_ausleihungen: if decrypted_user != username:
# Get the item ID from the appointment continue
item_id = appointment.get('Item') item_id = appointment.get('Item')
if not item_id:
if not item_id or str(item_id) in processed_item_ids: continue
continue # Skip if we already processed this item or no item ID
try:
# Get item details if isinstance(item_id, str):
item_obj = items_collection.find_one({'_id': ObjectId(item_id)}) query_id = ObjectId(item_id)
else:
query_id = item_id
item_obj = items_collection.find_one({'_id': query_id})
except Exception:
item_obj = None
if item_obj: if item_obj:
# Convert ObjectId to string for template
item_obj['_id'] = str(item_obj['_id']) item_obj['_id'] = str(item_obj['_id'])
# Add appointment data
item_obj['AppointmentData'] = { item_obj['AppointmentData'] = {
'id': str(appointment['_id']), 'id': str(appointment['_id']),
'start': appointment.get('Start'), 'start': appointment.get('Start'),
@@ -10319,54 +10308,43 @@ def my_borrowed_items():
'period': appointment.get('Period'), 'period': appointment.get('Period'),
'status': appointment.get('VerifiedStatus', appointment.get('Status')), 'status': appointment.get('VerifiedStatus', appointment.get('Status')),
} }
# Mark that this item is part of an active appointment status = appointment.get('Status')
item_obj['ActiveAppointment'] = True if status == 'active':
item_obj['ActiveAppointment'] = True
# Add to the list only if not already there if str(item_obj['_id']) not in processed_item_ids:
if str(item_obj['_id']) not in processed_item_ids: active_items.append(item_obj)
active_items.append(item_obj) processed_item_ids.add(str(item_obj['_id']))
processed_item_ids.add(str(item_obj['_id'])) elif status == 'planned':
planned_items.append(item_obj)
# Process planned appointments
for appointment in planned_ausleihungen: all_borrowed_items = list(items_collection.find({'Verfuegbar': False}))
item_id = appointment.get('Item') for item in all_borrowed_items:
raw_item_user = item.get('User', '')
if not item_id: try:
continue dec_item_user = decrypt_text(raw_item_user) if raw_item_user else ''
except Exception:
item_obj = items_collection.find_one({'_id': ObjectId(item_id)}) dec_item_user = ''
if item_obj: if dec_item_user == username and str(item['_id']) not in processed_item_ids:
item_obj['_id'] = str(item_obj['_id']) item['_id'] = str(item['_id'])
item['ActiveAppointment'] = True
# Add appointment data item['AppointmentData'] = {'status': 'active (no document)'}
item_obj['AppointmentData'] = { active_items.append(item)
'id': str(appointment['_id']), processed_item_ids.add(item['_id'])
'start': appointment.get('Start'),
'end': appointment.get('End'),
'notes': appointment.get('Notes'),
'period': appointment.get('Period'),
'status': appointment.get('Status'),
}
planned_items.append(item_obj)
client.close() client.close()
# DEBUG: Log what we're passing to the template # DEBUG Logging
app.logger.info(f"Passing {len(active_items)} active items and {len(planned_items)} planned items to template") app.logger.info(
if planned_items: f"Passing {len(active_items)} active items and {len(planned_items)} planned items to template for user {username}")
for i, item in enumerate(planned_items):
app.logger.info(f"Planned item {i+1}: {item['Name']}, Appointment ID: {item['AppointmentData']['id']}")
return render_template( return render_template(
'my_borrowed_items.html', 'my_borrowed_items.html',
items=active_items, items=active_items,
planned_items=planned_items planned_items=planned_items
) )
@app.route('/api/push/vapid-key', methods=['GET']) @app.route('/api/push/vapid-key', methods=['GET'])
def get_vapid_key(): def get_vapid_key():
""" """