Deduplicated parts and missing parts

This commit is contained in:
Gregoo 2025-01-28 19:18:51 +01:00
parent 7ff1605c21
commit c4bb3c7607
55 changed files with 871 additions and 756 deletions

View File

@ -28,7 +28,7 @@
# BK_AUTHENTICATION_KEY=change-this-to-something-random # BK_AUTHENTICATION_KEY=change-this-to-something-random
# Optional: Pattern of the link to Bricklink for a part. Will be passed to Python .format() # Optional: Pattern of the link to Bricklink for a part. Will be passed to Python .format()
# Default: https://www.bricklink.com/v2/catalog/catalogitem.page?P={number} # Default: https://www.bricklink.com/v2/catalog/catalogitem.page?P={part}
# BK_BRICKLINK_LINK_PART_PATTERN= # BK_BRICKLINK_LINK_PART_PATTERN=
# Optional: Display Bricklink links wherever applicable # Optional: Display Bricklink links wherever applicable
@ -139,13 +139,13 @@
# Optional: Change the default order of parts. By default ordered by insertion order. # Optional: Change the default order of parts. By default ordered by insertion order.
# Useful column names for this option are: # Useful column names for this option are:
# - "inventory"."part_num": part number # - "bricktracker_parts"."part": part number
# - "inventory"."name": part name # - "bricktracker_parts"."spare": part is a spare part
# - "inventory"."color_name": part color name # - "rebrickable_parts"."name": part name
# - "inventory"."is_spare": par is a spare part # - "rebrickable_parts"."color_name": part color name
# - "total_missing": number of missing parts # - "total_missing": number of missing parts
# Default: "inventory"."name" ASC, "inventory"."color_name" ASC, "inventory"."is_spare" ASC # Default: "rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "bricktracker_parts"."spare" ASC
# BK_PARTS_DEFAULT_ORDER="total_missing" DESC, "inventory"."name" ASC # BK_PARTS_DEFAULT_ORDER="total_missing" DESC, "rebrickable_parts"."name"."name" ASC
# Optional: Folder where to store the parts images, relative to the '/app/static/' folder # Optional: Folder where to store the parts images, relative to the '/app/static/' folder
# Default: parts # Default: parts
@ -180,7 +180,7 @@
# BK_REBRICKABLE_LINK_MINIFIGURE_PATTERN= # BK_REBRICKABLE_LINK_MINIFIGURE_PATTERN=
# Optional: Pattern of the link to Rebrickable for a part. Will be passed to Python .format() # Optional: Pattern of the link to Rebrickable for a part. Will be passed to Python .format()
# Default: https://rebrickable.com/parts/{number}/_/{color} # Default: https://rebrickable.com/parts/{part}/_/{color}
# BK_REBRICKABLE_LINK_PART_PATTERN= # BK_REBRICKABLE_LINK_PART_PATTERN=
# Optional: Pattern of the link to Rebrickable for instructions. Will be passed to Python .format() # Optional: Pattern of the link to Rebrickable for instructions. Will be passed to Python .format()

View File

