forked from FrederikBaerentsen/BrickTracker
Refactor nil image code, and add a built-in fix for missing nil images
This commit is contained in:
parent
0d9276f639
commit
05484f6148
@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
from sqlite3 import Row
|
from sqlite3 import Row
|
||||||
from typing import Any, Self, TYPE_CHECKING
|
from typing import Any, Self, TYPE_CHECKING
|
||||||
|
|
||||||
@ -126,23 +125,7 @@ class BrickMinifigure(BrickRecord):
|
|||||||
else:
|
else:
|
||||||
file = self.fields.fig_num
|
file = self.fields.fig_num
|
||||||
|
|
||||||
folder: str = current_app.config['MINIFIGURES_FOLDER'].value
|
return RebrickableImage.static_url(file, 'MINIFIGURES_FOLDER')
|
||||||
|
|
||||||
# /!\ Everything is saved as .jpg, even if it came from a .png
|
|
||||||
# not changing this behaviour.
|
|
||||||
|
|
||||||
# Grab the extension
|
|
||||||
# _, extension = os.path.splitext(self.part_img_url)
|
|
||||||
extension = '.jpg'
|
|
||||||
|
|
||||||
# Compute the path
|
|
||||||
path = os.path.join(folder, '{number}{ext}'.format(
|
|
||||||
number=file,
|
|
||||||
ext=extension,
|
|
||||||
))
|
|
||||||
|
|
||||||
return url_for('static', filename=path)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if self.fields.set_img_url is None:
|
if self.fields.set_img_url is None:
|
||||||
return current_app.config['REBRICKABLE_IMAGE_NIL_MINIFIGURE'].value # noqa: E501
|
return current_app.config['REBRICKABLE_IMAGE_NIL_MINIFIGURE'].value # noqa: E501
|
||||||
|
@ -208,23 +208,7 @@ class BrickPart(BrickRecord):
|
|||||||
else:
|
else:
|
||||||
file = self.fields.part_img_url_id
|
file = self.fields.part_img_url_id
|
||||||
|
|
||||||
folder: str = current_app.config['PARTS_FOLDER'].value
|
return RebrickableImage.static_url(file, 'PARTS_FOLDER')
|
||||||
|
|
||||||
# /!\ Everything is saved as .jpg, even if it came from a .png
|
|
||||||
# not changing this behaviour.
|
|
||||||
|
|
||||||
# Grab the extension
|
|
||||||
# _, extension = os.path.splitext(self.part_img_url)
|
|
||||||
extension = '.jpg'
|
|
||||||
|
|
||||||
# Compute the path
|
|
||||||
path = os.path.join(folder, '{number}{ext}'.format(
|
|
||||||
number=file,
|
|
||||||
ext=extension,
|
|
||||||
))
|
|
||||||
|
|
||||||
return url_for('static', filename=path)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if self.fields.part_img_url is None:
|
if self.fields.part_img_url is None:
|
||||||
return current_app.config['REBRICKABLE_IMAGE_NIL'].value
|
return current_app.config['REBRICKABLE_IMAGE_NIL'].value
|
||||||
|
@ -2,7 +2,7 @@ import os
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app, url_for
|
||||||
import requests
|
import requests
|
||||||
from shutil import copyfileobj
|
from shutil import copyfileobj
|
||||||
|
|
||||||
@ -138,3 +138,23 @@ class RebrickableImage(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
# Return the static URL for an image given a name and folder
|
||||||
|
@staticmethod
|
||||||
|
def static_url(name: str, folder_name: str) -> str:
|
||||||
|
folder: str = current_app.config[folder_name].value
|
||||||
|
|
||||||
|
# /!\ Everything is saved as .jpg, even if it came from a .png
|
||||||
|
# not changing this behaviour.
|
||||||
|
|
||||||
|
# Grab the extension
|
||||||
|
# _, extension = os.path.splitext(self.part_img_url)
|
||||||
|
extension = '.jpg'
|
||||||
|
|
||||||
|
# Compute the path
|
||||||
|
path = os.path.join(folder, '{name}{ext}'.format(
|
||||||
|
name=name,
|
||||||
|
ext=extension,
|
||||||
|
))
|
||||||
|
|
||||||
|
return url_for('static', filename=path)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
from sqlite3 import Row
|
from sqlite3 import Row
|
||||||
from typing import Any, Self
|
from typing import Any, Self
|
||||||
|
|
||||||
@ -9,6 +8,7 @@ from .instructions import BrickInstructions
|
|||||||
from .instructions_list import BrickInstructionsList
|
from .instructions_list import BrickInstructionsList
|
||||||
from .minifigure_list import BrickMinifigureList
|
from .minifigure_list import BrickMinifigureList
|
||||||
from .part_list import BrickPartList
|
from .part_list import BrickPartList
|
||||||
|
from .rebrickable_image import RebrickableImage
|
||||||
from .record import BrickRecord
|
from .record import BrickRecord
|
||||||
from .sql import BrickSQL
|
from .sql import BrickSQL
|
||||||
from .theme_list import BrickThemeList
|
from .theme_list import BrickThemeList
|
||||||
@ -158,25 +158,10 @@ class BrickSet(BrickRecord):
|
|||||||
# Compute the url for the set image
|
# Compute the url for the set image
|
||||||
def url_for_image(self, /) -> str:
|
def url_for_image(self, /) -> str:
|
||||||
if not current_app.config['USE_REMOTE_IMAGES'].value:
|
if not current_app.config['USE_REMOTE_IMAGES'].value:
|
||||||
folder: str = current_app.config['SETS_FOLDER'].value
|
return RebrickableImage.static_url(
|
||||||
|
self.fields.set_num,
|
||||||
# /!\ Everything is saved as .jpg, even if it came from a .png
|
'SETS_FOLDER'
|
||||||
# not changing this behaviour.
|
)
|
||||||
|
|
||||||
# Grab the extension
|
|
||||||
# _, extension = os.path.splitext(self.fields.img_url)
|
|
||||||
extension = '.jpg'
|
|
||||||
# Grab the extension
|
|
||||||
_, extension = os.path.splitext(self.fields.set_img_url)
|
|
||||||
|
|
||||||
# Compute the path
|
|
||||||
path = os.path.join(folder, '{number}{ext}'.format(
|
|
||||||
number=self.fields.set_num,
|
|
||||||
ext=extension,
|
|
||||||
))
|
|
||||||
|
|
||||||
return url_for('static', filename=path)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return self.fields.set_img_url
|
return self.fields.set_img_url
|
||||||
|
|
||||||
|
@ -18,7 +18,11 @@ from werkzeug.wrappers.response import Response
|
|||||||
from ..configuration_list import BrickConfigurationList
|
from ..configuration_list import BrickConfigurationList
|
||||||
from .exceptions import exception_handler
|
from .exceptions import exception_handler
|
||||||
from ..instructions_list import BrickInstructionsList
|
from ..instructions_list import BrickInstructionsList
|
||||||
|
from ..minifigure import BrickMinifigure
|
||||||
|
from ..part import BrickPart
|
||||||
|
from ..rebrickable_image import RebrickableImage
|
||||||
from ..retired_list import BrickRetiredList
|
from ..retired_list import BrickRetiredList
|
||||||
|
from ..set import BrickSet
|
||||||
from ..sql import BrickSQL
|
from ..sql import BrickSQL
|
||||||
from ..theme_list import BrickThemeList
|
from ..theme_list import BrickThemeList
|
||||||
from .upload import upload_helper
|
from .upload import upload_helper
|
||||||
@ -34,9 +38,13 @@ admin_page = Blueprint('admin', __name__, url_prefix='/admin')
|
|||||||
@exception_handler(__file__)
|
@exception_handler(__file__)
|
||||||
def admin() -> str:
|
def admin() -> str:
|
||||||
counters: dict[str, int] = {}
|
counters: dict[str, int] = {}
|
||||||
|
count_none: int = 0
|
||||||
exception: Exception | None = None
|
exception: Exception | None = None
|
||||||
is_init: bool = False
|
is_init: bool = False
|
||||||
count_none: int = 0
|
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
|
# This view needs to be protected against SQL errors
|
||||||
try:
|
try:
|
||||||
@ -49,6 +57,18 @@ def admin() -> str:
|
|||||||
if record is not None:
|
if record is not None:
|
||||||
count_none = record['count']
|
count_none = record['count']
|
||||||
|
|
||||||
|
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'
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exception = e
|
exception = e
|
||||||
|
|
||||||
@ -57,12 +77,14 @@ def admin() -> str:
|
|||||||
exception=str(e),
|
exception=str(e),
|
||||||
))
|
))
|
||||||
|
|
||||||
|
open_image = request.args.get('open_image', None)
|
||||||
open_instructions = request.args.get('open_instructions', None)
|
open_instructions = request.args.get('open_instructions', None)
|
||||||
open_logout = request.args.get('open_logout', None)
|
open_logout = request.args.get('open_logout', None)
|
||||||
open_retired = request.args.get('open_retired', None)
|
open_retired = request.args.get('open_retired', None)
|
||||||
open_theme = request.args.get('open_theme', None)
|
open_theme = request.args.get('open_theme', None)
|
||||||
|
|
||||||
open_database = (
|
open_database = (
|
||||||
|
open_image is None and
|
||||||
open_instructions is None and
|
open_instructions is None and
|
||||||
open_logout is None and
|
open_logout is None and
|
||||||
open_retired is None and
|
open_retired is None and
|
||||||
@ -78,7 +100,12 @@ def admin() -> str:
|
|||||||
exception=exception,
|
exception=exception,
|
||||||
instructions=BrickInstructionsList(),
|
instructions=BrickInstructionsList(),
|
||||||
is_init=is_init,
|
is_init=is_init,
|
||||||
|
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_database=open_database,
|
open_database=open_database,
|
||||||
|
open_image=open_image,
|
||||||
open_instructions=open_instructions,
|
open_instructions=open_instructions,
|
||||||
open_logout=open_logout,
|
open_logout=open_logout,
|
||||||
open_retired=open_retired,
|
open_retired=open_retired,
|
||||||
@ -241,6 +268,31 @@ def refresh_themes() -> Response:
|
|||||||
return redirect(url_for('admin.admin', open_theme=True))
|
return redirect(url_for('admin.admin', open_theme=True))
|
||||||
|
|
||||||
|
|
||||||
|
# Update the default images
|
||||||
|
@admin_page.route('/update-image', methods=['GET'])
|
||||||
|
@login_required
|
||||||
|
@exception_handler(__file__)
|
||||||
|
def update_image() -> Response:
|
||||||
|
# Abusing the object to create a 'nil' minifigure
|
||||||
|
RebrickableImage(
|
||||||
|
BrickSet(),
|
||||||
|
minifigure=BrickMinifigure(record={
|
||||||
|
'set_img_url': None,
|
||||||
|
})
|
||||||
|
).download()
|
||||||
|
|
||||||
|
# Abusing the object to create a 'nil' part
|
||||||
|
RebrickableImage(
|
||||||
|
BrickSet(),
|
||||||
|
part=BrickPart(record={
|
||||||
|
'part_img_url': None,
|
||||||
|
'part_img_url_id': None
|
||||||
|
})
|
||||||
|
).download()
|
||||||
|
|
||||||
|
return redirect(url_for('admin.admin', open_image=True))
|
||||||
|
|
||||||
|
|
||||||
# Update the themes file
|
# Update the themes file
|
||||||
@admin_page.route('/update-retired', methods=['GET'])
|
@admin_page.route('/update-retired', methods=['GET'])
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -21,6 +21,9 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
{% include 'admin/logout.html' %}
|
{% include 'admin/logout.html' %}
|
||||||
{% include 'admin/instructions.html' %}
|
{% include 'admin/instructions.html' %}
|
||||||
|
{% if not config['USE_REMOTE_IMAGES'].value %}
|
||||||
|
{% include 'admin/image.html' %}
|
||||||
|
{% endif %}
|
||||||
{% include 'admin/theme.html' %}
|
{% include 'admin/theme.html' %}
|
||||||
{% include 'admin/retired.html' %}
|
{% include 'admin/retired.html' %}
|
||||||
{% include 'admin/database.html' %}
|
{% include 'admin/database.html' %}
|
||||||
|
14
templates/admin/image.html
Normal file
14
templates/admin/image.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{% import 'macro/accordion.html' as accordion %}
|
||||||
|
|
||||||
|
{{ accordion.header('Default images', 'image', 'admin', expanded=open_image, icon='image-line') }}
|
||||||
|
<p>
|
||||||
|
If you do not see the following image, you either do not need them or you are coming from the original version of BrickTracker and or they have been moved.</code>.
|
||||||
|
</p>
|
||||||
|
<div class="d-flex justify-content-center border-bottom pb-2 mb-2">
|
||||||
|
<img class="table-img border mx-1" src="{{ nil_minifigure_url }}" alt="{{ nil_minifigure_name }}">
|
||||||
|
<img class="table-img border mx-1" src="{{ nil_part_url }}" alt="{{ nil_part_name }}">
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
<a href="{{ url_for('admin.update_image') }}" class="btn btn-primary" role="button"><i class="ri-download-line"></i> Update the images</a>
|
||||||
|
</p>
|
||||||
|
{{ accordion.footer() }}
|
Loading…
Reference in New Issue
Block a user