"""Build print pages: per-slip weights, always 4 slips per page.""" import math def _display_weight(w): """Whole numbers as int (5 not 5.0) for print labels.""" f = float(w) return int(f) if f.is_integer() else f def _pad_to_four(weights): """Pad short page by repeating the first slip on that page.""" if not weights: return weights first = weights[0] while len(weights) < 4: weights.append(first) return weights def _box_weight_template(order): """Box weights from order packaging (total_weight), not print counts.""" if not order.canister_weight or not order.canisters_per_box: return [] logical_cans = math.ceil(order.total_weight / order.canister_weight) if not logical_cans: return [] cans_left = logical_cans logical_boxes = math.ceil(logical_cans / order.canisters_per_box) template = [] for _ in range(logical_boxes): cans_in_box = min(order.canisters_per_box, cans_left) template.append(_display_weight(order.canister_weight * cans_in_box)) cans_left -= cans_in_box return template def _custom_weights(order): """User-defined slips. Counts repeat the stored weight; they are not derived.""" weights = [] for line in getattr(order, 'custom_labels', None) or []: weights.extend([_display_weight(line['weight'])] * int(line['count'])) return weights def build_print_pages(order): """Custom labels, then boxes, then canisters, on shared sheets (pad only last page). Counts are print quantities. Box and canister weights come from total_weight packaging, so those steppers do not change label content. Custom weights are the ones stored on the order. """ cw = _display_weight(order.canister_weight) canister_weights = [cw] * order.canister_count template = _box_weight_template(order) if not template: box_weights = [] else: box_weights = [template[i % len(template)] for i in range(order.box_count)] weights = _custom_weights(order) + box_weights + canister_weights pages = [] for i in range(0, len(weights), 4): pages.append({ 'slips': _pad_to_four(weights[i:i + 4]), }) return pages # ponytail: assert-based check; upgrade to pytest if suite grows if __name__ == '__main__': class O: total_weight = 50.0 canister_count = 10 canister_weight = 5.0 canisters_per_box = 4 box_count = 3 custom_labels = [] pages = build_print_pages(O()) assert len(pages) == 4 # 13 labels → 4 mixed pages assert all(len(p['slips']) == 4 for p in pages) assert pages[0]['slips'] == [20, 20, 10, 5] # boxes first, then cans assert all(isinstance(w, int) for w in pages[0]['slips']) assert pages[1]['slips'] == [5, 5, 5, 5] assert pages[2]['slips'] == [5, 5, 5, 5] assert pages[3]['slips'] == [5, 5, 5, 5] # last page full # Fewer canister labels to print — box weights unchanged class FewerCans(O): canister_count = 7 fewer = build_print_pages(FewerCans()) assert fewer[0]['slips'][:3] == [20, 20, 10] assert len(fewer) == 3 # 3 boxes + 7 cans = 10 → 3 pages # 3 cans + 1 box → one mixed page class Small: total_weight = 15.0 canister_count = 3 canister_weight = 5.0 canisters_per_box = 4 box_count = 1 one = build_print_pages(Small()) assert len(one) == 1 assert one[0]['slips'] == [15, 5, 5, 5] # Custom slips print first; box and canister weights stay on the packaging recipe class WithCustom(O): custom_labels = [{'weight': 8.0, 'count': 1}, {'weight': 1.5, 'count': 1}] mixed = build_print_pages(WithCustom()) assert mixed[0]['slips'] == [8, 1.5, 20, 20] assert isinstance(mixed[0]['slips'][0], int) assert mixed[1]['slips'] == [10, 5, 5, 5] assert len(mixed) == 4 # 2 custom + 3 boxes + 10 cans = 15 → 4 pages print('ok')