Skip to content

Commit

Permalink
Merge branch 'master' into frontend-release
Browse files Browse the repository at this point in the history
  • Loading branch information
ndepaola committed Jan 13, 2024
2 parents 59831a8 + f770cbd commit 9f97035
Show file tree
Hide file tree
Showing 29 changed files with 600 additions and 401 deletions.
3 changes: 2 additions & 1 deletion desktop-tool/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Faces(str, Enum):


class Cardstocks(str, Enum):
S27 = "(S27) Smooth"
S30 = "(S30) Standard Smooth"
S33 = "(S33) Superior Smooth"
M31 = "(M31) Linen"
Expand Down Expand Up @@ -204,5 +205,5 @@ class TargetSites(Enum):
DPI_HEIGHT_RATIO = 300 / 1110 # TODO: share this between desktop tool and backend


BRACKETS = [18, 36, 55, 72, 90, 108, 126, 144, 162, 180, 198, 216, 234, 396, 504, 612]
PROJECT_MAX_SIZE = 612 # shared between target sites
THREADS = 5 # shared between CardImageCollections
41 changes: 34 additions & 7 deletions desktop-tool/src/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,49 @@ def authenticate(self) -> None:

# region project management

@exception_retry_skip_handler
def set_bracket(self, dropdown_id: str) -> None:
"""
Configure the project to fit into the smallest
:raises: If the project size does not fit into any bracket
"""

qty_dropdown = Select(self.driver.find_element(by=By.ID, value=dropdown_id))
bracket_options = sorted(
[
option_value
for option in qty_dropdown.options
if (option_value := int(option.get_attribute("value"))) >= self.order.details.quantity
]
)
assert bracket_options, (
f"Your project contains {bold(self.order.details.quantity)} cards - this does not fit into any bracket "
f"that {bold(self.target_site.name)} offers! The brackets are: "
f"{', '.join([bold(option) for option in bracket_options])}"
)
bracket = bracket_options[0]
print(
f"Configuring your project of {bold(self.order.details.quantity)} "
f"in the bracket of up to {bold(bracket)} cards."
)
qty_dropdown.select_by_value(str(bracket))

@exception_retry_skip_handler
def define_project(self) -> None:
self.assert_state(States.defining_order)
# Select card stock
stock_to_select = self.target_site.value.cardstock_site_name_mapping[Cardstocks(self.order.details.stock)]
stock_to_select = self.target_site.value.cardstock_site_name_mapping.get(Cardstocks(self.order.details.stock))
assert (
stock_to_select
), f"Cardstock {bold(self.order.details.stock)} is not supported by {bold(self.target_site.name)}!"
stock_dropdown = Select(
self.driver.find_element(by=By.ID, value=self.target_site.value.cardstock_dropdown_element_id)
)
stock_dropdown.select_by_visible_text(stock_to_select)

# Select number of cards
qty_dropdown = Select(
self.driver.find_element(by=By.ID, value=self.target_site.value.quantity_dropdown_element_id)
)
qty_dropdown.select_by_value(str(self.order.details.bracket))
self.set_bracket(dropdown_id=self.target_site.value.quantity_dropdown_element_id)

