import logging from flask import Blueprint, request, render_template from flask_login import login_required from ...configuration_list import BrickConfigurationList from ..exceptions import exception_handler from ...instructions_list import BrickInstructionsList from ...rebrickable_image import RebrickableImage from ...retired_list import BrickRetiredList from ...set_checkbox import BrickSetCheckbox from ...set_checkbox_list import BrickSetCheckboxList from ...sql_counter import BrickCounter from ...sql import BrickSQL from ...theme_list import BrickThemeList logger = logging.getLogger(__name__) admin_page = Blueprint('admin', __name__, url_prefix='/admin') # Admin @admin_page.route('/', methods=['GET']) @login_required @exception_handler(__file__) def admin() -> str: brickset_checkboxes: list[BrickSetCheckbox] = [] database_counters: list[BrickCounter] = [] database_exception: Exception | None = None database_upgrade_needed: bool = False database_version: int = -1 nil_minifigure_name: str = '' nil_minifigure_url: str = '' nil_part_name: str = '' nil_part_url: str = '' # This view needs to be protected against SQL errors try: database = BrickSQL(failsafe=True) database_upgrade_needed = database.upgrade_needed() database_version = database.version database_counters = BrickSQL().count_records() brickset_checkboxes = BrickSetCheckboxList().list(all=True) except Exception as e: database_exception = e # Warning logger.warning('A database exception occured while loading the admin page: {exception}'.format( # noqa: E501 exception=str(e), )) nil_minifigure_name = RebrickableImage.nil_minifigure_name() nil_minifigure_url = RebrickableImage.static_url( nil_minifigure_name, 'MINIFIGURES_FOLDER' ) nil_part_name = RebrickableImage.nil_name() nil_part_url = RebrickableImage.static_url( nil_part_name, 'PARTS_FOLDER' ) open_checkbox = request.args.get('open_checkbox', None) open_image = request.args.get('open_image', None) open_instructions = request.args.get('open_instructions', None) open_logout = request.args.get('open_logout', None) open_retired = request.args.get('open_retired', None) open_theme = request.args.get('open_theme', None) open_database = ( open_checkbox is None and open_image is None and open_instructions is None and open_logout is None and open_retired is None and open_theme is None ) return render_template( 'admin.html', configuration=BrickConfigurationList.list(), brickset_checkboxes=brickset_checkboxes, database_counters=database_counters, database_error=request.args.get('error'), database_exception=database_exception, database_upgrade_needed=database_upgrade_needed, database_version=database_version, instructions=BrickInstructionsList(), nil_minifigure_name=nil_minifigure_name, nil_minifigure_url=nil_minifigure_url, nil_part_name=nil_part_name, nil_part_url=nil_part_url, open_checkbox=open_checkbox, open_database=open_database, open_image=open_image, open_instructions=open_instructions, open_logout=open_logout, open_retired=open_retired, open_theme=open_theme, retired=BrickRetiredList(), theme=BrickThemeList(), )