@ -10,7 +10,7 @@ from typing import Any, Final
CONFIG: Final[list[dict[str, Any]]] = [ CONFIG: Final[list[dict[str, Any]]] = [
{'n': 'AUTHENTICATION_PASSWORD', 'd': ''}, {'n': 'AUTHENTICATION_PASSWORD', 'd': ''},
{'n': 'AUTHENTICATION_KEY', 'd': ''}, {'n': 'AUTHENTICATION_KEY', 'd': ''},
{'n': 'BRICKLINK_LINK_PART_PATTERN', 'd': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P={number}'}, # noqa: E501 {'n': 'BRICKLINK_LINK_PART_PATTERN', 'd': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P={part}'}, # noqa: E501
{'n': 'BRICKLINK_LINKS', 'c': bool}, {'n': 'BRICKLINK_LINKS', 'c': bool},
{'n': 'DATABASE_PATH', 'd': './app.db'}, {'n': 'DATABASE_PATH', 'd': './app.db'},
{'n': 'DATABASE_TIMESTAMP_FORMAT', 'd': '%Y-%m-%d-%H-%M-%S'}, {'n': 'DATABASE_TIMESTAMP_FORMAT', 'd': '%Y-%m-%d-%H-%M-%S'},
@ -35,7 +35,7 @@ CONFIG: Final[list[dict[str, Any]]] = [
{'n': 'MINIFIGURES_DEFAULT_ORDER', 'd': '"rebrickable_minifigures"."name" ASC'}, # noqa: E501 {'n': 'MINIFIGURES_DEFAULT_ORDER', 'd': '"rebrickable_minifigures"."name" ASC'}, # noqa: E501
{'n': 'MINIFIGURES_FOLDER', 'd': 'minifigs', 's': True}, {'n': 'MINIFIGURES_FOLDER', 'd': 'minifigs', 's': True},
{'n': 'NO_THREADED_SOCKET', 'c': bool}, {'n': 'NO_THREADED_SOCKET', 'c': bool},
{'n': 'PARTS_DEFAULT_ORDER', 'd': '"inventory"."name" ASC, "inventory"."color_name" ASC, "inventory"."is_spare" ASC'}, # noqa: E501 {'n': 'PARTS_DEFAULT_ORDER', 'd': '"rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "bricktracker_parts"."spare" ASC'}, # noqa: E501
{'n': 'PARTS_FOLDER', 'd': 'parts', 's': True}, {'n': 'PARTS_FOLDER', 'd': 'parts', 's': True},
{'n': 'PORT', 'd': 3333, 'c': int}, {'n': 'PORT', 'd': 3333, 'c': int},
{'n': 'RANDOM', 'e': 'RANDOM', 'c': bool}, {'n': 'RANDOM', 'e': 'RANDOM', 'c': bool},
@ -43,7 +43,7 @@ CONFIG: Final[list[dict[str, Any]]] = [
{'n': 'REBRICKABLE_IMAGE_NIL', 'd': 'https://rebrickable.com/static/img/nil.png'}, # noqa: E501 {'n': 'REBRICKABLE_IMAGE_NIL', 'd': 'https://rebrickable.com/static/img/nil.png'}, # noqa: E501
{'n': 'REBRICKABLE_IMAGE_NIL_MINIFIGURE', 'd': 'https://rebrickable.com/static/img/nil_mf.jpg'}, # noqa: E501 {'n': 'REBRICKABLE_IMAGE_NIL_MINIFIGURE', 'd': 'https://rebrickable.com/static/img/nil_mf.jpg'}, # noqa: E501
{'n': 'REBRICKABLE_LINK_MINIFIGURE_PATTERN', 'd': 'https://rebrickable.com/minifigs/{figure}'}, # noqa: E501 {'n': 'REBRICKABLE_LINK_MINIFIGURE_PATTERN', 'd': 'https://rebrickable.com/minifigs/{figure}'}, # noqa: E501
{'n': 'REBRICKABLE_LINK_PART_PATTERN', 'd': 'https://rebrickable.com/parts/{number}/_/{color}'}, # noqa: E501 {'n': 'REBRICKABLE_LINK_PART_PATTERN', 'd': 'https://rebrickable.com/parts/{part}/_/{color}'}, # noqa: E501
{'n': 'REBRICKABLE_LINK_INSTRUCTIONS_PATTERN', 'd': 'https://rebrickable.com/instructions/{path}'}, # noqa: E501 {'n': 'REBRICKABLE_LINK_INSTRUCTIONS_PATTERN', 'd': 'https://rebrickable.com/instructions/{path}'}, # noqa: E501
{'n': 'REBRICKABLE_USER_AGENT', 'd': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}, # noqa: E501 {'n': 'REBRICKABLE_USER_AGENT', 'd': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}, # noqa: E501
{'n': 'REBRICKABLE_LINKS', 'e': 'LINKS', 'c': bool}, {'n': 'REBRICKABLE_LINKS', 'e': 'LINKS', 'c': bool},

View File

@ -4,7 +4,6 @@ from typing import Self, TYPE_CHECKING
from .exceptions import ErrorException, NotFoundException from .exceptions import ErrorException, NotFoundException
from .part_list import BrickPartList from .part_list import BrickPartList
from .rebrickable_parts import RebrickableParts
from .rebrickable_minifigure import RebrickableMinifigure from .rebrickable_minifigure import RebrickableMinifigure
if TYPE_CHECKING: if TYPE_CHECKING:
from .set import BrickSet from .set import BrickSet
@ -20,7 +19,8 @@ class BrickMinifigure(RebrickableMinifigure):
generic_query: str = 'minifigure/select/generic' generic_query: str = 'minifigure/select/generic'
select_query: str = 'minifigure/select/specific' select_query: str = 'minifigure/select/specific'
def download(self, socket: 'BrickSocket'): # Import a minifigure into the database
def download(self, socket: 'BrickSocket') -> bool:
if self.brickset is None: if self.brickset is None:
raise ErrorException('Importing a minifigure from Rebrickable outside of a set is not supported') # noqa: E501 raise ErrorException('Importing a minifigure from Rebrickable outside of a set is not supported') # noqa: E501
@ -40,11 +40,12 @@ class BrickMinifigure(RebrickableMinifigure):
self.insert_rebrickable() self.insert_rebrickable()
# Load the inventory # Load the inventory
RebrickableParts( if not BrickPartList.download(
socket, socket,
self.brickset, self.brickset,
minifigure=self, minifigure=self
).download() ):
return False
except Exception as e: except Exception as e:
socket.fail( socket.fail(
@ -57,6 +58,10 @@ class BrickMinifigure(RebrickableMinifigure):
logger.debug(traceback.format_exc()) logger.debug(traceback.format_exc())
return False
return True
# Parts # Parts
def generic_parts(self, /) -> BrickPartList: def generic_parts(self, /) -> BrickPartList:
return BrickPartList().from_minifigure(self) return BrickPartList().from_minifigure(self)
@ -68,7 +73,7 @@ class BrickMinifigure(RebrickableMinifigure):
figure=self.fields.figure, figure=self.fields.figure,
)) ))
return BrickPartList().load(self.brickset, minifigure=self) return BrickPartList().list_specific(self.brickset, minifigure=self)
# Select a generic minifigure # Select a generic minifigure
def select_generic(self, figure: str, /) -> Self: def select_generic(self, figure: str, /) -> Self:

View File

@ -82,16 +82,13 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
# Minifigures missing a part # Minifigures missing a part
def missing_part( def missing_part(
self, self,
part_num: str, part: str,
color_id: int, color: int,
/, /,
*,
element_id: int | None = None,
) -> Self: ) -> Self:
# Save the parameters to the fields # Save the parameters to the fields
self.fields.part_num = part_num self.fields.part = part
self.fields.color_id = color_id self.fields.color = color
self.fields.element_id = element_id
# Load the minifigures from the database # Load the minifigures from the database
for record in self.select( for record in self.select(
@ -107,16 +104,13 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
# Minifigure using a part # Minifigure using a part
def using_part( def using_part(
self, self,
part_num: str, part: str,
color_id: int, color: int,
/, /,
*,
element_id: int | None = None,
) -> Self: ) -> Self:
# Save the parameters to the fields # Save the parameters to the fields
self.fields.part_num = part_num self.fields.part = part
self.fields.color_id = color_id self.fields.color = color
self.fields.element_id = element_id
# Load the minifigures from the database # Load the minifigures from the database
for record in self.select( for record in self.select(
@ -140,7 +134,7 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
# Import the minifigures from Rebrickable # Import the minifigures from Rebrickable
@staticmethod @staticmethod
def download(socket: 'BrickSocket', brickset: 'BrickSet', /) -> None: def download(socket: 'BrickSocket', brickset: 'BrickSet', /) -> bool:
try: try:
socket.auto_progress( socket.auto_progress(
message='Set {set}: loading minifigures from Rebrickable'.format( # noqa: E501 message='Set {set}: loading minifigures from Rebrickable'.format( # noqa: E501
@ -162,10 +156,11 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
).list() ).list()
# Process each minifigure # Process each minifigure
socket.update_total(len(minifigures), add=True)
for minifigure in minifigures: for minifigure in minifigures:
minifigure.download(socket) if not minifigure.download(socket):
return False
return True
except Exception as e: except Exception as e:
socket.fail( socket.fail(
@ -176,3 +171,5 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
) )
logger.debug(traceback.format_exc()) logger.debug(traceback.format_exc())
return False

View File

@ -1,103 +1,104 @@
import os import logging
from sqlite3 import Row
from typing import Any, Self, TYPE_CHECKING from typing import Any, Self, TYPE_CHECKING
from urllib.parse import urlparse import traceback
from flask import current_app, url_for from .exceptions import ErrorException, NotFoundException
from .rebrickable_part import RebrickablePart
from .exceptions import DatabaseException, ErrorException, NotFoundException
from .rebrickable_image import RebrickableImage
from .record import BrickRecord
from .sql import BrickSQL from .sql import BrickSQL
if TYPE_CHECKING: if TYPE_CHECKING:
from .minifigure import BrickMinifigure from .minifigure import BrickMinifigure
from .set import BrickSet from .set import BrickSet
from .socket import BrickSocket
logger = logging.getLogger(__name__)
# Lego set or minifig part # Lego set or minifig part
class BrickPart(BrickRecord): class BrickPart(RebrickablePart):
brickset: 'BrickSet | None' identifier: str
minifigure: 'BrickMinifigure | None' kind: str
# Queries # Queries
insert_query: str = 'part/insert' insert_query: str = 'part/insert'
generic_query: str = 'part/select/generic' generic_query: str = 'part/select/generic'
select_query: str = 'part/select/specific' select_query: str = 'part/select/specific'
def __init__( def __init__(self, /, **kwargs):
self, super().__init__(**kwargs)
/,
*,
brickset: 'BrickSet | None' = None,
minifigure: 'BrickMinifigure | None' = None,
record: Row | dict[str, Any] | None = None,
):
super().__init__()
# Save the brickset and minifigure if self.minifigure is not None:
self.brickset = brickset self.identifier = self.minifigure.fields.figure
self.minifigure = minifigure self.kind = 'Minifigure'
elif self.brickset is not None:
self.identifier = self.brickset.fields.set
self.kind = 'Set'
# Ingest the record if it has one # Import a part into the database
if record is not None: def download(self, socket: 'BrickSocket') -> bool:
self.ingest(record) if self.brickset is None:
raise ErrorException('Importing a part from Rebrickable outside of a set is not supported') # noqa: E501
# Delete missing part try:
def delete_missing(self, /) -> None: # Insert into the database
BrickSQL().execute_and_commit( socket.auto_progress(
'missing/delete/from_set', message='{kind} {identifier}: inserting part {part} into database'.format( # noqa: E501
parameters=self.sql_parameters() kind=self.kind,
) identifier=self.identifier,
part=self.fields.part
# Set missing part
def set_missing(self, quantity: int, /) -> None:
parameters = self.sql_parameters()
parameters['quantity'] = quantity
# Can't use UPSERT because the database has no keys
# Try to update
database = BrickSQL()
rows, _ = database.execute(
'missing/update/from_set',
parameters=parameters,
)
# Insert if no row has been affected
if not rows:
rows, _ = database.execute(
'missing/insert',
parameters=parameters,
)
if rows != 1:
raise DatabaseException(
'Could not update the missing quantity for part {id}'.format( # noqa: E501
id=self.fields.id
) )
) )
database.commit() # Insert into database
self.insert(commit=False)
# Insert the rebrickable set into database
self.insert_rebrickable()
except Exception as e:
socket.fail(
message='Error while importing part {part} from {kind} {identifier}: {error}'.format( # noqa: E501
part=self.fields.part,
kind=self.kind,
identifier=self.identifier,
error=e,
)
)
logger.debug(traceback.format_exc())
return False
return True
# A identifier for HTML component
def html_id(self) -> str:
components: list[str] = []
if self.fields.figure is not None:
components.append(self.fields.figure)
components.append(self.fields.part)
components.append(str(self.fields.color))
components.append(str(self.fields.spare))
return '-'.join(components)
# Select a generic part # Select a generic part
def select_generic( def select_generic(
self, self,
part_num: str, part: str,
color_id: int, color: int,
/, /,
*,
element_id: int | None = None
) -> Self: ) -> Self:
# Save the parameters to the fields # Save the parameters to the fields
self.fields.part_num = part_num self.fields.part = part
self.fields.color_id = color_id self.fields.color = color
self.fields.element_id = element_id
if not self.select(override_query=self.generic_query): if not self.select(override_query=self.generic_query):
raise NotFoundException( raise NotFoundException(
'Part with number {number}, color ID {color} and element ID {element} was not found in the database'.format( # noqa: E501 'Part with number {number}, color ID {color} was not found in the database'.format( # noqa: E501
number=self.fields.part_num, number=self.fields.part,
color=self.fields.color_id, color=self.fields.color,
element=self.fields.element_id,
), ),
) )
@ -107,7 +108,9 @@ class BrickPart(BrickRecord):
def select_specific( def select_specific(
self, self,
brickset: 'BrickSet', brickset: 'BrickSet',
id: str, part: str,
color: int,
spare: int,
/, /,
*, *,
minifigure: 'BrickMinifigure | None' = None, minifigure: 'BrickMinifigure | None' = None,
@ -115,168 +118,48 @@ class BrickPart(BrickRecord):
# Save the parameters to the fields # Save the parameters to the fields
self.brickset = brickset self.brickset = brickset
self.minifigure = minifigure self.minifigure = minifigure
self.fields.id = id self.fields.part = part
self.fields.color = color
self.fields.spare = spare
if not self.select(): if not self.select():
if self.minifigure is not None:
figure = self.minifigure.fields.figure
else:
figure = None
raise NotFoundException( raise NotFoundException(
'Part with ID {id} from set {set} was not found in the database'.format( # noqa: E501 'Part {part} with color {color} (spare: {spare}) from set {set} ({id}) (minifigure: {figure}) was not found in the database'.format( # noqa: E501
part=self.fields.part,
color=self.fields.color,
spare=self.fields.spare,
id=self.fields.id, id=self.fields.id,
set=self.brickset.fields.set, set=self.brickset.fields.set,
figure=figure,
), ),
) )
return self return self
# Return a dict with common SQL parameters for a part
def sql_parameters(self, /) -> dict[str, Any]:
parameters = super().sql_parameters()
# Supplement from the brickset
if 'u_id' not in parameters and self.brickset is not None:
parameters['u_id'] = self.brickset.fields.id
if 'set_num' not in parameters:
if self.minifigure is not None:
parameters['set_num'] = self.minifigure.fields.figure
elif self.brickset is not None:
parameters['set_num'] = self.brickset.fields.set
return parameters
# Update the missing part # Update the missing part
def update_missing(self, missing: Any, /) -> None: def update_missing(self, missing: Any, /) -> None:
# If empty, delete it # We need a positive integer
if missing == '':
self.delete_missing()
else:
# Try to understand it as a number
try: try:
missing = int(missing) missing = int(missing)
if missing < 0:
missing = 0
except Exception: except Exception:
raise ErrorException('"{missing}" is not a valid integer'.format( # noqa: E501 raise ErrorException('"{missing}" is not a valid integer'.format(
missing=missing missing=missing
)) ))
# If 0, delete it
if missing == 0:
self.delete_missing()
else:
# If negative, it's an error
if missing < 0: if missing < 0:
raise ErrorException('Cannot set a negative missing value') raise ErrorException('Cannot set a negative missing value')
# Otherwise upsert it self.fields.missing = missing
# Not checking if it is too much, you do you
self.set_missing(missing)
# Self url BrickSQL().execute_and_commit(
def url(self, /) -> str: 'part/update/missing',
return url_for( parameters=self.sql_parameters()
'part.details',
number=self.fields.part_num,
color=self.fields.color_id,
element=self.fields.element_id,
) )
# Compute the url for the bricklink page
def url_for_bricklink(self, /) -> str:
if current_app.config['BRICKLINK_LINKS']:
try:
return current_app.config['BRICKLINK_LINK_PART_PATTERN'].format( # noqa: E501
number=self.fields.part_num,
)
except Exception:
pass
return ''
# Compute the url for the part image
def url_for_image(self, /) -> str:
if not current_app.config['USE_REMOTE_IMAGES']:
if self.fields.part_img_url is None:
file = RebrickableImage.nil_name()
else:
file = self.fields.part_img_url_id
return RebrickableImage.static_url(file, 'PARTS_FOLDER')
else:
if self.fields.part_img_url is None:
return current_app.config['REBRICKABLE_IMAGE_NIL']
else:
return self.fields.part_img_url
# Compute the url for missing part
def url_for_missing(self, /) -> str:
# Different URL for a minifigure part
if self.minifigure is not None:
return url_for(
'set.missing_minifigure_part',
id=self.fields.u_id,
figure=self.minifigure.fields.figure,
part=self.fields.id,
)
return url_for(
'set.missing_part',
id=self.fields.u_id,
part=self.fields.id
)
# Compute the url for the rebrickable page
def url_for_rebrickable(self, /) -> str:
if current_app.config['REBRICKABLE_LINKS']:
try:
return current_app.config['REBRICKABLE_LINK_PART_PATTERN'].format( # noqa: E501
number=self.fields.part_num,
color=self.fields.color_id,
)
except Exception:
pass
return ''
# Normalize from Rebrickable
@staticmethod
def from_rebrickable(
data: dict[str, Any],
/,
*,
brickset: 'BrickSet | None' = None,
minifigure: 'BrickMinifigure | None' = None,
**_,
) -> dict[str, Any]:
record = {
'set_num': data['set_num'],
'id': data['id'],
'part_num': data['part']['part_num'],
'name': data['part']['name'],
'part_img_url': data['part']['part_img_url'],
'part_img_url_id': None,
'color_id': data['color']['id'],
'color_name': data['color']['name'],
'quantity': data['quantity'],
'is_spare': data['is_spare'],
'element_id': data['element_id'],
}
if brickset is not None:
record['u_id'] = brickset.fields.id
if minifigure is not None:
record['set_num'] = data['fig_num']
# Extract the file name
if data['part']['part_img_url'] is not None:
part_img_url_file = os.path.basename(
urlparse(data['part']['part_img_url']).path
)
part_img_url_id, _ = os.path.splitext(part_img_url_file)
if part_img_url_id is not None or part_img_url_id != '':
record['part_img_url_id'] = part_img_url_id
return record

View File

@ -1,12 +1,18 @@
import logging
from typing import Any, Self, TYPE_CHECKING from typing import Any, Self, TYPE_CHECKING
import traceback
from flask import current_app from flask import current_app
from .part import BrickPart from .part import BrickPart
from .rebrickable import Rebrickable
from .record_list import BrickRecordList from .record_list import BrickRecordList
if TYPE_CHECKING: if TYPE_CHECKING:
from .minifigure import BrickMinifigure from .minifigure import BrickMinifigure
from .set import BrickSet from .set import BrickSet
from .socket import BrickSocket
logger = logging.getLogger(__name__)
# Lego set or minifig parts # Lego set or minifig parts
@ -20,7 +26,7 @@ class BrickPartList(BrickRecordList[BrickPart]):
last_query: str = 'part/list/last' last_query: str = 'part/list/last'
minifigure_query: str = 'part/list/from_minifigure' minifigure_query: str = 'part/list/from_minifigure'
missing_query: str = 'part/list/missing' missing_query: str = 'part/list/missing'
select_query: str = 'part/list/from_set' select_query: str = 'part/list/specific'
def __init__(self, /): def __init__(self, /):
super().__init__() super().__init__()
@ -44,8 +50,8 @@ class BrickPartList(BrickRecordList[BrickPart]):
return self return self
# Load parts from a brickset or minifigure # List specific parts from a brickset or minifigure
def load( def list_specific(
self, self,
brickset: 'BrickSet', brickset: 'BrickSet',
/, /,
@ -64,7 +70,7 @@ class BrickPartList(BrickRecordList[BrickPart]):
record=record, record=record,
) )
if current_app.config['SKIP_SPARE_PARTS'] and part.fields.is_spare: if current_app.config['SKIP_SPARE_PARTS'] and part.fields.spare:
continue continue
self.records.append(part) self.records.append(part)
@ -90,7 +96,7 @@ class BrickPartList(BrickRecordList[BrickPart]):
record=record, record=record,
) )
if current_app.config['SKIP_SPARE_PARTS'] and part.fields.is_spare: if current_app.config['SKIP_SPARE_PARTS'] and part.fields.spare:
continue continue
self.records.append(part) self.records.append(part)
@ -115,13 +121,73 @@ class BrickPartList(BrickRecordList[BrickPart]):
# Set id # Set id
if self.brickset is not None: if self.brickset is not None:
parameters['u_id'] = self.brickset.fields.id parameters['id'] = self.brickset.fields.id
# Use the minifigure number if present, # Use the minifigure number if present,
# otherwise use the set number
if self.minifigure is not None: if self.minifigure is not None:
parameters['set_num'] = self.minifigure.fields.figure parameters['figure'] = self.minifigure.fields.figure
elif self.brickset is not None: else:
parameters['set_num'] = self.brickset.fields.set parameters['figure'] = None
return parameters return parameters
# Import the parts from Rebrickable
@staticmethod
def download(
socket: 'BrickSocket',
brickset: 'BrickSet',
/,
*,
minifigure: 'BrickMinifigure | None' = None,
) -> bool:
if minifigure is not None:
identifier = minifigure.fields.figure
kind = 'Minifigure'
method = 'get_minifig_elements'
else:
identifier = brickset.fields.set
kind = 'Set'
method = 'get_set_elements'
try:
socket.auto_progress(
message='{kind} {identifier}: loading parts inventory from Rebrickable'.format( # noqa: E501
kind=kind,
identifier=identifier,
),
increment_total=True,
)
logger.debug('rebrick.lego.{method}("{identifier}")'.format(
method=method,
identifier=identifier,
))
inventory = Rebrickable[BrickPart](
method,
identifier,
BrickPart,
socket=socket,
brickset=brickset,
minifigure=minifigure,
).list()
# Process each part
for part in inventory:
if not part.download(socket):
return False
except Exception as e:
socket.fail(
message='Error while importing {kind} {identifier} parts list: {error}'.format( # noqa: E501
kind=kind,
identifier=identifier,
error=e,
)
)
logger.debug(traceback.format_exc())
return False
return True

View File

@ -9,7 +9,7 @@ from shutil import copyfileobj
from .exceptions import DownloadException from .exceptions import DownloadException
if TYPE_CHECKING: if TYPE_CHECKING:
from .rebrickable_minifigure import RebrickableMinifigure from .rebrickable_minifigure import RebrickableMinifigure
from .part import BrickPart from .rebrickable_part import RebrickablePart
from .rebrickable_set import RebrickableSet from .rebrickable_set import RebrickableSet
@ -17,7 +17,7 @@ if TYPE_CHECKING:
class RebrickableImage(object): class RebrickableImage(object):
set: 'RebrickableSet' set: 'RebrickableSet'
minifigure: 'RebrickableMinifigure | None' minifigure: 'RebrickableMinifigure | None'
part: 'BrickPart | None' part: 'RebrickablePart | None'
extension: str | None extension: str | None
@ -27,7 +27,7 @@ class RebrickableImage(object):
/, /,
*, *,
minifigure: 'RebrickableMinifigure | None' = None, minifigure: 'RebrickableMinifigure | None' = None,
part: 'BrickPart | None' = None, part: 'RebrickablePart | None' = None,
): ):
# Save all objects # Save all objects
self.set = set self.set = set
@ -81,10 +81,10 @@ class RebrickableImage(object):
# Return the id depending on the objects provided # Return the id depending on the objects provided
def id(self, /) -> str: def id(self, /) -> str:
if self.part is not None: if self.part is not None:
if self.part.fields.part_img_url_id is None: if self.part.fields.image_id is None:
return RebrickableImage.nil_name() return RebrickableImage.nil_name()
else: else:
return self.part.fields.part_img_url_id return self.part.fields.image_id
if self.minifigure is not None: if self.minifigure is not None:
if self.minifigure.fields.image is None: if self.minifigure.fields.image is None:
@ -105,10 +105,10 @@ class RebrickableImage(object):
# Return the url depending on the objects provided # Return the url depending on the objects provided
def url(self, /) -> str: def url(self, /) -> str:
if self.part is not None: if self.part is not None:
if self.part.fields.part_img_url is None: if self.part.fields.image is None:
return current_app.config['REBRICKABLE_IMAGE_NIL'] return current_app.config['REBRICKABLE_IMAGE_NIL']
else: else:
return self.part.fields.part_img_url return self.part.fields.image
if self.minifigure is not None: if self.minifigure is not None:
if self.minifigure.fields.image is None: if self.minifigure.fields.image is None:

View File

@ -0,0 +1,203 @@
import os
import logging
from sqlite3 import Row
from typing import Any, TYPE_CHECKING
from urllib.parse import urlparse
from flask import current_app, url_for
from .exceptions import ErrorException
from .rebrickable_image import RebrickableImage
from .record import BrickRecord
if TYPE_CHECKING:
from .minifigure import BrickMinifigure
from .set import BrickSet
from .socket import BrickSocket
logger = logging.getLogger(__name__)
# A part from Rebrickable
class RebrickablePart(BrickRecord):
socket: 'BrickSocket'
brickset: 'BrickSet | None'
minifigure: 'BrickMinifigure | None'
# Queries
select_query: str = 'rebrickable/part/select'
insert_query: str = 'rebrickable/part/insert'
def __init__(
self,
/,
*,
brickset: 'BrickSet | None' = None,
minifigure: 'BrickMinifigure | None' = None,
record: Row | dict[str, Any] | None = None
):
super().__init__()
# Save the brickset
self.brickset = brickset
# Save the minifigure
self.minifigure = minifigure
# Ingest the record if it has one
if record is not None:
self.ingest(record)
# Insert the part from Rebrickable
def insert_rebrickable(self, /) -> bool:
if self.brickset is None:
raise ErrorException('Importing a part from Rebrickable outside of a set is not supported') # noqa: E501
# Insert the Rebrickable part to the database
rows, _ = self.insert(
commit=False,
no_defer=True,
override_query=RebrickablePart.insert_query
)
inserted = rows > 0
if inserted:
if not current_app.config['USE_REMOTE_IMAGES']:
RebrickableImage(
self.brickset,
minifigure=self.minifigure,
part=self,
).download()
return inserted
# Return a dict with common SQL parameters for a part
def sql_parameters(self, /) -> dict[str, Any]:
parameters = super().sql_parameters()
# Set id
if self.brickset is not None:
parameters['id'] = self.brickset.fields.id
# Use the minifigure number if present,
if self.minifigure is not None:
parameters['figure'] = self.minifigure.fields.figure
else:
parameters['figure'] = None
return parameters
# Self url
def url(self, /) -> str:
return url_for(
'part.details',
part=self.fields.part,
color=self.fields.color,
)
# Compute the url for the bricklink page
def url_for_bricklink(self, /) -> str:
if current_app.config['BRICKLINK_LINKS']:
try:
return current_app.config['BRICKLINK_LINK_PART_PATTERN'].format( # noqa: E501
part=self.fields.part,
)
except Exception:
pass
return ''
# Compute the url for the part image
def url_for_image(self, /) -> str:
if not current_app.config['USE_REMOTE_IMAGES']:
if self.fields.image is None:
file = RebrickableImage.nil_name()
else:
file = self.fields.image_id
return RebrickableImage.static_url(file, 'PARTS_FOLDER')
else:
if self.fields.image is None:
return current_app.config['REBRICKABLE_IMAGE_NIL']
else:
return self.fields.image
# Compute the url for missing part
def url_for_missing(self, /) -> str:
# Different URL for a minifigure part
if self.minifigure is not None:
figure = self.minifigure.fields.figure
else:
figure = None
return url_for(
'set.missing_part',
id=self.fields.id,
figure=figure,
part=self.fields.part,
color=self.fields.color,
spare=self.fields.spare,
)
# Compute the url for the rebrickable page
def url_for_rebrickable(self, /) -> str:
if current_app.config['REBRICKABLE_LINKS']:
try:
return current_app.config['REBRICKABLE_LINK_PART_PATTERN'].format( # noqa: E501
part=self.fields.part,
color=self.fields.color,
)
except Exception:
pass
return ''
# Normalize from Rebrickable
@staticmethod
def from_rebrickable(
data: dict[str, Any],
/,
*,
brickset: 'BrickSet | None' = None,
minifigure: 'BrickMinifigure | None' = None,
**_,
) -> dict[str, Any]:
record = {
'id': None,
'figure': None,
'part': data['part']['part_num'],
'color': data['color']['id'],
'spare': data['is_spare'],
'quantity': data['quantity'],
'rebrickable_inventory': data['id'],
'element': data['element_id'],
'color_id': data['color']['id'],
'color_name': data['color']['name'],
'color_rgb': data['color']['rgb'],
'color_transparent': data['color']['is_trans'],
'name': data['part']['name'],
'category': data['part']['part_cat_id'],
'image': data['part']['part_img_url'],
'image_id': None,
'url': data['part']['part_url'],
'print': data['part']['print_of']
}
if brickset is not None:
record['id'] = brickset.fields.id
if minifigure is not None:
record['figure'] = minifigure.fields.figure
# Extract the file name
if record['image'] is not None:
image_id, _ = os.path.splitext(
os.path.basename(
urlparse(record['image']).path
)
)
if image_id is not None or image_id != '':
record['image_id'] = image_id
return record

