2025-01-24 10:09:12 +01:00
|
|
|
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
|
2025-01-31 16:34:52 +01:00
|
|
|
from ...set_owner import BrickSetOwner
|
|
|
|
from ...set_owner_list import BrickSetOwnerList
|
2025-01-30 15:03:16 +01:00
|
|
|
from ...set_status import BrickSetStatus
|
|
|
|
from ...set_status_list import BrickSetStatusList
|
2025-01-24 10:09:12 +01:00
|
|
|
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:
|
|
|
|
database_counters: list[BrickCounter] = []
|
|
|
|
database_exception: Exception | None = None
|
|
|
|
database_upgrade_needed: bool = False
|
|
|
|
database_version: int = -1
|
2025-01-31 16:34:52 +01:00
|
|
|
metadata_owners: list[BrickSetOwner] = []
|
2025-01-30 15:03:16 +01:00
|
|
|
metadata_statuses: list[BrickSetStatus] = []
|
2025-01-24 10:09:12 +01:00
|
|
|
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()
|
|
|
|
|
2025-01-31 16:34:52 +01:00
|
|
|
metadata_owners = BrickSetOwnerList(BrickSetOwner).list()
|
2025-01-30 16:23:47 +01:00
|
|
|
metadata_statuses = BrickSetStatusList(BrickSetStatus).list(all=True)
|
2025-01-24 10:09:12 +01:00
|
|
|
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_image = request.args.get('open_image', None)
|
|
|
|
open_instructions = request.args.get('open_instructions', None)
|
|
|
|
open_logout = request.args.get('open_logout', None)
|
2025-01-31 16:34:52 +01:00
|
|
|
open_owner = request.args.get('open_owner', None)
|
2025-01-24 10:09:12 +01:00
|
|
|
open_retired = request.args.get('open_retired', None)
|
2025-01-30 15:03:16 +01:00
|
|
|
open_status = request.args.get('open_status', None)
|
2025-01-24 10:09:12 +01:00
|
|
|
open_theme = request.args.get('open_theme', None)
|
|
|
|
|
|
|
|
open_database = (
|
|
|
|
open_image is None and
|
|
|
|
open_instructions is None and
|
|
|
|
open_logout is None and
|
2025-01-31 16:34:52 +01:00
|
|
|
open_owner is None and
|
2025-01-24 10:09:12 +01:00
|
|
|
open_retired is None and
|
2025-01-30 15:03:16 +01:00
|
|
|
open_status is None and
|
2025-01-24 10:09:12 +01:00
|
|
|
open_theme is None
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'admin.html',
|
|
|
|
configuration=BrickConfigurationList.list(),
|
|
|
|
database_counters=database_counters,
|
2025-01-29 22:49:17 +01:00
|
|
|
database_error=request.args.get('database_error'),
|
2025-01-24 10:09:12 +01:00
|
|
|
database_exception=database_exception,
|
|
|
|
database_upgrade_needed=database_upgrade_needed,
|
|
|
|
database_version=database_version,
|
|
|
|
instructions=BrickInstructionsList(),
|
2025-01-31 16:34:52 +01:00
|
|
|
metadata_owners=metadata_owners,
|
2025-01-30 15:03:16 +01:00
|
|
|
metadata_statuses=metadata_statuses,
|
2025-01-24 10:09:12 +01:00
|
|
|
nil_minifigure_name=nil_minifigure_name,
|
|
|
|
nil_minifigure_url=nil_minifigure_url,
|
|
|
|
nil_part_name=nil_part_name,
|
|
|
|
nil_part_url=nil_part_url,
|
2025-01-30 15:03:16 +01:00
|
|
|
open_status=open_status,
|
2025-01-24 10:09:12 +01:00
|
|
|
open_database=open_database,
|
|
|
|
open_image=open_image,
|
|
|
|
open_instructions=open_instructions,
|
|
|
|
open_logout=open_logout,
|
2025-01-31 16:34:52 +01:00
|
|
|
open_owner=open_owner,
|
2025-01-24 10:09:12 +01:00
|
|
|
open_retired=open_retired,
|
|
|
|
open_theme=open_theme,
|
2025-01-31 16:34:52 +01:00
|
|
|
owner_error=request.args.get('owner_error'),
|
|
|
|
status_error=request.args.get('status_error'),
|
2025-01-24 10:09:12 +01:00
|
|
|
retired=BrickRetiredList(),
|
|
|
|
theme=BrickThemeList(),
|
|
|
|
)
|