# Switch the finish to foil if the user ordered foil cards
if self.order.details.foil:
Expand Down Expand Up @@ -492,8 +520,7 @@ def redefine_project(self) -> None:
with self.switch_to_frame("sysifm_loginFrame"):
self.wait_until_javascript_object_is_defined("displayTotalCount")
self.execute_javascript("displayTotalCount()") # display the dropdown for "up to N cards"
qty_dropdown = Select(self.driver.find_element(by=By.ID, value="dro_total_count"))
qty_dropdown.select_by_value(str(self.order.details.bracket))
self.set_bracket(dropdown_id="dro_total_count")
qty = self.driver.find_element(by=By.ID, value="txt_card_number")
qty.clear()
qty.send_keys(str(self.order.details.quantity))
Expand Down
17 changes: 5 additions & 12 deletions desktop-tool/src/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,16 @@ def download_images(
@attr.s
class Details:
quantity: int = attr.ib(default=0)
bracket: int = attr.ib(default=0)
stock: str = attr.ib(default=constants.Cardstocks.S30.value)
foil: bool = attr.ib(default=False)

# region initialisation

def validate(self) -> None:
if not 0 < self.quantity <= self.bracket:
if self.quantity > constants.PROJECT_MAX_SIZE:
raise ValidationException(
f"Order quantity {self.quantity} outside allowable range of {bold(f'[0, {self.bracket}]')}!"
f"Order quantity {self.quantity} larger than maximum size of {bold(constants.PROJECT_MAX_SIZE)}!"
)
if self.bracket not in constants.BRACKETS:
raise ValidationException(f"Order bracket {self.bracket} not supported!")
if self.stock not in [x.value for x in constants.Cardstocks]:
raise ValidationException(f"Order cardstock {self.stock} not supported!")
if self.stock == constants.Cardstocks.P10 and self.foil is True:
Expand All @@ -270,13 +267,10 @@ def from_element(cls, element: Element) -> "Details":
quantity = 0
if (quantity_text := details_dict[constants.DetailsTags.quantity].text) is not None:
quantity = int(quantity_text)
bracket = 0
if (bracket_text := details_dict[constants.DetailsTags.bracket].text) is not None:
bracket = int(bracket_text)
stock = details_dict[constants.DetailsTags.stock].text or constants.Cardstocks.S30
foil: bool = details_dict[constants.DetailsTags.foil].text == "true"

details = cls(quantity=quantity, bracket=bracket, stock=stock, foil=foil)
details = cls(quantity=quantity, stock=stock, foil=foil)
return details

# endregion
Expand All @@ -295,9 +289,8 @@ def print_order_overview(self) -> None:
if self.name is not None:
print(f"Successfully parsed card order: {bold(self.name)}")
print(
f"Your order has a total of {bold(self.details.quantity)} cards, in the bracket of up "
f"to {bold(self.details.bracket)} cards.\n{bold(self.details.stock)} "
f"cardstock ({bold('foil' if self.details.foil else 'nonfoil')}.\n "
f"Your order has a total of {bold(self.details.quantity)} cards."
f"\n{bold(self.details.stock)} cardstock ({bold('foil' if self.details.foil else 'nonfoil')}).\n "
)

# endregion
Expand Down
2 changes: 2 additions & 0 deletions desktop-tool/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def wrapper(*args: Any, **kwargs: dict[str, Any]) -> Optional[F]:
while True:
try:
return func(*args, **kwargs)
except AssertionError as e:
raise e
except Exception as e:
print(f"An uncaught exception occurred:\n{bold(e)}\n")
action = inquirer.select(
Expand Down
44 changes: 6 additions & 38 deletions desktop-tool/tests/test_desktop_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def assert_card_image_collections_identical(a: CardImageCollection, b: CardImage


def assert_details_identical(a: Details, b: Details) -> None:
assert a.quantity == b.quantity and a.bracket == b.bracket and a.stock == b.stock and a.foil == b.foil
assert a.quantity == b.quantity and a.stock == b.stock and a.foil == b.foil


def assert_orders_identical(a: CardOrder, b: CardOrder) -> None:
Expand Down Expand Up @@ -292,7 +292,6 @@ def details_element_valid():
"""
<details>
<quantity>1</quantity>
<bracket>18</bracket>
<stock>(S30) Standard Smooth</stock>
<foil>false</foil>
</details>
Expand All @@ -302,13 +301,12 @@ def details_element_valid():


@pytest.fixture()
def details_element_quantity_greater_than_bracket() -> Generator[ElementTree.Element, None, None]:
def details_element_quantity_greater_than_max_size() -> Generator[ElementTree.Element, None, None]:
yield ElementTree.fromstring(
textwrap.dedent(
"""
<details>
<quantity>19</quantity>
<bracket>18</bracket>
<quantity>1900</quantity>
<stock>(S30) Standard Smooth</stock>
<foil>false</foil>
</details>
Expand All @@ -324,7 +322,6 @@ def details_element_invalid_cardstock() -> Generator[ElementTree.Element, None,
"""
<details>
<quantity>18</quantity>
<bracket>18</bracket>
<stock>Invalid Cardstock</stock>
<foil>false</foil>
</details>
Expand All @@ -333,22 +330,6 @@ def details_element_invalid_cardstock() -> Generator[ElementTree.Element, None,
)


@pytest.fixture()
def details_element_invalid_bracket() -> Generator[ElementTree.Element, None, None]:
yield ElementTree.fromstring(
textwrap.dedent(
"""
<details>
<quantity>18</quantity>
<bracket>940</bracket>
<stock>(S33) Superior Smooth</stock>
<foil>false</foil>
</details>
"""
)
)


# endregion
# region CardOrder

Expand All @@ -361,7 +342,6 @@ def card_order_element_valid() -> Generator[ElementTree.Element, None, None]:
<order>
<details>
<quantity>3</quantity>
<bracket>18</bracket>
<stock>(S30) Standard Smooth</stock>
<foil>false</foil>
</details>
Expand Down Expand Up @@ -399,7 +379,6 @@ def card_order_element_multiple_cardbacks() -> Generator[ElementTree.Element, No
<order>
<details>
<quantity>4</quantity>
<bracket>18</bracket>
<stock>(M31) Linen</stock>
<foil>false</foil>
</details>
Expand Down Expand Up @@ -447,7 +426,6 @@ def card_order_element_invalid_quantity() -> Generator[ElementTree.Element, None
<order>
<details>
<quantity>5</quantity>
<bracket>18</bracket>
<stock>(S33) Superior Smooth</stock>
<foil>true</foil>
</details>
Expand Down Expand Up @@ -480,7 +458,6 @@ def card_order_element_missing_front_image() -> Generator[ElementTree.Element, N
<order>
<details>
<quantity>4</quantity>
<bracket>18</bracket>
<stock>(S30) Standard Smooth</stock>
<foil>false</foil>
</details>
Expand Down Expand Up @@ -630,13 +607,13 @@ def test_details_valid(details_element_valid):
details = Details.from_element(details_element_valid)
assert_details_identical(
details,
Details(quantity=1, bracket=18, stock=constants.Cardstocks.S30, foil=False),
Details(quantity=1, stock=constants.Cardstocks.S30, foil=False),
)


def test_details_quantity_greater_than_bracket(input_enter, details_element_quantity_greater_than_bracket):
def test_details_quantity_greater_than_max_size(input_enter, details_element_quantity_greater_than_max_size):
with pytest.raises(SystemExit) as exc_info:
Details.from_element(details_element_quantity_greater_than_bracket)
Details.from_element(details_element_quantity_greater_than_max_size)
assert exc_info.value.code == 0


Expand All @@ -646,12 +623,6 @@ def test_details_invalid_cardstock(input_enter, details_element_invalid_cardstoc
assert exc_info.value.code == 0


def test_details_invalid_bracket(input_enter, details_element_invalid_bracket):
with pytest.raises(SystemExit) as exc_info:
Details.from_element(details_element_invalid_bracket)
assert exc_info.value.code == 0


# endregion

# region test CardOrder
Expand All @@ -663,7 +634,6 @@ def test_card_order_valid(card_order_valid):
CardOrder(
details=Details(
quantity=3,
bracket=18,
stock=constants.Cardstocks.S30,
foil=False,
),
Expand Down Expand Up @@ -710,7 +680,6 @@ def test_card_order_multiple_cardbacks(card_order_multiple_cardbacks):
CardOrder(
details=Details(
quantity=4,
bracket=18,
stock=constants.Cardstocks.M31,
foil=False,
),
Expand Down Expand Up @@ -767,7 +736,6 @@ def test_card_order_valid_from_file():
CardOrder(
details=Details(
quantity=10,
bracket=18,
stock=constants.Cardstocks.S30,
foil=True,
),
Expand Down
Loading

0 comments on commit 9f97035

Please sign in to comment.