diff --git a/Web/app.py b/Web/app.py index 7e94698..b4739fb 100755 --- a/Web/app.py +++ b/Web/app.py @@ -166,7 +166,7 @@ SCHEDULER_INTERVAL = cfg.SCHEDULER_INTERVAL_MIN SSL_CERT = cfg.SSL_CERT SSL_KEY = cfg.SSL_KEY -LIBRARY_ITEM_TYPES = ['book', 'cd', 'dvd', 'media'] +LIBRARY_ITEM_TYPES = ['book', 'cd', 'dvd', 'media', 'schulbuch'] INVOICE_CURRENCY = 'EUR' NOTIFICATION_STATUS_CACHE_TTL = max(3, int(os.getenv('INVENTAR_NOTIFICATION_STATUS_CACHE_TTL', '8'))) @@ -2717,6 +2717,49 @@ def tutorial_page(): ) @app.route('/library') + +@app.route('/library/export/') +def library_export_excel(scope): + if 'username' not in session: + return redirect(url_for('login')) + + username = session['username'] + is_admin_user = us.check_admin(username) + + import excel_export + client = MongoClient(MONGODB_HOST, MONGODB_PORT) + db = client[cfg.MONGODB_DB] + items_collection = db['items'] + + query = {'ItemType': {'$in': LIBRARY_ITEM_TYPES}, 'Deleted': {'$ne': True}} + + if scope == 'borrowed_by_me': + query['User'] = username + query['Verfuegbar'] = False + filename = f"Bibliothek_Ausgeliehen_{username}.xlsx" + elif scope == 'all_borrowed': + if not is_admin_user: + return redirect(url_for('library_view')) + query['Verfuegbar'] = False + filename = "Bibliothek_Alle_Ausleihen.xlsx" + elif scope == 'schulbuecher': + query['ItemType'] = 'schulbuch' + filename = "Schulbuecher_Bestand.xlsx" + else: + # 'all' + filename = "Bibliothek_Gesamtbestand.xlsx" + + items = list(items_collection.find(query)) + client.close() + + excel_file = excel_export.generate_library_excel(items) + return flask.send_file( + excel_file, + mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + as_attachment=True, + download_name=filename + ) + def library_view(): """ Dedicated page for viewing library items (books, CDs, etc.). diff --git a/Web/excel_export.py b/Web/excel_export.py new file mode 100644 index 0000000..0770c35 --- /dev/null +++ b/Web/excel_export.py @@ -0,0 +1,56 @@ +import io +from openpyxl import Workbook +from openpyxl.styles import Font, PatternFill + +def generate_library_excel(items): + wb = Workbook() + ws = wb.active + ws.title = "Export" + + headers = [ + "Code", "Titel", "Autor", "Typ", "ISBN/Code", + "Filter 1", "Filter 2", "Filter 3", + "Status", "Ausgeliehen von", "Rückgabe", "Kosten" + ] + + ws.append(headers) + + header_font = Font(bold=True, color="FFFFFF") + header_fill = PatternFill(start_color="1F2937", end_color="1F2937", fill_type="solid") + for cell in ws[1]: + cell.font = header_font + cell.fill = header_fill + + for item in items: + status = "Verfuegbar" if str(item.get("Verfuegbar", "True")).lower() == "true" else "Ausgeliehen" + row = [ + item.get("Code_4", ""), + item.get("Name", ""), + item.get("Author", ""), + item.get("ItemType", ""), + item.get("ISBN", ""), + item.get("Filter", ""), + item.get("Filter2", ""), + item.get("Filter3", ""), + status, + item.get("User", ""), + item.get("ReturnDate", ""), + item.get("Anschaffungskosten", "") + ] + ws.append(row) + + for col in ws.columns: + max_length = 0 + column = col[0].column_letter + for cell in col: + try: + if len(str(cell.value)) > max_length: + max_length = len(str(cell.value)) + except: + pass + ws.column_dimensions[column].width = min(max_length + 2, 50) + + excel_buffer = io.BytesIO() + wb.save(excel_buffer) + excel_buffer.seek(0) + return excel_buffer diff --git a/Web/templates/base.html b/Web/templates/base.html index 1a7d24d..1487139 100755 --- a/Web/templates/base.html +++ b/Web/templates/base.html @@ -1096,16 +1096,6 @@ Meine Ausleihen {% endif %} - {% if current_permissions.pages.get('tutorial_page', True) %} - - {% endif %} - {% endif %} - {% if 'username' in session and current_permissions.pages.get('upload_admin', True) and current_permissions.actions.get('can_insert', True) %} - {% endif %}