Revert Bambu Lab cart and price allocation changes
This commit is contained in:
1 parent
8806fc85e5
commit
9f07b07bb7
12 files changed
+7
-1090
No files matched your search
@@ -1,106 +0,0 @@
|
||||
import hashlib
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from .bambu_eu_variant_ids import BAMBU_EU_PRODUCTS
|
||||
from .models import OrderItem
|
||||
|
||||
SHOP_URL = "https://eu.store.bambulab.com"
|
||||
SHOP_PRODUCTS = {
|
||||
name.casefold(): (slug, {row[:4]: row[4] for row in variants})
|
||||
for name, (slug, variants) in BAMBU_EU_PRODUCTS.items()
|
||||
}
|
||||
SHOP_PRODUCT_NAMES = {"pla silk dual color": "pla silk multi-color"}
|
||||
|
||||
|
||||
@dataclass
|
||||
class CartLine:
|
||||
key: str
|
||||
product_url: str
|
||||
exact_link: bool
|
||||
entries: list[OrderItem] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def variant(self):
|
||||
return self.entries[0].variant
|
||||
|
||||
@property
|
||||
def quantity(self):
|
||||
return sum(entry.quantity for entry in self.entries)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CartPlan:
|
||||
lines: list[CartLine]
|
||||
|
||||
@property
|
||||
def signature(self):
|
||||
rows = [
|
||||
(line.key, entry.pk, entry.variant_id, entry.quantity)
|
||||
for line in self.lines
|
||||
for entry in line.entries
|
||||
]
|
||||
prefix = OrderItem.EU_CART_QUOTE_PREFIX
|
||||
digest = hashlib.sha256(json.dumps(sorted(rows)).encode()).hexdigest()
|
||||
return prefix + digest[: 64 - len(prefix)]
|
||||
|
||||
|
||||
def plan_cart(entries):
|
||||
lines = {}
|
||||
for entry in entries:
|
||||
variant = entry.variant
|
||||
if (
|
||||
entry.status != OrderItem.Status.OPEN
|
||||
or variant.manufacturer.name.casefold() != "bambu lab"
|
||||
or not variant.is_active
|
||||
):
|
||||
continue
|
||||
product = variant.product_type.name.strip().casefold()
|
||||
slug, variants = SHOP_PRODUCTS.get(SHOP_PRODUCT_NAMES.get(product, product), ("", {}))
|
||||
shop_id = variants.get(
|
||||
(
|
||||
variant.manufacturer_color_code.strip(),
|
||||
variant.spool_type,
|
||||
variant.net_weight_g,
|
||||
str(variant.diameter_mm),
|
||||
)
|
||||
)
|
||||
key = f"shop_{shop_id}" if shop_id else f"local_{variant.pk}"
|
||||
product_url = f"{SHOP_URL}/products/{slug}" if slug else ""
|
||||
if shop_id:
|
||||
product_url += f"?id={shop_id}"
|
||||
line = lines.setdefault(key, CartLine(key, product_url, bool(shop_id)))
|
||||
line.entries.append(entry)
|
||||
return CartPlan(
|
||||
sorted(
|
||||
lines.values(),
|
||||
key=lambda line: (
|
||||
line.variant.product_type.name.casefold(),
|
||||
line.variant.color_label.casefold(),
|
||||
line.key,
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _allocate_cents(total, weights):
|
||||
if not total:
|
||||
return [0] * len(weights)
|
||||
divisor = sum(weights)
|
||||
shares, remainders = zip(*(divmod(total * weight, divisor) for weight in weights), strict=True)
|
||||
allocations = list(shares)
|
||||
remaining = total - sum(shares)
|
||||
for index in sorted(range(len(weights)), key=lambda index: -remainders[index])[:remaining]:
|
||||
allocations[index] += 1
|
||||
return allocations
|
||||
|
||||
|
||||
def allocate_prices(plan, line_cents, total_cents):
|
||||
# Distribute any discount on the whole cart proportionally, keeping the sum exact.
|
||||
totals = _allocate_cents(total_cents, line_cents)
|
||||
result = {}
|
||||
for line, cents in zip(plan.lines, totals, strict=True):
|
||||
entries = sorted(line.entries, key=lambda entry: entry.pk)
|
||||
shares = _allocate_cents(cents, [entry.quantity for entry in entries])
|
||||
result.update((entry.pk, share) for entry, share in zip(entries, shares, strict=True))
|
||||
return result
|
||||
@@ -1,551 +0,0 @@
|
||||
"""Variant links in the Bambu Lab EU Store structured catalog."""
|
||||
|
||||
# Product, product page slug, and (colour code, spool type, grams, diameter, shop ID).
|
||||
BAMBU_EU_PRODUCTS = {
|
||||
"ABS": (
|
||||
"abs-filament",
|
||||
(
|
||||
("40100", "refill", 1000, "1.75", "42917259083995"),
|
||||
("40100", "spool", 1000, "1.75", "46963746046300"),
|
||||
("40101", "refill", 1000, "1.75", "42917259051227"),
|
||||
("40101", "spool", 1000, "1.75", "46963744145756"),
|
||||
("40102", "refill", 1000, "1.75", "49503790596444"),
|
||||
("40102", "spool", 1000, "1.75", "46841277022556"),
|
||||
("40200", "refill", 1000, "1.75", "42917259116763"),
|
||||
("40300", "refill", 1000, "1.75", "48953684394332"),
|
||||
("40402", "refill", 1000, "1.75", "49130709418332"),
|
||||
("40500", "refill", 1000, "1.75", "48953688850780"),
|
||||
("40502", "refill", 1000, "1.75", "49037506216284"),
|
||||
("40600", "refill", 1000, "1.75", "42917259182299"),
|
||||
("40601", "refill", 1000, "1.75", "49130708730204"),
|
||||
("40602", "refill", 1000, "1.75", "49130708009308"),
|
||||
),
|
||||
),
|
||||
"ABS-GF": (
|
||||
"abs-gf",
|
||||
(
|
||||
("41100", "spool", 1000, "1.75", "48011475845468"),
|
||||
("41101", "spool", 1000, "1.75", "48011475911004"),
|
||||
("41102", "spool", 1000, "1.75", "48011475878236"),
|
||||
("41200", "spool", 1000, "1.75", "48011475779932"),
|
||||
("41300", "spool", 1000, "1.75", "48011475714396"),
|
||||
("41600", "spool", 1000, "1.75", "48725868872028"),
|
||||
),
|
||||
),
|
||||
"ASA": (
|
||||
"asa-filament",
|
||||
(
|
||||
("45100", "spool", 1000, "1.75", "46930444353884"),
|
||||
("45101", "spool", 1000, "1.75", "46930444222812"),
|
||||
("45102", "spool", 1000, "1.75", "46930444255580"),
|
||||
("45200", "spool", 1000, "1.75", "46930444288348"),
|
||||
("45500", "spool", 1000, "1.75", "46930444386652"),
|
||||
("45600", "spool", 1000, "1.75", "46930444321116"),
|
||||
),
|
||||
),
|
||||
"ASA Aero": (
|
||||
"asa-aero",
|
||||
(("46100", "spool", 1000, "1.75", "47966894096732"),),
|
||||
),
|
||||
"ASA-CF": (
|
||||
"asa-cf",
|
||||
(("46101", "spool", 1000, "1.75", "49410590409052"),),
|
||||
),
|
||||
"PA6-CF (1 kg)": (
|
||||
"pa6-cf",
|
||||
(("72100", "spool", 1000, "1.75", "47023571534172"),),
|
||||
),
|
||||
"PA6-CF (500 g)": (
|
||||
"pa6-cf",
|
||||
(("72100", "spool", 500, "1.75", "47023571468636"),),
|
||||
),
|
||||
"PA6-GF": (
|
||||
"pa6-gf",
|
||||
(
|
||||
("72102", "spool", 1000, "1.75", "47833416270172"),
|
||||
("72103", "spool", 1000, "1.75", "47833416302940"),
|
||||
("72104", "spool", 1000, "1.75", "47833416335708"),
|
||||
("72200", "spool", 1000, "1.75", "47833416139100"),
|
||||
("72400", "spool", 1000, "1.75", "47833416171868"),
|
||||
("72500", "spool", 1000, "1.75", "47833416204636"),
|
||||
("72600", "spool", 1000, "1.75", "47833416106332"),
|
||||
("72800", "spool", 1000, "1.75", "47833416237404"),
|
||||
),
|
||||
),
|
||||
"PAHT-CF (1 kg)": (
|
||||
"paht-cf",
|
||||
(("70100", "spool", 1000, "1.75", "43923846889691"),),
|
||||
),
|
||||
"PAHT-CF (500 g)": (
|
||||
"paht-cf",
|
||||
(("70100", "spool", 500, "1.75", "43923846856923"),),
|
||||
),
|
||||
"PC": (
|
||||
"pc-filament",
|
||||
(
|
||||
("60100", "spool", 1000, "1.75", "43408627138779"),
|
||||
("60101", "spool", 1000, "1.75", "46673173807452"),
|
||||
("60102", "spool", 1000, "1.75", "43408627106011"),
|
||||
("60103", "spool", 1000, "1.75", "46673173774684"),
|
||||
),
|
||||
),
|
||||
"PC FR": (
|
||||
"pc-fr",
|
||||
(
|
||||
("63100", "spool", 1000, "1.75", "53649920950620"),
|
||||
("63101", "spool", 1000, "1.75", "53649920983388"),
|
||||
("63102", "spool", 1000, "1.75", "53649921016156"),
|
||||
),
|
||||
),
|
||||
"PET-CF (1 kg)": (
|
||||
"pet-cf",
|
||||
(("71100", "spool", 1000, "1.75", "44012072763611"),),
|
||||
),
|
||||
"PET-CF (500 g)": (
|
||||
"pet-cf",
|
||||
(("71100", "spool", 500, "1.75", "44012072730843"),),
|
||||
),
|
||||
"PETG Basic": (
|
||||
"petg-basic",
|
||||
(
|
||||
("30105", "refill", 1000, "1.75", "703100873995440140"),
|
||||
("30105", "spool", 1000, "1.75", "703100873995440146"),
|
||||
("30106", "refill", 1000, "1.75", "703100873995440152"),
|
||||
("30106", "spool", 1000, "1.75", "703100873995440158"),
|
||||
("30107", "refill", 1000, "1.75", "703100873995440164"),
|
||||
("30107", "spool", 1000, "1.75", "703100873995440170"),
|
||||
("30108", "refill", 1000, "1.75", "738173299117862983"),
|
||||
("30108", "spool", 1000, "1.75", "738173299117862991"),
|
||||
("30201", "refill", 1000, "1.75", "703100873995440176"),
|
||||
("30201", "spool", 1000, "1.75", "703100873995440182"),
|
||||
("30301", "refill", 1000, "1.75", "738173299117862919"),
|
||||
("30301", "spool", 1000, "1.75", "738173299117862927"),
|
||||
("30402", "refill", 1000, "1.75", "703100873995440188"),
|
||||
("30402", "spool", 1000, "1.75", "703100873995440194"),
|
||||
("30403", "refill", 1000, "1.75", "738173299117862967"),
|
||||
("30403", "spool", 1000, "1.75", "738173299117862975"),
|
||||
("30502", "refill", 1000, "1.75", "738173299117862935"),
|
||||
("30502", "spool", 1000, "1.75", "738173299117862943"),
|
||||
("30503", "refill", 1000, "1.75", "738173299117862999"),
|
||||
("30503", "spool", 1000, "1.75", "738173299117863007"),
|
||||
("30603", "refill", 1000, "1.75", "703100873995440200"),
|
||||
("30603", "spool", 1000, "1.75", "703100873995440206"),
|
||||
("30604", "refill", 1000, "1.75", "738173299117862951"),
|
||||
("30604", "spool", 1000, "1.75", "738173299117862959"),
|
||||
("30800", "refill", 1000, "1.75", "703100873995440212"),
|
||||
("30800", "spool", 1000, "1.75", "703100873995440218"),
|
||||
),
|
||||
),
|
||||
"PETG Matte": (
|
||||
"petg-matte",
|
||||
(
|
||||
("35100", "refill", 1000, "1.75", "775952393450868750"),
|
||||
("35100", "spool", 1000, "1.75", "775952393450868758"),
|
||||
("35101", "refill", 1000, "1.75", "775952393450868766"),
|
||||
("35101", "spool", 1000, "1.75", "775952393450868774"),
|
||||
("35102", "refill", 1000, "1.75", "775952393450868798"),
|
||||
("35102", "spool", 1000, "1.75", "775952393450868806"),
|
||||
("35500", "refill", 1000, "1.75", "775952393450868782"),
|
||||
("35500", "spool", 1000, "1.75", "775952393450868790"),
|
||||
("35501", "refill", 1000, "1.75", "775952393450868862"),
|
||||
("35501", "spool", 1000, "1.75", "775952393450868870"),
|
||||
("35600", "refill", 1000, "1.75", "775952393450868830"),
|
||||
("35600", "spool", 1000, "1.75", "775952393450868838"),
|
||||
("35700", "refill", 1000, "1.75", "775952393450868814"),
|
||||
("35700", "spool", 1000, "1.75", "775952393450868822"),
|
||||
("35702", "refill", 1000, "1.75", "775952393450868846"),
|
||||
("35702", "spool", 1000, "1.75", "775952393450868854"),
|
||||
),
|
||||
),
|
||||
"PETG Translucent": (
|
||||
"petg-translucent",
|
||||
(
|
||||
("32100", "refill", 1000, "1.75", "49271784472924"),
|
||||
("32101", "refill", 1000, "1.75", "48786708922716"),
|
||||
("32101", "spool", 1000, "1.75", "48055896047964"),
|
||||
("32200", "refill", 1000, "1.75", "49074123145564"),
|
||||
("32300", "refill", 1000, "1.75", "49130706469212"),
|
||||
("32500", "refill", 1000, "1.75", "49130705977692"),
|
||||
("32501", "refill", 1000, "1.75", "49074122162524"),
|
||||
("32501", "spool", 1000, "1.75", "47502089290076"),
|
||||
("32600", "refill", 1000, "1.75", "49192018149724"),
|
||||
("32600", "spool", 1000, "1.75", "47502089191772"),
|
||||
("32700", "refill", 1000, "1.75", "49130706829660"),
|
||||
("32800", "refill", 1000, "1.75", "49192019788124"),
|
||||
),
|
||||
),
|
||||
"PETG-CF": (
|
||||
"petg-cf",
|
||||
(
|
||||
("31100", "refill", 1000, "1.75", "49238020686172"),
|
||||
("31100", "spool", 1000, "1.75", "43982559412443"),
|
||||
("31101", "refill", 1000, "1.75", "785030589559357440"),
|
||||
("31101", "spool", 1000, "1.75", "47228683714908"),
|
||||
("31200", "spool", 1000, "1.75", "46935828300124"),
|
||||
("31500", "spool", 1000, "1.75", "46935828365660"),
|
||||
("31600", "spool", 1000, "1.75", "46935828332892"),
|
||||
("31700", "spool", 1000, "1.75", "46935828267356"),
|
||||
),
|
||||
),
|
||||
"PLA Aero": (
|
||||
"pla-aero",
|
||||
(
|
||||
("14102", "spool", 1000, "1.75", "46953506505052"),
|
||||
("14104", "spool", 1000, "1.75", "49078101279068"),
|
||||
),
|
||||
),
|
||||
"PLA Basic": (
|
||||
"pla-basic-filament",
|
||||
(
|
||||
("10100", "refill", 1000, "1.75", "42911327846619"),
|
||||
("10100", "spool", 1000, "1.75", "43992829919451"),
|
||||
("10101", "refill", 1000, "1.75", "43923829129435"),
|
||||
("10101", "spool", 1000, "1.75", "43992830017755"),
|
||||
("10102", "refill", 1000, "1.75", "42911328010459"),
|
||||
("10103", "refill", 1000, "1.75", "42911327944923"),
|
||||
("10103", "spool", 1000, "1.75", "46956524241244"),
|
||||
("10104", "refill", 1000, "1.75", "49299256148316"),
|
||||
("10105", "refill", 1000, "1.75", "49299261423964"),
|
||||
("10200", "refill", 1000, "1.75", "42911328239835"),
|
||||
("10200", "spool", 1000, "1.75", "43992829952219"),
|
||||
("10201", "refill", 1000, "1.75", "42911327977691"),
|
||||
("10202", "refill", 1000, "1.75", "47794655068508"),
|
||||
("10203", "refill", 1000, "1.75", "42911328141531"),
|
||||
("10204", "refill", 1000, "1.75", "49636028285276"),
|
||||
("10204", "spool", 1000, "1.75", "53951742509404"),
|
||||
("10205", "refill", 1000, "1.75", "49636008395100"),
|
||||
("10300", "refill", 1000, "1.75", "42911327879387"),
|
||||
("10301", "refill", 1000, "1.75", "49636029530460"),
|
||||
("10301", "spool", 1000, "1.75", "53951744213340"),
|
||||
("10400", "refill", 1000, "1.75", "42911328043227"),
|
||||
("10401", "refill", 1000, "1.75", "47794656706908"),
|
||||
("10402", "refill", 1000, "1.75", "49636014031196"),
|
||||
("10402", "spool", 1000, "1.75", "53951741002076"),
|
||||
("10501", "refill", 1000, "1.75", "47794655461724"),
|
||||
("10501", "spool", 1000, "1.75", "46673378607452"),
|
||||
("10502", "refill", 1000, "1.75", "47794656477532"),
|
||||
("10502", "spool", 1000, "1.75", "599083438524203008"),
|
||||
("10503", "refill", 1000, "1.75", "49636024353116"),
|
||||
("10601", "refill", 1000, "1.75", "42911327912155"),
|
||||
("10601", "spool", 1000, "1.75", "46738546622812"),
|
||||
("10602", "refill", 1000, "1.75", "42911328075995"),
|
||||
("10603", "refill", 1000, "1.75", "47794657329500"),
|
||||
("10603", "spool", 1000, "1.75", "54004785217884"),
|
||||
("10604", "refill", 1000, "1.75", "49636029006172"),
|
||||
("10605", "refill", 1000, "1.75", "49636016783708"),
|
||||
("10700", "refill", 1000, "1.75", "47794657362268"),
|
||||
("10701", "refill", 1000, "1.75", "49636021436764"),
|
||||
("10701", "spool", 1000, "1.75", "53951741821276"),
|
||||
("10800", "refill", 1000, "1.75", "42911328207067"),
|
||||
("10801", "refill", 1000, "1.75", "47794655101276"),
|
||||
("10802", "refill", 1000, "1.75", "49636027236700"),
|
||||
("10802", "spool", 1000, "1.75", "53951748702556"),
|
||||
),
|
||||
),
|
||||
"PLA Basic Gradient": (
|
||||
"pla-basic-gradient",
|
||||
(
|
||||
("10900", "spool", 1000, "1.75", "46901930492252"),
|
||||
("10901", "spool", 1000, "1.75", "46901930525020"),
|
||||
("10902", "spool", 1000, "1.75", "46901930557788"),
|
||||
("10903", "spool", 1000, "1.75", "53638219071836"),
|
||||
("10904", "spool", 1000, "1.75", "53638219104604"),
|
||||
("10905", "spool", 1000, "1.75", "53638219006300"),
|
||||
("10906", "spool", 1000, "1.75", "53687078420828"),
|
||||
("10907", "spool", 1000, "1.75", "53638219039068"),
|
||||
),
|
||||
),
|
||||
"PLA Galaxy": (
|
||||
"pla-galaxy",
|
||||
(
|
||||
("13203", "spool", 1000, "1.75", "47730388074844"),
|
||||
("13503", "spool", 1000, "1.75", "47730388107612"),
|
||||
("13504", "spool", 1000, "1.75", "47730388140380"),
|
||||
("13602", "spool", 1000, "1.75", "47730388173148"),
|
||||
),
|
||||
),
|
||||
"PLA Glow": (
|
||||
"pla-glow",
|
||||
(
|
||||
("15200", "spool", 1000, "1.75", "47396823630172"),
|
||||
("15300", "spool", 1000, "1.75", "47396823531868"),
|
||||
("15400", "spool", 1000, "1.75", "47396823662940"),
|
||||
("15500", "spool", 1000, "1.75", "47396823597404"),
|
||||
("15600", "spool", 1000, "1.75", "47396823564636"),
|
||||
),
|
||||
),
|
||||
"PLA Marble": (
|
||||
"pla-marble",
|
||||
(
|
||||
("13103", "spool", 1000, "1.75", "43964050964699"),
|
||||
("13201", "spool", 1000, "1.75", "46756997333340"),
|
||||
),
|
||||
),
|
||||
"PLA Matte": (
|
||||
"pla-matte",
|
||||
(
|
||||
("11100", "refill", 1000, "1.75", "42996742586587"),
|
||||
("11100", "spool", 1000, "1.75", "43992833229019"),
|
||||
("11101", "refill", 1000, "1.75", "42996742750427"),
|
||||
("11101", "spool", 1000, "1.75", "43992833261787"),
|
||||
("11102", "refill", 1000, "1.75", "42996742619355"),
|
||||
("11102", "spool", 1000, "1.75", "43992833327323"),
|
||||
("11103", "refill", 1000, "1.75", "53926028607836"),
|
||||
("11104", "refill", 1000, "1.75", "53926028837212"),
|
||||
("11200", "refill", 1000, "1.75", "42996742848731"),
|
||||
("11200", "spool", 1000, "1.75", "43992833294555"),
|
||||
("11201", "refill", 1000, "1.75", "42996742684891"),
|
||||
("11202", "refill", 1000, "1.75", "47794659164508"),
|
||||
("11203", "refill", 1000, "1.75", "53926028804444"),
|
||||
("11204", "refill", 1000, "1.75", "53926028640604"),
|
||||
("11300", "refill", 1000, "1.75", "42996742652123"),
|
||||
("11300", "spool", 1000, "1.75", "46893218038108"),
|
||||
("11400", "refill", 1000, "1.75", "42996742717659"),
|
||||
("11401", "refill", 1000, "1.75", "48612736401756"),
|
||||
("11500", "refill", 1000, "1.75", "42996742783195"),
|
||||
("11501", "refill", 1000, "1.75", "47794659656028"),
|
||||
("11502", "refill", 1000, "1.75", "53926028706140"),
|
||||
("11600", "refill", 1000, "1.75", "42996751073499"),
|
||||
("11600", "spool", 1000, "1.75", "43992833360091"),
|
||||
("11601", "refill", 1000, "1.75", "42996742881499"),
|
||||
("11602", "refill", 1000, "1.75", "47794659688796"),
|
||||
("11603", "refill", 1000, "1.75", "53926028673372"),
|
||||
("11700", "refill", 1000, "1.75", "42996742914267"),
|
||||
("11800", "refill", 1000, "1.75", "42996742815963"),
|
||||
("11801", "refill", 1000, "1.75", "47794659426652"),
|
||||
("11802", "refill", 1000, "1.75", "53926028738908"),
|
||||
("11803", "refill", 1000, "1.75", "53926028771676"),
|
||||
),
|
||||
),
|
||||
"PLA Metal": (
|
||||
"pla-metal",
|
||||
(
|
||||
("13100", "spool", 1000, "1.75", "46797850902876"),
|
||||
("13400", "spool", 1000, "1.75", "46797851099484"),
|
||||
("13500", "spool", 1000, "1.75", "43959185277147"),
|
||||
("13600", "spool", 1000, "1.75", "43959185244379"),
|
||||
("13800", "spool", 1000, "1.75", "43959185015003"),
|
||||
),
|
||||
),
|
||||
"PLA Palette": (
|
||||
"pla-palette",
|
||||
(
|
||||
("18100", "spool", 1000, "1.75", "792649782224003114"),
|
||||
("18101", "spool", 1000, "1.75", "792649782224003106"),
|
||||
("18200", "spool", 1000, "1.75", "792649782224003090"),
|
||||
("18400", "spool", 1000, "1.75", "792649782224003098"),
|
||||
("18600", "spool", 1000, "1.75", "792649782224003082"),
|
||||
),
|
||||
),
|
||||
"PLA Pure": (
|
||||
"pla-pure",
|
||||
(
|
||||
("17100", "refill", 1000, "1.75", "738891519803023418"),
|
||||
("17100", "spool", 1000, "1.75", "738891519803023426"),
|
||||
("17101", "refill", 1000, "1.75", "738891519803023434"),
|
||||
("17101", "spool", 1000, "1.75", "738891519803023442"),
|
||||
("17200", "refill", 1000, "1.75", "738891519803023370"),
|
||||
("17200", "spool", 1000, "1.75", "738891519803023378"),
|
||||
("17300", "refill", 1000, "1.75", "738891519803023402"),
|
||||
("17300", "spool", 1000, "1.75", "738891519803023410"),
|
||||
("17600", "refill", 1000, "1.75", "738891519803023386"),
|
||||
("17600", "spool", 1000, "1.75", "738891519803023394"),
|
||||
),
|
||||
),
|
||||
"PLA Silk Multi-Color": (
|
||||
"pla-silk-multi-color",
|
||||
(
|
||||
("13901", "spool", 1000, "1.75", "47489270776156"),
|
||||
("13902", "spool", 1000, "1.75", "47489270808924"),
|
||||
("13903", "spool", 1000, "1.75", "47489270841692"),
|
||||
("13904", "spool", 1000, "1.75", "47489270874460"),
|
||||
("13905", "spool", 1000, "1.75", "53515458085212"),
|
||||
("13906", "spool", 1000, "1.75", "601314264425144336"),
|
||||
("13909", "spool", 1000, "1.75", "601314264425144330"),
|
||||
("13912", "spool", 1000, "1.75", "601314264425144324"),
|
||||
("13913", "spool", 1000, "1.75", "665320335394844675"),
|
||||
("13916", "spool", 1000, "1.75", "665320335394844681"),
|
||||
),
|
||||
),
|
||||
"PLA Silk+": (
|
||||
"pla-silk-upgrade",
|
||||
(
|
||||
("13108", "refill", 1000, "1.75", "678380546391728130"),
|
||||
("13108", "spool", 1000, "1.75", "53583205925212"),
|
||||
("13109", "refill", 1000, "1.75", "678380546395922438"),
|
||||
("13109", "spool", 1000, "1.75", "53583205826908"),
|
||||
("13110", "refill", 1000, "1.75", "678380546395922432"),
|
||||
("13110", "spool", 1000, "1.75", "53583205794140"),
|
||||
("13205", "spool", 1000, "1.75", "53583205663068"),
|
||||
("13206", "spool", 1000, "1.75", "53583205957980"),
|
||||
("13207", "spool", 1000, "1.75", "53583205761372"),
|
||||
("13404", "spool", 1000, "1.75", "53583205990748"),
|
||||
("13405", "refill", 1000, "1.75", "678380546395922444"),
|
||||
("13405", "spool", 1000, "1.75", "53583205859676"),
|
||||
("13506", "spool", 1000, "1.75", "53583205630300"),
|
||||
("13507", "spool", 1000, "1.75", "53583205892444"),
|
||||
("13603", "spool", 1000, "1.75", "53583206023516"),
|
||||
("13604", "spool", 1000, "1.75", "53583205695836"),
|
||||
("13702", "spool", 1000, "1.75", "53583205728604"),
|
||||
),
|
||||
),
|
||||
"PLA Sparkle": (
|
||||
"pla-sparkle",
|
||||
(
|
||||
("13101", "spool", 1000, "1.75", "43959190946011"),
|
||||
("13102", "spool", 1000, "1.75", "46797853163868"),
|
||||
("13200", "spool", 1000, "1.75", "43959190913243"),
|
||||
("13402", "spool", 1000, "1.75", "48725896266076"),
|
||||
("13501", "spool", 1000, "1.75", "43959190880475"),
|
||||
("13700", "spool", 1000, "1.75", "46797853032796"),
|
||||
),
|
||||
),
|
||||
"PLA Tough+": (
|
||||
"pla-tough-upgrade",
|
||||
(
|
||||
("12104", "refill", 1000, "1.75", "661695156312956942"),
|
||||
("12104", "spool", 1000, "1.75", "624496486489088048"),
|
||||
("12105", "refill", 1000, "1.75", "661695156312956936"),
|
||||
("12105", "spool", 1000, "1.75", "624496486489088030"),
|
||||
("12106", "spool", 1000, "1.75", "624496486489088036"),
|
||||
("12107", "refill", 1000, "1.75", "661695156312956930"),
|
||||
("12107", "spool", 1000, "1.75", "624496486489088012"),
|
||||
("12301", "spool", 1000, "1.75", "624496486489088024"),
|
||||
("12401", "spool", 1000, "1.75", "624496486489088018"),
|
||||
("12601", "spool", 1000, "1.75", "624496486489088042"),
|
||||
),
|
||||
),
|
||||
"PLA Translucent": (
|
||||
"pla-translucent",
|
||||
(
|
||||
("13210", "spool", 1000, "1.75", "54345716695388"),
|
||||
("13211", "spool", 1000, "1.75", "54407223050588"),
|
||||
("13301", "spool", 1000, "1.75", "54345716662620"),
|
||||
("13410", "spool", 1000, "1.75", "54407223017820"),
|
||||
("13510", "spool", 1000, "1.75", "54407222985052"),
|
||||
("13610", "spool", 1000, "1.75", "54407223083356"),
|
||||
("13611", "spool", 1000, "1.75", "54345716597084"),
|
||||
("13612", "spool", 1000, "1.75", "54345716564316"),
|
||||
("13710", "spool", 1000, "1.75", "54345716629852"),
|
||||
("13711", "spool", 1000, "1.75", "54407223116124"),
|
||||
),
|
||||
),
|
||||
"PLA Wood": (
|
||||
"pla-wood",
|
||||
(
|
||||
("13106", "spool", 1000, "1.75", "53441583186268"),
|
||||
("13107", "spool", 1000, "1.75", "53441583055196"),
|
||||
("13204", "spool", 1000, "1.75", "53441583087964"),
|
||||
("13403", "spool", 1000, "1.75", "53441583219036"),
|
||||
("13505", "spool", 1000, "1.75", "53441583153500"),
|
||||
("13801", "spool", 1000, "1.75", "53441583120732"),
|
||||
),
|
||||
),
|
||||
"PLA-CF": (
|
||||
"pla-cf",
|
||||
(
|
||||
("14100", "refill", 1000, "1.75", "49200517448028"),
|
||||
("14100", "spool", 1000, "1.75", "43944001994971"),
|
||||
("14101", "refill", 1000, "1.75", "49238028845404"),
|
||||
("14101", "spool", 1000, "1.75", "46702306296156"),
|
||||
("14200", "spool", 1000, "1.75", "46702306230620"),
|
||||
("14500", "spool", 1000, "1.75", "46797856342364"),
|
||||
("14600", "spool", 1000, "1.75", "46702306263388"),
|
||||
("14601", "spool", 1000, "1.75", "47068316565852"),
|
||||
("14700", "spool", 1000, "1.75", "47068316598620"),
|
||||
),
|
||||
),
|
||||
"PPA-CF": (
|
||||
"ppa-cf",
|
||||
(("73100", "spool", 750, "1.75", "49237640642908"),),
|
||||
),
|
||||
"PPS-CF": (
|
||||
"pps-cf",
|
||||
(("76100", "spool", 750, "1.75", "49259815731548"),),
|
||||
),
|
||||
"PVA": (
|
||||
"pva",
|
||||
(("66400", "spool", 500, "1.75", "47790189281628"),),
|
||||
),
|
||||
"Support for ABS": (
|
||||
"support-for-abs",
|
||||
(("66100", "spool", 500, "1.75", "49063577420124"),),
|
||||
),
|
||||
"Support for PA/PET": (
|
||||
"support-for-pa-pet",
|
||||
(("65500", "spool", 500, "1.75", "43050119364827"),),
|
||||
),
|
||||
"Support for PLA (New Version)": (
|
||||
"support-for-pla-new",
|
||||
(("65104", "spool", 500, "1.75", "54200625725788"),),
|
||||
),
|
||||
"Support for PLA/PETG": (
|
||||
"support-for-pla-petg",
|
||||
(
|
||||
("65102", "spool", 500, "1.75", "48921550553436"),
|
||||
("65103", "spool", 500, "1.75", "53534477320540"),
|
||||
),
|
||||
),
|
||||
"TPU 75A (2.85mm)": (
|
||||
"tpu-75a-2-85mm",
|
||||
(
|
||||
("56100", "spool", 1000, "2.85", "788659415745044543"),
|
||||
("56200", "spool", 1000, "2.85", "788659415740850191"),
|
||||
("56300", "spool", 1000, "2.85", "788659415745044503"),
|
||||
("56400", "spool", 1000, "2.85", "788659415745044535"),
|
||||
("56401", "spool", 1000, "2.85", "788659415745044527"),
|
||||
("56500", "spool", 1000, "2.85", "788659415745044519"),
|
||||
("56501", "spool", 1000, "2.85", "788659415745044511"),
|
||||
("56600", "spool", 1000, "2.85", "788659415745044551"),
|
||||
("56700", "spool", 1000, "2.85", "788659415745044495"),
|
||||
("56701", "spool", 1000, "2.85", "788659415745044487"),
|
||||
),
|
||||
),
|
||||
"TPU 85A": (
|
||||
"tpu-85a-tpu-90a",
|
||||
(
|
||||
("51107", "spool", 1000, "1.75", "682714606946189336"),
|
||||
("51201", "spool", 1000, "1.75", "682714606946189342"),
|
||||
("51305", "spool", 1000, "1.75", "669790315686178817"),
|
||||
("51500", "spool", 1000, "1.75", "54085774442844"),
|
||||
("51501", "spool", 1000, "1.75", "682714606946189348"),
|
||||
),
|
||||
),
|
||||
"TPU 90A": (
|
||||
"tpu-85a-tpu-90a",
|
||||
(
|
||||
("51103", "spool", 1000, "1.75", "54085774541148"),
|
||||
("51105", "spool", 1000, "1.75", "664621573916450817"),
|
||||
("51106", "spool", 1000, "1.75", "697574082204368897"),
|
||||
("51601", "spool", 1000, "1.75", "682714606946189324"),
|
||||
("51700", "spool", 1000, "1.75", "682714606946189318"),
|
||||
("51800", "spool", 1000, "1.75", "682714606946189330"),
|
||||
("51900", "spool", 1000, "1.75", "54085774508380"),
|
||||
("51901", "spool", 1000, "1.75", "54084062511452"),
|
||||
),
|
||||
),
|
||||
"TPU 95A HF": (
|
||||
"tpu-95a-hf",
|
||||
(
|
||||
("51100", "spool", 1000, "1.75", "47305218687324"),
|
||||
("51101", "spool", 1000, "1.75", "47305218785628"),
|
||||
("51102", "spool", 1000, "1.75", "47305218752860"),
|
||||
("51200", "spool", 1000, "1.75", "47305218883932"),
|
||||
("51400", "spool", 1000, "1.75", "47305218818396"),
|
||||
("51600", "spool", 1000, "1.75", "47305218851164"),
|
||||
),
|
||||
),
|
||||
"TPU for AMS": (
|
||||
"tpu-for-ams",
|
||||
(
|
||||
("53100", "spool", 1000, "1.75", "53421837320540"),
|
||||
("53101", "refill", 1000, "1.75", "680212034153209858"),
|
||||
("53101", "spool", 1000, "1.75", "53421837386076"),
|
||||
("53102", "spool", 1000, "1.75", "53421837353308"),
|
||||
("53200", "spool", 1000, "1.75", "53421837189468"),
|
||||
("53400", "spool", 1000, "1.75", "53421837222236"),
|
||||
("53500", "spool", 1000, "1.75", "53421837287772"),
|
||||
("53600", "spool", 1000, "1.75", "53421837255004"),
|
||||
),
|
||||
),
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django import forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db.models import Q
|
||||
@@ -47,45 +45,6 @@ class MarketplaceOfferForm(forms.ModelForm):
|
||||
return quantity
|
||||
|
||||
|
||||
class BambuCartPricesForm(forms.Form):
|
||||
signature = forms.CharField(widget=forms.HiddenInput)
|
||||
cart_total = forms.DecimalField(
|
||||
label="Warensumme nach allen Rabatten (€)",
|
||||
min_value=0,
|
||||
max_value=Decimal("21474836.47"),
|
||||
decimal_places=2,
|
||||
localize=True,
|
||||
widget=forms.TextInput(attrs={"inputmode": "decimal"}),
|
||||
)
|
||||
|
||||
def __init__(self, *args, plan, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.plan = plan
|
||||
self.initial["signature"] = plan.signature
|
||||
for line in plan.lines:
|
||||
self.fields[line.key] = forms.DecimalField(
|
||||
label=f"Preis für {line.quantity} Stück · {line.variant}",
|
||||
min_value=0,
|
||||
max_value=Decimal("21474836.47"),
|
||||
decimal_places=2,
|
||||
localize=True,
|
||||
widget=forms.TextInput(attrs={"inputmode": "decimal"}),
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
cleaned = super().clean()
|
||||
if cleaned.get("signature") != self.plan.signature:
|
||||
raise forms.ValidationError("Die Bestellliste hat sich geändert. Lade die Seite neu.")
|
||||
total = cleaned.get("cart_total")
|
||||
if total is not None and all(line.key in cleaned for line in self.plan.lines):
|
||||
if total > sum(cleaned[line.key] for line in self.plan.lines):
|
||||
self.add_error(
|
||||
"cart_total",
|
||||
"Die Warensumme darf die Summe der Positionen nicht übersteigen. Prüfe Versand und weitere Artikel.",
|
||||
)
|
||||
return cleaned
|
||||
|
||||
|
||||
def selection_id(value):
|
||||
text = str(value or "")
|
||||
return int(text) if text.isascii() and text.isdecimal() and len(text) < 19 else None
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
# Generated by Django 5.2.19 on 2026-11-26 19:48
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("filament", "0022_marketplace_keep_history_on_seller_delete"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="orderitem",
|
||||
name="bambu_quote_cents",
|
||||
field=models.PositiveIntegerField(editable=False, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="orderitem",
|
||||
name="bambu_quote_quantity",
|
||||
field=models.PositiveSmallIntegerField(editable=False, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="orderitem",
|
||||
name="bambu_quote_signature",
|
||||
field=models.CharField(blank=True, editable=False, max_length=64),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="orderitem",
|
||||
name="bambu_quoted_at",
|
||||
field=models.DateTimeField(editable=False, null=True),
|
||||
),
|
||||
]
|
||||
@@ -405,8 +405,6 @@ class StockMovement(models.Model):
|
||||
|
||||
|
||||
class OrderItem(QuantityPosition):
|
||||
EU_CART_QUOTE_PREFIX = "eu-cart:"
|
||||
|
||||
class Status(models.TextChoices):
|
||||
OPEN = "open", "Offen"
|
||||
UNAVAILABLE = "unavailable", "Aktuell nicht verfügbar"
|
||||
@@ -426,10 +424,6 @@ class OrderItem(QuantityPosition):
|
||||
handover_remainder = models.ForeignKey(
|
||||
"self", on_delete=models.SET_NULL, null=True, blank=True, related_name="+"
|
||||
)
|
||||
bambu_quote_cents = models.PositiveIntegerField(null=True, editable=False)
|
||||
bambu_quote_quantity = models.PositiveSmallIntegerField(null=True, editable=False)
|
||||
bambu_quote_signature = models.CharField(max_length=64, blank=True, editable=False)
|
||||
bambu_quoted_at = models.DateTimeField(null=True, editable=False)
|
||||
|
||||
class Meta(QuantityPosition.Meta):
|
||||
constraints = [
|
||||
@@ -454,21 +448,6 @@ class OrderItem(QuantityPosition):
|
||||
def is_pending(self):
|
||||
return self.status in self.PENDING_STATUSES
|
||||
|
||||
@property
|
||||
def bambu_line_total(self):
|
||||
if (
|
||||
self.bambu_quote_cents is not None
|
||||
and self.quantity == self.bambu_quote_quantity
|
||||
and self.bambu_quote_signature.startswith(self.EU_CART_QUOTE_PREFIX)
|
||||
):
|
||||
return Decimal(self.bambu_quote_cents) / 100
|
||||
return None
|
||||
|
||||
@property
|
||||
def bambu_unit_price(self):
|
||||
total = self.bambu_line_total
|
||||
return total / self.quantity if total is not None else None
|
||||
|
||||
|
||||
class Printer(models.Model):
|
||||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
|
||||
@@ -738,26 +738,6 @@ td.quantity {
|
||||
.actions a + a {
|
||||
margin-left: 14px;
|
||||
}
|
||||
.order-cart-price {
|
||||
margin: 0;
|
||||
min-width: 110px;
|
||||
}
|
||||
.order-person-total {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: baseline;
|
||||
justify-content: space-between;
|
||||
gap: 6px 20px;
|
||||
border-top: 1px solid var(--line);
|
||||
padding: 14px 22px;
|
||||
background: var(--table-heading);
|
||||
font-size: 13px;
|
||||
}
|
||||
.order-person-total strong {
|
||||
margin-left: 4px;
|
||||
color: var(--green);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.danger-link,
|
||||
.warning {
|
||||
color: var(--danger);
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
from django.test import Client
|
||||
from django.urls import reverse
|
||||
|
||||
from filament.bambu_cart import plan_cart
|
||||
from filament.models import Color, OrderItem, ProductType, Variant
|
||||
|
||||
from .base import CatalogTestCase
|
||||
|
||||
|
||||
class BambuCartTests(CatalogTestCase):
|
||||
def setUp(self):
|
||||
self.product.name = "PLA Basic"
|
||||
self.product.save()
|
||||
self.spool.manufacturer_color_code = "10100"
|
||||
self.refill.manufacturer_color_code = "10604"
|
||||
for variant in (self.spool, self.refill):
|
||||
variant.net_weight_g = 1000
|
||||
variant.diameter_mm = "1.75"
|
||||
variant.save()
|
||||
self.first = OrderItem.objects.create(owner=self.alice, variant=self.spool, quantity=1)
|
||||
self.second = OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=2)
|
||||
self.third = OrderItem.objects.create(owner=self.bob, variant=self.refill, quantity=1)
|
||||
self.client.force_login(self.alice)
|
||||
self.url = reverse("bambu_order_cart")
|
||||
|
||||
def price_data(self, **values):
|
||||
form = self.client.get(self.url).context["form"]
|
||||
return {
|
||||
"signature": form.initial["signature"],
|
||||
"shop_43992829919451": "77,97",
|
||||
"shop_49636029006172": "22,99",
|
||||
"cart_total": "95,01",
|
||||
} | values
|
||||
|
||||
def test_cart_preparation_displays_aggregated_quantities_without_saving_prices(self):
|
||||
page = self.client.get(self.url)
|
||||
rows = {line.key: line for line, _ in page.context["rows"]}
|
||||
self.assertEqual(rows["shop_43992829919451"].quantity, 3)
|
||||
self.assertEqual(rows["shop_49636029006172"].quantity, 1)
|
||||
self.assertContains(page, "https://eu.store.bambulab.com/cart")
|
||||
self.assertContains(page, "/products/pla-basic-filament?id=43992829919451")
|
||||
self.assertNotContains(page, "myshopify.com")
|
||||
self.assertFalse(OrderItem.objects.filter(bambu_quoted_at__isnull=False).exists())
|
||||
self.assertEqual(self.client.post(self.url, {"action": "prices"}).status_code, 200)
|
||||
self.assertFalse(OrderItem.objects.filter(bambu_quoted_at__isnull=False).exists())
|
||||
|
||||
def test_old_shopify_prices_do_not_count_towards_amount_due(self):
|
||||
OrderItem.objects.filter(pk=self.first.pk).update(
|
||||
bambu_quote_cents=2599,
|
||||
bambu_quote_quantity=1,
|
||||
bambu_quote_signature="0" * 64,
|
||||
)
|
||||
response = self.client.get(reverse("orders"))
|
||||
self.assertContains(response, "Gesamtbetrag offen")
|
||||
self.assertNotContains(response, "25,99 €")
|
||||
|
||||
def test_all_five_reported_variants_open_on_the_current_eu_shop(self):
|
||||
cases = (
|
||||
("PETG Basic", "petg-basic", "30403", "spool", "738173299117862975"),
|
||||
("PETG Basic", "petg-basic", "30108", "refill", "738173299117862983"),
|
||||
("PETG Basic", "petg-basic", "30106", "refill", "703100873995440152"),
|
||||
("PLA Pure", "pla-pure", "17300", "spool", "738891519803023410"),
|
||||
("PLA Pure", "pla-pure", "17600", "spool", "738891519803023394"),
|
||||
)
|
||||
for name, _, code, spool, _ in cases:
|
||||
product, _ = ProductType.objects.get_or_create(name=name, manufacturer=self.maker)
|
||||
variant = Variant.objects.create(
|
||||
product_type=product,
|
||||
color=Color.objects.create(name=code),
|
||||
manufacturer_color_code=code,
|
||||
spool_type=spool,
|
||||
net_weight_g=1000,
|
||||
diameter_mm="1.75",
|
||||
)
|
||||
OrderItem.objects.create(owner=self.bob, variant=variant, quantity=1)
|
||||
|
||||
page = self.client.get(self.url)
|
||||
for _, slug, _, _, shop_id in cases:
|
||||
self.assertContains(page, f"https://eu.store.bambulab.com/products/{slug}?id={shop_id}")
|
||||
|
||||
def test_import_distributes_actual_cart_total_including_discount_and_rounding(self):
|
||||
ordered = OrderItem.objects.create(
|
||||
owner=self.alice, variant=self.refill, quantity=2, status=OrderItem.Status.ORDERED
|
||||
)
|
||||
unavailable = OrderItem.objects.create(
|
||||
owner=self.alice, variant=self.refill, quantity=4, status=OrderItem.Status.UNAVAILABLE
|
||||
)
|
||||
self.assertRedirects(self.client.post(self.url, self.price_data()), reverse("orders"))
|
||||
for entry in (self.first, self.second, self.third, ordered, unavailable):
|
||||
entry.refresh_from_db()
|
||||
self.assertEqual(self.first.bambu_quote_cents, 2446)
|
||||
self.assertEqual(self.second.bambu_quote_cents, 4891)
|
||||
self.assertEqual(self.third.bambu_quote_cents, 2164)
|
||||
self.assertIsNone(ordered.bambu_quote_cents)
|
||||
self.assertIsNone(unavailable.bambu_quote_cents)
|
||||
self.assertEqual(self.first.status, OrderItem.Status.OPEN)
|
||||
self.client.force_login(self.bob)
|
||||
page = self.client.get(reverse("orders"))
|
||||
self.assertContains(
|
||||
page, "<div><span>Zwischensumme</span> <strong>24,46 €</strong></div>", html=True
|
||||
)
|
||||
self.assertContains(
|
||||
page, "<div><span>Gesamtbetrag</span> <strong>70,55 €</strong></div>", html=True
|
||||
)
|
||||
self.assertContains(page, 'class="order-person-total"', count=2)
|
||||
self.assertContains(page, "Für 1 Position fehlt der Preis.", count=1)
|
||||
|
||||
def test_import_rejects_incomplete_amounts_or_a_total_including_shipping(self):
|
||||
for values in (
|
||||
{"cart_total": "102,00"},
|
||||
{"shop_49636029006172": ""},
|
||||
{"shop_49636029006172": "-1,50"},
|
||||
):
|
||||
response = self.client.post(self.url, self.price_data(**values))
|
||||
self.assertTrue(response.context["form"].errors)
|
||||
self.assertFalse(OrderItem.objects.filter(bambu_quoted_at__isnull=False).exists())
|
||||
|
||||
def test_quantity_change_rejects_import_prepared_for_another_cart(self):
|
||||
data = self.price_data()
|
||||
self.second.quantity = 1
|
||||
self.second.save(update_fields=["quantity"])
|
||||
response = self.client.post(self.url, data)
|
||||
self.assertContains(response, "Die Bestellliste hat sich geändert.")
|
||||
self.assertFalse(OrderItem.objects.filter(bambu_quoted_at__isnull=False).exists())
|
||||
|
||||
def test_existing_price_is_marked_stale_after_cart_changes(self):
|
||||
self.client.post(self.url, self.price_data())
|
||||
self.third.status = OrderItem.Status.UNAVAILABLE
|
||||
self.third.save(update_fields=["status"])
|
||||
page = self.client.get(reverse("orders"))
|
||||
self.assertContains(
|
||||
page, "<div><span>Gesamtbetrag</span> <strong>48,91 €</strong></div>", html=True
|
||||
)
|
||||
self.assertNotContains(page, "70,55 €")
|
||||
self.assertContains(page, "Preise vor der letzten Listenänderung.")
|
||||
|
||||
def test_cart_action_requires_login_and_import_requires_csrf(self):
|
||||
self.client.logout()
|
||||
self.assertEqual(self.client.get(self.url).status_code, 302)
|
||||
self.assertEqual(self.client.post(self.url).status_code, 302)
|
||||
csrf = Client(enforce_csrf_checks=True)
|
||||
csrf.force_login(self.alice)
|
||||
self.assertEqual(csrf.post(self.url).status_code, 403)
|
||||
|
||||
def test_silk_dual_uses_current_shop_multi_color_listing(self):
|
||||
self.product.name = "PLA Silk Dual Color"
|
||||
self.product.save(update_fields=["name"])
|
||||
self.spool.manufacturer_color_code = "13909"
|
||||
self.spool.save(update_fields=["manufacturer_color_code"])
|
||||
plan = plan_cart(OrderItem.objects.filter(pk=self.first.pk))
|
||||
self.assertEqual(
|
||||
plan.lines[0].product_url,
|
||||
"https://eu.store.bambulab.com/products/pla-silk-multi-color?id=601314264425144330",
|
||||
)
|
||||
|
||||
def test_a_custom_catalog_variant_stays_on_the_shopping_list(self):
|
||||
OrderItem.objects.create(owner=self.bob, variant=self.black_spool, quantity=1)
|
||||
page = self.client.get(self.url)
|
||||
self.assertContains(page, "Ausführung im Shop prüfen")
|
||||
self.assertContains(page, f'id="id_local_{self.black_spool.pk}"')
|
||||
@@ -106,7 +106,7 @@ class SharedOrderTests(CatalogTestCase):
|
||||
self.client.post("/bestellung/hinzufuegen/", self.selection(self.refill, 4))
|
||||
response = self.client.get("/bestellung/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
groups = {group["username"]: group["entries"] for group in response.context["groups"]}
|
||||
groups = dict(response.context["groups"])
|
||||
self.assertEqual(
|
||||
{name: {e.variant_id: e.quantity for e in entries} for name, entries in groups.items()},
|
||||
{"michael": {self.spool.pk: 2}, "person-zwei": {self.spool.pk: 1, self.refill.pk: 4}},
|
||||
|
||||
+4
-81
@@ -14,14 +14,11 @@ from django.db.models.functions import Coalesce
|
||||
from django.http import FileResponse, Http404, HttpResponse, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.http import require_http_methods
|
||||
|
||||
from .bambu_cart import SHOP_URL, allocate_prices, plan_cart
|
||||
from .forms import (
|
||||
ArticleLookupForm,
|
||||
AssignEmptySpoolForm,
|
||||
BambuCartPricesForm,
|
||||
ConsumeSpoolForm,
|
||||
EmptySpoolForm,
|
||||
FeedbackForm,
|
||||
@@ -927,30 +924,10 @@ def orders(request):
|
||||
"pk",
|
||||
)
|
||||
)
|
||||
plan = plan_cart(entries)
|
||||
signature = plan.signature
|
||||
groups = []
|
||||
for username, items in groupby(entries, key=lambda e: e.owner.username):
|
||||
positions = list(items)
|
||||
priced = [entry for entry in positions if entry.bambu_line_total is not None]
|
||||
for entry in priced:
|
||||
entry.bambu_quote_stale = entry.status in OrderItem.PENDING_STATUSES and (
|
||||
entry.status != OrderItem.Status.OPEN or entry.bambu_quote_signature != signature
|
||||
)
|
||||
payable_count = sum(entry.status != OrderItem.Status.UNAVAILABLE for entry in positions)
|
||||
priced_payable = [entry for entry in priced if entry.status != OrderItem.Status.UNAVAILABLE]
|
||||
groups.append(
|
||||
{
|
||||
"username": username,
|
||||
"entries": positions,
|
||||
"payable_count": payable_count,
|
||||
"missing_prices": payable_count - len(priced_payable),
|
||||
"amount": sum(entry.bambu_line_total for entry in priced_payable)
|
||||
if priced_payable
|
||||
else None,
|
||||
"stale_prices": any(entry.bambu_quote_stale for entry in priced_payable),
|
||||
}
|
||||
)
|
||||
groups = [
|
||||
(username, list(items))
|
||||
for username, items in groupby(entries, key=lambda e: e.owner.username)
|
||||
]
|
||||
return render(
|
||||
request,
|
||||
"filament/orders.html",
|
||||
@@ -959,60 +936,6 @@ def orders(request):
|
||||
"total": sum(entry.quantity for entry in entries),
|
||||
"variant_count": len({entry.variant_id for entry in entries}),
|
||||
"status_choices": OrderItem.Status.choices,
|
||||
"has_bambu_cart": bool(plan.lines),
|
||||
"has_bambu_prices": any(entry.bambu_line_total is not None for entry in entries),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _open_bambu_orders():
|
||||
return OrderItem.objects.filter(
|
||||
status=OrderItem.Status.OPEN, variant__product_type__manufacturer__name__iexact="Bambu Lab"
|
||||
).select_related("variant__product_type__manufacturer", "variant__color", "owner")
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(["GET", "POST"])
|
||||
def bambu_order_cart(request):
|
||||
plan = plan_cart(_open_bambu_orders())
|
||||
form = BambuCartPricesForm(request.POST if request.method == "POST" else None, plan=plan)
|
||||
if plan.lines and request.method == "POST" and form.is_valid():
|
||||
signature = plan.signature
|
||||
prices = allocate_prices(
|
||||
plan,
|
||||
[int(form.cleaned_data[line.key] * 100) for line in plan.lines],
|
||||
int(form.cleaned_data["cart_total"] * 100),
|
||||
)
|
||||
with transaction.atomic():
|
||||
current = plan_cart(_open_bambu_orders())
|
||||
if current.signature != signature:
|
||||
form.add_error(None, "Die Bestellliste hat sich geändert. Lade die Seite neu.")
|
||||
else:
|
||||
now = timezone.now()
|
||||
positions = [entry for line in current.lines for entry in line.entries]
|
||||
for entry in positions:
|
||||
entry.bambu_quote_cents = prices[entry.pk]
|
||||
entry.bambu_quote_quantity = entry.quantity
|
||||
entry.bambu_quote_signature = signature
|
||||
entry.bambu_quoted_at = now
|
||||
OrderItem.objects.bulk_update(
|
||||
positions,
|
||||
(
|
||||
"bambu_quote_cents",
|
||||
"bambu_quote_quantity",
|
||||
"bambu_quote_signature",
|
||||
"bambu_quoted_at",
|
||||
),
|
||||
)
|
||||
messages.success(request, "Warensumme mit Rabatt auf die Bestellwünsche verteilt.")
|
||||
return redirect("orders")
|
||||
return render(
|
||||
request,
|
||||
"filament/bambu_order_cart.html",
|
||||
{
|
||||
"cart_url": f"{SHOP_URL}/cart",
|
||||
"rows": [(line, form[line.key]) for line in plan.lines],
|
||||
"form": form,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user