BrickTracker/bricktracker/set_list.py

188 lines
5.2 KiB
Python
Raw Normal View History

from typing import Any, Self, Union
2025-01-17 11:03:00 +01:00
from flask import current_app
from .record_list import BrickRecordList
from .set_owner import BrickSetOwner
2025-01-31 16:34:52 +01:00
from .set_owner_list import BrickSetOwnerList
from .set_status_list import BrickSetStatusList
2025-02-03 23:45:35 +01:00
from .set_storage import BrickSetStorage
from .set_storage_list import BrickSetStorageList
from .set_tag import BrickSetTag
2025-01-31 18:08:53 +01:00
from .set_tag_list import BrickSetTagList
2025-01-17 11:03:00 +01:00
from .set import BrickSet
# All the sets from the database
class BrickSetList(BrickRecordList[BrickSet]):
themes: list[str]
order: str
2025-01-17 11:03:00 +01:00
# Queries
2025-01-31 20:46:36 +01:00
damaged_minifigure_query: str = 'set/list/damaged_minifigure'
damaged_part_query: str = 'set/list/damaged_part'
2025-01-17 11:03:00 +01:00
generic_query: str = 'set/list/generic'
light_query: str = 'set/list/light'
2025-01-17 11:03:00 +01:00
missing_minifigure_query: str = 'set/list/missing_minifigure'
missing_part_query: str = 'set/list/missing_part'
select_query: str = 'set/list/all'
using_minifigure_query: str = 'set/list/using_minifigure'
using_part_query: str = 'set/list/using_part'
2025-02-03 23:45:35 +01:00
using_storage_query: str = 'set/list/using_storage'
2025-01-17 11:03:00 +01:00
def __init__(self, /):
super().__init__()
# Placeholders
self.themes = []
# Store the order for this list
self.order = current_app.config['SETS_DEFAULT_ORDER']
2025-01-17 11:03:00 +01:00
# All the sets
def all(self, /) -> Self:
# Load the sets from the database
self.list(do_theme=True)
2025-01-17 11:03:00 +01:00
return self
2025-01-31 20:46:36 +01:00
# Sets with a minifigure part damaged
def damaged_minifigure(self, figure: str, /) -> Self:
# Save the parameters to the fields
self.fields.figure = figure
# Load the sets from the database
self.list(override_query=self.damaged_minifigure_query)
2025-01-31 20:46:36 +01:00
return self
# Sets with a part damaged
def damaged_part(self, part: str, color: int, /) -> Self:
# Save the parameters to the fields
self.fields.part = part
self.fields.color = color
# Load the sets from the database
self.list(override_query=self.damaged_part_query)
2025-01-17 11:03:00 +01:00
return self
# Last added sets
def last(self, /, *, limit: int = 6) -> Self:
2025-01-17 11:03:00 +01:00
# Randomize
if current_app.config['RANDOM']:
2025-01-17 11:03:00 +01:00
order = 'RANDOM()'
else:
order = '"bricktracker_sets"."rowid" DESC'
2025-01-17 11:03:00 +01:00
self.list(order=order, limit=limit)
return self
# Base set list
def list(
self,
/,
*,
override_query: str | None = None,
order: str | None = None,
limit: int | None = None,
do_theme: bool = False,
**context: Any,
) -> None:
themes = set()
if order is None:
order = self.order
# Load the sets from the database
for record in super().select(
override_query=override_query,
order=order,
limit=limit,
2025-02-03 16:46:45 +01:00
owners=BrickSetOwnerList.as_columns(),
statuses=BrickSetStatusList.as_columns(),
tags=BrickSetTagList.as_columns(),
):
2025-01-17 11:03:00 +01:00
brickset = BrickSet(record=record)
self.records.append(brickset)
if do_theme:
themes.add(brickset.theme.name)
2025-01-17 11:03:00 +01:00
# Convert the set into a list and sort it
if do_theme:
self.themes = list(themes)
self.themes.sort()
2025-01-17 11:03:00 +01:00
2025-01-31 20:46:36 +01:00
# Sets missing a minifigure part
2025-01-27 18:39:35 +01:00
def missing_minifigure(self, figure: str, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
2025-01-27 18:39:35 +01:00
self.fields.figure = figure
2025-01-17 11:03:00 +01:00
# Load the sets from the database
self.list(override_query=self.missing_minifigure_query)
2025-01-17 11:03:00 +01:00
return self
# Sets missing a part
2025-01-31 16:37:42 +01:00
def missing_part(self, part: str, color: int, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
2025-01-28 19:18:51 +01:00
self.fields.part = part
self.fields.color = color
2025-01-17 11:03:00 +01:00
# Load the sets from the database
self.list(override_query=self.missing_part_query)
2025-01-17 11:03:00 +01:00
return self
# Sets using a minifigure
2025-01-27 18:39:35 +01:00
def using_minifigure(self, figure: str, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
2025-01-27 18:39:35 +01:00
self.fields.figure = figure
2025-01-17 11:03:00 +01:00
# Load the sets from the database
self.list(override_query=self.using_minifigure_query)
2025-01-17 11:03:00 +01:00
return self
# Sets using a part
2025-01-31 16:37:42 +01:00
def using_part(self, part: str, color: int, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
2025-01-28 19:18:51 +01:00
self.fields.part = part
self.fields.color = color
2025-01-17 11:03:00 +01:00
# Load the sets from the database
self.list(override_query=self.using_part_query)
2025-01-17 11:03:00 +01:00
return self
2025-02-03 23:45:35 +01:00
# Sets using a storage
def using_storage(self, storage: BrickSetStorage, /) -> Self:
# Save the parameters to the fields
self.fields.storage = storage.fields.id
# Load the sets from the database
self.list(override_query=self.using_storage_query)
return self
# Helper to build the metadata lists
def set_metadata_lists(
as_class: bool = False
) -> dict[
str,
Union[
list[BrickSetOwner],
list[BrickSetStorage],
BrickSetStorageList,
list[BrickSetTag]
]
]:
return {
'brickset_owners': BrickSetOwnerList.list(),
'brickset_storages': BrickSetStorageList.list(as_class=as_class),
'brickset_tags': BrickSetTagList.list(),
}