Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d00569798 | |||
| 21d104280c | |||
| 5946f86dfa | |||
| 0155144881 | |||
| 1353153394 | |||
| 0f45192f8e | |||
| b02f851865 | |||
| bddfbb5235 | |||
| dc34916331 | |||
| a8d36bc5f1 | |||
| bd32ca5b8f | |||
| 2ed60e3fe3 | |||
| 0ec1d37c36 |
+32
-9
@@ -61,6 +61,10 @@
|
||||
# Default: 25
|
||||
# BK_DEFAULT_TABLE_PER_PAGE=50
|
||||
|
||||
# Optional: Maximum length for description text in badges before truncating with ellipsis
|
||||
# Default: 15
|
||||
# BK_DESCRIPTION_BADGE_MAX_LENGTH=15
|
||||
|
||||
# Optional: if set up, will add a CORS allow origin restriction to the socket.
|
||||
# Default:
|
||||
# Legacy name: DOMAIN_NAME
|
||||
@@ -118,6 +122,20 @@
|
||||
# Default: false
|
||||
# BK_HIDE_ALL_MINIFIGURES=true
|
||||
|
||||
# Optional: Disable the individual/loose minifigures system. This hides all individual
|
||||
# minifigure UI elements and prevents adding new individual minifigures. The routes remain
|
||||
# accessible so existing individual minifigures can still be viewed. Users who only track
|
||||
# set-based minifigures can use this to simplify the interface. Does not disable the route.
|
||||
# Default: false
|
||||
# BK_DISABLE_INDIVIDUAL_MINIFIGURES=false
|
||||
|
||||
# Optional: Disable the individual/loose parts system. This hides all individual part UI
|
||||
# elements and prevents adding new individual parts (parts not associated with any set).
|
||||
# The routes remain accessible so existing individual parts can still be viewed. Users who
|
||||
# only track set-based parts can use this to simplify the interface. Does not disable the route.
|
||||
# Default: false
|
||||
# BK_DISABLE_INDIVIDUAL_PARTS=false
|
||||
|
||||
# Optional: Hide the 'Parts' entry from the menu. Does not disable the route.
|
||||
# Default: false
|
||||
# BK_HIDE_ALL_PARTS=true
|
||||
@@ -160,12 +178,15 @@
|
||||
# BK_HIDE_WISHES=true
|
||||
|
||||
# Optional: Change the default order of minifigures. By default ordered by insertion order.
|
||||
# Note: Minifigures are queried from a combined view that merges both set-based and individual minifigures.
|
||||
# Therefore, column references should use the "combined" table alias.
|
||||
# Useful column names for this option are:
|
||||
# - "rebrickable_minifigures"."figure": minifigure ID (fig-xxxxx)
|
||||
# - "rebrickable_minifigures"."number": minifigure ID as an integer (xxxxx)
|
||||
# - "rebrickable_minifigures"."name": minifigure name
|
||||
# Default: "rebrickable_minifigures"."name" ASC
|
||||
# BK_MINIFIGURES_DEFAULT_ORDER="rebrickable_minifigures"."name" ASC
|
||||
# - "combined"."figure": minifigure ID (fig-xxxxx)
|
||||
# - "combined"."number": minifigure ID as an integer (xxxxx)
|
||||
# - "combined"."name": minifigure name
|
||||
# - "combined"."rowid": insertion order (for both set and individual minifigures)
|
||||
# Default: "combined"."name" ASC
|
||||
# BK_MINIFIGURES_DEFAULT_ORDER="combined"."name" ASC
|
||||
|
||||
# Optional: Folder where to store the minifigures images, relative to the '/app/static/' folder
|
||||
# Default: minifigs
|
||||
@@ -178,14 +199,16 @@
|
||||
# BK_NO_THREADED_SOCKET=true
|
||||
|
||||
# Optional: Change the default order of parts. By default ordered by insertion order.
|
||||
# Note: Parts are queried from a combined view that merges both set-based and individual minifigure parts.
|
||||
# Some columns use the "combined" table alias for fields from the merged view.
|
||||
# Useful column names for this option are:
|
||||
# - "bricktracker_parts"."part": part number
|
||||
# - "bricktracker_parts"."spare": part is a spare part
|
||||
# - "combined"."part": part number
|
||||
# - "combined"."spare": part is a spare part (use "combined" not "bricktracker_parts")
|
||||
# - "rebrickable_parts"."name": part name
|
||||
# - "rebrickable_parts"."color_name": part color name
|
||||
# - "total_missing": number of missing parts
|
||||
# Default: "rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "bricktracker_parts"."spare" ASC
|
||||
# BK_PARTS_DEFAULT_ORDER="total_missing" DESC, "rebrickable_parts"."name"."name" ASC
|
||||
# Default: "rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "combined"."spare" ASC
|
||||
# BK_PARTS_DEFAULT_ORDER="total_missing" DESC, "rebrickable_parts"."name" ASC
|
||||
|
||||
# Optional: Folder where to store the parts images, relative to the '/app/static/' folder
|
||||
# Default: parts
|
||||
|
||||
@@ -33,3 +33,7 @@ vitepress/
|
||||
|
||||
# Local data
|
||||
offline/
|
||||
TODO.md
|
||||
run-local.sh
|
||||
test-server.sh
|
||||
data/
|
||||
|
||||
@@ -29,6 +29,8 @@ from bricktracker.views.error import error_404
|
||||
from bricktracker.views.index import index_page
|
||||
from bricktracker.views.instructions import instructions_page
|
||||
from bricktracker.views.login import login_page
|
||||
from bricktracker.views.individual_minifigure import individual_minifigure_page
|
||||
from bricktracker.views.individual_part import individual_part_page
|
||||
from bricktracker.views.minifigure import minifigure_page
|
||||
from bricktracker.views.part import part_page
|
||||
from bricktracker.views.set import set_page
|
||||
@@ -36,11 +38,42 @@ from bricktracker.views.statistics import statistics_page
|
||||
from bricktracker.views.storage import storage_page
|
||||
from bricktracker.views.wish import wish_page
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _validate_config(app: Flask) -> None:
|
||||
"""
|
||||
Validate application configuration and log warnings for potential issues.
|
||||
"""
|
||||
# Check if both individual features are disabled
|
||||
if app.config.get('DISABLE_INDIVIDUAL_PARTS') and app.config.get('DISABLE_INDIVIDUAL_MINIFIGURES'):
|
||||
logger.warning(
|
||||
'Both DISABLE_INDIVIDUAL_PARTS and DISABLE_INDIVIDUAL_MINIFIGURES are enabled. '
|
||||
'Users will not be able to track standalone parts or minifigures.'
|
||||
)
|
||||
|
||||
# Check if Rebrickable API key is missing
|
||||
if not app.config.get('REBRICKABLE_API_KEY'):
|
||||
logger.warning(
|
||||
'REBRICKABLE_API_KEY is not set. You will not be able to fetch data from Rebrickable API. '
|
||||
'Please set this in your .env file or environment variables.'
|
||||
)
|
||||
|
||||
# Check authentication configuration
|
||||
if not app.config.get('AUTHENTICATION_PASSWORD') and not app.config.get('AUTHENTICATION_KEY'):
|
||||
logger.info(
|
||||
'No authentication configured (AUTHENTICATION_PASSWORD or AUTHENTICATION_KEY). '
|
||||
'Admin features will be accessible without login.'
|
||||
)
|
||||
|
||||
|
||||
def setup_app(app: Flask) -> None:
|
||||
# Load the configuration
|
||||
BrickConfigurationList(app)
|
||||
|
||||
# Validate configuration
|
||||
_validate_config(app)
|
||||
|
||||
# Set the logging level
|
||||
if app.config['DEBUG']:
|
||||
logging.basicConfig(
|
||||
@@ -80,6 +113,8 @@ def setup_app(app: Flask) -> None:
|
||||
app.register_blueprint(index_page)
|
||||
app.register_blueprint(instructions_page)
|
||||
app.register_blueprint(login_page)
|
||||
app.register_blueprint(individual_minifigure_page)
|
||||
app.register_blueprint(individual_part_page)
|
||||
app.register_blueprint(minifigure_page)
|
||||
app.register_blueprint(part_page)
|
||||
app.register_blueprint(set_page)
|
||||
|
||||
@@ -17,6 +17,9 @@ CONFIG: Final[list[dict[str, Any]]] = [
|
||||
{'n': 'DATABASE_TIMESTAMP_FORMAT', 'd': '%Y-%m-%d-%H-%M-%S'},
|
||||
{'n': 'DEBUG', 'c': bool},
|
||||
{'n': 'DEFAULT_TABLE_PER_PAGE', 'd': 25, 'c': int},
|
||||
{'n': 'DESCRIPTION_BADGE_MAX_LENGTH', 'd': 15, 'c': int},
|
||||
{'n': 'DISABLE_INDIVIDUAL_MINIFIGURES', 'c': bool},
|
||||
{'n': 'DISABLE_INDIVIDUAL_PARTS', 'c': bool},
|
||||
{'n': 'DOMAIN_NAME', 'e': 'DOMAIN_NAME', 'd': ''},
|
||||
{'n': 'FILE_DATETIME_FORMAT', 'd': '%d/%m/%Y, %H:%M:%S'},
|
||||
{'n': 'HOST', 'd': '0.0.0.0'},
|
||||
@@ -39,7 +42,7 @@ CONFIG: Final[list[dict[str, Any]]] = [
|
||||
{'n': 'HIDE_TABLE_MISSING_PARTS', 'c': bool},
|
||||
{'n': 'HIDE_TABLE_CHECKED_PARTS', 'c': bool},
|
||||
{'n': 'HIDE_WISHES', 'c': bool},
|
||||
{'n': 'MINIFIGURES_DEFAULT_ORDER', 'd': '"rebrickable_minifigures"."name" ASC'}, # noqa: E501
|
||||
{'n': 'MINIFIGURES_DEFAULT_ORDER', 'd': '"combined"."name" ASC'}, # noqa: E501
|
||||
{'n': 'MINIFIGURES_FOLDER', 'd': 'minifigures', 's': True},
|
||||
{'n': 'MINIFIGURES_PAGINATION_SIZE_DESKTOP', 'd': 10, 'c': int},
|
||||
{'n': 'MINIFIGURES_PAGINATION_SIZE_MOBILE', 'd': 5, 'c': int},
|
||||
@@ -47,7 +50,7 @@ CONFIG: Final[list[dict[str, Any]]] = [
|
||||
{'n': 'NO_THREADED_SOCKET', 'c': bool},
|
||||
{'n': 'PARTS_SERVER_SIDE_PAGINATION', 'c': bool},
|
||||
{'n': 'SETS_SERVER_SIDE_PAGINATION', 'c': bool},
|
||||
{'n': 'PARTS_DEFAULT_ORDER', 'd': '"rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "bricktracker_parts"."spare" ASC'}, # noqa: E501
|
||||
{'n': 'PARTS_DEFAULT_ORDER', 'd': '"rebrickable_parts"."name" ASC, "rebrickable_parts"."color_name" ASC, "combined"."spare" ASC'}, # noqa: E501
|
||||
{'n': 'PARTS_FOLDER', 'd': 'parts', 's': True},
|
||||
{'n': 'PARTS_PAGINATION_SIZE_DESKTOP', 'd': 10, 'c': int},
|
||||
{'n': 'PARTS_PAGINATION_SIZE_MOBILE', 'd': 5, 'c': int},
|
||||
|
||||
@@ -10,6 +10,7 @@ logger = logging.getLogger(__name__)
|
||||
LIVE_CHANGEABLE_VARS: Final[List[str]] = [
|
||||
'BK_BRICKLINK_LINKS',
|
||||
'BK_DEFAULT_TABLE_PER_PAGE',
|
||||
'BK_DESCRIPTION_BADGE_MAX_LENGTH',
|
||||
'BK_INDEPENDENT_ACCORDIONS',
|
||||
'BK_HIDE_ADD_SET',
|
||||
'BK_HIDE_ADD_BULK_SET',
|
||||
@@ -83,6 +84,8 @@ RESTART_REQUIRED_VARS: Final[List[str]] = [
|
||||
'BK_AUTHENTICATION_KEY',
|
||||
'BK_DATABASE_PATH',
|
||||
'BK_DEBUG',
|
||||
'BK_DISABLE_INDIVIDUAL_MINIFIGURES',
|
||||
'BK_DISABLE_INDIVIDUAL_PARTS',
|
||||
'BK_DOMAIN_NAME',
|
||||
'BK_HOST',
|
||||
'BK_PORT',
|
||||
@@ -173,7 +176,7 @@ class ConfigManager:
|
||||
else:
|
||||
return []
|
||||
# Integer variables (pagination sizes, delays, etc.) - Check BEFORE boolean check
|
||||
if any(keyword in var_name.lower() for keyword in ['_size', '_page', 'delay', 'min_', 'per_page', 'page_size']):
|
||||
if any(keyword in var_name.lower() for keyword in ['_size', '_page', 'delay', 'min_', 'per_page', 'page_size', '_length']):
|
||||
try:
|
||||
return int(value)
|
||||
except (ValueError, TypeError):
|
||||
|
||||
@@ -0,0 +1,500 @@
|
||||
import logging
|
||||
import traceback
|
||||
from typing import Any, Self, TYPE_CHECKING
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import current_app, url_for
|
||||
|
||||
from .exceptions import NotFoundException, DatabaseException, ErrorException
|
||||
from .parser import parse_minifig
|
||||
from .rebrickable import Rebrickable
|
||||
from .rebrickable_minifigure import RebrickableMinifigure
|
||||
from .set_owner_list import BrickSetOwnerList
|
||||
from .set_purchase_location_list import BrickSetPurchaseLocationList
|
||||
from .set_storage_list import BrickSetStorageList
|
||||
from .set_tag_list import BrickSetTagList
|
||||
from .sql import BrickSQL
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .socket import BrickSocket
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Individual minifigure (not associated with a set)
|
||||
class IndividualMinifigure(RebrickableMinifigure):
|
||||
# Queries
|
||||
select_query: str = 'individual_minifigure/select/by_id'
|
||||
light_query: str = 'individual_minifigure/select/light'
|
||||
insert_query: str = 'individual_minifigure/insert'
|
||||
|
||||
# Delete a individual minifigure
|
||||
def delete(self, /) -> None:
|
||||
BrickSQL().executescript(
|
||||
'individual_minifigure/delete/individual_minifigure',
|
||||
id=self.fields.id
|
||||
)
|
||||
|
||||
# Import a individual minifigure into the database
|
||||
def download(self, socket: 'BrickSocket', data: dict[str, Any], /) -> bool:
|
||||
# Load the minifigure
|
||||
if not self.load(socket, data, from_download=True):
|
||||
return False
|
||||
|
||||
try:
|
||||
# Insert into the database
|
||||
socket.auto_progress(
|
||||
message='Minifigure {figure}: inserting into database'.format(
|
||||
figure=self.fields.figure
|
||||
),
|
||||
increment_total=True,
|
||||
)
|
||||
|
||||
# Generate an UUID for self
|
||||
self.fields.id = str(uuid4())
|
||||
|
||||
# Save the storage
|
||||
storage = BrickSetStorageList.get(
|
||||
data.get('storage', ''),
|
||||
allow_none=True
|
||||
)
|
||||
self.fields.storage = storage.fields.id if storage else None
|
||||
|
||||
# Save the purchase location
|
||||
purchase_location = BrickSetPurchaseLocationList.get(
|
||||
data.get('purchase_location', ''),
|
||||
allow_none=True
|
||||
)
|
||||
self.fields.purchase_location = purchase_location.fields.id if purchase_location else None
|
||||
|
||||
# Save quantity and description
|
||||
self.fields.quantity = int(data.get('quantity', 1))
|
||||
self.fields.description = data.get('description', '')
|
||||
|
||||
# IMPORTANT: Insert rebrickable minifigure FIRST
|
||||
# bricktracker_individual_minifigures has FK to rebrickable_minifigures
|
||||
self.insert_rebrickable_loose()
|
||||
|
||||
# Now insert into bricktracker_individual_minifigures
|
||||
# Use no_defer=True to ensure the insert happens before we insert parts
|
||||
# (parts have a foreign key constraint on this id)
|
||||
self.insert(commit=False, no_defer=True)
|
||||
|
||||
# Save the owners
|
||||
owners: list[str] = list(data.get('owners', []))
|
||||
for id in owners:
|
||||
owner = BrickSetOwnerList.get(id)
|
||||
owner.update_individual_minifigure_state(self, state=True)
|
||||
|
||||
# Save the tags
|
||||
tags: list[str] = list(data.get('tags', []))
|
||||
for id in tags:
|
||||
tag = BrickSetTagList.get(id)
|
||||
tag.update_individual_minifigure_state(self, state=True)
|
||||
|
||||
# Load the parts (elements) for this minifigure
|
||||
if not self.download_parts(socket):
|
||||
return False
|
||||
|
||||
# Commit the transaction to the database
|
||||
socket.auto_progress(
|
||||
message='Minifigure {figure}: writing to the database'.format(
|
||||
figure=self.fields.figure
|
||||
),
|
||||
increment_total=True,
|
||||
)
|
||||
|
||||
BrickSQL().commit()
|
||||
|
||||
# Info
|
||||
logger.info('Minifigure {figure}: imported (id: {id})'.format(
|
||||
figure=self.fields.figure,
|
||||
id=self.fields.id,
|
||||
))
|
||||
|
||||
# Complete
|
||||
socket.complete(
|
||||
message='Minifigure {figure}: imported (<a href="{url}">Go to the minifigure</a>)'.format(
|
||||
figure=self.fields.figure,
|
||||
url=self.url()
|
||||
),
|
||||
download=True
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
socket.fail(
|
||||
message='Error while importing minifigure {figure}: {error}'.format(
|
||||
figure=self.fields.figure,
|
||||
error=e,
|
||||
)
|
||||
)
|
||||
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# Download parts (elements) for this individual minifigure
|
||||
def download_parts(self, socket: 'BrickSocket', /) -> bool:
|
||||
"""Download minifigure parts using get_minifig_elements()"""
|
||||
try:
|
||||
# Check if we have cached parts data from load()
|
||||
if hasattr(self, '_cached_parts_response'):
|
||||
response = self._cached_parts_response
|
||||
logger.debug('Using cached parts data from load()')
|
||||
else:
|
||||
# Need to fetch parts data
|
||||
socket.auto_progress(
|
||||
message='Minifigure {figure}: loading parts from Rebrickable'.format(
|
||||
figure=self.fields.figure
|
||||
),
|
||||
increment_total=True,
|
||||
)
|
||||
|
||||
logger.debug('rebrick.lego.get_minifig_elements("{figure}")'.format(
|
||||
figure=self.fields.figure,
|
||||
))
|
||||
|
||||
# Load parts data from Rebrickable API
|
||||
import json
|
||||
from rebrick import lego
|
||||
|
||||
parameters = {
|
||||
'api_key': current_app.config['REBRICKABLE_API_KEY'],
|
||||
'page_size': current_app.config['REBRICKABLE_PAGE_SIZE'],
|
||||
}
|
||||
|
||||
response = json.loads(lego.get_minifig_elements(
|
||||
self.fields.figure,
|
||||
**parameters
|
||||
).read())
|
||||
|
||||
socket.auto_progress(
|
||||
message='Minifigure {figure}: saving parts to database'.format(
|
||||
figure=self.fields.figure
|
||||
),
|
||||
)
|
||||
|
||||
# Insert each part into individual_minifigure_parts table
|
||||
from .rebrickable_part import RebrickablePart
|
||||
|
||||
if 'results' in response:
|
||||
logger.debug(f'Processing {len(response["results"])} parts for minifigure {self.fields.figure}')
|
||||
|
||||
for idx, result in enumerate(response['results']):
|
||||
part_num = result['part']['part_num']
|
||||
color_id = result['color']['id']
|
||||
|
||||
logger.debug(
|
||||
f'Part {idx+1}/{len(response["results"])}: {part_num} '
|
||||
f'(color: {color_id}, quantity: {result["quantity"]})'
|
||||
)
|
||||
|
||||
# Insert rebrickable part data first
|
||||
part_data = RebrickablePart.from_rebrickable(result)
|
||||
logger.debug(f'Rebrickable part data keys: {list(part_data.keys())}')
|
||||
|
||||
# Insert into rebrickable_parts if not exists
|
||||
BrickSQL().execute(
|
||||
'rebrickable/part/insert',
|
||||
parameters=part_data,
|
||||
commit=False,
|
||||
)
|
||||
|
||||
# Download part image if not using remote images
|
||||
if not current_app.config['USE_REMOTE_IMAGES']:
|
||||
# Create a RebrickablePart instance for image download
|
||||
from .set import BrickSet
|
||||
try:
|
||||
part_instance = RebrickablePart(record=part_data)
|
||||
from .rebrickable_image import RebrickableImage
|
||||
RebrickableImage(
|
||||
BrickSet(), # Dummy set
|
||||
minifigure=self,
|
||||
part=part_instance,
|
||||
).download()
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f'Could not download image for part {part_num}: {e}'
|
||||
)
|
||||
|
||||
# Insert into bricktracker_individual_minifigure_parts
|
||||
individual_part_params = {
|
||||
'id': self.fields.id,
|
||||
'part': part_num,
|
||||
'color': color_id,
|
||||
'spare': result.get('is_spare', False),
|
||||
'quantity': result['quantity'],
|
||||
'element': result.get('element_id'),
|
||||
'rebrickable_inventory': result['id'],
|
||||
}
|
||||
logger.debug(f'Individual part params: {individual_part_params}')
|
||||
|
||||
BrickSQL().execute(
|
||||
'individual_minifigure/part/insert',
|
||||
parameters=individual_part_params,
|
||||
commit=False,
|
||||
)
|
||||
|
||||
logger.debug(f'Successfully inserted all {len(response["results"])} parts')
|
||||
else:
|
||||
logger.warning(f'No results in parts response for minifigure {self.fields.figure}')
|
||||
|
||||
# Clean up cached data
|
||||
if hasattr(self, '_cached_parts_response'):
|
||||
delattr(self, '_cached_parts_response')
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
socket.fail(
|
||||
message='Error loading parts for minifigure {figure}: {error}'.format(
|
||||
figure=self.fields.figure,
|
||||
error=e,
|
||||
)
|
||||
)
|
||||
logger.debug(traceback.format_exc())
|
||||
return False
|
||||
|
||||
# Insert the individual minifigure from Rebrickable
|
||||
def insert_rebrickable_loose(self, /) -> None:
|
||||
"""Insert rebrickable minifigure data (without set association)"""
|
||||
# Insert the Rebrickable minifigure to the database
|
||||
# Note: We override the parent's insert_rebrickable since we don't have a brickset
|
||||
from .rebrickable_image import RebrickableImage
|
||||
|
||||
# Explicitly build parameters for rebrickable_minifigures insert
|
||||
params = {
|
||||
'figure': self.fields.figure,
|
||||
'number': self.fields.number,
|
||||
'name': self.fields.name,
|
||||
'image': self.fields.image,
|
||||
'number_of_parts': self.fields.number_of_parts,
|
||||
}
|
||||
|
||||
BrickSQL().execute(
|
||||
RebrickableMinifigure.insert_query,
|
||||
parameters=params,
|
||||
commit=False,
|
||||
)
|
||||
|
||||
# Download image locally if not using remote images
|
||||
if not current_app.config['USE_REMOTE_IMAGES']:
|
||||
# Create a dummy BrickSet for RebrickableImage
|
||||
# RebrickableImage checks minifigure first before set, so this works
|
||||
from .set import BrickSet
|
||||
try:
|
||||
RebrickableImage(
|
||||
BrickSet(), # Dummy set - not used since minifigure takes priority
|
||||
minifigure=self,
|
||||
).download()
|
||||
logger.debug(f'Downloaded image for individual minifigure {self.fields.figure}')
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f'Could not download image for individual minifigure {self.fields.figure}: {e}'
|
||||
)
|
||||
|
||||
# Load the minifigure from Rebrickable
|
||||
def load(
|
||||
self,
|
||||
socket: 'BrickSocket',
|
||||
data: dict[str, Any],
|
||||
/,
|
||||
*,
|
||||
from_download=False,
|
||||
) -> bool:
|
||||
# Reset the progress
|
||||
socket.progress_count = 0
|
||||
socket.progress_total = 2
|
||||
|
||||
try:
|
||||
# Check if individual minifigures are disabled
|
||||
from flask import current_app
|
||||
if current_app.config.get('DISABLE_INDIVIDUAL_MINIFIGURES', False):
|
||||
raise ErrorException(
|
||||
'Individual minifigures system is disabled. '
|
||||
'Only set-based minifigures can be added.'
|
||||
)
|
||||
|
||||
socket.auto_progress(message='Parsing minifigure number')
|
||||
figure = parse_minifig(str(data['figure']))
|
||||
|
||||
socket.auto_progress(
|
||||
message='Minifigure {figure}: loading from Rebrickable'.format(
|
||||
figure=figure,
|
||||
),
|
||||
)
|
||||
|
||||
logger.debug('rebrick.lego.get_minifig_elements("{figure}")'.format(
|
||||
figure=figure,
|
||||
))
|
||||
|
||||
# Load from Rebrickable using get_minifig_elements
|
||||
# This gives us both minifigure info and parts in one call
|
||||
import json
|
||||
from rebrick import lego
|
||||
|
||||
parameters = {
|
||||
'api_key': current_app.config['REBRICKABLE_API_KEY'],
|
||||
'page_size': current_app.config['REBRICKABLE_PAGE_SIZE'],
|
||||
}
|
||||
|
||||
response = json.loads(lego.get_minifig_elements(
|
||||
figure,
|
||||
**parameters
|
||||
).read())
|
||||
|
||||
# Extract minifigure info from the first part's metadata
|
||||
if 'results' in response and len(response['results']) > 0:
|
||||
first_part = response['results'][0]
|
||||
|
||||
# Build minifigure data from the response
|
||||
self.fields.figure = first_part['set_num']
|
||||
self.fields.number_of_parts = response['count']
|
||||
|
||||
# We need to fetch the proper name and image from get_minifig()
|
||||
# This is a small additional call but gives us the proper minifigure data
|
||||
try:
|
||||
# get_minifig() only needs api_key, not page_size
|
||||
minifig_params = {
|
||||
'api_key': current_app.config['REBRICKABLE_API_KEY']
|
||||
}
|
||||
minifig_response = json.loads(lego.get_minifig(
|
||||
figure,
|
||||
**minifig_params
|
||||
).read())
|
||||
self.fields.name = minifig_response.get('name', f"Minifigure {figure}")
|
||||
|
||||
# Use the minifig image from get_minifig() - this is the assembled minifig
|
||||
self.fields.image = minifig_response.get('set_img_url')
|
||||
|
||||
# Extract number from figure (e.g., fig-005997 -> 5997)
|
||||
try:
|
||||
self.fields.number = int(figure.split('-')[1])
|
||||
except:
|
||||
self.fields.number = 0
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not fetch minifigure name: {e}')
|
||||
self.fields.name = f"Minifigure {figure}"
|
||||
# Try to extract number anyway
|
||||
try:
|
||||
self.fields.number = int(figure.split('-')[1])
|
||||
except:
|
||||
self.fields.number = 0
|
||||
|
||||
# Fallback: try to extract image from first part with element_id
|
||||
self.fields.image = None
|
||||
for result in response['results']:
|
||||
if result.get('element_id') and result['part'].get('part_img_url'):
|
||||
self.fields.image = result['part']['part_img_url']
|
||||
break
|
||||
|
||||
# Store the parts data for later use in download
|
||||
self._cached_parts_response = response
|
||||
else:
|
||||
raise NotFoundException(f'Minifigure {figure} has no parts in Rebrickable')
|
||||
|
||||
socket.emit('MINIFIGURE_LOADED', self.short(
|
||||
from_download=from_download
|
||||
))
|
||||
|
||||
if not from_download:
|
||||
socket.complete(
|
||||
message='Minifigure {figure}: loaded from Rebrickable'.format(
|
||||
figure=self.fields.figure
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
# Check if this is the "disabled" error - if so, show cleaner message
|
||||
error_msg = str(e)
|
||||
if 'Individual minifigures system is disabled' in error_msg:
|
||||
socket.fail(message=error_msg)
|
||||
else:
|
||||
socket.fail(
|
||||
message='Could not load the minifigure from Rebrickable: {error}. Data: {data}'.format(
|
||||
error=error_msg,
|
||||
data=data,
|
||||
)
|
||||
)
|
||||
|
||||
if not isinstance(e, (NotFoundException, ErrorException)):
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
return False
|
||||
|
||||
# Return a short form of the minifigure
|
||||
def short(self, /, *, from_download: bool = False) -> dict[str, Any]:
|
||||
return {
|
||||
'download': from_download,
|
||||
'image': self.url_for_image(),
|
||||
'name': self.fields.name,
|
||||
'figure': self.fields.figure,
|
||||
}
|
||||
|
||||
# Select a individual minifigure by ID
|
||||
def select_by_id(self, id: str, /) -> Self:
|
||||
# Save the ID parameter
|
||||
self.fields.id = id
|
||||
|
||||
# Import status list here to get metadata columns
|
||||
from .set_status_list import BrickSetStatusList
|
||||
|
||||
# Pass metadata columns to the query with correct table names for individual minifigures
|
||||
context = {
|
||||
'owners': ', ' + BrickSetOwnerList.as_columns(table='bricktracker_individual_minifigure_owners') if BrickSetOwnerList.list() else '',
|
||||
'statuses': ', ' + BrickSetStatusList.as_columns(table='bricktracker_individual_minifigure_statuses', all=True) if BrickSetStatusList.list(all=True) else '',
|
||||
'tags': ', ' + BrickSetTagList.as_columns(table='bricktracker_individual_minifigure_tags') if BrickSetTagList.list() else '',
|
||||
}
|
||||
|
||||
if not self.select(**context):
|
||||
raise NotFoundException(
|
||||
'Individual minifigure with ID {id} was not found in the database'.format(
|
||||
id=id,
|
||||
),
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
# URL to this individual minifigure instance
|
||||
def url(self, /) -> str:
|
||||
return url_for('individual_minifigure.details', id=self.fields.id)
|
||||
|
||||
# String representation for debugging
|
||||
def __repr__(self, /) -> str:
|
||||
"""String representation for debugging"""
|
||||
figure = getattr(self.fields, 'figure', 'unknown')
|
||||
name = getattr(self.fields, 'name', 'Unknown')
|
||||
qty = getattr(self.fields, 'quantity', 0)
|
||||
return f'<IndividualMinifigure {figure} "{name}" qty:{qty}>'
|
||||
|
||||
# URL for updating quantity
|
||||
def url_for_quantity(self, /) -> str:
|
||||
return url_for('individual_minifigure.update_quantity', id=self.fields.id)
|
||||
|
||||
# URL for updating description
|
||||
def url_for_description(self, /) -> str:
|
||||
return url_for('individual_minifigure.update_description', id=self.fields.id)
|
||||
|
||||
# Parts
|
||||
def generic_parts(self, /):
|
||||
from .part_list import BrickPartList
|
||||
return BrickPartList().from_individual_minifigure(self)
|
||||
|
||||
# Override from_rebrickable to handle minifigure data
|
||||
@staticmethod
|
||||
def from_rebrickable(data: dict[str, Any], /, **_) -> dict[str, Any]:
|
||||
# Extracting number
|
||||
number = int(str(data['set_num'])[5:])
|
||||
|
||||
return {
|
||||
'figure': str(data['set_num']),
|
||||
'number': int(number),
|
||||
'name': str(data['set_name']),
|
||||
'image': data.get('set_img_url'),
|
||||
'number_of_parts': int(data.get('num_parts', 0)),
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
from typing import Self
|
||||
|
||||
from .individual_minifigure import IndividualMinifigure
|
||||
from .record_list import BrickRecordList
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Individual minifigures list
|
||||
class IndividualMinifigureList(BrickRecordList[IndividualMinifigure]):
|
||||
# Queries
|
||||
instances_by_figure_query: str = 'individual_minifigure/select/instances_by_figure'
|
||||
using_storage_query: str = 'individual_minifigure/list/using_storage'
|
||||
without_storage_query: str = 'individual_minifigure/list/without_storage'
|
||||
|
||||
def __init__(self, /):
|
||||
super().__init__()
|
||||
|
||||
# Load all individual instances of a specific minifigure figure
|
||||
def instances_by_figure(self, figure: str, /) -> Self:
|
||||
# Save the figure parameter
|
||||
self.fields.figure = figure
|
||||
|
||||
# Import metadata lists to get columns
|
||||
from .set_owner_list import BrickSetOwnerList
|
||||
from .set_status_list import BrickSetStatusList
|
||||
from .set_tag_list import BrickSetTagList
|
||||
|
||||
# Prepare context with metadata columns
|
||||
context = {
|
||||
'owners': BrickSetOwnerList.as_columns(table='bricktracker_individual_minifigure_owners') if BrickSetOwnerList.list() else 'NULL AS "no_owners"',
|
||||
'statuses': BrickSetStatusList.as_columns(table='bricktracker_individual_minifigure_statuses', all=True) if BrickSetStatusList.list(all=True) else 'NULL AS "no_statuses"',
|
||||
'tags': BrickSetTagList.as_columns(table='bricktracker_individual_minifigure_tags') if BrickSetTagList.list() else 'NULL AS "no_tags"',
|
||||
}
|
||||
|
||||
# Load the instances from the database
|
||||
self.list(override_query=self.instances_by_figure_query, **context)
|
||||
|
||||
return self
|
||||
|
||||
# Load all individual minifigures using a specific storage
|
||||
def using_storage(self, storage: 'BrickSetStorage', /) -> Self:
|
||||
# Save the storage parameter
|
||||
self.fields.storage = storage.fields.id
|
||||
|
||||
# Load the minifigures from the database
|
||||
self.list(override_query=self.using_storage_query)
|
||||
|
||||
return self
|
||||
|
||||
# Load all individual minifigures without storage
|
||||
def without_storage(self, /) -> Self:
|
||||
# Load minifigures with no storage
|
||||
self.list(override_query=self.without_storage_query)
|
||||
|
||||
return self
|
||||
|
||||
# Base individual minifigure list
|
||||
def list(
|
||||
self,
|
||||
/,
|
||||
*,
|
||||
override_query: str | None = None,
|
||||
order: str | None = None,
|
||||
limit: int | None = None,
|
||||
**context,
|
||||
) -> None:
|
||||
# Load the individual minifigures from the database
|
||||
for record in super().select(
|
||||
override_query=override_query,
|
||||
order=order,
|
||||
limit=limit,
|
||||
**context
|
||||
):
|
||||
individual_minifigure = IndividualMinifigure(record=record)
|
||||
self.records.append(individual_minifigure)
|
||||
@@ -0,0 +1,700 @@
|
||||
import logging
|
||||
import os
|
||||
import traceback
|
||||
from typing import Any, Self, TYPE_CHECKING
|
||||
from urllib.parse import urlparse
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import current_app, url_for
|
||||
import requests
|
||||
from shutil import copyfileobj
|
||||
|
||||
from .exceptions import NotFoundException, DatabaseException, ErrorException
|
||||
from .record import BrickRecord
|
||||
from .set_owner_list import BrickSetOwnerList
|
||||
from .set_purchase_location_list import BrickSetPurchaseLocationList
|
||||
from .set_storage_list import BrickSetStorageList
|
||||
from .set_tag_list import BrickSetTagList
|
||||
from .sql import BrickSQL
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .socket import BrickSocket
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Individual part (standalone, not associated with a set or minifigure)
|
||||
class IndividualPart(BrickRecord):
|
||||
# Queries
|
||||
select_query: str = 'individual_part/select/by_id'
|
||||
insert_query: str = 'individual_part/insert'
|
||||
update_query: str = 'individual_part/update'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
/,
|
||||
*,
|
||||
record: Any | None = None
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
# Ingest the record if it has one
|
||||
if record is not None:
|
||||
self.ingest(record)
|
||||
|
||||
# Select a specific individual part by UUID
|
||||
def select_by_id(self, id: str, /) -> Self:
|
||||
self.fields.id = id
|
||||
if not self.select(override_query=self.select_query):
|
||||
raise NotFoundException(
|
||||
'Individual part with id "{id}" not found'.format(id=id)
|
||||
)
|
||||
return self
|
||||
|
||||
# Delete an individual part
|
||||
def delete(self, /) -> None:
|
||||
sql = BrickSQL()
|
||||
sql.executescript(
|
||||
'individual_part/delete',
|
||||
id=self.fields.id
|
||||
)
|
||||
sql.commit()
|
||||
|
||||
# Generate HTML ID for form elements
|
||||
def html_id(self, prefix: str | None = None, /) -> str:
|
||||
"""Generate HTML ID for form elements"""
|
||||
components: list[str] = ['individual-part']
|
||||
|
||||
if prefix is not None:
|
||||
components.append(prefix)
|
||||
|
||||
components.append(self.fields.part)
|
||||
components.append(str(self.fields.color))
|
||||
components.append(self.fields.id)
|
||||
|
||||
return '-'.join(components)
|
||||
|
||||
# URL for quantity update
|
||||
def url_for_quantity(self, /) -> str:
|
||||
"""URL for updating quantity"""
|
||||
return url_for('individual_part.update_quantity', id=self.fields.id)
|
||||
|
||||
# URL for description update
|
||||
def url_for_description(self, /) -> str:
|
||||
"""URL for updating description"""
|
||||
return url_for('individual_part.update_description', id=self.fields.id)
|
||||
|
||||
# URL for problem (missing/damaged) update
|
||||
def url_for_problem(self, problem_type: str, /) -> str:
|
||||
"""URL for updating problem counts (missing/damaged)"""
|
||||
if problem_type == 'missing':
|
||||
return url_for('individual_part.update_missing', id=self.fields.id)
|
||||
elif problem_type == 'damaged':
|
||||
return url_for('individual_part.update_damaged', id=self.fields.id)
|
||||
else:
|
||||
raise ValueError(f'Invalid problem type: {problem_type}')
|
||||
|
||||
# URL for checked status update
|
||||
def url_for_checked(self, /) -> str:
|
||||
"""URL for updating checked status"""
|
||||
return url_for('individual_part.update_checked', id=self.fields.id)
|
||||
|
||||
# URL for this part's detail page
|
||||
def url(self, /) -> str:
|
||||
"""URL for this part's detail page"""
|
||||
return url_for('individual_part.details', id=self.fields.id)
|
||||
|
||||
# String representation for debugging
|
||||
def __repr__(self, /) -> str:
|
||||
"""String representation for debugging"""
|
||||
part_id = getattr(self.fields, 'part', 'unknown')
|
||||
color_id = getattr(self.fields, 'color', 'unknown')
|
||||
qty = getattr(self.fields, 'quantity', 0)
|
||||
return f'<IndividualPart {part_id} color:{color_id} qty:{qty}>'
|
||||
|
||||
# Get or fetch color information from rebrickable_colors table
|
||||
@staticmethod
|
||||
def get_or_fetch_color(color_id: int, /) -> dict[str, Any] | None:
|
||||
"""
|
||||
Get color information from cache table, or fetch from API if not cached.
|
||||
Returns dict with: name, rgb, is_trans, bricklink_color_id, bricklink_color_name
|
||||
"""
|
||||
sql = BrickSQL()
|
||||
|
||||
# Check if color exists in cache
|
||||
check_query = """
|
||||
SELECT "color_id", "name", "rgb", "is_trans",
|
||||
"bricklink_color_id", "bricklink_color_name"
|
||||
FROM "rebrickable_colors"
|
||||
WHERE "color_id" = :color_id
|
||||
"""
|
||||
sql.cursor.execute(check_query, {'color_id': color_id})
|
||||
result = sql.cursor.fetchone()
|
||||
|
||||
if result:
|
||||
# Color found in cache
|
||||
return {
|
||||
'color_id': result[0],
|
||||
'name': result[1],
|
||||
'rgb': result[2],
|
||||
'is_trans': result[3],
|
||||
'bricklink_color_id': result[4],
|
||||
'bricklink_color_name': result[5]
|
||||
}
|
||||
|
||||
# Color not in cache, fetch from API
|
||||
try:
|
||||
import rebrick
|
||||
import json
|
||||
|
||||
rebrick.init(current_app.config['REBRICKABLE_API_KEY'])
|
||||
color_response = rebrick.lego.get_color(color_id)
|
||||
color_data = json.loads(color_response.read())
|
||||
|
||||
# Extract BrickLink color info
|
||||
bricklink_color_id = None
|
||||
bricklink_color_name = None
|
||||
|
||||
if 'external_ids' in color_data and 'BrickLink' in color_data['external_ids']:
|
||||
bricklink_info = color_data['external_ids']['BrickLink']
|
||||
if 'ext_ids' in bricklink_info and bricklink_info['ext_ids']:
|
||||
bricklink_color_id = bricklink_info['ext_ids'][0]
|
||||
if 'ext_descrs' in bricklink_info and bricklink_info['ext_descrs']:
|
||||
bricklink_color_name = bricklink_info['ext_descrs'][0][0] if bricklink_info['ext_descrs'][0] else None
|
||||
|
||||
# Store in cache
|
||||
insert_query = """
|
||||
INSERT OR REPLACE INTO "rebrickable_colors" (
|
||||
"color_id", "name", "rgb", "is_trans",
|
||||
"bricklink_color_id", "bricklink_color_name"
|
||||
) VALUES (
|
||||
:color_id, :name, :rgb, :is_trans,
|
||||
:bricklink_color_id, :bricklink_color_name
|
||||
)
|
||||
"""
|
||||
sql.cursor.execute(insert_query, {
|
||||
'color_id': color_data['id'],
|
||||
'name': color_data['name'],
|
||||
'rgb': color_data.get('rgb'),
|
||||
'is_trans': color_data.get('is_trans', False),
|
||||
'bricklink_color_id': bricklink_color_id,
|
||||
'bricklink_color_name': bricklink_color_name
|
||||
})
|
||||
sql.connection.commit()
|
||||
|
||||
logger.info(f'Cached color {color_id} ({color_data["name"]}) with BrickLink ID {bricklink_color_id}')
|
||||
|
||||
return {
|
||||
'color_id': color_data['id'],
|
||||
'name': color_data['name'],
|
||||
'rgb': color_data.get('rgb'),
|
||||
'is_trans': color_data.get('is_trans', False),
|
||||
'bricklink_color_id': bricklink_color_id,
|
||||
'bricklink_color_name': bricklink_color_name
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not fetch color {color_id} from API: {e}')
|
||||
return None
|
||||
|
||||
# Download image for this part
|
||||
def download_image(self, image_url: str, /) -> None:
|
||||
if not image_url:
|
||||
return
|
||||
|
||||
# Create image_id from URL
|
||||
image_id, _ = os.path.splitext(os.path.basename(urlparse(image_url).path))
|
||||
|
||||
if not image_id:
|
||||
return
|
||||
|
||||
# Build path
|
||||
parts_folder = current_app.config['PARTS_FOLDER']
|
||||
extension = 'jpg' # Everything is saved as jpg
|
||||
path = os.path.join(
|
||||
current_app.static_folder, # type: ignore
|
||||
parts_folder,
|
||||
f'{image_id}.{extension}'
|
||||
)
|
||||
|
||||
# Avoid downloading if file exists
|
||||
if os.path.exists(path):
|
||||
return
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
|
||||
# Download the image
|
||||
try:
|
||||
response = requests.get(image_url, stream=True)
|
||||
if response.ok:
|
||||
with open(path, 'wb') as f:
|
||||
copyfileobj(response.raw, f)
|
||||
logger.info(f'Downloaded image for part {self.fields.part} color {self.fields.color} to {path}')
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not download image for part {self.fields.part} color {self.fields.color}: {e}')
|
||||
|
||||
# Load available colors for a part
|
||||
def load_colors(self, socket: 'BrickSocket', data: dict[str, Any], /) -> bool:
|
||||
# Check if individual parts are disabled
|
||||
if current_app.config.get('DISABLE_INDIVIDUAL_PARTS', False):
|
||||
socket.fail(message='Individual parts system is disabled.')
|
||||
return False
|
||||
|
||||
try:
|
||||
# Extract part number
|
||||
part_num = str(data.get('part', '')).strip()
|
||||
|
||||
if not part_num:
|
||||
raise ErrorException('Part number is required')
|
||||
|
||||
# Fetch available colors from Rebrickable
|
||||
import rebrick
|
||||
import json
|
||||
|
||||
rebrick.init(current_app.config['REBRICKABLE_API_KEY'])
|
||||
|
||||
# Setup progress tracking
|
||||
socket.progress_count = 0
|
||||
socket.progress_total = 2 # Fetch part info + fetch colors
|
||||
|
||||
try:
|
||||
# Get part information for the name
|
||||
socket.auto_progress(message='Fetching part information')
|
||||
part_response = rebrick.lego.get_part(part_num)
|
||||
part_data = json.loads(part_response.read())
|
||||
part_name = part_data.get('name', part_num)
|
||||
|
||||
# Get all available colors for this part
|
||||
socket.auto_progress(message='Fetching available colors')
|
||||
colors_response = rebrick.lego.get_part_colors(part_num)
|
||||
colors_data = json.loads(colors_response.read())
|
||||
|
||||
# Extract the results
|
||||
colors = colors_data.get('results', [])
|
||||
|
||||
if not colors:
|
||||
raise ErrorException(f'No colors found for part {part_num}')
|
||||
|
||||
# Download images locally if USE_REMOTE_IMAGES is False
|
||||
if not current_app.config.get('USE_REMOTE_IMAGES', False):
|
||||
# Add image downloads to progress
|
||||
socket.progress_total += len(colors)
|
||||
|
||||
for color in colors:
|
||||
image_url = color.get('part_img_url', '')
|
||||
if image_url:
|
||||
socket.auto_progress(message=f'Downloading image for {color.get("color_name", "color")}')
|
||||
try:
|
||||
self.download_image(image_url)
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not download image for part {part_num} color {color.get("color_id")}: {e}')
|
||||
|
||||
# Emit the part colors loaded event
|
||||
logger.info(f'Emitting {len(colors)} colors for part {part_num} ({part_name})')
|
||||
|
||||
socket.emit(
|
||||
'PART_COLORS_LOADED',
|
||||
{
|
||||
'part': part_num,
|
||||
'part_name': part_name,
|
||||
'colors': colors,
|
||||
'count': len(colors)
|
||||
}
|
||||
)
|
||||
|
||||
logger.info(f'Successfully loaded {len(colors)} colors for part {part_num}')
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
|
||||
# Provide helpful error message for printed/decorated parts
|
||||
if '404' in error_msg or 'Not Found' in error_msg:
|
||||
# Check if this might be a printed part (has letters/pattern code)
|
||||
base_part = ''.join(c for c in part_num if c.isdigit())
|
||||
|
||||
if base_part and base_part != part_num:
|
||||
raise ErrorException(
|
||||
f'Part {part_num} not found in Rebrickable. This appears to be a printed/decorated part. '
|
||||
f'Try searching for the base part number: {base_part}'
|
||||
)
|
||||
else:
|
||||
raise ErrorException(
|
||||
f'Part {part_num} not found in Rebrickable. '
|
||||
f'Please verify the part number is correct.'
|
||||
)
|
||||
else:
|
||||
raise ErrorException(
|
||||
f'Could not fetch colors for part {part_num}: {error_msg}'
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
socket.fail(message=f'Could not load part colors: {error_msg}')
|
||||
|
||||
if not isinstance(e, (NotFoundException, ErrorException)):
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
return False
|
||||
|
||||
# Add a new individual part
|
||||
def add(self, socket: 'BrickSocket', data: dict[str, Any], /) -> bool:
|
||||
# Check if individual parts are disabled
|
||||
if current_app.config.get('DISABLE_INDIVIDUAL_PARTS', False):
|
||||
socket.fail(message='Individual parts system is disabled.')
|
||||
return False
|
||||
|
||||
try:
|
||||
# Reset progress
|
||||
socket.progress_count = 0
|
||||
socket.progress_total = 3
|
||||
|
||||
socket.auto_progress(message='Validating part and color')
|
||||
|
||||
# Extract data
|
||||
part_num = str(data.get('part', '')).strip()
|
||||
color_id = int(data.get('color', 0))
|
||||
quantity = int(data.get('quantity', 1))
|
||||
|
||||
if not part_num:
|
||||
raise ErrorException('Part number is required')
|
||||
if color_id <= 0:
|
||||
raise ErrorException('Valid color ID is required')
|
||||
if quantity <= 0:
|
||||
raise ErrorException('Quantity must be greater than 0')
|
||||
|
||||
# Check if color info was pre-loaded (from load_colors)
|
||||
color_data = data.get('color_info', None)
|
||||
part_name = data.get('part_name', None)
|
||||
|
||||
# Validate part+color exists in rebrickable_parts
|
||||
# If not, fetch from Rebrickable or use pre-loaded data and insert
|
||||
sql = BrickSQL()
|
||||
check_query = """
|
||||
SELECT COUNT(*) FROM "rebrickable_parts"
|
||||
WHERE "part" = :part AND "color_id" = :color_id
|
||||
"""
|
||||
sql.cursor.execute(check_query, {'part': part_num, 'color_id': color_id})
|
||||
exists = sql.cursor.fetchone()[0] > 0
|
||||
|
||||
# Store image URL for downloading later
|
||||
image_url = None
|
||||
|
||||
if not exists:
|
||||
# Fetch full color information (with BrickLink mapping)
|
||||
socket.auto_progress(message='Fetching color information')
|
||||
full_color_info = IndividualPart.get_or_fetch_color(color_id)
|
||||
|
||||
# If we have pre-loaded color data, use it; otherwise fetch from Rebrickable
|
||||
if color_data and part_name:
|
||||
# Use pre-loaded data from get_part_colors() response
|
||||
socket.auto_progress(message='Using cached part info')
|
||||
|
||||
image_url = color_data.get('part_img_url', '')
|
||||
|
||||
# Insert into rebrickable_parts using the pre-loaded data
|
||||
insert_part_query = """
|
||||
INSERT OR IGNORE INTO "rebrickable_parts" (
|
||||
"part", "color_id", "color_name", "color_rgb", "color_transparent",
|
||||
"bricklink_color_id", "bricklink_color_name",
|
||||
"name", "image", "url"
|
||||
) VALUES (
|
||||
:part, :color_id, :color_name, :color_rgb, :color_transparent,
|
||||
:bricklink_color_id, :bricklink_color_name,
|
||||
:name, :image, :url
|
||||
)
|
||||
"""
|
||||
sql.cursor.execute(insert_part_query, {
|
||||
'part': part_num,
|
||||
'color_id': color_id,
|
||||
'color_name': color_data.get('color_name', ''),
|
||||
'color_rgb': full_color_info.get('rgb') if full_color_info else None,
|
||||
'color_transparent': full_color_info.get('is_trans') if full_color_info else None,
|
||||
'bricklink_color_id': full_color_info.get('bricklink_color_id') if full_color_info else None,
|
||||
'bricklink_color_name': full_color_info.get('bricklink_color_name') if full_color_info else None,
|
||||
'name': part_name,
|
||||
'image': image_url,
|
||||
'url': f'https://rebrickable.com/parts/{part_num}/'
|
||||
})
|
||||
else:
|
||||
# Fetch from Rebrickable (fallback for old workflow)
|
||||
socket.auto_progress(message='Fetching part info from Rebrickable')
|
||||
import rebrick
|
||||
import json
|
||||
|
||||
# Initialize rebrick with API key
|
||||
rebrick.init(current_app.config['REBRICKABLE_API_KEY'])
|
||||
|
||||
try:
|
||||
# Get part information
|
||||
part_info = json.loads(rebrick.lego.get_part(part_num).read())
|
||||
|
||||
# Get color information (this also caches it in rebrickable_colors)
|
||||
# full_color_info already fetched above, but get again to be sure
|
||||
if not full_color_info:
|
||||
full_color_info = IndividualPart.get_or_fetch_color(color_id)
|
||||
|
||||
# Get part+color specific info (for the image)
|
||||
part_color_info = json.loads(rebrick.lego.get_part_color(part_num, color_id).read())
|
||||
|
||||
# Get image URL
|
||||
image_url = part_color_info.get('part_img_url', part_info.get('part_img_url', ''))
|
||||
|
||||
# Insert into rebrickable_parts with BrickLink color info
|
||||
insert_part_query = """
|
||||
INSERT OR IGNORE INTO "rebrickable_parts" (
|
||||
"part", "color_id", "color_name", "color_rgb", "color_transparent",
|
||||
"bricklink_color_id", "bricklink_color_name",
|
||||
"name", "image", "url"
|
||||
) VALUES (
|
||||
:part, :color_id, :color_name, :color_rgb, :color_transparent,
|
||||
:bricklink_color_id, :bricklink_color_name,
|
||||
:name, :image, :url
|
||||
)
|
||||
"""
|
||||
sql.cursor.execute(insert_part_query, {
|
||||
'part': part_info['part_num'],
|
||||
'color_id': full_color_info['color_id'] if full_color_info else color_id,
|
||||
'color_name': full_color_info['name'] if full_color_info else '',
|
||||
'color_rgb': full_color_info['rgb'] if full_color_info else None,
|
||||
'color_transparent': full_color_info['is_trans'] if full_color_info else None,
|
||||
'bricklink_color_id': full_color_info.get('bricklink_color_id') if full_color_info else None,
|
||||
'bricklink_color_name': full_color_info.get('bricklink_color_name') if full_color_info else None,
|
||||
'name': part_info['name'],
|
||||
'image': image_url,
|
||||
'url': part_info['part_url']
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
|
||||
# Provide helpful error message for printed/decorated parts
|
||||
if '404' in error_msg or 'Not Found' in error_msg:
|
||||
base_part = ''.join(c for c in part_num if c.isdigit())
|
||||
|
||||
if base_part and base_part != part_num:
|
||||
raise ErrorException(
|
||||
f'Part {part_num} with color {color_id} not found in Rebrickable. '
|
||||
f'This appears to be a printed/decorated part. '
|
||||
f'Try using the base part number: {base_part}'
|
||||
)
|
||||
else:
|
||||
raise ErrorException(
|
||||
f'Part {part_num} with color {color_id} not found in Rebrickable. '
|
||||
f'Please verify the part number is correct.'
|
||||
)
|
||||
else:
|
||||
raise ErrorException(
|
||||
f'Part {part_num} with color {color_id} not found in Rebrickable: {error_msg}'
|
||||
)
|
||||
else:
|
||||
# Part already exists in rebrickable_parts, get the image URL
|
||||
sql.cursor.execute(
|
||||
'SELECT "image" FROM "rebrickable_parts" WHERE "part" = :part AND "color_id" = :color_id',
|
||||
{'part': part_num, 'color_id': color_id}
|
||||
)
|
||||
result = sql.cursor.fetchone()
|
||||
if result and result[0]:
|
||||
image_url = result[0]
|
||||
|
||||
# Generate UUID and insert individual part
|
||||
socket.auto_progress(message='Adding part to collection')
|
||||
part_id = str(uuid4())
|
||||
|
||||
# Get storage and purchase location
|
||||
storage = BrickSetStorageList.get(
|
||||
data.get('storage', ''),
|
||||
allow_none=True
|
||||
)
|
||||
purchase_location = BrickSetPurchaseLocationList.get(
|
||||
data.get('purchase_location', ''),
|
||||
allow_none=True
|
||||
)
|
||||
|
||||
# Set fields
|
||||
self.fields.id = part_id
|
||||
self.fields.part = part_num
|
||||
self.fields.color = color_id
|
||||
self.fields.quantity = quantity
|
||||
self.fields.missing = 0
|
||||
self.fields.damaged = 0
|
||||
self.fields.checked = 0
|
||||
self.fields.description = data.get('description', '')
|
||||
self.fields.storage = storage.fields.id if storage else None
|
||||
self.fields.purchase_location = purchase_location.fields.id if purchase_location else None
|
||||
self.fields.purchase_date = data.get('purchase_date', None)
|
||||
self.fields.purchase_price = data.get('purchase_price', None)
|
||||
|
||||
# Insert into database
|
||||
self.insert(commit=False, no_defer=True)
|
||||
|
||||
# Save owners
|
||||
owners: list[str] = list(data.get('owners', []))
|
||||
for owner_id in owners:
|
||||
owner = BrickSetOwnerList.get(owner_id)
|
||||
owner.update_individual_part_state(self, state=True)
|
||||
|
||||
# Save tags
|
||||
tags: list[str] = list(data.get('tags', []))
|
||||
for tag_id in tags:
|
||||
tag = BrickSetTagList.get(tag_id)
|
||||
tag.update_individual_part_state(self, state=True)
|
||||
|
||||
# Commit
|
||||
sql.connection.commit()
|
||||
|
||||
# Download image if we have a URL
|
||||
if image_url:
|
||||
try:
|
||||
self.download_image(image_url)
|
||||
except Exception as e:
|
||||
# Don't fail the whole operation if image download fails
|
||||
logger.warning(f'Could not download image for part {part_num} color {color_id}: {e}')
|
||||
|
||||
# Get color name for success message
|
||||
color_name = 'Unknown'
|
||||
if color_data and color_data.get('color_name'):
|
||||
color_name = color_data.get('color_name')
|
||||
elif full_color_info and full_color_info.get('name'):
|
||||
color_name = full_color_info.get('name')
|
||||
|
||||
# Generate link to part details page
|
||||
part_url = url_for('part.details', part=part_num, color=color_id)
|
||||
|
||||
socket.complete(
|
||||
message=f'Successfully added part {part_num} in {color_name} (<a href="{part_url}">View details</a>)'
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
if 'Individual parts system is disabled' in error_msg:
|
||||
socket.fail(message=error_msg)
|
||||
else:
|
||||
socket.fail(
|
||||
message=f'Could not add individual part: {error_msg}'
|
||||
)
|
||||
|
||||
if not isinstance(e, (NotFoundException, ErrorException)):
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
return False
|
||||
|
||||
# Update a field
|
||||
def update_field(self, field: str, value: Any, /) -> Self:
|
||||
setattr(self.fields, field, value)
|
||||
|
||||
# Use a specific update query for each field
|
||||
sql = BrickSQL()
|
||||
update_query = f"""
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET "{field}" = :value
|
||||
WHERE "id" = :id
|
||||
"""
|
||||
sql.cursor.execute(update_query, {
|
||||
'id': self.fields.id,
|
||||
'value': value
|
||||
})
|
||||
sql.commit()
|
||||
|
||||
return self
|
||||
|
||||
# Update problem count (missing/damaged)
|
||||
def update_problem(self, problem: str, data: dict[str, Any], /) -> int:
|
||||
# Handle both 'value' key and 'amount' key
|
||||
amount: str | int = data.get('value', data.get('amount', '')) # type: ignore
|
||||
|
||||
# We need a positive integer
|
||||
try:
|
||||
if amount == '':
|
||||
amount = 0
|
||||
|
||||
amount = int(amount)
|
||||
|
||||
if amount < 0:
|
||||
amount = 0
|
||||
except Exception:
|
||||
raise ErrorException(f'"{amount}" is not a valid integer')
|
||||
|
||||
if amount < 0:
|
||||
raise ErrorException('Cannot set a negative amount')
|
||||
|
||||
setattr(self.fields, problem, amount)
|
||||
|
||||
BrickSQL().execute_and_commit(
|
||||
f'individual_part/update/{problem}',
|
||||
parameters={
|
||||
'id': self.fields.id,
|
||||
problem: amount
|
||||
}
|
||||
)
|
||||
|
||||
return amount
|
||||
|
||||
# Update checked status
|
||||
def update_checked(self, data: dict[str, Any], /) -> bool:
|
||||
# Handle both direct 'checked' key and changer.js 'value' key format
|
||||
if data:
|
||||
checked = data.get('checked', data.get('value', False))
|
||||
else:
|
||||
checked = False
|
||||
|
||||
checked = bool(checked)
|
||||
self.fields.checked = 1 if checked else 0
|
||||
|
||||
BrickSQL().execute_and_commit(
|
||||
'individual_part/update/checked',
|
||||
parameters={
|
||||
'id': self.fields.id,
|
||||
'checked': self.fields.checked
|
||||
}
|
||||
)
|
||||
|
||||
return checked
|
||||
|
||||
# URL methods
|
||||
def url(self, /) -> str:
|
||||
return url_for('individual_part.details', id=self.fields.id)
|
||||
|
||||
def url_for_quantity(self, /) -> str:
|
||||
return url_for('individual_part.update_quantity', id=self.fields.id)
|
||||
|
||||
def url_for_description(self, /) -> str:
|
||||
return url_for('individual_part.update_description', id=self.fields.id)
|
||||
|
||||
def url_for_problem(self, problem: str, /) -> str:
|
||||
if problem == 'missing':
|
||||
return url_for('individual_part.update_missing', id=self.fields.id)
|
||||
elif problem == 'damaged':
|
||||
return url_for('individual_part.update_damaged', id=self.fields.id)
|
||||
return ''
|
||||
|
||||
def url_for_checked(self, /) -> str:
|
||||
return url_for('individual_part.update_checked', id=self.fields.id)
|
||||
|
||||
def url_for_delete(self, /) -> str:
|
||||
return url_for('individual_part.delete_part', id=self.fields.id)
|
||||
|
||||
def url_for_image(self, /) -> str:
|
||||
# Check if we should use remote images
|
||||
if current_app.config.get('USE_REMOTE_IMAGES', False):
|
||||
# Return remote URL directly
|
||||
if hasattr(self.fields, 'image') and self.fields.image:
|
||||
return self.fields.image
|
||||
else:
|
||||
return current_app.config.get('REBRICKABLE_IMAGE_NIL', '')
|
||||
else:
|
||||
# Use local images
|
||||
from .rebrickable_image import RebrickableImage
|
||||
|
||||
if hasattr(self.fields, 'image') and self.fields.image:
|
||||
# Extract image_id from URL
|
||||
image_id, _ = os.path.splitext(os.path.basename(urlparse(self.fields.image).path))
|
||||
|
||||
if image_id:
|
||||
# Return local static URL using RebrickableImage helper
|
||||
return RebrickableImage.static_url(image_id, 'PARTS_FOLDER')
|
||||
|
||||
# Fallback to nil image
|
||||
return RebrickableImage.static_url(RebrickableImage.nil_name(), 'PARTS_FOLDER')
|
||||
@@ -0,0 +1,93 @@
|
||||
import logging
|
||||
from typing import Self, TYPE_CHECKING
|
||||
|
||||
from .record_list import BrickRecordList
|
||||
from .individual_part import IndividualPart
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .set_storage import BrickSetStorage
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# List of individual parts
|
||||
class IndividualPartList(BrickRecordList):
|
||||
# Queries
|
||||
list_query: str = 'individual_part/list/all'
|
||||
by_part_query: str = 'individual_part/list/by_part'
|
||||
by_color_query: str = 'individual_part/list/by_color'
|
||||
by_part_and_color_query: str = 'individual_part/list/by_part_and_color'
|
||||
by_storage_query: str = 'individual_part/list/by_storage'
|
||||
using_storage_query: str = 'individual_part/list/using_storage'
|
||||
without_storage_query: str = 'individual_part/list/without_storage'
|
||||
problem_query: str = 'individual_part/list/problem'
|
||||
|
||||
# Get all individual parts
|
||||
def all(self, /) -> Self:
|
||||
self.list(override_query=self.list_query)
|
||||
return self
|
||||
|
||||
# Get individual parts by part number
|
||||
def by_part(self, part: str, /) -> Self:
|
||||
self.fields.part = part
|
||||
self.list(override_query=self.by_part_query)
|
||||
return self
|
||||
|
||||
# Get individual parts by color
|
||||
def by_color(self, color_id: int, /) -> Self:
|
||||
self.fields.color = color_id
|
||||
self.list(override_query=self.by_color_query)
|
||||
return self
|
||||
|
||||
# Get individual parts by part number and color
|
||||
def by_part_and_color(self, part: str, color_id: int, /) -> Self:
|
||||
self.fields.part = part
|
||||
self.fields.color = color_id
|
||||
self.list(override_query=self.by_part_and_color_query)
|
||||
return self
|
||||
|
||||
# Get individual parts by storage location
|
||||
def by_storage(self, storage: 'BrickSetStorage', /) -> Self:
|
||||
self.fields.storage = storage.fields.id
|
||||
self.list(override_query=self.by_storage_query)
|
||||
return self
|
||||
|
||||
# Get individual parts using a specific storage location
|
||||
def using_storage(self, storage: 'BrickSetStorage', /) -> Self:
|
||||
self.fields.storage = storage.fields.id
|
||||
self.list(override_query=self.using_storage_query)
|
||||
return self
|
||||
|
||||
# Get individual parts without storage
|
||||
def without_storage(self, /) -> Self:
|
||||
self.list(override_query=self.without_storage_query)
|
||||
return self
|
||||
|
||||
# Get individual parts with problems (missing or damaged)
|
||||
def with_problems(self, /) -> Self:
|
||||
self.list(override_query=self.problem_query)
|
||||
return self
|
||||
|
||||
# Base individual part list
|
||||
def list(
|
||||
self,
|
||||
/,
|
||||
*,
|
||||
override_query: str | None = None,
|
||||
order: str | None = None,
|
||||
limit: int | None = None,
|
||||
**context,
|
||||
) -> None:
|
||||
# Load the individual parts from the database
|
||||
for record in super().select(
|
||||
override_query=override_query,
|
||||
order=order,
|
||||
limit=limit,
|
||||
**context
|
||||
):
|
||||
individual_part = IndividualPart(record=record)
|
||||
self.records.append(individual_part)
|
||||
|
||||
# Set the record class
|
||||
def set_record_class(self, /) -> None:
|
||||
self.record_class = IndividualPart
|
||||
@@ -0,0 +1,340 @@
|
||||
import logging
|
||||
import traceback
|
||||
from typing import Any, Self, TYPE_CHECKING
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import url_for
|
||||
|
||||
from .exceptions import NotFoundException, DatabaseException, ErrorException
|
||||
from .individual_part import IndividualPart
|
||||
from .record import BrickRecord, format_timestamp
|
||||
from .set_owner_list import BrickSetOwnerList
|
||||
from .set_purchase_location_list import BrickSetPurchaseLocationList
|
||||
from .set_storage_list import BrickSetStorageList
|
||||
from .set_tag_list import BrickSetTagList
|
||||
from .sql import BrickSQL
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .socket import BrickSocket
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Individual part lot (collection/batch of individual parts added together)
|
||||
class IndividualPartLot(BrickRecord):
|
||||
# Queries
|
||||
select_query: str = 'individual_part_lot/select/by_id'
|
||||
insert_query: str = 'individual_part_lot/insert'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
/,
|
||||
*,
|
||||
record: Any | None = None
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
# Ingest the record if it has one
|
||||
if record is not None:
|
||||
self.ingest(record)
|
||||
|
||||
# Select a specific lot by UUID
|
||||
def select_by_id(self, id: str, /) -> Self:
|
||||
self.fields.id = id
|
||||
if not self.select(override_query=self.select_query):
|
||||
raise NotFoundException(
|
||||
'Individual part lot with id "{id}" not found'.format(id=id)
|
||||
)
|
||||
return self
|
||||
|
||||
# Delete a lot and all its parts
|
||||
def delete(self, /) -> None:
|
||||
BrickSQL().executescript(
|
||||
'individual_part_lot/delete',
|
||||
id=self.fields.id
|
||||
)
|
||||
|
||||
# Get the URL for this lot
|
||||
def url(self, /) -> str:
|
||||
return url_for('individual_part.lot_details', lot_id=self.fields.id)
|
||||
|
||||
# String representation for debugging
|
||||
def __repr__(self, /) -> str:
|
||||
"""String representation for debugging"""
|
||||
name = getattr(self.fields, 'name', 'Unnamed') or 'Unnamed'
|
||||
lot_id = getattr(self.fields, 'id', 'unknown')
|
||||
# Try to get part_count if available (from optimized query)
|
||||
part_count = getattr(self.fields, 'part_count', '?')
|
||||
return f'<IndividualPartLot "{name}" ({part_count} parts) id:{lot_id[:8]}...>'
|
||||
|
||||
# Format created date
|
||||
def created_date_formatted(self, /) -> str:
|
||||
"""Format the created date for display"""
|
||||
return format_timestamp(self.fields.created_date)
|
||||
|
||||
# Format purchase date
|
||||
def purchase_date_formatted(self, /) -> str:
|
||||
"""Format the purchase date for display"""
|
||||
return format_timestamp(self.fields.purchase_date)
|
||||
|
||||
# Get all parts in this lot
|
||||
def parts(self, /) -> list['IndividualPart']:
|
||||
"""Get all individual parts that belong to this lot"""
|
||||
sql = BrickSQL()
|
||||
parts_data = sql.fetchall('individual_part_lot/list/parts', lot_id=self.fields.id)
|
||||
|
||||
# Convert to list of IndividualPart objects using ingest()
|
||||
return [IndividualPart(record=record) for record in parts_data]
|
||||
|
||||
# Create a new lot with parts from cart
|
||||
def create(self, socket: 'BrickSocket', data: dict[str, Any], /) -> bool:
|
||||
"""
|
||||
Create a new individual part lot with multiple parts.
|
||||
|
||||
Expected data format:
|
||||
{
|
||||
'cart': [
|
||||
{
|
||||
'part': '3001',
|
||||
'part_name': 'Brick 2 x 4',
|
||||
'color_id': 1,
|
||||
'color_name': 'White',
|
||||
'quantity': 10,
|
||||
'color_info': {...}
|
||||
},
|
||||
...
|
||||
],
|
||||
'name': 'Optional lot name',
|
||||
'description': 'Optional lot description',
|
||||
'storage': 'storage_id',
|
||||
'purchase_location': 'purchase_location_id',
|
||||
'purchase_date': timestamp,
|
||||
'purchase_price': 0.0,
|
||||
'owners': ['owner_id1', ...],
|
||||
'tags': ['tag_id1', ...]
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# Validate cart data
|
||||
cart = data.get('cart', [])
|
||||
if not cart or not isinstance(cart, list):
|
||||
raise ErrorException('Cart is empty or invalid')
|
||||
|
||||
socket.auto_progress(
|
||||
message=f'Creating lot with {len(cart)} parts',
|
||||
increment_total=True
|
||||
)
|
||||
|
||||
# Generate UUID for the lot
|
||||
lot_id = str(uuid4())
|
||||
self.fields.id = lot_id
|
||||
|
||||
# Set lot metadata
|
||||
self.fields.name = data.get('name', None)
|
||||
self.fields.description = data.get('description', None)
|
||||
self.fields.created_date = datetime.now().timestamp()
|
||||
|
||||
# Get storage
|
||||
storage = BrickSetStorageList.get(
|
||||
data.get('storage', ''),
|
||||
allow_none=True
|
||||
)
|
||||
self.fields.storage = storage.fields.id if storage else None
|
||||
|
||||
# Get purchase location
|
||||
purchase_location = BrickSetPurchaseLocationList.get(
|
||||
data.get('purchase_location', ''),
|
||||
allow_none=True
|
||||
)
|
||||
self.fields.purchase_location = purchase_location.fields.id if purchase_location else None
|
||||
|
||||
# Set purchase info
|
||||
self.fields.purchase_date = data.get('purchase_date', None)
|
||||
self.fields.purchase_price = data.get('purchase_price', None)
|
||||
|
||||
# Insert the lot record
|
||||
socket.auto_progress(
|
||||
message='Inserting lot into database',
|
||||
increment_total=True
|
||||
)
|
||||
self.insert(commit=False)
|
||||
|
||||
# Save owners
|
||||
owners: list[str] = list(data.get('owners', []))
|
||||
for owner_id in owners:
|
||||
owner = BrickSetOwnerList.get(owner_id)
|
||||
# Insert into junction table
|
||||
sql = BrickSQL()
|
||||
sql.cursor.execute(
|
||||
'INSERT INTO "bricktracker_individual_part_lot_owners" ("id") VALUES (:id)',
|
||||
{'id': lot_id}
|
||||
)
|
||||
|
||||
# Save tags
|
||||
tags: list[str] = list(data.get('tags', []))
|
||||
for tag_id in tags:
|
||||
tag = BrickSetTagList.get(tag_id)
|
||||
# Insert into junction table
|
||||
sql = BrickSQL()
|
||||
sql.cursor.execute(
|
||||
'INSERT INTO "bricktracker_individual_part_lot_tags" ("id") VALUES (:id)',
|
||||
{'id': lot_id}
|
||||
)
|
||||
|
||||
# Add all parts from cart
|
||||
socket.auto_progress(
|
||||
message=f'Adding {len(cart)} parts to lot',
|
||||
increment_total=True
|
||||
)
|
||||
|
||||
for idx, cart_item in enumerate(cart):
|
||||
part_num = cart_item.get('part')
|
||||
color_id = cart_item.get('color_id')
|
||||
quantity = cart_item.get('quantity', 1)
|
||||
color_info = cart_item.get('color_info', {})
|
||||
|
||||
socket.auto_progress(
|
||||
message=f'Adding part {idx + 1}/{len(cart)}: {part_num} in {cart_item.get("color_name", "unknown color")}',
|
||||
increment_total=True
|
||||
)
|
||||
|
||||
# Create individual part with lot_id
|
||||
part_uuid = str(uuid4())
|
||||
|
||||
# Use the add method but with lot_id
|
||||
# We need to insert the part with the lot_id
|
||||
sql = BrickSQL()
|
||||
|
||||
# First ensure the part exists in rebrickable_parts
|
||||
IndividualPart.get_or_fetch_color(color_id)
|
||||
|
||||
# Insert the part with lot_id (NO individual metadata - inherited from lot)
|
||||
insert_query = """
|
||||
INSERT INTO "bricktracker_individual_parts" (
|
||||
"id",
|
||||
"part",
|
||||
"color",
|
||||
"quantity",
|
||||
"missing",
|
||||
"damaged",
|
||||
"checked",
|
||||
"description",
|
||||
"storage",
|
||||
"purchase_location",
|
||||
"purchase_date",
|
||||
"purchase_price",
|
||||
"lot_id"
|
||||
) VALUES (
|
||||
:id,
|
||||
:part,
|
||||
:color,
|
||||
:quantity,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
:lot_id
|
||||
)
|
||||
"""
|
||||
|
||||
sql.cursor.execute(insert_query, {
|
||||
'id': part_uuid,
|
||||
'part': part_num,
|
||||
'color': color_id,
|
||||
'quantity': quantity,
|
||||
'lot_id': lot_id
|
||||
})
|
||||
|
||||
# Ensure part data is in rebrickable_parts
|
||||
try:
|
||||
# Check if part exists
|
||||
check_query = """
|
||||
SELECT COUNT(*) FROM "rebrickable_parts"
|
||||
WHERE "part" = :part AND "color_id" = :color_id
|
||||
"""
|
||||
sql.cursor.execute(check_query, {'part': part_num, 'color_id': color_id})
|
||||
exists = sql.cursor.fetchone()[0] > 0
|
||||
|
||||
if not exists:
|
||||
# Insert part data
|
||||
part_name = cart_item.get('part_name', '')
|
||||
color_name = cart_item.get('color_name', '')
|
||||
|
||||
insert_part_query = """
|
||||
INSERT OR IGNORE INTO "rebrickable_parts" (
|
||||
"part",
|
||||
"name",
|
||||
"color_id",
|
||||
"color_name",
|
||||
"color_rgb",
|
||||
"color_transparent",
|
||||
"image",
|
||||
"url",
|
||||
"bricklink_color_id",
|
||||
"bricklink_color_name"
|
||||
) VALUES (
|
||||
:part,
|
||||
:name,
|
||||
:color_id,
|
||||
:color_name,
|
||||
:color_rgb,
|
||||
:color_transparent,
|
||||
:image,
|
||||
:url,
|
||||
:bricklink_color_id,
|
||||
:bricklink_color_name
|
||||
)
|
||||
"""
|
||||
|
||||
sql.cursor.execute(insert_part_query, {
|
||||
'part': part_num,
|
||||
'name': part_name,
|
||||
'color_id': color_id,
|
||||
'color_name': color_name,
|
||||
'color_rgb': color_info.get('rgb', ''),
|
||||
'color_transparent': color_info.get('is_trans', False),
|
||||
'image': color_info.get('part_img_url', ''),
|
||||
'url': f'https://rebrickable.com/parts/{part_num}/',
|
||||
'bricklink_color_id': color_info.get('bricklink_color_id', None),
|
||||
'bricklink_color_name': color_info.get('bricklink_color_name', None)
|
||||
})
|
||||
except Exception as e:
|
||||
logger.warning(f'Could not ensure part data for {part_num}/{color_id}: {e}')
|
||||
|
||||
# Commit all changes
|
||||
socket.auto_progress(
|
||||
message='Committing changes to database',
|
||||
increment_total=True
|
||||
)
|
||||
sql.commit()
|
||||
|
||||
socket.auto_progress(
|
||||
message=f'Lot created successfully with {len(cart)} parts',
|
||||
increment_total=True
|
||||
)
|
||||
|
||||
# Complete with success message and lot URL
|
||||
lot_url = self.url()
|
||||
socket.complete(
|
||||
message=f'Successfully created lot with {len(cart)} parts. <a href="{lot_url}">View lot</a>',
|
||||
data={
|
||||
'lot_id': lot_id,
|
||||
'lot_url': lot_url
|
||||
}
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
except ErrorException as e:
|
||||
socket.fail(message=str(e))
|
||||
logger.error(f'Error creating lot: {e}')
|
||||
return False
|
||||
except Exception as e:
|
||||
socket.fail(message=f'Unexpected error creating lot: {str(e)}')
|
||||
logger.error(f'Unexpected error creating lot: {e}')
|
||||
logger.error(traceback.format_exc())
|
||||
return False
|
||||
@@ -0,0 +1,45 @@
|
||||
import logging
|
||||
from typing import Self, TYPE_CHECKING
|
||||
|
||||
from .record_list import BrickRecordList
|
||||
from .individual_part_lot import IndividualPartLot
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .set_storage import BrickSetStorage
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# List of individual part lots
|
||||
class IndividualPartLotList(BrickRecordList):
|
||||
# Queries
|
||||
list_query: str = 'individual_part_lot/list/all'
|
||||
|
||||
# Get all individual part lots
|
||||
def all(self, /) -> Self:
|
||||
self.list(override_query=self.list_query)
|
||||
return self
|
||||
|
||||
# Base individual part lot list
|
||||
def list(
|
||||
self,
|
||||
/,
|
||||
*,
|
||||
override_query: str | None = None,
|
||||
order: str | None = None,
|
||||
limit: int | None = None,
|
||||
**context,
|
||||
) -> None:
|
||||
# Load the individual part lots from the database
|
||||
for record in super().select(
|
||||
override_query=override_query,
|
||||
order=order,
|
||||
limit=limit,
|
||||
**context
|
||||
):
|
||||
lot = IndividualPartLot(record=record)
|
||||
self.records.append(lot)
|
||||
|
||||
# Set the record class
|
||||
def set_record_class(self, /) -> None:
|
||||
self.record_class = IndividualPartLot
|
||||
+153
-30
@@ -9,6 +9,8 @@ from .exceptions import DatabaseException, ErrorException, NotFoundException
|
||||
from .record import BrickRecord
|
||||
from .sql import BrickSQL
|
||||
if TYPE_CHECKING:
|
||||
from .individual_minifigure import IndividualMinifigure
|
||||
from .individual_part import IndividualPart
|
||||
from .set import BrickSet
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -18,16 +20,24 @@ logger = logging.getLogger(__name__)
|
||||
class BrickMetadata(BrickRecord):
|
||||
kind: str
|
||||
|
||||
# Set state endpoint
|
||||
set_state_endpoint: str
|
||||
# Endpoints (optional, not all metadata types use all of these)
|
||||
set_state_endpoint: str = ''
|
||||
individual_minifigure_state_endpoint: str = ''
|
||||
individual_minifigure_value_endpoint: str = ''
|
||||
individual_part_state_endpoint: str = ''
|
||||
individual_part_value_endpoint: str = ''
|
||||
|
||||
# Queries
|
||||
delete_query: str
|
||||
insert_query: str
|
||||
select_query: str
|
||||
update_field_query: str
|
||||
update_set_state_query: str
|
||||
update_set_value_query: str
|
||||
update_set_state_query: str = ''
|
||||
update_set_value_query: str = ''
|
||||
update_individual_minifigure_state_query: str = ''
|
||||
update_individual_minifigure_value_query: str = ''
|
||||
update_individual_part_state_query: str = ''
|
||||
update_individual_part_value_query: str = ''
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -106,6 +116,36 @@ class BrickMetadata(BrickRecord):
|
||||
metadata_id=self.fields.id
|
||||
)
|
||||
|
||||
# URL to change the selected state of this metadata item for an individual minifigure
|
||||
def url_for_individual_minifigure_state(self, id: str, /) -> str:
|
||||
return url_for(
|
||||
self.individual_minifigure_state_endpoint,
|
||||
id=id,
|
||||
metadata_id=self.fields.id
|
||||
)
|
||||
|
||||
# URL to change the value for an individual minifigure
|
||||
def url_for_individual_minifigure_value(self, id: str, /) -> str:
|
||||
return url_for(
|
||||
self.individual_minifigure_value_endpoint,
|
||||
id=id
|
||||
)
|
||||
|
||||
# URL to change the selected state of this metadata item for an individual part
|
||||
def url_for_individual_part_state(self, id: str, /) -> str:
|
||||
return url_for(
|
||||
self.individual_part_state_endpoint,
|
||||
id=id,
|
||||
metadata_id=self.fields.id
|
||||
)
|
||||
|
||||
# URL to change the value for an individual part
|
||||
def url_for_individual_part_value(self, id: str, /) -> str:
|
||||
return url_for(
|
||||
self.individual_part_value_endpoint,
|
||||
id=id
|
||||
)
|
||||
|
||||
# Select a specific metadata (with an id)
|
||||
def select_specific(self, id: str, /) -> Self:
|
||||
# Save the parameters to the fields
|
||||
@@ -175,6 +215,40 @@ class BrickMetadata(BrickRecord):
|
||||
|
||||
return value
|
||||
|
||||
# Generic method to update state for any entity type
|
||||
def _update_entity_state(
|
||||
self,
|
||||
entity_type: str,
|
||||
entity_id: str,
|
||||
entity_name: str,
|
||||
query: str,
|
||||
/,
|
||||
*,
|
||||
json: Any | None = None,
|
||||
state: Any | None = None
|
||||
) -> Any:
|
||||
"""Generic state update logic for sets, minifigures, and parts"""
|
||||
if state is None and json is not None:
|
||||
state = json.get('value', False)
|
||||
|
||||
parameters = self.sql_parameters()
|
||||
parameters['id'] = entity_id
|
||||
parameters['state'] = state
|
||||
|
||||
rows, _ = BrickSQL().execute_and_commit(
|
||||
query,
|
||||
parameters=parameters,
|
||||
name=self.as_column(),
|
||||
)
|
||||
|
||||
if rows != 1:
|
||||
raise DatabaseException(f'Could not update the {self.kind} "{self.fields.name}" state for {entity_type} {entity_name} ({entity_id})')
|
||||
|
||||
# Info
|
||||
logger.info(f'{self.kind.capitalize()} "{self.fields.name}" state changed to "{state}" for {entity_type} {entity_name} ({entity_id})')
|
||||
|
||||
return state
|
||||
|
||||
# Update the selected state of this metadata item for a set
|
||||
def update_set_state(
|
||||
self,
|
||||
@@ -184,37 +258,86 @@ class BrickMetadata(BrickRecord):
|
||||
json: Any | None = None,
|
||||
state: Any | None = None
|
||||
) -> Any:
|
||||
if state is None and json is not None:
|
||||
state = json.get('value', False)
|
||||
|
||||
parameters = self.sql_parameters()
|
||||
parameters['set_id'] = brickset.fields.id
|
||||
parameters['state'] = state
|
||||
|
||||
rows, _ = BrickSQL().execute_and_commit(
|
||||
return self._update_entity_state(
|
||||
'set',
|
||||
brickset.fields.id,
|
||||
brickset.fields.set,
|
||||
self.update_set_state_query,
|
||||
parameters=parameters,
|
||||
name=self.as_column(),
|
||||
json=json,
|
||||
state=state
|
||||
)
|
||||
|
||||
if rows != 1:
|
||||
raise DatabaseException('Could not update the {kind} "{name}" state for set {set} ({id})'.format( # noqa: E501
|
||||
kind=self.kind,
|
||||
name=self.fields.name,
|
||||
set=brickset.fields.set,
|
||||
id=brickset.fields.id,
|
||||
))
|
||||
# Check if this metadata has a specific individual minifigure
|
||||
def has_individual_minifigure(
|
||||
self,
|
||||
individual_minifigure: 'IndividualMinifigure',
|
||||
/,
|
||||
) -> bool:
|
||||
"""Check if this owner/tag/status is assigned to a individual minifigure"""
|
||||
# Determine the table name based on metadata type
|
||||
table_name = f'bricktracker_individual_minifigure_{self.kind}s'
|
||||
column_name = f'{self.kind}_{self.fields.id}'
|
||||
|
||||
# Info
|
||||
logger.info('{kind} "{name}" state changed to "{state}" for set {set} ({id})'.format( # noqa: E501
|
||||
kind=self.kind,
|
||||
name=self.fields.name,
|
||||
state=state,
|
||||
set=brickset.fields.set,
|
||||
id=brickset.fields.id,
|
||||
))
|
||||
# Query to check if the relationship exists using raw SQL
|
||||
sql = BrickSQL()
|
||||
query = f'SELECT COUNT(*) as count FROM "{table_name}" WHERE "id" = ? AND "{column_name}" = 1'
|
||||
result = sql.cursor.execute(query, (individual_minifigure.fields.id,)).fetchone()
|
||||
|
||||
return state
|
||||
return result and result['count'] > 0
|
||||
|
||||
# Update the selected state of this metadata item for a individual minifigure
|
||||
def update_individual_minifigure_state(
|
||||
self,
|
||||
individual_minifigure: 'IndividualMinifigure',
|
||||
/,
|
||||
*,
|
||||
json: Any | None = None,
|
||||
state: Any | None = None
|
||||
) -> Any:
|
||||
return self._update_entity_state(
|
||||
'individual minifigure',
|
||||
individual_minifigure.fields.id,
|
||||
individual_minifigure.fields.figure,
|
||||
self.update_individual_minifigure_state_query,
|
||||
json=json,
|
||||
state=state
|
||||
)
|
||||
|
||||
# Check if this metadata has a specific individual part
|
||||
def has_individual_part(
|
||||
self,
|
||||
individual_part: 'IndividualPart',
|
||||
/,
|
||||
) -> bool:
|
||||
"""Check if this owner/tag/status is assigned to an individual part"""
|
||||
# Determine the table name based on metadata type
|
||||
table_name = f'bricktracker_individual_part_{self.kind}s'
|
||||
column_name = f'{self.kind}_{self.fields.id}'
|
||||
|
||||
# Query to check if the relationship exists using raw SQL
|
||||
sql = BrickSQL()
|
||||
query = f'SELECT COUNT(*) as count FROM "{table_name}" WHERE "id" = ? AND "{column_name}" = 1'
|
||||
result = sql.cursor.execute(query, (individual_part.fields.id,)).fetchone()
|
||||
|
||||
return result and result['count'] > 0
|
||||
|
||||
# Update the selected state of this metadata item for an individual part
|
||||
def update_individual_part_state(
|
||||
self,
|
||||
individual_part: 'IndividualPart',
|
||||
/,
|
||||
*,
|
||||
json: Any | None = None,
|
||||
state: Any | None = None
|
||||
) -> Any:
|
||||
return self._update_entity_state(
|
||||
'individual part',
|
||||
individual_part.fields.id,
|
||||
f'{individual_part.fields.part} color {individual_part.fields.color}',
|
||||
self.update_individual_part_state_query,
|
||||
json=json,
|
||||
state=state
|
||||
)
|
||||
|
||||
# Update the selected value of this metadata item for a set
|
||||
def update_set_value(
|
||||
|
||||
@@ -39,9 +39,10 @@ class BrickMetadataList(BrickRecordList[T]):
|
||||
# Queries
|
||||
select_query: str
|
||||
|
||||
# Set endpoints
|
||||
set_state_endpoint: str
|
||||
set_value_endpoint: str
|
||||
# List-specific endpoints (for operations on the list itself)
|
||||
set_state_endpoint: str = ''
|
||||
set_value_endpoint: str = ''
|
||||
individual_minifigure_value_endpoint: str = ''
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -99,12 +100,15 @@ class BrickMetadataList(BrickRecordList[T]):
|
||||
|
||||
# Return the items as columns for a select
|
||||
@classmethod
|
||||
def as_columns(cls, /, **kwargs) -> str:
|
||||
def as_columns(cls, /, table: str | None = None, **kwargs) -> str:
|
||||
new = cls.new()
|
||||
|
||||
# Use provided table name or default to class table
|
||||
table_name = table if table is not None else cls.table
|
||||
|
||||
return ', '.join([
|
||||
'"{table}"."{column}"'.format(
|
||||
table=cls.table,
|
||||
table=table_name,
|
||||
column=record.as_column(),
|
||||
)
|
||||
for record
|
||||
@@ -184,3 +188,11 @@ class BrickMetadataList(BrickRecordList[T]):
|
||||
cls.set_value_endpoint,
|
||||
id=id,
|
||||
)
|
||||
|
||||
# URL to change the selected value of this metadata item for an individual minifigure
|
||||
@classmethod
|
||||
def url_for_individual_minifigure_value(cls, id: str, /) -> str:
|
||||
return url_for(
|
||||
cls.individual_minifigure_value_endpoint,
|
||||
id=id,
|
||||
)
|
||||
|
||||
@@ -76,12 +76,13 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
|
||||
|
||||
# Field mapping for sorting
|
||||
field_mapping = {
|
||||
'name': '"rebrickable_minifigures"."name"',
|
||||
'parts': '"rebrickable_minifigures"."number_of_parts"',
|
||||
'name': '"combined"."name"',
|
||||
'parts': '"combined"."number_of_parts"',
|
||||
'quantity': '"total_quantity"',
|
||||
'missing': '"total_missing"',
|
||||
'damaged': '"total_damaged"',
|
||||
'sets': '"total_sets"'
|
||||
'sets': '"total_sets"',
|
||||
'individual': '"total_individual"'
|
||||
}
|
||||
|
||||
# Use the base pagination method
|
||||
@@ -112,7 +113,7 @@ class BrickMinifigureList(BrickRecordList[BrickMinifigure]):
|
||||
if current_app.config['RANDOM']:
|
||||
order = 'RANDOM()'
|
||||
else:
|
||||
order = '"bricktracker_minifigures"."rowid" DESC'
|
||||
order = '"combined"."rowid" DESC'
|
||||
|
||||
self.list(override_query=self.last_query, order=order, limit=limit)
|
||||
|
||||
|
||||
@@ -35,3 +35,28 @@ def parse_set(set: str, /) -> str:
|
||||
))
|
||||
|
||||
return '{number}-{version}'.format(number=number, version=version)
|
||||
|
||||
|
||||
# Make sense of string supposed to contain a minifigure ID
|
||||
def parse_minifig(figure: str, /) -> str:
|
||||
# Minifigure format is typically fig-XXXXXX
|
||||
# We'll accept with or without the 'fig-' prefix
|
||||
figure = figure.strip()
|
||||
|
||||
if not figure.startswith('fig-'):
|
||||
# Try to add the prefix if it's just numbers
|
||||
if figure.isdigit():
|
||||
figure = 'fig-{figure}'.format(figure=figure.zfill(6))
|
||||
else:
|
||||
raise ErrorException('Minifigure "{figure}" must start with "fig-"'.format(
|
||||
figure=figure,
|
||||
))
|
||||
|
||||
# Validate format: fig-XXXXXX where X can be digits or letters
|
||||
parts = figure.split('-')
|
||||
if len(parts) != 2 or parts[0] != 'fig':
|
||||
raise ErrorException('Invalid minifigure format "{figure}". Expected format: fig-XXXXXX'.format(
|
||||
figure=figure,
|
||||
))
|
||||
|
||||
return figure
|
||||
|
||||
+104
-2
@@ -181,7 +181,18 @@ class BrickPart(RebrickablePart):
|
||||
|
||||
# Compute the url for updating checked state
|
||||
def url_for_checked(self, /) -> str:
|
||||
# Different URL for a minifigure part
|
||||
# Check if this is an individual minifigure (has minifigure with id field, no brickset)
|
||||
if self.minifigure is not None and hasattr(self.minifigure.fields, 'id') and self.brickset is None:
|
||||
# Individual minifigure part
|
||||
return url_for(
|
||||
'individual_minifigure.checked_part',
|
||||
id=self.minifigure.fields.id,
|
||||
part=self.fields.part,
|
||||
color=self.fields.color,
|
||||
spare=self.fields.spare,
|
||||
)
|
||||
|
||||
# Set-based part (with or without minifigure)
|
||||
if self.minifigure is not None:
|
||||
figure = self.minifigure.fields.figure
|
||||
else:
|
||||
@@ -228,7 +239,19 @@ class BrickPart(RebrickablePart):
|
||||
|
||||
# Compute the url for problematic part
|
||||
def url_for_problem(self, problem: str, /) -> str:
|
||||
# Different URL for a minifigure part
|
||||
# Check if this is an individual minifigure (has minifigure with id field, no brickset)
|
||||
if self.minifigure is not None and hasattr(self.minifigure.fields, 'id') and self.brickset is None:
|
||||
# Individual minifigure part
|
||||
return url_for(
|
||||
'individual_minifigure.problem_part',
|
||||
id=self.minifigure.fields.id,
|
||||
part=self.fields.part,
|
||||
color=self.fields.color,
|
||||
spare=self.fields.spare,
|
||||
problem=problem,
|
||||
)
|
||||
|
||||
# Set-based part (with or without minifigure)
|
||||
if self.minifigure is not None:
|
||||
figure = self.minifigure.fields.figure
|
||||
else:
|
||||
@@ -243,3 +266,82 @@ class BrickPart(RebrickablePart):
|
||||
spare=self.fields.spare,
|
||||
problem=problem,
|
||||
)
|
||||
|
||||
# Select a specific part from an individual minifigure
|
||||
def select_specific_individual_minifigure(
|
||||
self,
|
||||
minifigure: 'BrickMinifigure',
|
||||
part: str,
|
||||
color: int,
|
||||
spare: int,
|
||||
/,
|
||||
) -> Self:
|
||||
# Save the parameters to the fields
|
||||
self.minifigure = minifigure
|
||||
self.fields.id = minifigure.fields.id
|
||||
self.fields.part = part
|
||||
self.fields.color = color
|
||||
self.fields.spare = spare
|
||||
|
||||
if not self.select(override_query='individual_minifigure/part/select/specific'):
|
||||
raise NotFoundException(
|
||||
'Part {part} with color {color} (spare: {spare}) from individual minifigure {figure} ({id}) was not found in the database'.format(
|
||||
part=self.fields.part,
|
||||
color=self.fields.color,
|
||||
spare=self.fields.spare,
|
||||
figure=self.minifigure.fields.figure,
|
||||
id=self.minifigure.fields.id,
|
||||
),
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
# Update a problematic part for individual minifigure
|
||||
def update_problem_individual_minifigure(self, problem: str, json: Any | None, /) -> int:
|
||||
amount: str | int = json.get('value', '') # type: ignore
|
||||
|
||||
# We need a positive integer
|
||||
try:
|
||||
if amount == '':
|
||||
amount = 0
|
||||
|
||||
amount = int(amount)
|
||||
|
||||
if amount < 0:
|
||||
amount = 0
|
||||
except Exception:
|
||||
raise ErrorException('"{amount}" is not a valid integer'.format(
|
||||
amount=amount
|
||||
))
|
||||
|
||||
if amount < 0:
|
||||
raise ErrorException('Cannot set a negative amount')
|
||||
|
||||
setattr(self.fields, problem, amount)
|
||||
|
||||
BrickSQL().execute_and_commit(
|
||||
'individual_minifigure/part/update/{problem}'.format(problem=problem),
|
||||
parameters=self.sql_parameters()
|
||||
)
|
||||
|
||||
return amount
|
||||
|
||||
# Update checked state for individual minifigure part
|
||||
def update_checked_individual_minifigure(self, json: Any | None, /) -> bool:
|
||||
# Handle both direct 'checked' key and changer.js 'value' key format
|
||||
if json:
|
||||
checked = json.get('checked', json.get('value', False))
|
||||
else:
|
||||
checked = False
|
||||
|
||||
checked = bool(checked)
|
||||
|
||||
# Update the field
|
||||
self.fields.checked = checked
|
||||
|
||||
BrickSQL().execute_and_commit(
|
||||
'individual_minifigure/part/update/checked',
|
||||
parameters=self.sql_parameters()
|
||||
)
|
||||
|
||||
return checked
|
||||
|
||||
@@ -25,6 +25,7 @@ class BrickPartList(BrickRecordList[BrickPart]):
|
||||
all_query: str = 'part/list/all'
|
||||
all_by_owner_query: str = 'part/list/all_by_owner'
|
||||
different_color_query = 'part/list/with_different_color'
|
||||
individual_minifigure_query: str = 'individual_minifigure/part/list/from_instance'
|
||||
last_query: str = 'part/list/last'
|
||||
minifigure_query: str = 'part/list/from_minifigure'
|
||||
problem_query: str = 'part/list/problem'
|
||||
@@ -212,6 +213,20 @@ class BrickPartList(BrickRecordList[BrickPart]):
|
||||
|
||||
return self
|
||||
|
||||
# Load parts from an individual minifigure instance
|
||||
def from_individual_minifigure(
|
||||
self,
|
||||
minifigure: 'BrickMinifigure',
|
||||
/,
|
||||
) -> Self:
|
||||
# Save the minifigure
|
||||
self.minifigure = minifigure
|
||||
|
||||
# Load the parts from the database using the instance-specific query
|
||||
self.list(override_query=self.individual_minifigure_query)
|
||||
|
||||
return self
|
||||
|
||||
# Load generic parts from a print
|
||||
def from_print(
|
||||
self,
|
||||
@@ -306,9 +321,11 @@ class BrickPartList(BrickRecordList[BrickPart]):
|
||||
def sql_parameters(self, /) -> dict[str, Any]:
|
||||
parameters: dict[str, Any] = super().sql_parameters()
|
||||
|
||||
# Set id
|
||||
# Set id - prioritize brickset, then check minifigure
|
||||
if self.brickset is not None:
|
||||
parameters['id'] = self.brickset.fields.id
|
||||
elif self.minifigure is not None and hasattr(self.minifigure.fields, 'id'):
|
||||
parameters['id'] = self.minifigure.fields.id
|
||||
|
||||
# Use the minifigure number if present,
|
||||
if self.minifigure is not None:
|
||||
|
||||
@@ -114,7 +114,19 @@ class RebrickablePart(BrickRecord):
|
||||
if self.fields.image is None:
|
||||
file = RebrickableImage.nil_name()
|
||||
else:
|
||||
file = self.fields.image_id
|
||||
# Use image_id if available, otherwise extract from image URL
|
||||
if hasattr(self.fields, 'image_id') and self.fields.image_id:
|
||||
file = self.fields.image_id
|
||||
else:
|
||||
# Extract image_id from URL on-the-fly
|
||||
from urllib.parse import urlparse
|
||||
import os
|
||||
image_id, _ = os.path.splitext(
|
||||
os.path.basename(
|
||||
urlparse(self.fields.image).path
|
||||
)
|
||||
)
|
||||
file = image_id if image_id else RebrickableImage.nil_name()
|
||||
|
||||
return RebrickableImage.static_url(file, 'PARTS_FOLDER')
|
||||
else:
|
||||
@@ -204,6 +216,48 @@ class RebrickablePart(BrickRecord):
|
||||
if len(bricklink_data['ext_descrs']) > 0 and len(bricklink_data['ext_descrs'][0]) > 0:
|
||||
record['bricklink_color_name'] = bricklink_data['ext_descrs'][0][0]
|
||||
|
||||
# Cache color information in rebrickable_colors table for future lookups
|
||||
# This builds the translation table automatically as sets are imported
|
||||
if 'color' in data:
|
||||
try:
|
||||
from .sql import BrickSQL
|
||||
sql = BrickSQL()
|
||||
|
||||
# Check if color already exists in cache
|
||||
check_query = """
|
||||
SELECT COUNT(*) FROM "rebrickable_colors"
|
||||
WHERE "color_id" = :color_id
|
||||
"""
|
||||
sql.cursor.execute(check_query, {'color_id': record['color_id']})
|
||||
exists = sql.cursor.fetchone()[0] > 0
|
||||
|
||||
if not exists:
|
||||
# Insert color into cache
|
||||
insert_query = """
|
||||
INSERT OR IGNORE INTO "rebrickable_colors" (
|
||||
"color_id", "name", "rgb", "is_trans",
|
||||
"bricklink_color_id", "bricklink_color_name"
|
||||
) VALUES (
|
||||
:color_id, :name, :rgb, :is_trans,
|
||||
:bricklink_color_id, :bricklink_color_name
|
||||
)
|
||||
"""
|
||||
sql.cursor.execute(insert_query, {
|
||||
'color_id': record['color_id'],
|
||||
'name': record['color_name'],
|
||||
'rgb': record['color_rgb'],
|
||||
'is_trans': record['color_transparent'],
|
||||
'bricklink_color_id': record['bricklink_color_id'],
|
||||
'bricklink_color_name': record['bricklink_color_name']
|
||||
})
|
||||
# Commit is handled by parent transaction
|
||||
|
||||
except Exception as e:
|
||||
# Don't fail part import if color caching fails
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.debug(f'Could not cache color {record["color_id"]}: {e}')
|
||||
|
||||
# Extract BrickLink part number if available
|
||||
if 'part' in data and 'external_ids' in data['part']:
|
||||
part_external_ids = data['part']['external_ids']
|
||||
@@ -226,7 +280,7 @@ class RebrickablePart(BrickRecord):
|
||||
)
|
||||
)
|
||||
|
||||
if image_id is not None or image_id != '':
|
||||
if image_id is not None and image_id != '':
|
||||
record['image_id'] = image_id
|
||||
|
||||
return record
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from sqlite3 import Row
|
||||
from typing import Any, ItemsView
|
||||
|
||||
@@ -5,6 +6,24 @@ from .fields import BrickRecordFields
|
||||
from .sql import BrickSQL
|
||||
|
||||
|
||||
def format_timestamp(timestamp: float | None, format_key: str = 'PURCHASE_DATE_FORMAT') -> str:
|
||||
"""
|
||||
Format a timestamp for display.
|
||||
|
||||
Args:
|
||||
timestamp: Unix timestamp (float) or None
|
||||
format_key: Config key for date format string
|
||||
|
||||
Returns:
|
||||
Formatted date string or empty string if timestamp is None
|
||||
"""
|
||||
if timestamp is not None:
|
||||
from flask import current_app
|
||||
time = datetime.fromtimestamp(timestamp)
|
||||
return time.strftime(current_app.config.get(format_key, '%Y/%m/%d'))
|
||||
return ''
|
||||
|
||||
|
||||
# SQLite record
|
||||
class BrickRecord(object):
|
||||
select_query: str
|
||||
|
||||
@@ -36,6 +36,7 @@ class BrickSetList(BrickRecordList[BrickSet]):
|
||||
using_minifigure_query: str = 'set/list/using_minifigure'
|
||||
using_part_query: str = 'set/list/using_part'
|
||||
using_storage_query: str = 'set/list/using_storage'
|
||||
without_storage_query: str = 'set/list/without_storage'
|
||||
|
||||
def __init__(self, /):
|
||||
super().__init__()
|
||||
@@ -670,10 +671,17 @@ class BrickSetList(BrickRecordList[BrickSet]):
|
||||
|
||||
return self
|
||||
|
||||
def without_storage(self, /) -> Self:
|
||||
# Load sets with no storage
|
||||
self.list(override_query=self.without_storage_query)
|
||||
|
||||
return self
|
||||
|
||||
|
||||
# Helper to build the metadata lists
|
||||
def set_metadata_lists(
|
||||
as_class: bool = False
|
||||
as_class: bool = False,
|
||||
hardcoded_statuses_only: bool = False
|
||||
) -> dict[
|
||||
str,
|
||||
Union[
|
||||
@@ -685,9 +693,20 @@ def set_metadata_lists(
|
||||
list[BrickSetTag]
|
||||
]
|
||||
]:
|
||||
# Get all statuses
|
||||
all_statuses = BrickSetStatusList.list(all=True)
|
||||
|
||||
# Filter to only hardcoded statuses if requested (for individual minifigures)
|
||||
if hardcoded_statuses_only:
|
||||
hardcoded_status_ids = ['minifigures_collected', 'set_checked', 'set_collected']
|
||||
statuses = [s for s in all_statuses if s.fields.id in hardcoded_status_ids]
|
||||
else:
|
||||
statuses = all_statuses
|
||||
|
||||
return {
|
||||
'brickset_owners': BrickSetOwnerList.list(),
|
||||
'brickset_purchase_locations': BrickSetPurchaseLocationList.list(as_class=as_class), # noqa: E501
|
||||
'brickset_statuses': statuses,
|
||||
'brickset_storages': BrickSetStorageList.list(as_class=as_class),
|
||||
'brickset_tags': BrickSetTagList.list(),
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ from .metadata import BrickMetadata
|
||||
class BrickSetOwner(BrickMetadata):
|
||||
kind: str = 'owner'
|
||||
|
||||
# Set state endpoint
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_owner'
|
||||
individual_minifigure_state_endpoint: str = 'individual_minifigure.update_owner'
|
||||
individual_part_state_endpoint: str = 'individual_part.update_owner'
|
||||
|
||||
# Queries
|
||||
delete_query: str = 'set/metadata/owner/delete'
|
||||
@@ -14,3 +16,5 @@ class BrickSetOwner(BrickMetadata):
|
||||
select_query: str = 'set/metadata/owner/select'
|
||||
update_field_query: str = 'set/metadata/owner/update/field'
|
||||
update_set_state_query: str = 'set/metadata/owner/update/state'
|
||||
update_individual_minifigure_state_query: str = 'individual_minifigure/metadata/owner/update/state'
|
||||
update_individual_part_state_query: str = 'individual_part/metadata/owner/update/state'
|
||||
|
||||
@@ -15,6 +15,9 @@ class BrickSetOwnerList(BrickMetadataList[BrickSetOwner]):
|
||||
# Queries
|
||||
select_query = 'set/metadata/owner/list'
|
||||
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_owner'
|
||||
|
||||
# Instantiate the list with the proper class
|
||||
@classmethod
|
||||
def new(cls, /, *, force: bool = False) -> Self:
|
||||
|
||||
@@ -5,9 +5,13 @@ from .metadata import BrickMetadata
|
||||
class BrickSetPurchaseLocation(BrickMetadata):
|
||||
kind: str = 'purchase location'
|
||||
|
||||
# Endpoints
|
||||
individual_minifigure_value_endpoint: str = 'individual_minifigure.update_purchase_location'
|
||||
|
||||
# Queries
|
||||
delete_query: str = 'set/metadata/purchase_location/delete'
|
||||
insert_query: str = 'set/metadata/purchase_location/insert'
|
||||
select_query: str = 'set/metadata/purchase_location/select'
|
||||
update_field_query: str = 'set/metadata/purchase_location/update/field'
|
||||
update_set_value_query: str = 'set/metadata/purchase_location/update/value'
|
||||
update_individual_minifigure_value_query: str = 'individual_minifigure/metadata/purchase_location/update/value'
|
||||
|
||||
@@ -22,6 +22,9 @@ class BrickSetPurchaseLocationList(
|
||||
# Set value endpoint
|
||||
set_value_endpoint: str = 'set.update_purchase_location'
|
||||
|
||||
# Individual minifigure value endpoint
|
||||
individual_minifigure_value_endpoint: str = 'individual_minifigure.update_purchase_location'
|
||||
|
||||
# Load all purchase locations
|
||||
@classmethod
|
||||
def all(cls, /) -> Self:
|
||||
|
||||
@@ -7,8 +7,10 @@ from .metadata import BrickMetadata
|
||||
class BrickSetStatus(BrickMetadata):
|
||||
kind: str = 'status'
|
||||
|
||||
# Set state endpoint
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_status'
|
||||
individual_minifigure_state_endpoint: str = 'individual_minifigure.update_status'
|
||||
individual_part_state_endpoint: str = 'individual_part.update_status'
|
||||
|
||||
# Queries
|
||||
delete_query: str = 'set/metadata/status/delete'
|
||||
@@ -16,6 +18,8 @@ class BrickSetStatus(BrickMetadata):
|
||||
select_query: str = 'set/metadata/status/select'
|
||||
update_field_query: str = 'set/metadata/status/update/field'
|
||||
update_set_state_query: str = 'set/metadata/status/update/state'
|
||||
update_individual_minifigure_state_query: str = 'individual_minifigure/metadata/status/update/state'
|
||||
update_individual_part_state_query: str = 'individual_part/metadata/status/update/state'
|
||||
|
||||
# Grab data from a form
|
||||
def from_form(self, form: dict[str, str], /) -> Self:
|
||||
|
||||
@@ -15,6 +15,9 @@ class BrickSetStatusList(BrickMetadataList[BrickSetStatus]):
|
||||
# Queries
|
||||
select_query = 'set/metadata/status/list'
|
||||
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_status'
|
||||
|
||||
# Filter the list of set status
|
||||
def filter(self, all: bool = False) -> list[BrickSetStatus]:
|
||||
return [
|
||||
|
||||
@@ -7,12 +7,16 @@ from flask import url_for
|
||||
class BrickSetStorage(BrickMetadata):
|
||||
kind: str = 'storage'
|
||||
|
||||
# Endpoints
|
||||
individual_minifigure_value_endpoint: str = 'individual_minifigure.update_storage'
|
||||
|
||||
# Queries
|
||||
delete_query: str = 'set/metadata/storage/delete'
|
||||
insert_query: str = 'set/metadata/storage/insert'
|
||||
select_query: str = 'set/metadata/storage/select'
|
||||
update_field_query: str = 'set/metadata/storage/update/field'
|
||||
update_set_value_query: str = 'set/metadata/storage/update/value'
|
||||
update_individual_minifigure_value_query: str = 'individual_minifigure/metadata/storage/update/value'
|
||||
|
||||
# Self url
|
||||
def url(self, /) -> str:
|
||||
|
||||
@@ -20,6 +20,9 @@ class BrickSetStorageList(BrickMetadataList[BrickSetStorage]):
|
||||
# Set value endpoint
|
||||
set_value_endpoint: str = 'set.update_storage'
|
||||
|
||||
# Individual minifigure value endpoint
|
||||
individual_minifigure_value_endpoint: str = 'individual_minifigure.update_storage'
|
||||
|
||||
# Load all storages
|
||||
@classmethod
|
||||
def all(cls, /) -> Self:
|
||||
|
||||
@@ -5,8 +5,10 @@ from .metadata import BrickMetadata
|
||||
class BrickSetTag(BrickMetadata):
|
||||
kind: str = 'tag'
|
||||
|
||||
# Set state endpoint
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_tag'
|
||||
individual_minifigure_state_endpoint: str = 'individual_minifigure.update_tag'
|
||||
individual_part_state_endpoint: str = 'individual_part.update_tag'
|
||||
|
||||
# Queries
|
||||
delete_query: str = 'set/metadata/tag/delete'
|
||||
@@ -14,3 +16,5 @@ class BrickSetTag(BrickMetadata):
|
||||
select_query: str = 'set/metadata/tag/select'
|
||||
update_field_query: str = 'set/metadata/tag/update/field'
|
||||
update_set_state_query: str = 'set/metadata/tag/update/state'
|
||||
update_individual_minifigure_state_query: str = 'individual_minifigure/metadata/tag/update/state'
|
||||
update_individual_part_state_query: str = 'individual_part/metadata/tag/update/state'
|
||||
|
||||
@@ -15,6 +15,9 @@ class BrickSetTagList(BrickMetadataList[BrickSetTag]):
|
||||
# Queries
|
||||
select_query: str = 'set/metadata/tag/list'
|
||||
|
||||
# Endpoints
|
||||
set_state_endpoint: str = 'set.update_tag'
|
||||
|
||||
# Instantiate the list with the proper class
|
||||
@classmethod
|
||||
def new(cls, /, *, force: bool = False) -> Self:
|
||||
|
||||
@@ -18,13 +18,21 @@ logger = logging.getLogger(__name__)
|
||||
MESSAGES: Final[dict[str, str]] = {
|
||||
'COMPLETE': 'complete',
|
||||
'CONNECT': 'connect',
|
||||
'CREATE_LOT': 'create_lot',
|
||||
'DISCONNECT': 'disconnect',
|
||||
'DOWNLOAD_INSTRUCTIONS': 'download_instructions',
|
||||
'DOWNLOAD_PEERON_PAGES': 'download_peeron_pages',
|
||||
'FAIL': 'fail',
|
||||
'IMPORT_MINIFIGURE': 'import_minifigure',
|
||||
'IMPORT_SET': 'import_set',
|
||||
'LOAD_MINIFIGURE': 'load_minifigure',
|
||||
'LOAD_PART': 'load_part',
|
||||
'LOAD_PART_COLORS': 'load_part_colors',
|
||||
'LOAD_PEERON_PAGES': 'load_peeron_pages',
|
||||
'LOAD_SET': 'load_set',
|
||||
'MINIFIGURE_LOADED': 'minifigure_loaded',
|
||||
'PART_COLORS_LOADED': 'part_colors_loaded',
|
||||
'PART_LOADED': 'part_loaded',
|
||||
'PROGRESS': 'progress',
|
||||
'SET_LOADED': 'set_loaded',
|
||||
}
|
||||
@@ -207,6 +215,57 @@ class BrickSocket(object):
|
||||
|
||||
BrickSet().load(self, data)
|
||||
|
||||
@self.socket.on(MESSAGES['IMPORT_MINIFIGURE'], namespace=self.namespace)
|
||||
@rebrickable_socket(self)
|
||||
def import_minifigure(data: dict[str, Any], /) -> None:
|
||||
logger.debug('Socket: IMPORT_MINIFIGURE={data} (from: {fr})'.format(
|
||||
data=data,
|
||||
fr=request.sid, # type: ignore
|
||||
))
|
||||
|
||||
from .individual_minifigure import IndividualMinifigure
|
||||
IndividualMinifigure().download(self, data)
|
||||
|
||||
@self.socket.on(MESSAGES['LOAD_MINIFIGURE'], namespace=self.namespace)
|
||||
def load_minifigure(data: dict[str, Any], /) -> None:
|
||||
logger.debug('Socket: LOAD_MINIFIGURE={data} (from: {fr})'.format(
|
||||
data=data,
|
||||
fr=request.sid, # type: ignore
|
||||
))
|
||||
|
||||
from .individual_minifigure import IndividualMinifigure
|
||||
IndividualMinifigure().load(self, data)
|
||||
|
||||
@self.socket.on(MESSAGES['LOAD_PART'], namespace=self.namespace)
|
||||
def load_part(data: dict[str, Any], /) -> None:
|
||||
logger.debug('Socket: LOAD_PART={data} (from: {fr})'.format(
|
||||
data=data,
|
||||
fr=request.sid, # type: ignore
|
||||
))
|
||||
|
||||
from .individual_part import IndividualPart
|
||||
IndividualPart().add(self, data)
|
||||
|
||||
@self.socket.on(MESSAGES['LOAD_PART_COLORS'], namespace=self.namespace)
|
||||
def load_part_colors(data: dict[str, Any], /) -> None:
|
||||
logger.debug('Socket: LOAD_PART_COLORS={data} (from: {fr})'.format(
|
||||
data=data,
|
||||
fr=request.sid, # type: ignore
|
||||
))
|
||||
|
||||
from .individual_part import IndividualPart
|
||||
IndividualPart().load_colors(self, data)
|
||||
|
||||
@self.socket.on(MESSAGES['CREATE_LOT'], namespace=self.namespace)
|
||||
@rebrickable_socket(self)
|
||||
def create_lot(data: dict[str, Any], /) -> None:
|
||||
logger.debug('Socket: CREATE_LOT (from: {fr})'.format(
|
||||
fr=request.sid, # type: ignore
|
||||
))
|
||||
|
||||
from .individual_part_lot import IndividualPartLot
|
||||
IndividualPartLot().create(self, data)
|
||||
|
||||
# Update the progress auto-incrementing
|
||||
def auto_progress(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Delete individual minifigure parts
|
||||
DELETE FROM "bricktracker_individual_minifigure_parts"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete individual minifigure owners
|
||||
DELETE FROM "bricktracker_individual_minifigure_owners"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete individual minifigure tags
|
||||
DELETE FROM "bricktracker_individual_minifigure_tags"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete individual minifigure statuses
|
||||
DELETE FROM "bricktracker_individual_minifigure_statuses"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete the individual minifigure itself
|
||||
DELETE FROM "bricktracker_individual_minifigures"
|
||||
WHERE "id" = :id;
|
||||
@@ -0,0 +1,15 @@
|
||||
INSERT OR IGNORE INTO "bricktracker_individual_minifigures" (
|
||||
"id",
|
||||
"figure",
|
||||
"quantity",
|
||||
"description",
|
||||
"storage",
|
||||
"purchase_location"
|
||||
) VALUES (
|
||||
:id,
|
||||
:figure,
|
||||
:quantity,
|
||||
:description,
|
||||
:storage,
|
||||
:purchase_location
|
||||
)
|
||||
@@ -0,0 +1,48 @@
|
||||
-- Get all individual minifigure instances for a specific storage location
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity",
|
||||
"bricktracker_individual_minifigures"."description",
|
||||
"bricktracker_individual_minifigures"."storage",
|
||||
"bricktracker_individual_minifigures"."purchase_location",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"storage_meta"."name" AS "storage_name",
|
||||
"purchase_meta"."name" AS "purchase_location_name",
|
||||
IFNULL("problem_join"."total_missing", 0) AS "total_missing",
|
||||
IFNULL("problem_join"."total_damaged", 0) AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_individual_minifigures"."figure" = "rebrickable_minifigures"."figure"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_storages" AS "storage_meta"
|
||||
ON "bricktracker_individual_minifigures"."storage" = "storage_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations" AS "purchase_meta"
|
||||
ON "bricktracker_individual_minifigures"."purchase_location" = "purchase_meta"."id"
|
||||
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
GROUP BY "bricktracker_individual_minifigure_parts"."id"
|
||||
) "problem_join"
|
||||
ON "bricktracker_individual_minifigures"."id" = "problem_join"."id"
|
||||
|
||||
WHERE "bricktracker_individual_minifigures"."storage" IS NOT DISTINCT FROM :storage
|
||||
|
||||
{% if order %}
|
||||
ORDER BY {{ order }}
|
||||
{% else %}
|
||||
ORDER BY "bricktracker_individual_minifigures"."rowid" DESC
|
||||
{% endif %}
|
||||
|
||||
{% if limit %}
|
||||
LIMIT {{ limit }}
|
||||
{% endif %}
|
||||
@@ -0,0 +1,48 @@
|
||||
-- Get all individual minifigure instances without storage
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity",
|
||||
"bricktracker_individual_minifigures"."description",
|
||||
"bricktracker_individual_minifigures"."storage",
|
||||
"bricktracker_individual_minifigures"."purchase_location",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"storage_meta"."name" AS "storage_name",
|
||||
"purchase_meta"."name" AS "purchase_location_name",
|
||||
IFNULL("problem_join"."total_missing", 0) AS "total_missing",
|
||||
IFNULL("problem_join"."total_damaged", 0) AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_individual_minifigures"."figure" = "rebrickable_minifigures"."figure"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_storages" AS "storage_meta"
|
||||
ON "bricktracker_individual_minifigures"."storage" = "storage_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations" AS "purchase_meta"
|
||||
ON "bricktracker_individual_minifigures"."purchase_location" = "purchase_meta"."id"
|
||||
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
GROUP BY "bricktracker_individual_minifigure_parts"."id"
|
||||
) "problem_join"
|
||||
ON "bricktracker_individual_minifigures"."id" = "problem_join"."id"
|
||||
|
||||
WHERE "bricktracker_individual_minifigures"."storage" IS NULL
|
||||
|
||||
{% if order %}
|
||||
ORDER BY {{ order }}
|
||||
{% else %}
|
||||
ORDER BY "bricktracker_individual_minifigures"."rowid" DESC
|
||||
{% endif %}
|
||||
|
||||
{% if limit %}
|
||||
LIMIT {{ limit }}
|
||||
{% endif %}
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_minifigure_owners" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_minifigure_owners"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_minifigure_statuses" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_minifigure_statuses"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_minifigure_tags" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_minifigure_tags"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,23 @@
|
||||
INSERT OR IGNORE INTO "bricktracker_individual_minifigure_parts" (
|
||||
"id",
|
||||
"part",
|
||||
"color",
|
||||
"spare",
|
||||
"quantity",
|
||||
"element",
|
||||
"rebrickable_inventory",
|
||||
"missing",
|
||||
"damaged",
|
||||
"checked"
|
||||
) VALUES (
|
||||
:id,
|
||||
:part,
|
||||
:color,
|
||||
:spare,
|
||||
:quantity,
|
||||
:element,
|
||||
:rebrickable_inventory,
|
||||
0,
|
||||
0,
|
||||
0
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
-- Query parts for a specific individual minifigure instance
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."part",
|
||||
"bricktracker_individual_minifigure_parts"."color",
|
||||
"bricktracker_individual_minifigure_parts"."spare",
|
||||
"bricktracker_individual_minifigure_parts"."quantity",
|
||||
"bricktracker_individual_minifigure_parts"."element",
|
||||
"bricktracker_individual_minifigure_parts"."missing" AS "total_missing",
|
||||
"bricktracker_individual_minifigure_parts"."damaged" AS "total_damaged",
|
||||
"bricktracker_individual_minifigure_parts"."checked",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."bricklink_color_id",
|
||||
"rebrickable_parts"."bricklink_color_name",
|
||||
"rebrickable_parts"."bricklink_part_num",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."image_id",
|
||||
"rebrickable_parts"."url",
|
||||
"rebrickable_parts"."print",
|
||||
NULL AS "total_quantity",
|
||||
NULL AS "total_spare",
|
||||
NULL AS "total_sets",
|
||||
NULL AS "total_minifigures"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_minifigure_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_minifigure_parts"."color" = "rebrickable_parts"."color_id"
|
||||
WHERE "bricktracker_individual_minifigure_parts"."id" IS NOT DISTINCT FROM :id
|
||||
|
||||
{% if order %}
|
||||
ORDER BY {{ order | replace('"combined"', '"bricktracker_individual_minifigure_parts"') | replace('"bricktracker_parts"', '"bricktracker_individual_minifigure_parts"') }}
|
||||
{% endif %}
|
||||
@@ -0,0 +1,33 @@
|
||||
-- Select a specific part from an individual minifigure instance
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."part",
|
||||
"bricktracker_individual_minifigure_parts"."color",
|
||||
"bricktracker_individual_minifigure_parts"."spare",
|
||||
"bricktracker_individual_minifigure_parts"."quantity",
|
||||
"bricktracker_individual_minifigure_parts"."element",
|
||||
"bricktracker_individual_minifigure_parts"."missing",
|
||||
"bricktracker_individual_minifigure_parts"."damaged",
|
||||
"bricktracker_individual_minifigure_parts"."checked",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."bricklink_color_id",
|
||||
"rebrickable_parts"."bricklink_color_name",
|
||||
"rebrickable_parts"."bricklink_part_num",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."image_id",
|
||||
"rebrickable_parts"."url",
|
||||
"rebrickable_parts"."print"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_minifigure_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_minifigure_parts"."color" = "rebrickable_parts"."color_id"
|
||||
WHERE "bricktracker_individual_minifigure_parts"."id" IS NOT DISTINCT FROM :id
|
||||
AND "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."spare" IS NOT DISTINCT FROM :spare
|
||||
@@ -0,0 +1,6 @@
|
||||
UPDATE "bricktracker_individual_minifigure_parts"
|
||||
SET "checked" = :checked
|
||||
WHERE "bricktracker_individual_minifigure_parts"."id" IS NOT DISTINCT FROM :id
|
||||
AND "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."spare" IS NOT DISTINCT FROM :spare
|
||||
@@ -0,0 +1,6 @@
|
||||
UPDATE "bricktracker_individual_minifigure_parts"
|
||||
SET "damaged" = :damaged
|
||||
WHERE "bricktracker_individual_minifigure_parts"."id" IS NOT DISTINCT FROM :id
|
||||
AND "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."spare" IS NOT DISTINCT FROM :spare
|
||||
@@ -0,0 +1,6 @@
|
||||
UPDATE "bricktracker_individual_minifigure_parts"
|
||||
SET "missing" = :missing
|
||||
WHERE "bricktracker_individual_minifigure_parts"."id" IS NOT DISTINCT FROM :id
|
||||
AND "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."spare" IS NOT DISTINCT FROM :spare
|
||||
@@ -0,0 +1,35 @@
|
||||
-- Get a specific individual minifigure instance by ID
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity",
|
||||
"bricktracker_individual_minifigures"."description",
|
||||
"bricktracker_individual_minifigures"."storage",
|
||||
"bricktracker_individual_minifigures"."purchase_location",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"storage_meta"."name" AS "storage_name",
|
||||
"purchase_meta"."name" AS "purchase_location_name"{{ owners }}{{ statuses }}{{ tags }}
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_individual_minifigures"."figure" = "rebrickable_minifigures"."figure"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_storages" AS "storage_meta"
|
||||
ON "bricktracker_individual_minifigures"."storage" = "storage_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations" AS "purchase_meta"
|
||||
ON "bricktracker_individual_minifigures"."purchase_location" = "purchase_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners"
|
||||
ON "bricktracker_individual_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigure_owners"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_statuses"
|
||||
ON "bricktracker_individual_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigure_statuses"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_tags"
|
||||
ON "bricktracker_individual_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigure_tags"."id"
|
||||
|
||||
WHERE "bricktracker_individual_minifigures"."id" = :id
|
||||
@@ -0,0 +1,52 @@
|
||||
-- Get all individual minifigure instances for a specific figure
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity",
|
||||
"bricktracker_individual_minifigures"."description",
|
||||
"bricktracker_individual_minifigures"."storage",
|
||||
"bricktracker_individual_minifigures"."purchase_location",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"storage_meta"."name" AS "storage_name",
|
||||
"purchase_meta"."name" AS "purchase_location_name",
|
||||
{{ owners }},
|
||||
{{ statuses }},
|
||||
{{ tags }},
|
||||
IFNULL("problem_join"."total_missing", 0) AS "total_missing",
|
||||
IFNULL("problem_join"."total_damaged", 0) AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_individual_minifigures"."figure" = "rebrickable_minifigures"."figure"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_storages" AS "storage_meta"
|
||||
ON "bricktracker_individual_minifigures"."storage" = "storage_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations" AS "purchase_meta"
|
||||
ON "bricktracker_individual_minifigures"."purchase_location" = "purchase_meta"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners"
|
||||
ON "bricktracker_individual_minifigures"."id" = "bricktracker_individual_minifigure_owners"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_statuses"
|
||||
ON "bricktracker_individual_minifigures"."id" = "bricktracker_individual_minifigure_statuses"."id"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigure_tags"
|
||||
ON "bricktracker_individual_minifigures"."id" = "bricktracker_individual_minifigure_tags"."id"
|
||||
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
GROUP BY "bricktracker_individual_minifigure_parts"."id"
|
||||
) "problem_join"
|
||||
ON "bricktracker_individual_minifigures"."id" = "problem_join"."id"
|
||||
|
||||
WHERE "bricktracker_individual_minifigures"."figure" = :figure
|
||||
|
||||
ORDER BY "bricktracker_individual_minifigures"."rowid" DESC
|
||||
@@ -0,0 +1,7 @@
|
||||
UPDATE "bricktracker_individual_minifigures"
|
||||
SET
|
||||
"quantity" = :quantity,
|
||||
"description" = :description,
|
||||
"storage" = :storage,
|
||||
"purchase_location" = :purchase_location
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,13 @@
|
||||
-- Delete metadata first (foreign keys with CASCADE will handle this, but being explicit)
|
||||
DELETE FROM "bricktracker_individual_part_owners"
|
||||
WHERE "id" = '{{ id }}';
|
||||
|
||||
DELETE FROM "bricktracker_individual_part_tags"
|
||||
WHERE "id" = '{{ id }}';
|
||||
|
||||
DELETE FROM "bricktracker_individual_part_statuses"
|
||||
WHERE "id" = '{{ id }}';
|
||||
|
||||
-- Delete the individual part itself
|
||||
DELETE FROM "bricktracker_individual_parts"
|
||||
WHERE "id" = '{{ id }}';
|
||||
@@ -0,0 +1,27 @@
|
||||
INSERT INTO "bricktracker_individual_parts" (
|
||||
"id",
|
||||
"part",
|
||||
"color",
|
||||
"quantity",
|
||||
"missing",
|
||||
"damaged",
|
||||
"checked",
|
||||
"description",
|
||||
"storage",
|
||||
"purchase_location",
|
||||
"purchase_date",
|
||||
"purchase_price"
|
||||
) VALUES (
|
||||
:id,
|
||||
:part,
|
||||
:color,
|
||||
:quantity,
|
||||
:missing,
|
||||
:damaged,
|
||||
:checked,
|
||||
:description,
|
||||
:storage,
|
||||
:purchase_location,
|
||||
:purchase_date,
|
||||
:purchase_price
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
ORDER BY "bricktracker_individual_parts"."part", "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."color" = :color
|
||||
ORDER BY "bricktracker_individual_parts"."part"
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."part" = :part
|
||||
ORDER BY "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,32 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."part" = :part
|
||||
AND "bricktracker_individual_parts"."color" = :color
|
||||
ORDER BY "bricktracker_individual_parts"."id"
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."storage" = :storage
|
||||
ORDER BY "bricktracker_individual_parts"."part", "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,32 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."missing" > 0
|
||||
OR "bricktracker_individual_parts"."damaged" > 0
|
||||
ORDER BY "bricktracker_individual_parts"."part", "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM :storage
|
||||
ORDER BY "bricktracker_individual_parts"."part", "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."storage" IS NULL
|
||||
ORDER BY "bricktracker_individual_parts"."part", "bricktracker_individual_parts"."color"
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_part_owners" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_part_owners"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_part_statuses" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_part_statuses"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,10 @@
|
||||
INSERT INTO "bricktracker_individual_part_tags" (
|
||||
"id",
|
||||
"{{name}}"
|
||||
) VALUES (
|
||||
:id,
|
||||
:state
|
||||
)
|
||||
ON CONFLICT("id")
|
||||
DO UPDATE SET "{{name}}" = :state
|
||||
WHERE "bricktracker_individual_part_tags"."id" IS NOT DISTINCT FROM :id
|
||||
@@ -0,0 +1,31 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"bricktracker_individual_parts"."lot_id",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_parts"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_parts"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_parts"."id" = :id
|
||||
@@ -0,0 +1,3 @@
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET "{{ field }}" = :value
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,3 @@
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET "checked" = :checked
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,3 @@
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET "damaged" = :damaged
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,3 @@
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET "missing" = :missing
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,9 @@
|
||||
UPDATE "bricktracker_individual_parts"
|
||||
SET
|
||||
"quantity" = :quantity,
|
||||
"description" = :description,
|
||||
"storage" = :storage,
|
||||
"purchase_location" = :purchase_location,
|
||||
"purchase_date" = :purchase_date,
|
||||
"purchase_price" = :purchase_price
|
||||
WHERE "id" = :id
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Delete all individual parts associated with this lot
|
||||
DELETE FROM "bricktracker_individual_parts"
|
||||
WHERE "lot_id" = :id;
|
||||
|
||||
-- Delete lot owners
|
||||
DELETE FROM "bricktracker_individual_part_lot_owners"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete lot tags
|
||||
DELETE FROM "bricktracker_individual_part_lot_tags"
|
||||
WHERE "id" = :id;
|
||||
|
||||
-- Delete the lot itself
|
||||
DELETE FROM "bricktracker_individual_part_lots"
|
||||
WHERE "id" = :id;
|
||||
@@ -0,0 +1,19 @@
|
||||
INSERT INTO "bricktracker_individual_part_lots" (
|
||||
"id",
|
||||
"name",
|
||||
"description",
|
||||
"created_date",
|
||||
"storage",
|
||||
"purchase_location",
|
||||
"purchase_date",
|
||||
"purchase_price"
|
||||
) VALUES (
|
||||
:id,
|
||||
:name,
|
||||
:description,
|
||||
:created_date,
|
||||
:storage,
|
||||
:purchase_location,
|
||||
:purchase_date,
|
||||
:purchase_price
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
SELECT
|
||||
"bricktracker_individual_part_lots"."id",
|
||||
"bricktracker_individual_part_lots"."name",
|
||||
"bricktracker_individual_part_lots"."description",
|
||||
"bricktracker_individual_part_lots"."created_date",
|
||||
"bricktracker_individual_part_lots"."storage",
|
||||
"bricktracker_individual_part_lots"."purchase_location",
|
||||
"bricktracker_individual_part_lots"."purchase_date",
|
||||
"bricktracker_individual_part_lots"."purchase_price",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name",
|
||||
COUNT("bricktracker_individual_parts"."id") AS "part_count"
|
||||
FROM "bricktracker_individual_part_lots"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_part_lots"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_part_lots"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
LEFT JOIN "bricktracker_individual_parts"
|
||||
ON "bricktracker_individual_part_lots"."id" = "bricktracker_individual_parts"."lot_id"
|
||||
GROUP BY "bricktracker_individual_part_lots"."id"
|
||||
ORDER BY "bricktracker_individual_part_lots"."created_date" DESC
|
||||
@@ -0,0 +1,26 @@
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
"bricktracker_individual_parts"."description",
|
||||
"bricktracker_individual_parts"."storage",
|
||||
"bricktracker_individual_parts"."purchase_location",
|
||||
"bricktracker_individual_parts"."purchase_date",
|
||||
"bricktracker_individual_parts"."purchase_price",
|
||||
"bricktracker_individual_parts"."lot_id",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."url"
|
||||
FROM "bricktracker_individual_parts"
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "bricktracker_individual_parts"."part" = "rebrickable_parts"."part"
|
||||
AND "bricktracker_individual_parts"."color" = "rebrickable_parts"."color_id"
|
||||
WHERE "bricktracker_individual_parts"."lot_id" = :lot_id
|
||||
ORDER BY "rebrickable_parts"."name" ASC, "bricktracker_individual_parts"."color" ASC
|
||||
@@ -0,0 +1,17 @@
|
||||
SELECT
|
||||
"bricktracker_individual_part_lots"."id",
|
||||
"bricktracker_individual_part_lots"."name",
|
||||
"bricktracker_individual_part_lots"."description",
|
||||
"bricktracker_individual_part_lots"."created_date",
|
||||
"bricktracker_individual_part_lots"."storage",
|
||||
"bricktracker_individual_part_lots"."purchase_location",
|
||||
"bricktracker_individual_part_lots"."purchase_date",
|
||||
"bricktracker_individual_part_lots"."purchase_price",
|
||||
"bricktracker_metadata_storages"."name" AS "storage_name",
|
||||
"bricktracker_metadata_purchase_locations"."name" AS "purchase_location_name"
|
||||
FROM "bricktracker_individual_part_lots"
|
||||
LEFT JOIN "bricktracker_metadata_storages"
|
||||
ON "bricktracker_individual_part_lots"."storage" IS NOT DISTINCT FROM "bricktracker_metadata_storages"."id"
|
||||
LEFT JOIN "bricktracker_metadata_purchase_locations"
|
||||
ON "bricktracker_individual_part_lots"."purchase_location" IS NOT DISTINCT FROM "bricktracker_metadata_purchase_locations"."id"
|
||||
WHERE "bricktracker_individual_part_lots"."id" = :id
|
||||
@@ -0,0 +1,132 @@
|
||||
-- description: Add individual minifigures and individual parts tables
|
||||
|
||||
-- Individual minifigures table - tracks individual minifigures not associated with sets
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_minifigures" (
|
||||
"id" TEXT NOT NULL,
|
||||
"figure" TEXT NOT NULL,
|
||||
"quantity" INTEGER NOT NULL DEFAULT 1,
|
||||
"description" TEXT,
|
||||
"storage" TEXT, -- Storage bin location
|
||||
"purchase_date" REAL, -- Purchase date
|
||||
"purchase_location" TEXT, -- Purchase location
|
||||
"purchase_price" REAL, -- Purchase price
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("figure") REFERENCES "rebrickable_minifigures"("figure"),
|
||||
FOREIGN KEY("storage") REFERENCES "bricktracker_metadata_storages"("id"),
|
||||
FOREIGN KEY("purchase_location") REFERENCES "bricktracker_metadata_purchase_locations"("id")
|
||||
);
|
||||
|
||||
-- Individual minifigure statuses
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_minifigure_statuses" (
|
||||
"id" TEXT NOT NULL,
|
||||
"status_minifigures_collected" BOOLEAN NOT NULL DEFAULT 0,
|
||||
"status_set_checked" BOOLEAN NOT NULL DEFAULT 0,
|
||||
"status_set_collected" BOOLEAN NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_minifigures"("id")
|
||||
);
|
||||
|
||||
-- Individual minifigure owners
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_minifigure_owners" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_minifigures"("id")
|
||||
);
|
||||
|
||||
-- Individual minifigure tags
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_minifigure_tags" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_minifigures"("id")
|
||||
);
|
||||
|
||||
-- Parts table for individual minifigures - tracks constituent parts
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_minifigure_parts" (
|
||||
"id" TEXT NOT NULL,
|
||||
"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,
|
||||
"checked" BOOLEAN DEFAULT 0,
|
||||
PRIMARY KEY("id", "part", "color", "spare"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_minifigures"("id"),
|
||||
FOREIGN KEY("part", "color") REFERENCES "rebrickable_parts"("part", "color_id")
|
||||
);
|
||||
|
||||
-- Individual parts table - tracks individual parts not associated with sets
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_parts" (
|
||||
"id" TEXT NOT NULL,
|
||||
"part" TEXT NOT NULL,
|
||||
"color" INTEGER NOT NULL,
|
||||
"quantity" INTEGER NOT NULL DEFAULT 1,
|
||||
"description" TEXT,
|
||||
"storage" TEXT, -- Storage bin location
|
||||
"purchase_date" REAL, -- Purchase date
|
||||
"purchase_location" TEXT, -- Purchase location
|
||||
"purchase_price" REAL, -- Purchase price
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("part", "color") REFERENCES "rebrickable_parts"("part", "color_id"),
|
||||
FOREIGN KEY("storage") REFERENCES "bricktracker_metadata_storages"("id"),
|
||||
FOREIGN KEY("purchase_location") REFERENCES "bricktracker_metadata_purchase_locations"("id")
|
||||
);
|
||||
|
||||
-- Individual part owners
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_owners" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_parts"("id")
|
||||
);
|
||||
|
||||
-- Individual part tags
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_tags" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_parts"("id")
|
||||
);
|
||||
|
||||
-- Individual part statuses
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_statuses" (
|
||||
"id" TEXT NOT NULL,
|
||||
"status_minifigures_collected" BOOLEAN NOT NULL DEFAULT 0,
|
||||
"status_set_checked" BOOLEAN NOT NULL DEFAULT 0,
|
||||
"status_set_collected" BOOLEAN NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_parts"("id")
|
||||
);
|
||||
|
||||
-- Indexes for individual minifigures
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigures_figure
|
||||
ON bricktracker_individual_minifigures(figure);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigures_storage
|
||||
ON bricktracker_individual_minifigures(storage);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigures_purchase_location
|
||||
ON bricktracker_individual_minifigures(purchase_location);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigures_purchase_date
|
||||
ON bricktracker_individual_minifigures(purchase_date);
|
||||
|
||||
-- Indexes for individual minifigure parts
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigure_parts_id_missing_damaged
|
||||
ON bricktracker_individual_minifigure_parts(id, missing, damaged);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_minifigure_parts_part_color
|
||||
ON bricktracker_individual_minifigure_parts(part, color);
|
||||
|
||||
-- Indexes for individual parts
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_parts_part_color
|
||||
ON bricktracker_individual_parts(part, color);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_parts_storage
|
||||
ON bricktracker_individual_parts(storage);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_parts_purchase_location
|
||||
ON bricktracker_individual_parts(purchase_location);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bricktracker_individual_parts_purchase_date
|
||||
ON bricktracker_individual_parts(purchase_date);
|
||||
@@ -0,0 +1,15 @@
|
||||
-- description: Populate missing image_id values in rebrickable_parts from image URLs
|
||||
-- Extract image_id from image URL for records with 'elements/' path
|
||||
-- Note: The url_for_image() method now handles extraction on-the-fly for missing values,
|
||||
-- so this migration only needs to handle the common case to improve performance
|
||||
|
||||
-- For images with 'elements/' in the path, extract the element ID (e.g., 300126 from .../elements/300126.jpg)
|
||||
UPDATE "rebrickable_parts"
|
||||
SET "image_id" = SUBSTR(
|
||||
"image",
|
||||
INSTR("image", 'elements/') + 9,
|
||||
INSTR(SUBSTR("image", INSTR("image", 'elements/') + 9), '.') - 1
|
||||
)
|
||||
WHERE "image" IS NOT NULL
|
||||
AND ("image_id" IS NULL OR "image_id" = '')
|
||||
AND "image" LIKE '%elements/%';
|
||||
@@ -0,0 +1,16 @@
|
||||
-- description: Create rebrickable_colors translation table for BrickLink color ID mapping
|
||||
-- This table caches color information from Rebrickable API to avoid repeated API calls
|
||||
-- and provides mapping between Rebrickable and BrickLink color IDs
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "rebrickable_colors" (
|
||||
"color_id" INTEGER PRIMARY KEY,
|
||||
"name" TEXT NOT NULL,
|
||||
"rgb" TEXT,
|
||||
"is_trans" BOOLEAN,
|
||||
"bricklink_color_id" INTEGER,
|
||||
"bricklink_color_name" TEXT
|
||||
);
|
||||
|
||||
-- Create index for faster lookups
|
||||
CREATE INDEX IF NOT EXISTS "idx_rebrickable_colors_bricklink"
|
||||
ON "rebrickable_colors"("bricklink_color_id");
|
||||
@@ -0,0 +1,54 @@
|
||||
-- description: Add individual part lots system for bulk/cart adding of parts
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
-- Create individual part lots table
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_lots" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT,
|
||||
"description" TEXT,
|
||||
"created_date" REAL NOT NULL,
|
||||
"storage" TEXT,
|
||||
"purchase_location" TEXT,
|
||||
"purchase_date" REAL,
|
||||
"purchase_price" REAL,
|
||||
FOREIGN KEY("storage") REFERENCES "bricktracker_metadata_storages"("id") ON DELETE SET NULL,
|
||||
FOREIGN KEY("purchase_location") REFERENCES "bricktracker_metadata_purchase_locations"("id") ON DELETE SET NULL
|
||||
);
|
||||
|
||||
-- Create index for faster lookups
|
||||
CREATE INDEX IF NOT EXISTS "idx_individual_part_lots_created_date"
|
||||
ON "bricktracker_individual_part_lots"("created_date");
|
||||
|
||||
-- Add missing/damaged/checked fields to individual parts table
|
||||
ALTER TABLE "bricktracker_individual_parts"
|
||||
ADD COLUMN "missing" INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE "bricktracker_individual_parts"
|
||||
ADD COLUMN "damaged" INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE "bricktracker_individual_parts"
|
||||
ADD COLUMN "checked" BOOLEAN NOT NULL DEFAULT 0;
|
||||
|
||||
-- Add lot_id column to individual parts table
|
||||
ALTER TABLE "bricktracker_individual_parts"
|
||||
ADD COLUMN "lot_id" TEXT;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "idx_individual_parts_lot_id"
|
||||
ON "bricktracker_individual_parts"("lot_id");
|
||||
|
||||
-- Create lot owners junction table
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_lot_owners" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_part_lots"("id") ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Create lot tags junction table
|
||||
CREATE TABLE IF NOT EXISTS "bricktracker_individual_part_lot_tags" (
|
||||
"id" TEXT NOT NULL,
|
||||
PRIMARY KEY("id"),
|
||||
FOREIGN KEY("id") REFERENCES "bricktracker_individual_part_lots"("id") ON DELETE CASCADE
|
||||
);
|
||||
|
||||
COMMIT;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- description: Add missing indexes for individual part lots optimization
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
-- Add storage index for lots table (for filtering by storage)
|
||||
CREATE INDEX IF NOT EXISTS "idx_individual_part_lots_storage"
|
||||
ON "bricktracker_individual_part_lots"("storage");
|
||||
|
||||
-- Add purchase location index for lots table (for filtering by purchase location)
|
||||
CREATE INDEX IF NOT EXISTS "idx_individual_part_lots_purchase_location"
|
||||
ON "bricktracker_individual_part_lots"("purchase_location");
|
||||
|
||||
COMMIT;
|
||||
@@ -1,10 +1,11 @@
|
||||
-- Combined query for both set-based and individual minifigures
|
||||
SELECT
|
||||
"bricktracker_minifigures"."quantity",
|
||||
"rebrickable_minifigures"."figure",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"combined"."quantity",
|
||||
"combined"."figure",
|
||||
"combined"."number",
|
||||
"combined"."number_of_parts",
|
||||
"combined"."name",
|
||||
"combined"."image",
|
||||
{% block total_missing %}
|
||||
NULL AS "total_missing", -- dummy for order: total_missing
|
||||
{% endblock %}
|
||||
@@ -15,12 +16,44 @@ SELECT
|
||||
NULL AS "total_quantity", -- dummy for order: total_quantity
|
||||
{% endblock %}
|
||||
{% block total_sets %}
|
||||
NULL AS "total_sets" -- dummy for order: total_sets
|
||||
NULL AS "total_sets", -- dummy for order: total_sets
|
||||
{% endblock %}
|
||||
FROM "bricktracker_minifigures"
|
||||
{% block total_individual %}
|
||||
NULL AS "total_individual" -- dummy for order: total_individual
|
||||
{% endblock %}
|
||||
FROM (
|
||||
-- Set-based minifigures
|
||||
SELECT
|
||||
"bricktracker_minifigures"."id",
|
||||
"bricktracker_minifigures"."quantity",
|
||||
"rebrickable_minifigures"."figure",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"bricktracker_minifigures"."rowid" AS "rowid",
|
||||
'set' AS "source_type"
|
||||
FROM "bricktracker_minifigures"
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_minifigures"."figure" IS NOT DISTINCT FROM "rebrickable_minifigures"."figure"
|
||||
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_minifigures"."figure" IS NOT DISTINCT FROM "rebrickable_minifigures"."figure"
|
||||
UNION ALL
|
||||
|
||||
-- Individual minifigures
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."quantity",
|
||||
"rebrickable_minifigures"."figure",
|
||||
"rebrickable_minifigures"."number",
|
||||
"rebrickable_minifigures"."number_of_parts",
|
||||
"rebrickable_minifigures"."name",
|
||||
"rebrickable_minifigures"."image",
|
||||
"bricktracker_individual_minifigures"."rowid" AS "rowid",
|
||||
'individual' AS "source_type"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
INNER JOIN "rebrickable_minifigures"
|
||||
ON "bricktracker_individual_minifigures"."figure" IS NOT DISTINCT FROM "rebrickable_minifigures"."figure"
|
||||
) AS "combined"
|
||||
|
||||
{% block join %}{% endblock %}
|
||||
|
||||
|
||||
@@ -9,16 +9,22 @@ SUM(IFNULL("problem_join"."total_damaged", 0)) AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity",
|
||||
SUM(IFNULL("combined"."quantity", 0)) AS "total_quantity",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
IFNULL(COUNT("bricktracker_minifigures"."id"), 0) AS "total_sets"
|
||||
SUM(CASE WHEN "combined"."source_type" = 'set' THEN 1 ELSE 0 END) AS "total_sets",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_individual %}
|
||||
SUM(CASE WHEN "combined"."source_type" = 'individual' THEN 1 ELSE 0 END) AS "total_individual"
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
-- LEFT JOIN + SELECT to avoid messing the total
|
||||
-- Combine parts from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
-- Set-based minifigure parts
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
@@ -29,18 +35,33 @@ LEFT JOIN (
|
||||
GROUP BY
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Individual minifigure parts
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"combined"."figure",
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures" ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
INNER JOIN "rebrickable_minifigures" AS "combined" ON "bricktracker_individual_minifigures"."figure" = "combined"."figure"
|
||||
GROUP BY
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"combined"."figure"
|
||||
) "problem_join"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "problem_join"."id"
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "problem_join"."figure"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "problem_join"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "problem_join"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
{% if search_query %}
|
||||
WHERE (LOWER("rebrickable_minifigures"."name") LIKE LOWER('%{{ search_query }}%'))
|
||||
WHERE (LOWER("combined"."name") LIKE LOWER('%{{ search_query }}%'))
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
@@ -10,31 +10,53 @@ SUM(IFNULL("problem_join"."total_damaged", 0)) AS "total_damaged",
|
||||
|
||||
{% block total_quantity %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_minifigures"."quantity", 0) ELSE 0 END) AS "total_quantity",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("combined"."quantity", 0)
|
||||
WHEN "combined"."source_type" = 'individual' AND "individual_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("combined"."quantity", 0)
|
||||
ELSE 0
|
||||
END) AS "total_quantity",
|
||||
{% else %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity",
|
||||
SUM(IFNULL("combined"."quantity", 0)) AS "total_quantity",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
COUNT(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_minifigures"."id" ELSE NULL END) AS "total_sets"
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "set_owners"."owner_{{ owner_id }}" = 1 THEN 1
|
||||
ELSE 0
|
||||
END) AS "total_sets",
|
||||
{% else %}
|
||||
COUNT("bricktracker_minifigures"."id") AS "total_sets"
|
||||
SUM(CASE WHEN "combined"."source_type" = 'set' THEN 1 ELSE 0 END) AS "total_sets",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_individual %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'individual' AND "individual_owners"."owner_{{ owner_id }}" = 1 THEN 1
|
||||
ELSE 0
|
||||
END) AS "total_individual"
|
||||
{% else %}
|
||||
SUM(CASE WHEN "combined"."source_type" = 'individual' THEN 1 ELSE 0 END) AS "total_individual"
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
-- Join with sets to get owner information
|
||||
INNER JOIN "bricktracker_sets"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_sets"."id"
|
||||
-- Join with set owners for set-based minifigures
|
||||
LEFT JOIN "bricktracker_sets"
|
||||
ON "combined"."id" = "bricktracker_sets"."id" AND "combined"."source_type" = 'set'
|
||||
|
||||
-- Left join with set owners (using dynamic columns)
|
||||
LEFT JOIN "bricktracker_set_owners"
|
||||
ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_owners"."id"
|
||||
LEFT JOIN "bricktracker_set_owners" AS "set_owners"
|
||||
ON "bricktracker_sets"."id" = "set_owners"."id"
|
||||
|
||||
-- Join with individual minifigure owners for individual minifigures
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners" AS "individual_owners"
|
||||
ON "combined"."id" = "individual_owners"."id" AND "combined"."source_type" = 'individual'
|
||||
|
||||
-- LEFT JOIN + SELECT to avoid messing the total
|
||||
LEFT JOIN (
|
||||
-- Set-based minifigure parts
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
@@ -47,25 +69,47 @@ LEFT JOIN (
|
||||
{% endif %}
|
||||
FROM "bricktracker_parts"
|
||||
INNER JOIN "bricktracker_sets" AS "parts_sets"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "parts_sets"."id"
|
||||
ON "bricktracker_parts"."id" = "parts_sets"."id"
|
||||
LEFT JOIN "bricktracker_set_owners" AS "owner_parts"
|
||||
ON "parts_sets"."id" IS NOT DISTINCT FROM "owner_parts"."id"
|
||||
ON "parts_sets"."id" = "owner_parts"."id"
|
||||
WHERE "bricktracker_parts"."figure" IS NOT NULL
|
||||
GROUP BY
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Individual minifigure parts
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "owner_individual"."owner_{{ owner_id }}" = 1 THEN "bricktracker_individual_minifigure_parts"."missing" ELSE 0 END) AS "total_missing",
|
||||
SUM(CASE WHEN "owner_individual"."owner_{{ owner_id }}" = 1 THEN "bricktracker_individual_minifigure_parts"."damaged" ELSE 0 END) AS "total_damaged"
|
||||
{% else %}
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
{% endif %}
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners" AS "owner_individual"
|
||||
ON "bricktracker_individual_minifigures"."id" = "owner_individual"."id"
|
||||
GROUP BY
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure"
|
||||
) "problem_join"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "problem_join"."id"
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "problem_join"."figure"
|
||||
ON "combined"."id" = "problem_join"."id"
|
||||
AND "combined"."figure" = "problem_join"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
{% set conditions = [] %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_set_owners"."owner_' ~ owner_id ~ '" = 1') %}
|
||||
{% set _ = conditions.append('(("combined"."source_type" = \'set\' AND "set_owners"."owner_' ~ owner_id ~ '" = 1) OR ("combined"."source_type" = \'individual\' AND "individual_owners"."owner_' ~ owner_id ~ '" = 1))') %}
|
||||
{% endif %}
|
||||
{% if search_query %}
|
||||
{% set _ = conditions.append('(LOWER("rebrickable_minifigures"."name") LIKE LOWER(\'%' ~ search_query ~ '%\'))') %}
|
||||
{% set _ = conditions.append('(LOWER("combined"."name") LIKE LOWER(\'%' ~ search_query ~ '%\'))') %}
|
||||
{% endif %}
|
||||
{% if conditions %}
|
||||
WHERE {{ conditions | join(' AND ') }}
|
||||
@@ -74,5 +118,5 @@ WHERE {{ conditions | join(' AND ') }}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
@@ -1,28 +1,59 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block total_damaged %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("parts_combined"."damaged") AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_parts"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_parts"."id"
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "bricktracker_parts"."figure"
|
||||
-- Join with parts from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."damaged"
|
||||
FROM "bricktracker_parts"
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
) AS "parts_combined"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "parts_combined"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "parts_combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "rebrickable_minifigures"."figure" IN (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
AND "bricktracker_parts"."damaged" > 0
|
||||
GROUP BY "bricktracker_parts"."figure"
|
||||
WHERE "combined"."figure" IN (
|
||||
-- Find figures with damaged parts from both sources
|
||||
SELECT "figure"
|
||||
FROM (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
AND "bricktracker_parts"."damaged" > 0
|
||||
|
||||
UNION
|
||||
|
||||
SELECT "bricktracker_individual_minifigures"."figure"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
WHERE "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."damaged" > 0
|
||||
) AS "damaged_figures"
|
||||
GROUP BY "figure"
|
||||
)
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id
|
||||
WHERE "combined"."id" IS NOT DISTINCT FROM :id AND "combined"."source_type" = 'set'
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,21 +1,40 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block total_missing %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("parts_combined"."missing") AS "total_missing",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("parts_combined"."damaged") AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_parts"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_parts"."id"
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "bricktracker_parts"."figure"
|
||||
-- Join with parts from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."missing",
|
||||
"bricktracker_parts"."damaged"
|
||||
FROM "bricktracker_parts"
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."missing",
|
||||
"bricktracker_individual_minifigure_parts"."damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
) AS "parts_combined"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "parts_combined"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "parts_combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure",
|
||||
"bricktracker_minifigures"."id"
|
||||
"combined"."figure",
|
||||
"combined"."id"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,28 +1,59 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block total_missing %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("parts_combined"."missing") AS "total_missing",
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_parts"
|
||||
ON "bricktracker_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_parts"."id"
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "bricktracker_parts"."figure"
|
||||
-- Join with parts from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."missing"
|
||||
FROM "bricktracker_parts"
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."missing"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
) AS "parts_combined"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "parts_combined"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "parts_combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "rebrickable_minifigures"."figure" IN (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
AND "bricktracker_parts"."missing" > 0
|
||||
GROUP BY "bricktracker_parts"."figure"
|
||||
WHERE "combined"."figure" IN (
|
||||
-- Find figures with missing parts from both sources
|
||||
SELECT "figure"
|
||||
FROM (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
AND "bricktracker_parts"."missing" > 0
|
||||
|
||||
UNION
|
||||
|
||||
SELECT "bricktracker_individual_minifigures"."figure"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
WHERE "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_individual_minifigure_parts"."missing" > 0
|
||||
) AS "missing_figures"
|
||||
GROUP BY "figure"
|
||||
)
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,21 +1,34 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block total_quantity %}
|
||||
SUM("bricktracker_minifigures"."quantity") AS "total_quantity",
|
||||
SUM("combined"."quantity") AS "total_quantity",
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "rebrickable_minifigures"."figure" IN (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
GROUP BY "bricktracker_parts"."figure"
|
||||
WHERE "combined"."figure" IN (
|
||||
-- Find figures from both set-based and individual minifigure parts
|
||||
SELECT "figure"
|
||||
FROM (
|
||||
SELECT "bricktracker_parts"."figure"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."figure" IS NOT NULL
|
||||
|
||||
UNION
|
||||
|
||||
SELECT "bricktracker_individual_minifigures"."figure"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
WHERE "bricktracker_individual_minifigure_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_individual_minifigure_parts"."color" IS NOT DISTINCT FROM :color
|
||||
) AS "parts_figures"
|
||||
GROUP BY "figure"
|
||||
)
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
@@ -9,16 +9,22 @@ IFNULL("problem_join"."total_damaged", 0) AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_quantity",
|
||||
SUM(IFNULL("combined"."quantity", 0)) AS "total_quantity",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
IFNULL(COUNT(DISTINCT "bricktracker_minifigures"."id"), 0) AS "total_sets"
|
||||
IFNULL(COUNT(DISTINCT "combined"."id"), 0) AS "total_sets",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_individual %}
|
||||
IFNULL(COUNT(DISTINCT "combined"."id"), 0) AS "total_individual"
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
-- LEFT JOIN + SELECT to avoid messing the total
|
||||
-- Combine parts from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
-- Set-based minifigure parts
|
||||
SELECT
|
||||
"bricktracker_parts"."figure",
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
@@ -26,15 +32,27 @@ LEFT JOIN (
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
|
||||
GROUP BY "bricktracker_parts"."figure"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Individual minifigure parts
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
SUM("bricktracker_individual_minifigure_parts"."missing") AS "total_missing",
|
||||
SUM("bricktracker_individual_minifigure_parts"."damaged") AS "total_damaged"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures" ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
WHERE "bricktracker_individual_minifigures"."figure" IS NOT DISTINCT FROM :figure
|
||||
GROUP BY "bricktracker_individual_minifigures"."figure"
|
||||
) "problem_join"
|
||||
ON "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM "problem_join"."figure"
|
||||
ON "combined"."figure" IS NOT DISTINCT FROM "problem_join"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure
|
||||
WHERE "combined"."figure" IS NOT DISTINCT FROM :figure
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"rebrickable_minifigures"."figure"
|
||||
"combined"."figure"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{% extends 'minifigure/base/base.sql' %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_minifigures"."id" IS NOT DISTINCT FROM :id
|
||||
AND "rebrickable_minifigures"."figure" IS NOT DISTINCT FROM :figure
|
||||
WHERE "combined"."id" IS NOT DISTINCT FROM :id
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM :figure
|
||||
AND "combined"."source_type" = 'set'
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
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",
|
||||
"bricktracker_parts"."checked",
|
||||
--"rebrickable_parts"."part",
|
||||
--"rebrickable_parts"."color_id",
|
||||
"combined"."id",
|
||||
"combined"."figure",
|
||||
"combined"."part",
|
||||
"combined"."color",
|
||||
"combined"."spare",
|
||||
"combined"."quantity",
|
||||
"combined"."element",
|
||||
"combined"."missing",
|
||||
"combined"."damaged",
|
||||
"combined"."checked",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
@@ -19,7 +16,6 @@ SELECT
|
||||
"rebrickable_parts"."bricklink_color_name",
|
||||
"rebrickable_parts"."bricklink_part_num",
|
||||
"rebrickable_parts"."name",
|
||||
--"rebrickable_parts"."category",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."image_id",
|
||||
"rebrickable_parts"."url",
|
||||
@@ -42,11 +38,62 @@ SELECT
|
||||
{% block total_minifigures %}
|
||||
NULL AS "total_minifigures" -- dummy for order: total_minifigures
|
||||
{% endblock %}
|
||||
FROM "bricktracker_parts"
|
||||
FROM (
|
||||
-- Parts from set-based minifigures
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare",
|
||||
"bricktracker_parts"."quantity",
|
||||
"bricktracker_parts"."element",
|
||||
"bricktracker_parts"."missing",
|
||||
"bricktracker_parts"."damaged",
|
||||
"bricktracker_parts"."checked",
|
||||
'set' AS "source_type"
|
||||
FROM "bricktracker_parts"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Parts from individual minifigures
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."part",
|
||||
"bricktracker_individual_minifigure_parts"."color",
|
||||
"bricktracker_individual_minifigure_parts"."spare",
|
||||
"bricktracker_individual_minifigure_parts"."quantity",
|
||||
"bricktracker_individual_minifigure_parts"."element",
|
||||
"bricktracker_individual_minifigure_parts"."missing",
|
||||
"bricktracker_individual_minifigure_parts"."damaged",
|
||||
"bricktracker_individual_minifigure_parts"."checked",
|
||||
'individual_minifigure' AS "source_type"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Individual/standalone parts (not from any set or minifigure)
|
||||
SELECT
|
||||
"bricktracker_individual_parts"."id",
|
||||
NULL AS "figure",
|
||||
"bricktracker_individual_parts"."part",
|
||||
"bricktracker_individual_parts"."color",
|
||||
0 AS "spare",
|
||||
"bricktracker_individual_parts"."quantity",
|
||||
NULL AS "element",
|
||||
"bricktracker_individual_parts"."missing",
|
||||
"bricktracker_individual_parts"."damaged",
|
||||
"bricktracker_individual_parts"."checked",
|
||||
'individual_part' AS "source_type"
|
||||
FROM "bricktracker_individual_parts"
|
||||
) AS "combined"
|
||||
|
||||
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"
|
||||
ON "combined"."part" IS NOT DISTINCT FROM "rebrickable_parts"."part"
|
||||
AND "combined"."color" IS NOT DISTINCT FROM "rebrickable_parts"."color_id"
|
||||
|
||||
{% block join %}{% endblock %}
|
||||
|
||||
|
||||
@@ -1,42 +1,57 @@
|
||||
{% extends 'part/base/base.sql' %}
|
||||
|
||||
{% block total_missing %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("combined"."missing") AS "total_missing",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("combined"."damaged") AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
SUM("bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
|
||||
SUM("combined"."quantity" * IFNULL("minifigure_quantities"."quantity", 1)) AS "total_quantity",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
IFNULL(COUNT(DISTINCT "bricktracker_parts"."id"), 0) AS "total_sets",
|
||||
IFNULL(COUNT(DISTINCT "combined"."id"), 0) AS "total_sets",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_minifigures %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_minifigures"
|
||||
SUM(IFNULL("minifigure_quantities"."quantity", 0)) AS "total_minifigures"
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_minifigures"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
-- Join to get minifigure quantities from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
"bricktracker_minifigures"."id",
|
||||
"bricktracker_minifigures"."figure",
|
||||
"bricktracker_minifigures"."quantity"
|
||||
FROM "bricktracker_minifigures"
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
) AS "minifigure_quantities"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "minifigure_quantities"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "minifigure_quantities"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
{% set conditions = [] %}
|
||||
{% if color_id and color_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."color" = ' ~ color_id) %}
|
||||
{% set _ = conditions.append('"combined"."color" = ' ~ color_id) %}
|
||||
{% endif %}
|
||||
{% if search_query %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("bricktracker_parts"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("combined"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set _ = conditions.append(search_condition) %}
|
||||
{% endif %}
|
||||
{% if skip_spare_parts %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."spare" = 0') %}
|
||||
{% set _ = conditions.append('"combined"."spare" = 0') %}
|
||||
{% endif %}
|
||||
{% if conditions %}
|
||||
WHERE {{ conditions | join(' AND ') }}
|
||||
@@ -45,7 +60,7 @@ WHERE {{ conditions | join(' AND ') }}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare"
|
||||
"combined"."part",
|
||||
"combined"."color",
|
||||
"combined"."spare"
|
||||
{% endblock %}
|
||||
|
||||
@@ -2,73 +2,126 @@
|
||||
|
||||
{% block total_missing %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."missing" ELSE 0 END) AS "total_missing",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."missing"
|
||||
WHEN "combined"."source_type" = 'individual_minifigure' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."missing"
|
||||
WHEN "combined"."source_type" = 'individual_part' AND "bricktracker_individual_part_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."missing"
|
||||
ELSE 0
|
||||
END) AS "total_missing",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("combined"."missing") AS "total_missing",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."damaged" ELSE 0 END) AS "total_damaged",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."damaged"
|
||||
WHEN "combined"."source_type" = 'individual_minifigure' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."damaged"
|
||||
WHEN "combined"."source_type" = 'individual_part' AND "bricktracker_individual_part_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."damaged"
|
||||
ELSE 0
|
||||
END) AS "total_damaged",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("combined"."damaged") AS "total_damaged",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1) ELSE 0 END) AS "total_quantity",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)
|
||||
WHEN "combined"."source_type" = 'individual_minifigure' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."quantity"
|
||||
WHEN "combined"."source_type" = 'individual_part' AND "bricktracker_individual_part_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."quantity"
|
||||
ELSE 0
|
||||
END) AS "total_quantity",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' THEN "combined"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)
|
||||
ELSE "combined"."quantity"
|
||||
END) AS "total_quantity",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
COUNT(DISTINCT CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."id" ELSE NULL END) AS "total_sets",
|
||||
COUNT(DISTINCT CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."id"
|
||||
ELSE NULL
|
||||
END) AS "total_sets",
|
||||
{% else %}
|
||||
COUNT(DISTINCT "bricktracker_parts"."id") AS "total_sets",
|
||||
COUNT(DISTINCT CASE WHEN "combined"."source_type" = 'set' THEN "combined"."id" ELSE NULL END) AS "total_sets",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_minifigures %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_minifigures"."quantity", 0) ELSE 0 END) AS "total_minifigures"
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_minifigures"."quantity", 0)
|
||||
WHEN "combined"."source_type" = 'individual_minifigure' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN 1
|
||||
ELSE 0
|
||||
END) AS "total_minifigures"
|
||||
{% else %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_minifigures"
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' THEN IFNULL("bricktracker_minifigures"."quantity", 0)
|
||||
WHEN "combined"."source_type" = 'individual_minifigure' THEN 1
|
||||
ELSE 0
|
||||
END) AS "total_minifigures"
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
-- Join with sets to get owner information
|
||||
INNER JOIN "bricktracker_sets"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_sets"."id"
|
||||
-- Left join with sets (for set-based parts)
|
||||
LEFT JOIN "bricktracker_sets"
|
||||
ON "combined"."source_type" = 'set'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_sets"."id"
|
||||
|
||||
-- Left join with set owners (using dynamic columns)
|
||||
LEFT JOIN "bricktracker_set_owners"
|
||||
ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_owners"."id"
|
||||
ON "combined"."source_type" = 'set'
|
||||
AND "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_owners"."id"
|
||||
|
||||
-- Left join with minifigures
|
||||
-- Left join with set-based minifigures
|
||||
LEFT JOIN "bricktracker_minifigures"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
ON "combined"."source_type" = 'set'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
|
||||
-- Left join with individual minifigures (for individual minifigure parts)
|
||||
LEFT JOIN "bricktracker_individual_minifigures"
|
||||
ON "combined"."source_type" = 'individual_minifigure'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigures"."id"
|
||||
|
||||
-- Left join with individual minifigure owners (using dynamic columns)
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners"
|
||||
ON "combined"."source_type" = 'individual_minifigure'
|
||||
AND "bricktracker_individual_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigure_owners"."id"
|
||||
|
||||
-- Left join with individual parts (for standalone parts)
|
||||
LEFT JOIN "bricktracker_individual_parts"
|
||||
ON "combined"."source_type" = 'individual_part'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_individual_parts"."id"
|
||||
|
||||
-- Left join with individual part owners (using dynamic columns)
|
||||
LEFT JOIN "bricktracker_individual_part_owners"
|
||||
ON "combined"."source_type" = 'individual_part'
|
||||
AND "bricktracker_individual_parts"."id" IS NOT DISTINCT FROM "bricktracker_individual_part_owners"."id"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
{% set conditions = [] %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_set_owners"."owner_' ~ owner_id ~ '" = 1') %}
|
||||
{% set owner_condition = '(("combined"."source_type" = \'set\' AND "bricktracker_set_owners"."owner_' ~ owner_id ~ '" = 1) OR ("combined"."source_type" = \'individual_minifigure\' AND "bricktracker_individual_minifigure_owners"."owner_' ~ owner_id ~ '" = 1) OR ("combined"."source_type" = \'individual_part\' AND "bricktracker_individual_part_owners"."owner_' ~ owner_id ~ '" = 1))' %}
|
||||
{% set _ = conditions.append(owner_condition) %}
|
||||
{% endif %}
|
||||
{% if color_id and color_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."color" = ' ~ color_id) %}
|
||||
{% set _ = conditions.append('"combined"."color" = ' ~ color_id) %}
|
||||
{% endif %}
|
||||
{% if search_query %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("bricktracker_parts"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("combined"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set _ = conditions.append(search_condition) %}
|
||||
{% endif %}
|
||||
{% if skip_spare_parts %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."spare" = 0') %}
|
||||
{% set _ = conditions.append('"combined"."spare" = 0') %}
|
||||
{% endif %}
|
||||
{% if conditions %}
|
||||
WHERE {{ conditions | join(' AND ') }}
|
||||
@@ -77,7 +130,7 @@ WHERE {{ conditions | join(' AND ') }}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare"
|
||||
"combined"."part",
|
||||
"combined"."color",
|
||||
"combined"."spare"
|
||||
{% endblock %}
|
||||
@@ -1,21 +1,88 @@
|
||||
-- Query parts from both set-based and individual minifigures
|
||||
SELECT
|
||||
"parts_combined"."id",
|
||||
"parts_combined"."figure",
|
||||
"parts_combined"."part",
|
||||
"parts_combined"."color",
|
||||
"parts_combined"."spare",
|
||||
SUM("parts_combined"."quantity") AS "quantity",
|
||||
"parts_combined"."element",
|
||||
SUM("parts_combined"."missing") AS "total_missing",
|
||||
SUM("parts_combined"."damaged") AS "total_damaged",
|
||||
MAX("parts_combined"."checked") AS "checked",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."bricklink_color_id",
|
||||
"rebrickable_parts"."bricklink_color_name",
|
||||
"rebrickable_parts"."bricklink_part_num",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."image_id",
|
||||
"rebrickable_parts"."url",
|
||||
"rebrickable_parts"."print",
|
||||
NULL AS "total_quantity",
|
||||
NULL AS "total_spare",
|
||||
NULL AS "total_sets",
|
||||
NULL AS "total_minifigures"
|
||||
FROM (
|
||||
-- Set-based minifigure parts
|
||||
SELECT
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare",
|
||||
"bricktracker_parts"."quantity",
|
||||
"bricktracker_parts"."element",
|
||||
"bricktracker_parts"."missing",
|
||||
"bricktracker_parts"."damaged",
|
||||
"bricktracker_parts"."checked"
|
||||
FROM "bricktracker_parts"
|
||||
WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
|
||||
|
||||
{% extends 'part/base/base.sql' %}
|
||||
UNION ALL
|
||||
|
||||
{% block total_missing %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
{% endblock %}
|
||||
-- Individual minifigure parts
|
||||
SELECT
|
||||
"bricktracker_individual_minifigure_parts"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigure_parts"."part",
|
||||
"bricktracker_individual_minifigure_parts"."color",
|
||||
"bricktracker_individual_minifigure_parts"."spare",
|
||||
"bricktracker_individual_minifigure_parts"."quantity",
|
||||
"bricktracker_individual_minifigure_parts"."element",
|
||||
"bricktracker_individual_minifigure_parts"."missing",
|
||||
"bricktracker_individual_minifigure_parts"."damaged",
|
||||
"bricktracker_individual_minifigure_parts"."checked"
|
||||
FROM "bricktracker_individual_minifigure_parts"
|
||||
INNER JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_individual_minifigure_parts"."id" = "bricktracker_individual_minifigures"."id"
|
||||
WHERE "bricktracker_individual_minifigures"."figure" IS NOT DISTINCT FROM :figure
|
||||
) AS "parts_combined"
|
||||
|
||||
{% block total_damaged %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
{% endblock %}
|
||||
INNER JOIN "rebrickable_parts"
|
||||
ON "parts_combined"."part" = "rebrickable_parts"."part"
|
||||
AND "parts_combined"."color" = "rebrickable_parts"."color_id"
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare"
|
||||
{% endblock %}
|
||||
"parts_combined"."part",
|
||||
"parts_combined"."color",
|
||||
"parts_combined"."spare",
|
||||
"parts_combined"."element",
|
||||
"rebrickable_parts"."color_name",
|
||||
"rebrickable_parts"."color_rgb",
|
||||
"rebrickable_parts"."color_transparent",
|
||||
"rebrickable_parts"."bricklink_color_id",
|
||||
"rebrickable_parts"."bricklink_color_name",
|
||||
"rebrickable_parts"."bricklink_part_num",
|
||||
"rebrickable_parts"."name",
|
||||
"rebrickable_parts"."image",
|
||||
"rebrickable_parts"."image_id",
|
||||
"rebrickable_parts"."url",
|
||||
"rebrickable_parts"."print"
|
||||
|
||||
{% if order %}
|
||||
-- Replace combined/bricktracker_parts references with parts_combined for this query
|
||||
ORDER BY {{ order | replace('"combined"', '"parts_combined"') | replace('"bricktracker_parts"', '"parts_combined"') }}
|
||||
{% endif %}
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
|
||||
{% block where %}
|
||||
WHERE "rebrickable_parts"."print" IS NOT DISTINCT FROM :print
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."part" IS DISTINCT FROM :part
|
||||
AND "combined"."color" IS NOT DISTINCT FROM :color
|
||||
AND "combined"."part" IS DISTINCT FROM :part
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color"
|
||||
"combined"."part",
|
||||
"combined"."color"
|
||||
{% endblock %}
|
||||
|
||||
@@ -2,82 +2,121 @@
|
||||
|
||||
{% block total_missing %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."missing" ELSE 0 END) AS "total_missing",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."missing"
|
||||
WHEN "combined"."source_type" = 'individual' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."missing"
|
||||
ELSE 0
|
||||
END) AS "total_missing",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("combined"."missing") AS "total_missing",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."damaged" ELSE 0 END) AS "total_damaged",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."damaged"
|
||||
WHEN "combined"."source_type" = 'individual' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."damaged"
|
||||
ELSE 0
|
||||
END) AS "total_damaged",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("combined"."damaged") AS "total_damaged",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1) ELSE 0 END) AS "total_quantity",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)
|
||||
WHEN "combined"."source_type" = 'individual' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."quantity" * IFNULL("bricktracker_individual_minifigures"."quantity", 1)
|
||||
ELSE 0
|
||||
END) AS "total_quantity",
|
||||
{% else %}
|
||||
SUM("bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' THEN "combined"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)
|
||||
WHEN "combined"."source_type" = 'individual' THEN "combined"."quantity" * IFNULL("bricktracker_individual_minifigures"."quantity", 1)
|
||||
ELSE "combined"."quantity"
|
||||
END) AS "total_quantity",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_sets %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
COUNT(DISTINCT CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "bricktracker_parts"."id" ELSE NULL END) AS "total_sets",
|
||||
COUNT(DISTINCT CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN "combined"."id"
|
||||
ELSE NULL
|
||||
END) AS "total_sets",
|
||||
{% else %}
|
||||
COUNT(DISTINCT "bricktracker_parts"."id") AS "total_sets",
|
||||
COUNT(DISTINCT CASE WHEN "combined"."source_type" = 'set' THEN "combined"."id" ELSE NULL END) AS "total_sets",
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block total_minifigures %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
SUM(CASE WHEN "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_minifigures"."quantity", 0) ELSE 0 END) AS "total_minifigures"
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' AND "bricktracker_set_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_minifigures"."quantity", 0)
|
||||
WHEN "combined"."source_type" = 'individual' AND "bricktracker_individual_minifigure_owners"."owner_{{ owner_id }}" = 1 THEN IFNULL("bricktracker_individual_minifigures"."quantity", 0)
|
||||
ELSE 0
|
||||
END) AS "total_minifigures"
|
||||
{% else %}
|
||||
SUM(IFNULL("bricktracker_minifigures"."quantity", 0)) AS "total_minifigures"
|
||||
SUM(CASE
|
||||
WHEN "combined"."source_type" = 'set' THEN IFNULL("bricktracker_minifigures"."quantity", 0)
|
||||
WHEN "combined"."source_type" = 'individual' THEN IFNULL("bricktracker_individual_minifigures"."quantity", 0)
|
||||
ELSE 0
|
||||
END) AS "total_minifigures"
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
-- Join with sets to get owner information
|
||||
INNER JOIN "bricktracker_sets"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_sets"."id"
|
||||
-- Left join with sets for set-based parts
|
||||
LEFT JOIN "bricktracker_sets"
|
||||
ON "combined"."source_type" = 'set'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_sets"."id"
|
||||
|
||||
-- Left join with set owners (using dynamic columns)
|
||||
LEFT JOIN "bricktracker_set_owners"
|
||||
ON "bricktracker_sets"."id" IS NOT DISTINCT FROM "bricktracker_set_owners"."id"
|
||||
|
||||
-- Left join with minifigures
|
||||
-- Left join with set-based minifigures
|
||||
LEFT JOIN "bricktracker_minifigures"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
ON "combined"."source_type" = 'set'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
|
||||
-- Left join with individual minifigures
|
||||
LEFT JOIN "bricktracker_individual_minifigures"
|
||||
ON "combined"."source_type" = 'individual'
|
||||
AND "combined"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigures"."id"
|
||||
|
||||
-- Left join with individual minifigure owners
|
||||
LEFT JOIN "bricktracker_individual_minifigure_owners"
|
||||
ON "bricktracker_individual_minifigures"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigure_owners"."id"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
{% set conditions = [] %}
|
||||
-- Always filter for problematic parts
|
||||
{% set _ = conditions.append('("bricktracker_parts"."missing" > 0 OR "bricktracker_parts"."damaged" > 0)') %}
|
||||
{% set _ = conditions.append('("combined"."missing" > 0 OR "combined"."damaged" > 0)') %}
|
||||
{% if owner_id and owner_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_set_owners"."owner_' ~ owner_id ~ '" = 1') %}
|
||||
{% set owner_condition = '(("combined"."source_type" = \'set\' AND "bricktracker_set_owners"."owner_' ~ owner_id ~ '" = 1) OR ("combined"."source_type" = \'individual\' AND "bricktracker_individual_minifigure_owners"."owner_' ~ owner_id ~ '" = 1))' %}
|
||||
{% set _ = conditions.append(owner_condition) %}
|
||||
{% endif %}
|
||||
{% if color_id and color_id != 'all' %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."color" = ' ~ color_id) %}
|
||||
{% set _ = conditions.append('"combined"."color" = ' ~ color_id) %}
|
||||
{% endif %}
|
||||
{% if search_query %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("bricktracker_parts"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set search_condition = '(LOWER("rebrickable_parts"."name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("rebrickable_parts"."color_name") LIKE LOWER(\'%' ~ search_query ~ '%\') OR LOWER("combined"."part") LIKE LOWER(\'%' ~ search_query ~ '%\'))' %}
|
||||
{% set _ = conditions.append(search_condition) %}
|
||||
{% endif %}
|
||||
{% if skip_spare_parts %}
|
||||
{% set _ = conditions.append('"bricktracker_parts"."spare" = 0') %}
|
||||
{% set _ = conditions.append('"combined"."spare" = 0') %}
|
||||
{% endif %}
|
||||
WHERE {{ conditions | join(' AND ') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare"
|
||||
"combined"."part",
|
||||
"combined"."color",
|
||||
"combined"."spare"
|
||||
{% endblock %}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
{% extends 'part/base/base.sql' %}
|
||||
|
||||
{% block total_missing %}
|
||||
IFNULL("bricktracker_parts"."missing", 0) AS "total_missing",
|
||||
IFNULL("combined"."missing", 0) AS "total_missing",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
IFNULL("bricktracker_parts"."damaged", 0) AS "total_damaged",
|
||||
IFNULL("combined"."damaged", 0) AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_parts"."id" IS NOT DISTINCT FROM :id
|
||||
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM :figure
|
||||
WHERE "combined"."id" IS NOT DISTINCT FROM :id
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM :figure
|
||||
{% endblock %}
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
{% block total_damaged %}{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_parts"."color" IS DISTINCT FROM :color
|
||||
AND "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
WHERE "combined"."color" IS DISTINCT FROM :color
|
||||
AND "combined"."part" IS NOT DISTINCT FROM :part
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color"
|
||||
"combined"."part",
|
||||
"combined"."color"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,34 +1,51 @@
|
||||
{% extends 'part/base/base.sql' %}
|
||||
|
||||
{% block total_missing %}
|
||||
SUM("bricktracker_parts"."missing") AS "total_missing",
|
||||
SUM("combined"."missing") AS "total_missing",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_damaged %}
|
||||
SUM("bricktracker_parts"."damaged") AS "total_damaged",
|
||||
SUM("combined"."damaged") AS "total_damaged",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_quantity %}
|
||||
SUM((NOT "bricktracker_parts"."spare") * "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_quantity",
|
||||
SUM((NOT "combined"."spare") * "combined"."quantity" * IFNULL("minifigure_quantities"."quantity", 1)) AS "total_quantity",
|
||||
{% endblock %}
|
||||
|
||||
{% block total_spare %}
|
||||
SUM("bricktracker_parts"."spare" * "bricktracker_parts"."quantity" * IFNULL("bricktracker_minifigures"."quantity", 1)) AS "total_spare",
|
||||
SUM("combined"."spare" * "combined"."quantity" * IFNULL("minifigure_quantities"."quantity", 1)) AS "total_spare",
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_minifigures"
|
||||
ON "bricktracker_parts"."id" IS NOT DISTINCT FROM "bricktracker_minifigures"."id"
|
||||
AND "bricktracker_parts"."figure" IS NOT DISTINCT FROM "bricktracker_minifigures"."figure"
|
||||
-- Join to get minifigure quantities from both set-based and individual minifigures
|
||||
LEFT JOIN (
|
||||
-- Set-based minifigure quantities
|
||||
SELECT
|
||||
"bricktracker_minifigures"."id",
|
||||
"bricktracker_minifigures"."figure",
|
||||
"bricktracker_minifigures"."quantity"
|
||||
FROM "bricktracker_minifigures"
|
||||
|
||||
UNION ALL
|
||||
|
||||
-- Individual minifigure quantities
|
||||
SELECT
|
||||
"bricktracker_individual_minifigures"."id",
|
||||
"bricktracker_individual_minifigures"."figure",
|
||||
"bricktracker_individual_minifigures"."quantity"
|
||||
FROM "bricktracker_individual_minifigures"
|
||||
) AS "minifigure_quantities"
|
||||
ON "combined"."id" IS NOT DISTINCT FROM "minifigure_quantities"."id"
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM "minifigure_quantities"."figure"
|
||||
{% endblock %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_parts"."part" IS NOT DISTINCT FROM :part
|
||||
AND "bricktracker_parts"."color" IS NOT DISTINCT FROM :color
|
||||
WHERE "combined"."part" IS NOT DISTINCT FROM :part
|
||||
AND "combined"."color" IS NOT DISTINCT FROM :color
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color"
|
||||
"combined"."part",
|
||||
"combined"."color"
|
||||
{% endblock %}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
{% extends 'part/base/base.sql' %}
|
||||
|
||||
{% block where %}
|
||||
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
|
||||
WHERE "combined"."id" IS NOT DISTINCT FROM :id
|
||||
AND "combined"."figure" IS NOT DISTINCT FROM :figure
|
||||
AND "combined"."part" IS NOT DISTINCT FROM :part
|
||||
AND "combined"."color" IS NOT DISTINCT FROM :color
|
||||
AND "combined"."spare" IS NOT DISTINCT FROM :spare
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
GROUP BY
|
||||
"bricktracker_parts"."id",
|
||||
"bricktracker_parts"."figure",
|
||||
"bricktracker_parts"."part",
|
||||
"bricktracker_parts"."color",
|
||||
"bricktracker_parts"."spare"
|
||||
"combined"."id",
|
||||
"combined"."figure",
|
||||
"combined"."part",
|
||||
"combined"."color",
|
||||
"combined"."spare"
|
||||
{% endblock %}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{% extends 'set/base/full.sql' %}
|
||||
|
||||
{% block where %}
|
||||
WHERE "bricktracker_sets"."storage" IS NULL
|
||||
{% endblock %}
|
||||
@@ -7,6 +7,18 @@ DROP COLUMN "owner_{{ id }}";
|
||||
ALTER TABLE "bricktracker_wish_owners"
|
||||
DROP COLUMN "owner_{{ id }}";
|
||||
|
||||
-- Also drop from individual minifigures
|
||||
ALTER TABLE "bricktracker_individual_minifigure_owners"
|
||||
DROP COLUMN "owner_{{ id }}";
|
||||
|
||||
-- Also drop from individual parts
|
||||
ALTER TABLE "bricktracker_individual_part_owners"
|
||||
DROP COLUMN "owner_{{ id }}";
|
||||
|
||||
-- Also drop from individual part lots
|
||||
ALTER TABLE "bricktracker_individual_part_lot_owners"
|
||||
DROP COLUMN "owner_{{ id }}";
|
||||
|
||||
DELETE FROM "bricktracker_metadata_owners"
|
||||
WHERE "bricktracker_metadata_owners"."id" IS NOT DISTINCT FROM '{{ id }}';
|
||||
|
||||
|
||||
@@ -7,6 +7,18 @@ ADD COLUMN "owner_{{ id }}" BOOLEAN NOT NULL DEFAULT 0;
|
||||
ALTER TABLE "bricktracker_wish_owners"
|
||||
ADD COLUMN "owner_{{ id }}" BOOLEAN NOT NULL DEFAULT 0;
|
||||
|
||||
-- Also inject into individual minifigures
|
||||
ALTER TABLE "bricktracker_individual_minifigure_owners"
|
||||
ADD COLUMN "owner_{{ id }}" BOOLEAN NOT NULL DEFAULT 0;
|
||||
|
||||
-- Also inject into individual parts
|
||||
ALTER TABLE "bricktracker_individual_part_owners"
|
||||
ADD COLUMN "owner_{{ id }}" BOOLEAN NOT NULL DEFAULT 0;
|
||||
|
||||
-- Also inject into individual part lots
|
||||
ALTER TABLE "bricktracker_individual_part_lot_owners"
|
||||
ADD COLUMN "owner_{{ id }}" BOOLEAN NOT NULL DEFAULT 0;
|
||||
|
||||
INSERT INTO "bricktracker_metadata_owners" (
|
||||
"id",
|
||||
"name"
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
{% extends 'set/metadata/storage/base.sql' %}
|
||||
|
||||
{% block total_sets %}
|
||||
IFNULL(COUNT("bricktracker_sets"."id"), 0) AS "total_sets"
|
||||
IFNULL(COUNT(DISTINCT "bricktracker_sets"."id"), 0) AS "total_sets",
|
||||
IFNULL(COUNT(DISTINCT "bricktracker_individual_minifigures"."id"), 0) AS "total_individual_minifigures",
|
||||
IFNULL(COUNT(DISTINCT "bricktracker_individual_parts"."id"), 0) AS "total_individual_parts"
|
||||
{% endblock %}
|
||||
|
||||
{% block join %}
|
||||
LEFT JOIN "bricktracker_sets"
|
||||
ON "bricktracker_metadata_storages"."id" IS NOT DISTINCT FROM "bricktracker_sets"."storage"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_minifigures"
|
||||
ON "bricktracker_metadata_storages"."id" IS NOT DISTINCT FROM "bricktracker_individual_minifigures"."storage"
|
||||
|
||||
LEFT JOIN "bricktracker_individual_parts"
|
||||
ON "bricktracker_metadata_storages"."id" IS NOT DISTINCT FROM "bricktracker_individual_parts"."storage"
|
||||
{% endblock %}
|
||||
|
||||
{% block group %}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user