View File

@ -1,113 +0,0 @@
import logging
from typing import TYPE_CHECKING
from flask import current_app
from .part import BrickPart
from .rebrickable import Rebrickable
from .rebrickable_image import RebrickableImage
if TYPE_CHECKING:
from .minifigure import BrickMinifigure
from .set import BrickSet
from .socket import BrickSocket
logger = logging.getLogger(__name__)
# A list of parts from Rebrickable
class RebrickableParts(object):
socket: 'BrickSocket'
brickset: 'BrickSet'
minifigure: 'BrickMinifigure | None'
number: str
kind: str
method: str
def __init__(
self,
socket: 'BrickSocket',
brickset: 'BrickSet',
/,
*,
minifigure: 'BrickMinifigure | None' = None,
):
# Save the socket
self.socket = socket
# Save the objects
self.brickset = brickset
self.minifigure = minifigure
if self.minifigure is not None:
self.number = self.minifigure.fields.figure
self.kind = 'Minifigure'
self.method = 'get_minifig_elements'
else:
self.number = self.brickset.fields.set
self.kind = 'Set'
self.method = 'get_set_elements'
# Import the parts from Rebrickable
def download(self, /) -> None:
self.socket.auto_progress(
message='{kind} {number}: loading parts inventory from Rebrickable'.format( # noqa: E501
kind=self.kind,
number=self.number,
),
increment_total=True,
)
logger.debug('rebrick.lego.{method}("{number}")'.format(
method=self.method,
number=self.number,
))
inventory = Rebrickable[BrickPart](
self.method,
self.number,
BrickPart,
socket=self.socket,
brickset=self.brickset,
minifigure=self.minifigure,
).list()
# Process each part
total = len(inventory)
for index, part in enumerate(inventory):
# Skip spare parts
if (
current_app.config['SKIP_SPARE_PARTS'] and
part.fields.is_spare
):
continue
# Insert into the database
self.socket.auto_progress(
message='{kind} {number}: inserting part {current}/{total} into database'.format( # noqa: E501
kind=self.kind,
number=self.number,
current=index+1,
total=total,
)
)
# Insert into database
part.insert(commit=False)
# Grab the image
self.socket.progress(
message='{kind} {number}: downloading part {current}/{total} image'.format( # noqa: E501
kind=self.kind,
number=self.number,
current=index+1,
total=total,
)
)
if not current_app.config['USE_REMOTE_IMAGES']:
RebrickableImage(
self.brickset,
minifigure=self.minifigure,
part=part,
).download()

View File

@ -8,7 +8,6 @@ from flask import current_app, url_for
from .exceptions import DatabaseException, NotFoundException from .exceptions import DatabaseException, NotFoundException
from .minifigure_list import BrickMinifigureList from .minifigure_list import BrickMinifigureList
from .part_list import BrickPartList from .part_list import BrickPartList
from .rebrickable_parts import RebrickableParts
from .rebrickable_set import RebrickableSet from .rebrickable_set import RebrickableSet
from .set_checkbox import BrickSetCheckbox from .set_checkbox import BrickSetCheckbox
from .set_checkbox_list import BrickSetCheckboxList from .set_checkbox_list import BrickSetCheckboxList
@ -34,10 +33,10 @@ class BrickSet(RebrickableSet):
) )
# Import a set into the database # Import a set into the database
def download(self, socket: 'BrickSocket', data: dict[str, Any], /) -> None: def download(self, socket: 'BrickSocket', data: dict[str, Any], /) -> bool:
# Load the set # Load the set
if not self.load(socket, data, from_download=True): if not self.load(socket, data, from_download=True):
return return False
try: try:
# Insert into the database # Insert into the database
@ -58,10 +57,12 @@ class BrickSet(RebrickableSet):
self.insert_rebrickable() self.insert_rebrickable()
# Load the inventory # Load the inventory
RebrickableParts(socket, self).download() if not BrickPartList.download(socket, self):
return False
# Load the minifigures # Load the minifigures
BrickMinifigureList.download(socket, self) if not BrickMinifigureList.download(socket, self):
return False
# Commit the transaction to the database # Commit the transaction to the database
socket.auto_progress( socket.auto_progress(
@ -98,13 +99,17 @@ class BrickSet(RebrickableSet):
logger.debug(traceback.format_exc()) logger.debug(traceback.format_exc())
return False
return True
# Minifigures # Minifigures
def minifigures(self, /) -> BrickMinifigureList: def minifigures(self, /) -> BrickMinifigureList:
return BrickMinifigureList().from_set(self) return BrickMinifigureList().from_set(self)
# Parts # Parts
def parts(self, /) -> BrickPartList: def parts(self, /) -> BrickPartList:
return BrickPartList().load(self) return BrickPartList().list_specific(self)
# Select a light set (with an id) # Select a light set (with an id)
def select_light(self, id: str, /) -> Self: def select_light(self, id: str, /) -> Self:

View File

@ -100,16 +100,13 @@ class BrickSetList(BrickRecordList[BrickSet]):
# Sets missing a part # Sets missing a part
def missing_part( def missing_part(
self, self,
part_num: str, part: str,
color_id: int, color: int,
/, /
*,
element_id: int | None = None,
) -> Self: ) -> Self:
# Save the parameters to the fields # Save the parameters to the fields
self.fields.part_num = part_num self.fields.part = part
self.fields.color_id = color_id self.fields.color = color
self.fields.element_id = element_id
# Load the sets from the database # Load the sets from the database
for record in self.select( for record in self.select(
@ -141,16 +138,13 @@ class BrickSetList(BrickRecordList[BrickSet]):
# Sets using a part # Sets using a part
def using_part( def using_part(
self, self,
part_num: str, part: str,
color_id: int, color: int,
/, /
*,
element_id: int | None = None,
) -> Self: ) -> Self:
# Save the parameters to the fields # Save the parameters to the fields
self.fields.part_num = part_num self.fields.part = part
self.fields.color_id = color_id self.fields.color = color
self.fields.element_id = element_id
# Load the sets from the database # Load the sets from the database
for record in self.select( for record in self.select(

View File

@ -0,0 +1,42 @@
-- description: Creation of the deduplicated table of Rebrickable parts, and add a bunch of extra fields for later
BEGIN TRANSACTION;
-- Create a Rebrickable parts table: each unique part imported from Rebrickable
CREATE TABLE "rebrickable_parts" (
"part" TEXT NOT NULL,
"color_id" INTEGER NOT NULL,
"color_name" TEXT NOT NULL,
"color_rgb" TEXT, -- can be NULL because it was not saved before
"color_transparent" BOOLEAN, -- can be NULL because it was not saved before
"name" TEXT NOT NULL,
"category" INTEGER, -- can be NULL because it was not saved before
"image" TEXT,
"image_id" TEXT,
"url" TEXT, -- can be NULL because it was not saved before
"print" INTEGER, -- can be NULL, was not saved before
PRIMARY KEY("part", "color_id")
);
-- Insert existing parts into the new table
INSERT INTO "rebrickable_parts" (
"part",
"color_id",
"color_name",
"name",
"image",
"image_id"
)
SELECT
"inventory"."part_num",
"inventory"."color_id",
"inventory"."color_name",
"inventory"."name",
"inventory"."part_img_url",
"inventory"."part_img_url_id"
FROM "inventory"
GROUP BY
"inventory"."part_num",
"inventory"."color_id";
COMMIT;

View File

@ -0,0 +1,60 @@
-- description: Migrate the Bricktracker parts (and missing parts), and add a bunch of extra fields for later
PRAGMA foreign_keys = ON;
BEGIN TRANSACTION;
-- Create a Bricktracker parts table: an amount of parts linked to a Bricktracker set
CREATE TABLE "bricktracker_parts" (
"id" TEXT NOT NULL,
"figure" TEXT,
"part" TEXT NOT NULL,
"color" INTEGER NOT NULL,
"spare" BOOLEAN NOT NULL,
"quantity" INTEGER NOT NULL,
"element" INTEGER,
"rebrickable_inventory" INTEGER NOT NULL,
"missing" INTEGER NOT NULL DEFAULT 0,
"damaged" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY("id", "figure", "part", "color", "spare"),
FOREIGN KEY("id") REFERENCES "bricktracker_sets"("id"),
FOREIGN KEY("figure") REFERENCES "rebrickable_minifigures"("figure"),
FOREIGN KEY("part", "color") REFERENCES "rebrickable_parts"("part", "color_id")
);
-- Insert existing parts into the new table
INSERT INTO "bricktracker_parts" (
"id",
"figure",
"part",
"color",
"spare",
"quantity",
"element",
"rebrickable_inventory",
"missing"
)
SELECT
"inventory"."u_id",
CASE WHEN SUBSTR("inventory"."set_num", 0, 5) = 'fig-' THEN "inventory"."set_num" ELSE NULL END,
"inventory"."part_num",
"inventory"."color_id",
"inventory"."is_spare",
"inventory"."quantity",
"inventory"."element_id",
"inventory"."id",
IFNULL("missing"."quantity", 0)
FROM "inventory"
LEFT JOIN "missing"
ON "inventory"."set_num" IS NOT DISTINCT FROM "missing"."set_num"
AND "inventory"."id" IS NOT DISTINCT FROM "missing"."id"
AND "inventory"."part_num" IS NOT DISTINCT FROM "missing"."part_num"
AND "inventory"."color_id" IS NOT DISTINCT FROM "missing"."color_id"
AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id";
-- Rename the original table (don't delete it yet?)
ALTER TABLE "inventory" RENAME TO "inventory_old";
ALTER TABLE "missing" RENAME TO "missing_old";
COMMIT;

View File

@ -1,5 +1,4 @@
SELECT SELECT
{% block set %}{% endblock %}
"bricktracker_minifigures"."quantity", "bricktracker_minifigures"."quantity",
"rebrickable_minifigures"."figure", "rebrickable_minifigures"."figure",
"rebrickable_minifigures"."number", "rebrickable_minifigures"."number",

View File

@ -16,16 +16,17 @@ COUNT("bricktracker_minifigures"."id") AS "total_sets"
-- LEFT JOIN + SELECT to avoid messing the total -- LEFT JOIN + SELECT to avoid messing the total
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
"missing"."set_num", "bricktracker_parts"."id",
"missing"."u_id", "bricktracker_parts"."figure",
SUM("missing"."quantity") AS total SUM("bricktracker_parts"."missing") AS total
FROM "missing" FROM "bricktracker_parts"
WHERE "bricktracker_parts"."figure" IS NOT NULL
GROUP BY GROUP BY
"missing"."set_num", "bricktracker_parts"."id",
"missing"."u_id" "bricktracker_parts"."figure"
) missing_join ) missing_join
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing_join"."u_id" ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing_join"."id"
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing_join"."set_num" AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing_join"."figure"
{% endblock %} {% endblock %}
{% block group %} {% block group %}

View File

@ -1,13 +1,13 @@
{% extends 'minifigure/base/base.sql' %} {% extends 'minifigure/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing" LEFT JOIN "bricktracker_parts"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_parts"."id"
AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "bricktracker_parts"."figure"
{% endblock %} {% endblock %}
{% block group %} {% block group %}

View File

@ -1,26 +1,24 @@
{% extends 'minifigure/base/base.sql' %} {% extends 'minifigure/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing" LEFT JOIN "bricktracker_parts"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_parts"."id"
AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "bricktracker_parts"."figure"
{% endblock %} {% endblock %}
{% block where %} {% block where %}
WHERE "rebrickable_minifigures"."figure" IN ( WHERE "rebrickable_minifigures"."figure" IN (
SELECT SELECT "bricktracker_parts"."figure"
"missing"."set_num" FROM "bricktracker_parts"
FROM "missing" WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
WHERE "missing"."color_id" IS NOT DISTINCT FROM :color_id AND "bricktracker_parts"."figure" IS NOT NULL
AND "missing"."element_id" IS NOT DISTINCT FROM :element_id AND "bricktracker_parts"."missing" > 0
AND "missing"."part_num" IS NOT DISTINCT FROM :part_num GROUP BY "bricktracker_parts"."figure"
GROUP BY "missing"."set_num"
) )
{% endblock %} {% endblock %}

View File

@ -6,15 +6,12 @@ SUM("bricktracker_minifigures"."quantity") AS "total_quantity",
{% block where %} {% block where %}
WHERE "rebrickable_minifigures"."figure" IN ( WHERE "rebrickable_minifigures"."figure" IN (
SELECT SELECT "bricktracker_parts"."figure"
"inventory"."set_num" FROM "bricktracker_parts"
FROM "inventory" WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
WHERE "inventory"."color_id" IS NOT DISTINCT FROM :color_id AND "bricktracker_parts"."figure" IS NOT NULL
AND "inventory"."element_id" IS NOT DISTINCT FROM :element_id GROUP BY "bricktracker_parts"."figure"
AND "inventory"."part_num" IS NOT DISTINCT FROM :part_num
GROUP BY "inventory"."set_num"
) )
{% endblock %} {% endblock %}

View File

@ -1,7 +1,7 @@
{% extends 'minifigure/base/base.sql' %} {% extends 'minifigure/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", IFNULL("missing_join"."total", 0) AS "total_missing",
{% endblock %} {% endblock %}
{% block total_quantity %} {% block total_quantity %}
@ -13,9 +13,16 @@ COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets"
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing" -- LEFT JOIN + SELECT to avoid messing the total
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing"."set_num" LEFT JOIN (
AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "missing"."u_id" SELECT
"bricktracker_parts"."figure",
SUM("bricktracker_parts"."missing") AS "total"
FROM "bricktracker_parts"
WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
GROUP BY "bricktracker_parts"."figure"
) "missing_join"
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "missing_join"."figure"
{% endblock %} {% endblock %}
{% block where %} {% block where %}

View File

@ -1,6 +1,6 @@
{% extends 'minifigure/base/base.sql' %} {% extends 'minifigure/base/base.sql' %}
{% block where %} {% block where %}
WHERE "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id
AND "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure
{% endblock %} {% endblock %}

View File

@ -1,4 +0,0 @@
DELETE FROM "missing"
WHERE "missing"."set_num" IS NOT DISTINCT FROM :set_num
AND "missing"."id" IS NOT DISTINCT FROM :id
AND "missing"."u_id" IS NOT DISTINCT FROM :u_id

View File

@ -1,20 +0,0 @@
INSERT INTO "missing" (
"set_num",
"id",
"part_num",
"part_img_url_id",
"color_id",
"quantity",
"element_id",
"u_id"
)
VALUES(
:set_num,
:id,
:part_num,
:part_img_url_id,
:color_id,
:quantity,
:element_id,
:u_id
)

View File

@ -1,5 +0,0 @@
UPDATE "missing"
SET "quantity" = :quantity
WHERE "missing"."set_num" IS NOT DISTINCT FROM :set_num
AND "missing"."id" IS NOT DISTINCT FROM :id
AND "missing"."u_id" IS NOT DISTINCT FROM :u_id

View File

@ -0,0 +1,56 @@
SELECT
"bricktracker_parts"."id",
"bricktracker_parts"."figure",
"bricktracker_parts"."part",
"bricktracker_parts"."color",
"bricktracker_parts"."spare",
"bricktracker_parts"."quantity",
"bricktracker_parts"."element",
--"bricktracker_parts"."rebrickable_inventory",
"bricktracker_parts"."missing",
"bricktracker_parts"."damaged",
--"rebrickable_parts"."part",
--"rebrickable_parts"."color_id",
"rebrickable_parts"."color_name",
"rebrickable_parts"."color_rgb",
"rebrickable_parts"."color_transparent",
"rebrickable_parts"."name",
--"rebrickable_parts"."category",
"rebrickable_parts"."image",
"rebrickable_parts"."image_id",
"rebrickable_parts"."url",
--"rebrickable_parts"."print",
{% block total_missing %}
NULL AS "total_missing", -- dummy for order: total_missing
{% endblock %}
{% block total_quantity %}
NULL AS "total_quantity", -- dummy for order: total_quantity
{% endblock %}
{% block total_spare %}
NULL AS "total_spare", -- dummy for order: total_spare
{% endblock %}
{% block total_sets %}
NULL AS "total_sets", -- dummy for order: total_sets
{% endblock %}
{% block total_minifigures %}
NULL AS "total_minifigures" -- dummy for order: total_minifigures
{% endblock %}
FROM "bricktracker_parts"
INNER JOIN "rebrickable_parts"
ON "bricktracker_parts"."part" IS NOT DISTINCT FROM "rebrickable_parts"."part"
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM "rebrickable_parts"."color_id"
{% block join %}{% endblock %}
{% block where %}{% endblock %}
{% block group %}{% endblock %}
{% if order %}
ORDER BY {{ order }}
{% endif %}
{% if limit %}
LIMIT {{ limit }}
{% endif %}

View File

@ -1,43 +0,0 @@
SELECT
"inventory"."set_num",
"inventory"."id",
"inventory"."part_num",
"inventory"."name",
"inventory"."part_img_url",
"inventory"."part_img_url_id",
"inventory"."color_id",
"inventory"."color_name",
"inventory"."quantity",
"inventory"."is_spare",
"inventory"."element_id",
"inventory"."u_id",
{% block total_missing %}
NULL AS "total_missing", -- dummy for order: total_missing
{% endblock %}
{% block total_quantity %}
NULL AS "total_quantity", -- dummy for order: total_quantity
{% endblock %}
{% block total_spare %}
NULL AS "total_spare", -- dummy for order: total_spare
{% endblock %}
{% block total_sets %}
NULL AS "total_sets", -- dummy for order: total_sets
{% endblock %}
{% block total_minifigures %}
NULL AS "total_minifigures" -- dummy for order: total_minifigures
{% endblock %}
FROM "inventory"
{% block join %}{% endblock %}
{% block where %}{% endblock %}
{% block group %}{% endblock %}
{% if order %}
ORDER BY {{ order }}
{% endif %}
{% if limit %}
LIMIT {{ limit }}
{% endif %}

View File

@ -1,27 +1,19 @@
INSERT INTO inventory ( INSERT INTO "bricktracker_parts" (
"set_num",
"id", "id",
"part_num", "figure",
"name", "part",
"part_img_url", "color",
"part_img_url_id", "spare",
"color_id",
"color_name",
"quantity", "quantity",
"is_spare", "element",
"element_id", "rebrickable_inventory"
"u_id"
) VALUES ( ) VALUES (
:set_num,
:id, :id,
:part_num, :figure,
:name, :part,
:part_img_url, :color,
:part_img_url_id, :spare,
:color_id,
:color_name,
:quantity, :quantity,
:is_spare, :element,
:element_id, :rebrickable_inventory
:u_id
) )

View File

@ -1,15 +1,15 @@
{% extends 'part/base/select.sql' %} {% extends 'part/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %} {% endblock %}
{% block total_quantity %} {% block total_quantity %}
SUM("inventory"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity", SUM("bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
{% endblock %} {% endblock %}
{% block total_sets %} {% block total_sets %}
COUNT(DISTINCT "bricktracker_minifigures"."id") AS "total_sets", COUNT(DISTINCT "bricktracker_parts"."id") AS "total_sets",
{% endblock %} {% endblock %}
{% block total_minifigures %} {% block total_minifigures %}
@ -17,24 +17,14 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_minifigures"
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing"
ON "inventory"."set_num" IS NOT DISTINCT FROM "missing"."set_num"
AND "inventory"."id" IS NOT DISTINCT FROM "missing"."id"
AND "inventory"."part_num" IS NOT DISTINCT FROM "missing"."part_num"
AND "inventory"."color_id" IS NOT DISTINCT FROM "missing"."color_id"
AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
{% endblock %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"inventory"."part_num", "bricktracker_parts"."part",
"inventory"."name", "bricktracker_parts"."color",
"inventory"."color_id", "bricktracker_parts"."spare"
"inventory"."is_spare",
"inventory"."element_id"
{% endblock %} {% endblock %}

View File

@ -1,28 +1,17 @@
{% extends 'part/base/select.sql' %} {% extends 'part/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %}
{% block join %}
LEFT JOIN "missing"
ON "missing"."set_num" IS NOT DISTINCT FROM "inventory"."set_num"
AND "missing"."id" IS NOT DISTINCT FROM "inventory"."id"
AND "missing"."part_num" IS NOT DISTINCT FROM "inventory"."part_num"
AND "missing"."color_id" IS NOT DISTINCT FROM "inventory"."color_id"
AND "missing"."element_id" IS NOT DISTINCT FROM "inventory"."element_id"
{% endblock %} {% endblock %}
{% block where %} {% block where %}
WHERE "inventory"."set_num" IS NOT DISTINCT FROM :set_num WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
{% endblock %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"inventory"."part_num", "bricktracker_parts"."part",
"inventory"."name", "bricktracker_parts"."color",
"inventory"."color_id", "bricktracker_parts"."spare"
"inventory"."is_spare",
"inventory"."element_id"
{% endblock %} {% endblock %}

View File

@ -1,21 +0,0 @@
{% extends 'part/base/select.sql' %}
{% block total_missing %}
IFNULL("missing"."quantity", 0) AS "total_missing",
{% endblock %}
{% block join %}
LEFT JOIN "missing"
ON "inventory"."set_num" IS NOT DISTINCT FROM "missing"."set_num"
AND "inventory"."id" IS NOT DISTINCT FROM "missing"."id"
AND "inventory"."part_num" IS NOT DISTINCT FROM "missing"."part_num"
AND "inventory"."color_id" IS NOT DISTINCT FROM "missing"."color_id"
AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
{% endblock %}
{% block where %}
WHERE "inventory"."u_id" IS NOT DISTINCT FROM :u_id
AND "inventory"."set_num" IS NOT DISTINCT FROM :set_num
{% endblock %}

View File

@ -1,11 +1,11 @@
{% extends 'part/base/select.sql' %} {% extends 'part/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %} {% endblock %}
{% block total_sets %} {% block total_sets %}
COUNT("inventory"."u_id") - COUNT("bricktracker_minifigures"."id") AS "total_sets", COUNT("bricktracker_parts"."id") - COUNT("bricktracker_parts"."figure") AS "total_sets",
{% endblock %} {% endblock %}
{% block total_minifigures %} {% block total_minifigures %}
@ -13,24 +13,18 @@ SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_minifigures"
{% endblock %} {% endblock %}
{% block join %} {% block join %}
INNER JOIN "missing"
ON "missing"."set_num" IS NOT DISTINCT FROM "inventory"."set_num"
AND "missing"."id" IS NOT DISTINCT FROM "inventory"."id"
AND "missing"."part_num" IS NOT DISTINCT FROM "inventory"."part_num"
AND "missing"."color_id" IS NOT DISTINCT FROM "inventory"."color_id"
AND "missing"."element_id" IS NOT DISTINCT FROM "inventory"."element_id"
AND "missing"."u_id" IS NOT DISTINCT FROM "inventory"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
{% endblock %}
{% block where %}
WHERE "bricktracker_parts"."missing" > 0
{% endblock %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"inventory"."part_num", "bricktracker_parts"."part",
"inventory"."name", "bricktracker_parts"."color",
"inventory"."color_id", "bricktracker_parts"."spare"
"inventory"."is_spare",
"inventory"."element_id"
{% endblock %} {% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'part/base/base.sql' %}
{% block total_missing %}
IFNULL("bricktracker_parts"."missing", 0) AS "total_missing",
{% endblock %}
{% block where %}
WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM :id
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
{% endblock %}

View File

@ -1,40 +1,30 @@
{% extends 'part/base/select.sql' %} {% extends 'part/base/base.sql' %}
{% block total_missing %} {% block total_missing %}
SUM(IFNULL("missing"."quantity", 0)) AS "total_missing", SUM("bricktracker_parts"."missing") AS "total_missing",
{% endblock %} {% endblock %}
{% block total_quantity %} {% block total_quantity %}
SUM((NOT "inventory"."is_spare") * "inventory"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity", SUM((NOT "bricktracker_parts"."spare") * "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
{% endblock %} {% endblock %}
{% block total_spare %} {% block total_spare %}
SUM("inventory"."is_spare" * "inventory"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_spare", SUM("bricktracker_parts"."spare" * "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_spare",
{% endblock %} {% endblock %}
{% block join %} {% block join %}
LEFT JOIN "missing"
ON "inventory"."set_num" IS NOT DISTINCT FROM "missing"."set_num"
AND "inventory"."id" IS NOT DISTINCT FROM "missing"."id"
AND "inventory"."part_num" IS NOT DISTINCT FROM "missing"."part_num"
AND "inventory"."color_id" IS NOT DISTINCT FROM "missing"."color_id"
AND "inventory"."element_id" IS NOT DISTINCT FROM "missing"."element_id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
LEFT JOIN "bricktracker_minifigures" LEFT JOIN "bricktracker_minifigures"
ON "inventory"."set_num" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure" ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id" AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
{% endblock %} {% endblock %}
{% block where %} {% block where %}
WHERE "inventory"."part_num" IS NOT DISTINCT FROM :part_num WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "inventory"."color_id" IS NOT DISTINCT FROM :color_id AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
AND "inventory"."element_id" IS NOT DISTINCT FROM :element_id
{% endblock %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"inventory"."part_num", "bricktracker_parts"."part",
"inventory"."color_id", "bricktracker_parts"."color"
"inventory"."element_id"
{% endblock %} {% endblock %}

View File

@ -1,24 +1,18 @@
{% extends 'part/base/select.sql' %} {% extends 'part/base/base.sql' %}
{% block join %}
LEFT JOIN "missing"
ON "inventory"."set_num" IS NOT DISTINCT FROM "missing"."set_num"
AND "inventory"."id" IS NOT DISTINCT FROM "missing"."id"
AND "inventory"."u_id" IS NOT DISTINCT FROM "missing"."u_id"
{% endblock %}
{% block where %} {% block where %}
WHERE "inventory"."u_id" IS NOT DISTINCT FROM :u_id WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM :id
AND "inventory"."set_num" IS NOT DISTINCT FROM :set_num AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
AND "inventory"."id" IS NOT DISTINCT FROM :id AND "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
AND "bricktracker_parts"."spare" IS NOT DISTINCT FROM :spare
{% endblock %} {% endblock %}
{% block group %} {% block group %}
GROUP BY GROUP BY
"inventory"."set_num", "bricktracker_parts"."id",
"inventory"."id", "bricktracker_parts"."figure",
"inventory"."part_num", "bricktracker_parts"."part",
"inventory"."color_id", "bricktracker_parts"."color",
"inventory"."element_id", "bricktracker_parts"."spare"
"inventory"."u_id"
{% endblock %} {% endblock %}

View File

@ -0,0 +1,7 @@
UPDATE "bricktracker_parts"
SET "missing" = :missing
WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM :id
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
AND "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
AND "bricktracker_parts"."spare" IS NOT DISTINCT FROM :spare

View File

@ -0,0 +1,25 @@
INSERT OR IGNORE INTO "rebrickable_parts" (
"part",
"color_id",
"color_name",
"color_rgb",
"color_transparent",
"name",
"category",
"image",
"image_id",
"url",
"print"
) VALUES (
:part,
:color_id,
:color_name,
:color_rgb,
:color_transparent,
:name,
:category,
:image,
:image_id,
:url,
:print
)

View File

@ -0,0 +1,13 @@
SELECT
"rebrickable_parts"."part",
"rebrickable_parts"."color_id",
"rebrickable_parts"."color_name",
"rebrickable_parts"."color_rgb",
"rebrickable_parts"."color_transparent",
"rebrickable_parts"."name",
"rebrickable_parts"."category",
"rebrickable_parts"."image",
"rebrickable_parts"."image_id",
"rebrickable_parts"."url",
"rebrickable_parts"."print"
FROM "rebrickable_parts"

View File

@ -0,0 +1,16 @@
SELECT
"rebrickable_parts"."part",
"rebrickable_parts"."color_id",
"rebrickable_parts"."color_name",
"rebrickable_parts"."color_rgb",
"rebrickable_parts"."color_transparent",
"rebrickable_parts"."name",
"rebrickable_parts"."category",
"rebrickable_parts"."image",
"rebrickable_parts"."image_id",
"rebrickable_parts"."url",
"rebrickable_parts"."print"
FROM "rebrickable_parts"
WHERE "rebrickable_minifigures"."part" IS NOT DISTINCT FROM :figure
AND "rebrickable_minifigures"."color_id" IS NOT DISTINCT FROM :color

View File

@ -1,15 +1,20 @@
BEGIN transaction; BEGIN transaction;
DROP TABLE IF EXISTS "bricktracker_minifigures"; DROP TABLE IF EXISTS "bricktracker_minifigures";
DROP TABLE IF EXISTS "bricktracker_parts";
DROP TABLE IF EXISTS "bricktracker_sets"; DROP TABLE IF EXISTS "bricktracker_sets";
DROP TABLE IF EXISTS "bricktracker_set_checkboxes"; DROP TABLE IF EXISTS "bricktracker_set_checkboxes";
DROP TABLE IF EXISTS "bricktracker_set_statuses"; DROP TABLE IF EXISTS "bricktracker_set_statuses";
DROP TABLE IF EXISTS "bricktracker_set_storages";
DROP TABLE IF EXISTS "bricktracker_wishes"; DROP TABLE IF EXISTS "bricktracker_wishes";
DROP TABLE IF EXISTS "inventory"; DROP TABLE IF EXISTS "inventory";
DROP TABLE IF EXISTS "inventory_old";
DROP TABLE IF EXISTS "minifigures"; DROP TABLE IF EXISTS "minifigures";
DROP TABLE IF EXISTS "minifigures_old"; DROP TABLE IF EXISTS "minifigures_old";
DROP TABLE IF EXISTS "missing"; DROP TABLE IF EXISTS "missing";
DROP TABLE IF EXISTS "missing_old";
DROP TABLE IF EXISTS "rebrickable_minifigures"; DROP TABLE IF EXISTS "rebrickable_minifigures";
DROP TABLE IF EXISTS "rebrickable_parts";
DROP TABLE IF EXISTS "rebrickable_sets"; DROP TABLE IF EXISTS "rebrickable_sets";
DROP TABLE IF EXISTS "sets"; DROP TABLE IF EXISTS "sets";
DROP TABLE IF EXISTS "sets_old"; DROP TABLE IF EXISTS "sets_old";

View File

@ -21,13 +21,13 @@ ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_statuses"."id
-- LEFT JOIN + SELECT to avoid messing the total -- LEFT JOIN + SELECT to avoid messing the total
LEFT JOIN ( LEFT JOIN (
SELECT SELECT
"missing"."u_id", "bricktracker_parts"."id",
SUM("missing"."quantity") AS "total" SUM("bricktracker_parts"."missing") AS "total"
FROM "missing" FROM "bricktracker_parts"
{% block where_missing %}{% endblock %} {% block where_missing %}{% endblock %}
GROUP BY "u_id" GROUP BY "bricktracker_parts"."id"
) "missing_join" ) "missing_join"
ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "missing_join"."u_id" ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "missing_join"."id"
-- LEFT JOIN + SELECT to avoid messing the total -- LEFT JOIN + SELECT to avoid messing the total
LEFT JOIN ( LEFT JOIN (

View File

@ -12,10 +12,7 @@ WHERE "bricktracker_set_statuses"."id" IS NOT DISTINCT FROM '{{ id }}';
DELETE FROM "bricktracker_minifigures" DELETE FROM "bricktracker_minifigures"
WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM '{{ id }}'; WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM '{{ id }}';
DELETE FROM "missing" DELETE FROM "bricktracker_parts"
WHERE "missing"."u_id" IS NOT DISTINCT FROM '{{ id }}'; WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM '{{ id }}';
DELETE FROM "inventory"
WHERE "inventory"."u_id" IS NOT DISTINCT FROM '{{ id }}';
COMMIT; COMMIT;

View File

@ -2,12 +2,10 @@
{% block where %} {% block where %}
WHERE "bricktracker_sets"."id" IN ( WHERE "bricktracker_sets"."id" IN (
SELECT SELECT "bricktracker_parts"."id"
"missing"."u_id" FROM "bricktracker_parts"
FROM "missing" WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
AND "bricktracker_parts"."missing" > 0
WHERE "missing"."set_num" IS NOT DISTINCT FROM :figure GROUP BY "bricktracker_parts"."id"
GROUP BY "missing"."u_id"
) )
{% endblock %} {% endblock %}

View File

@ -2,14 +2,11 @@
{% block where %} {% block where %}
WHERE "bricktracker_sets"."id" IN ( WHERE "bricktracker_sets"."id" IN (
SELECT SELECT "bricktracker_parts"."id"
"missing"."u_id" FROM "bricktracker_parts"
FROM "missing" WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
WHERE "missing"."color_id" IS NOT DISTINCT FROM :color_id AND "bricktracker_parts"."missing" > 0
AND "missing"."element_id" IS NOT DISTINCT FROM :element_id GROUP BY "bricktracker_parts"."id"
AND "missing"."part_num" IS NOT DISTINCT FROM :part_num
GROUP BY "missing"."u_id"
) )
{% endblock %} {% endblock %}

View File

@ -2,12 +2,9 @@
{% block where %} {% block where %}
WHERE "bricktracker_sets"."id" IN ( WHERE "bricktracker_sets"."id" IN (
SELECT SELECT "bricktracker_parts"."id"
"inventory"."u_id" FROM "bricktracker_parts"
FROM "inventory" WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
GROUP BY "bricktracker_parts"."id"
WHERE "inventory"."set_num" IS NOT DISTINCT FROM :figure
GROUP BY "inventory"."u_id"
) )
{% endblock %} {% endblock %}

View File

@ -2,14 +2,10 @@
{% block where %} {% block where %}
WHERE "bricktracker_sets"."id" IN ( WHERE "bricktracker_sets"."id" IN (
SELECT SELECT "bricktracker_parts"."id"
"inventory"."u_id" FROM "bricktracker_parts"
FROM "inventory" WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
WHERE "inventory"."color_id" IS NOT DISTINCT FROM :color_id GROUP BY "bricktracker_parts"."id"
AND "inventory"."element_id" IS NOT DISTINCT FROM :element_id
AND "inventory"."part_num" IS NOT DISTINCT FROM :part_num
GROUP BY "inventory"."u_id"
) )
{% endblock %} {% endblock %}

View File

@ -1,7 +1,7 @@
{% extends 'set/base/full.sql' %} {% extends 'set/base/full.sql' %}
{% block where_missing %} {% block where_missing %}
WHERE "missing"."u_id" IS NOT DISTINCT FROM :id WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM :id
{% endblock %} {% endblock %}
{% block where_minifigures %} {% block where_minifigures %}

View File

@ -3,15 +3,20 @@ from typing import Tuple
# Some table aliases to make it look cleaner (id: (name, icon)) # Some table aliases to make it look cleaner (id: (name, icon))
ALIASES: dict[str, Tuple[str, str]] = { ALIASES: dict[str, Tuple[str, str]] = {
'bricktracker_minifigures': ('Bricktracker minifigures', 'group-line'), 'bricktracker_minifigures': ('Bricktracker minifigures', 'group-line'),
'bricktracker_parts': ('Bricktracker parts', 'shapes-line'),
'bricktracker_set_checkboxes': ('Bricktracker set checkboxes', 'checkbox-line'), # noqa: E501 'bricktracker_set_checkboxes': ('Bricktracker set checkboxes', 'checkbox-line'), # noqa: E501
'bricktracker_set_statuses': ('Bricktracker sets status', 'checkbox-line'), 'bricktracker_set_statuses': ('Bricktracker sets status', 'checkbox-circle-line'), # noqa: E501
'bricktracker_set_storages': ('Bricktracker sets storages', 'archive-2-line'), # noqa: E501
'bricktracker_sets': ('Bricktracker sets', 'hashtag'), 'bricktracker_sets': ('Bricktracker sets', 'hashtag'),
'bricktracker_wishes': ('Bricktracker wishes', 'gift-line'), 'bricktracker_wishes': ('Bricktracker wishes', 'gift-line'),
'inventory': ('Parts', 'shapes-line'), 'inventory': ('Parts', 'shapes-line'),
'inventory_old': ('Parts (legacy)', 'shapes-line'),
'minifigures': ('Minifigures', 'group-line'), 'minifigures': ('Minifigures', 'group-line'),
'minifigures_old': ('Minifigures (legacy)', 'group-line'), 'minifigures_old': ('Minifigures (legacy)', 'group-line'),
'missing': ('Missing', 'error-warning-line'), 'missing': ('Missing', 'error-warning-line'),
'missing_old': ('Missing (legacy)', 'error-warning-line'),
'rebrickable_minifigures': ('Rebrickable minifigures', 'group-line'), 'rebrickable_minifigures': ('Rebrickable minifigures', 'group-line'),
'rebrickable_parts': ('Rebrickable parts', 'shapes-line'),
'rebrickable_sets': ('Rebrickable sets', 'hashtag'), 'rebrickable_sets': ('Rebrickable sets', 'hashtag'),
'sets': ('Sets', 'hashtag'), 'sets': ('Sets', 'hashtag'),
'sets_old': ('Sets (legacy)', 'hashtag'), 'sets_old': ('Sets (legacy)', 'hashtag'),

View File

@ -1,4 +1,4 @@
from typing import Final from typing import Final
__version__: Final[str] = '1.2.0' __version__: Final[str] = '1.2.0'
__database_version__: Final[int] = 9 __database_version__: Final[int] = 11

View File

@ -30,31 +30,26 @@ def missing() -> str:
# Part details # Part details
@part_page.route('/<number>/<int:color>/details', defaults={'element': None}, methods=['GET']) # noqa: E501 @part_page.route('/<part>/<int:color>/details', methods=['GET']) # noqa: E501
@part_page.route('/<number>/<int:color>/<int:element>/details', methods=['GET']) # noqa: E501
@exception_handler(__file__) @exception_handler(__file__)
def details(*, number: str, color: int, element: int | None) -> str: def details(*, part: str, color: int) -> str:
return render_template( return render_template(
'part.html', 'part.html',
item=BrickPart().select_generic(number, color, element_id=element), item=BrickPart().select_generic(part, color),
sets_using=BrickSetList().using_part( sets_using=BrickSetList().using_part(
number, part,
color, color
element_id=element
), ),
sets_missing=BrickSetList().missing_part( sets_missing=BrickSetList().missing_part(
number, part,
color, color
element_id=element
), ),
minifigures_using=BrickMinifigureList().using_part( minifigures_using=BrickMinifigureList().using_part(
number, part,
color, color
element_id=element
), ),
minifigures_missing=BrickMinifigureList().missing_part( minifigures_missing=BrickMinifigureList().missing_part(
number, part,
color, color
element_id=element
), ),
) )

View File

@ -107,16 +107,34 @@ def details(*, id: str) -> str:
) )
# Update the missing pieces of a minifig part # Update the missing pieces of a part
@set_page.route('/<id>/minifigures/<figure>/parts/<part>/missing', methods=['POST']) # noqa: E501 @set_page.route('/<id>/parts/<part>/<int:color>/<int:spare>/missing', defaults={'figure': None}, methods=['POST']) # noqa: E501
@set_page.route('/<id>/minifigures/<figure>/parts/<part>/<int:color>/<int:spare>/missing', methods=['POST']) # noqa: E501
@login_required @login_required
@exception_handler(__file__, json=True) @exception_handler(__file__, json=True)
def missing_minifigure_part(*, id: str, figure: str, part: str) -> Response: def missing_part(
*,
id: str,
figure: str | None,
part: str,
color: int,
spare: int,
) -> Response:
from pprint import pprint
pprint(locals())
brickset = BrickSet().select_specific(id) brickset = BrickSet().select_specific(id)
if figure is not None:
brickminifigure = BrickMinifigure().select_specific(brickset, figure) brickminifigure = BrickMinifigure().select_specific(brickset, figure)
else:
brickminifigure = None
brickpart = BrickPart().select_specific( brickpart = BrickPart().select_specific(
brickset, brickset,
part, part,
color,
spare,
minifigure=brickminifigure, minifigure=brickminifigure,
) )
@ -125,35 +143,14 @@ def missing_minifigure_part(*, id: str, figure: str, part: str) -> Response:
brickpart.update_missing(missing) brickpart.update_missing(missing)
# Info # Info
logger.info('Set {set} ({id}): updated minifigure ({figure}) part ({part}) missing count to {missing}'.format( # noqa: E501 logger.info('Set {set} ({id}): updated part ({part} color: {color}, spare: {spare}, minifigure: {figure}) missing count to {missing}'.format( # noqa: E501
set=brickset.fields.set, set=brickset.fields.set,
id=brickset.fields.id, id=brickset.fields.id,
figure=brickminifigure.fields.figure, figure=figure,
part=brickpart.fields.id, part=brickpart.fields.part,
missing=missing, color=brickpart.fields.color,
)) spare=brickpart.fields.spare,
missing=brickpart.fields.missing,
return jsonify({'missing': missing})
# Update the missing pieces of a part
@set_page.route('/<id>/parts/<part>/missing', methods=['POST'])
@login_required
@exception_handler(__file__, json=True)
def missing_part(*, id: str, part: str) -> Response:
brickset = BrickSet().select_specific(id)
brickpart = BrickPart().select_specific(brickset, part)
missing = request.json.get('missing', '') # type: ignore
brickpart.update_missing(missing)
# Info
logger.info('Set {set} ({id}): updated part ({part}) missing count to {missing}'.format( # noqa: E501
set=brickset.fields.set,
id=brickset.fields.id,
part=brickpart.fields.id,
missing=missing,
)) ))
return jsonify({'missing': missing}) return jsonify({'missing': missing})

View File

@ -1,10 +1,10 @@
{% macro header(item, name, solo=false, number=none, color=none, icon='hashtag') %} {% macro header(item, name, solo=false, identifier=none, color=none, icon='hashtag') %}
<div class="card-header"> <div class="card-header">
{% if not solo %} {% if not solo %}
<a class="text-decoration-none text-reset" href="{{ item.url() }}" data-bs-toggle="tooltip" title="{{ name }}"> <a class="text-decoration-none text-reset" href="{{ item.url() }}" data-bs-toggle="tooltip" title="{{ name }}">
{% endif %} {% endif %}
<h5 class="mb-0 {% if not solo %}fs-6 text-nowrap overflow-x-hidden text-truncate{% endif %}"> <h5 class="mb-0 {% if not solo %}fs-6 text-nowrap overflow-x-hidden text-truncate{% endif %}">
{% if number %}<span class="badge text-bg-secondary fw-normal"><i class="ri-{{ icon }}"></i>{{ number }}</span>{% endif %} {% if identifier %}<span class="badge text-bg-secondary fw-normal"><i class="ri-{{ icon }}"></i>{{ identifier }}</span>{% endif %}
{% if color %}<span class="badge text-bg-light fw-normal border"><i class="ri-palette-line"></i> {{ color }}</span>{% endif %} {% if color %}<span class="badge text-bg-light fw-normal border"><i class="ri-palette-line"></i> {{ color }}</span>{% endif %}
{{ name }} {{ name }}
</h5> </h5>

View File

@ -3,7 +3,7 @@
{% import 'macro/card.html' as card %} {% import 'macro/card.html' as card %}
<div class="card mb-3 flex-fill {% if solo %}card-solo{% endif %}"> <div class="card mb-3 flex-fill {% if solo %}card-solo{% endif %}">
{{ card.header(item, item.fields.name, solo=solo, number=item.fields.number, icon='user-line') }} {{ card.header(item, item.fields.name, solo=solo, identifier=item.fields.figure, icon='user-line') }}
{{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.figure, medium=true) }} {{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.figure, medium=true) }}
<div class="card-body border-bottom {% if not solo %}p-1{% endif %}"> <div class="card-body border-bottom {% if not solo %}p-1{% endif %}">
{% if last %} {% if last %}

View File

@ -3,8 +3,8 @@
{% import 'macro/card.html' as card %} {% import 'macro/card.html' as card %}
<div class="card mb-3 flex-fill card-solo"> <div class="card mb-3 flex-fill card-solo">
{{ card.header(item, item.fields.name, solo=solo, number=item.fields.part_num, color=item.fields.color_name, icon='shapes-line') }} {{ card.header(item, item.fields.name, solo=solo, identifier=item.fields.part, color=item.fields.color, icon='shapes-line') }}
{{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.part_img_url_id, medium=true) }} {{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.image_id, medium=true) }}
<div class="card-body border-bottom {% if not solo %}p-1{% endif %}"> <div class="card-body border-bottom {% if not solo %}p-1{% endif %}">
{{ badge.total_sets(sets_using | length, solo=solo, last=last) }} {{ badge.total_sets(sets_using | length, solo=solo, last=last) }}
{{ badge.total_minifigures(minifigures_using | length, solo=solo, last=last) }} {{ badge.total_minifigures(minifigures_using | length, solo=solo, last=last) }}

View File

@ -6,10 +6,10 @@
<tbody> <tbody>
{% for item in table_collection %} {% for item in table_collection %}
<tr> <tr>
{{ table.image(item.url_for_image(), caption=item.fields.name, alt=item.fields.part_num, accordion=solo) }} {{ table.image(item.url_for_image(), caption=item.fields.name, alt=item.fields.part, accordion=solo) }}
<td> <td>
<a class="text-reset" href="{{ item.url() }}">{{ item.fields.name }}</a> <a class="text-reset" href="{{ item.url() }}">{{ item.fields.name }}</a>
{% if item.fields.is_spare %}<span class="badge rounded-pill text-bg-warning fw-normal"><i class="ri-loop-left-line"></i> Spare</span>{% endif %} {% if item.fields.spare %}<span class="badge rounded-pill text-bg-warning fw-normal"><i class="ri-loop-left-line"></i> Spare</span>{% endif %}
{% if all %} {% if all %}
{{ table.rebrickable(item) }} {{ table.rebrickable(item) }}
{{ table.bricklink(item) }} {{ table.bricklink(item) }}
@ -24,15 +24,15 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if not no_missing %} {% if not no_missing %}
<td {% if not all %}id="sort-part-{{ item.fields.u_id }}-{{ item.fields.id }}" data-sort="{{ item.fields.total_missing }}"{% endif %} class="table-td-missing"> <td {% if not all %}id="sort-part-{{ item.fields.id }}-{{ item.html_id() }}" data-sort="{{ item.fields.total_missing }}"{% endif %} class="table-td-missing">
{% if all or read_only_missing %} {% if all or read_only_missing %}
{{ item.fields.total_missing }} {{ item.fields.total_missing }}
{% else %} {% else %}
<div class="input-group"> <div class="input-group">
{% if g.login.is_authenticated() %} {% if g.login.is_authenticated() %}
<input class="form-control form-control-sm flex-shrink-1" type="text" {% if item.fields.total_missing %}value="{{ item.fields.total_missing }}"{% endif %} <input class="form-control form-control-sm flex-shrink-1" type="text" {% if item.fields.total_missing %}value="{{ item.fields.total_missing }}"{% endif %}
onchange="change_part_missing_amount(this, '{{ item.fields.u_id }}', '{{ item.fields.id }}', '{{ item.url_for_missing() }}')" autocomplete="off"> onchange="change_part_missing_amount(this, '{{ item.fields.id }}', '{{ item.html_id() }}', '{{ item.url_for_missing() }}')" autocomplete="off">
<span id="status-part-{{ item.fields.u_id }}-{{ item.fields.id }}" class="input-group-text ri-save-line"></span> <span id="status-part-{{ item.fields.id }}-{{ item.html_id() }}" class="input-group-text ri-save-line"></span>
{% else %} {% else %}
<input class="form-control form-control-sm" type="text" {% if item.fields.total_missing %}value="{{ item.fields.total_missing }}"{% endif %} <input class="form-control form-control-sm" type="text" {% if item.fields.total_missing %}value="{{ item.fields.total_missing }}"{% endif %}
disabled autocomplete="off"> disabled autocomplete="off">

View File

@ -11,8 +11,8 @@
{% for checkbox in brickset_checkboxes %}data-{{ checkbox.as_dataset() }}="{{ item.fields[checkbox.as_column()] }}" {% endfor %} {% for checkbox in brickset_checkboxes %}data-{{ checkbox.as_dataset() }}="{{ item.fields[checkbox.as_column()] }}" {% endfor %}
{% endif %} {% endif %}
> >
{{ card.header(item, item.fields.name, solo=solo, number=item.fields.number) }} {{ card.header(item, item.fields.name, solo=solo, identifier=item.fields.set) }}
{{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.number) }} {{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.set) }}
<div class="card-body border-bottom-0 {% if not solo %}p-1{% endif %}"> <div class="card-body border-bottom-0 {% if not solo %}p-1{% endif %}">
{{ badge.theme(item.theme.name, solo=solo, last=last) }} {{ badge.theme(item.theme.name, solo=solo, last=last) }}
{{ badge.year(item.fields.year, solo=solo, last=last) }} {{ badge.year(item.fields.year, solo=solo, last=last) }}

View File

@ -2,7 +2,7 @@
{% import 'macro/card.html' as card %} {% import 'macro/card.html' as card %}
<div class="card mb-3"> <div class="card mb-3">
{{ card.header(item, item.fields.name, solo=true, number=item.fields.set) }} {{ card.header(item, item.fields.name, solo=true, identifier=item.fields.set) }}
{{ card.image(item, solo=true, last=false, caption=item.fields.name, alt=item.fields.set) }} {{ card.image(item, solo=true, last=false, caption=item.fields.name, alt=item.fields.set) }}
<div class="card-body p-1"> <div class="card-body p-1">
{{ badge.theme(item.theme.name) }} {{ badge.theme(item.theme.name) }}