def form_valid(self, form): product_ids_to_quantities = dict( (int(key.replace("q_", "")), value) for (key, value) in six.iteritems(form.cleaned_data) if key.startswith("q_") and value > 0 ) order = self.object product_map = Product.objects.in_bulk(set(product_ids_to_quantities.keys())) products_to_quantities = dict( (product_map[product_id], quantity) for (product_id, quantity) in six.iteritems(product_ids_to_quantities) ) unsaved_shipment = Shipment(order=order, supplier=form.cleaned_data["supplier"]) for extend_class in get_provide_objects(FORM_MODIFIER_PROVIDER_KEY): extend_class().form_valid_hook(form, unsaved_shipment) try: shipment = order.create_shipment( product_quantities=products_to_quantities, shipment=unsaved_shipment ) except NoProductsToShipException: messages.error(self.request, _("No products to ship.")) return self.form_invalid(form) else: messages.success(self.request, _("Shipment %s created.") % shipment.id) return HttpResponseRedirect(get_model_url(order))
def test_shipment_creation_from_unsaved_shipment(): shop = get_default_shop() supplier = get_default_supplier() order = _get_order(shop, supplier) product_lines = order.lines.exclude(product_id=None) for line in product_lines: for i in range(0, int(line.quantity)): unsaved_shipment = Shipment(order=order, supplier=supplier) shipment = order.create_shipment({line.product: 1}, shipment=unsaved_shipment) expected_key_start = "%s/%s" % (order.pk, i) assert shipment.identifier.startswith(expected_key_start) assert order.shipments.count() == int(line.quantity)
def test_shipment_creation_with_invalid_unsaved_shipment(): shop = get_default_shop() supplier = get_default_supplier() order = _get_order(shop, supplier) second_order = create_empty_order(shop=shop) second_order.full_clean() second_order.save() product_lines = order.lines.exclude(product_id=None) for line in product_lines: for i in range(0, int(line.quantity)): with pytest.raises(AssertionError): unsaved_shipment = Shipment(supplier=supplier, order=second_order) order.create_shipment({line.product: 1}, shipment=unsaved_shipment) assert order.shipments.count() == 0
def form_valid(self, form): product_ids_to_quantities = dict( (int(key.replace("q_", "")), value) for (key, value) in six.iteritems(form.cleaned_data) if key.startswith("q_") and (value > 0 if value else False) ) order = self.object product_map = Product.objects.in_bulk(set(product_ids_to_quantities.keys())) products_to_quantities = dict( (product_map[product_id], quantity) for (product_id, quantity) in six.iteritems(product_ids_to_quantities) ) unsaved_shipment = Shipment(order=order, supplier=form.cleaned_data["supplier"], tracking_code=form.cleaned_data.get("tracking_code"), description=form.cleaned_data.get("description"),) has_extension_errors = False for extend_class in get_provide_objects(FORM_MODIFIER_PROVIDER_KEY): try: extend_class().form_valid_hook(form, unsaved_shipment) except Problem as problem: has_extension_errors = True messages.error(self.request, problem) if has_extension_errors: return self.form_invalid(form) try: shipment = order.create_shipment( product_quantities=products_to_quantities, shipment=unsaved_shipment ) except Problem as problem: messages.error(self.request, problem) return self.form_invalid(form) except NoProductsToShipException: messages.error(self.request, _("No products to ship.")) return self.form_invalid(form) except NoShippingAddressException: messages.error(self.request, _("Shipping address is not set.")) else: messages.success(self.request, _("Shipment %s created.") % shipment.id) return HttpResponseRedirect(get_model_url(order))
def form_valid(self, form): product_ids_to_quantities = dict( (int(key.replace("q_", "")), value) for (key, value) in six.iteritems(form.cleaned_data) if key.startswith("q_") and (value > 0 if value else False)) order = self.object product_map = Product.objects.in_bulk( set(product_ids_to_quantities.keys())) products_to_quantities = dict( (product_map[product_id], quantity) for (product_id, quantity) in six.iteritems(product_ids_to_quantities)) unsaved_shipment = Shipment( order=order, supplier_id=self._get_supplier_id(), tracking_code=form.cleaned_data.get("tracking_code"), tracking_url=form.cleaned_data.get("tracking_url"), description=form.cleaned_data.get("description"), ) has_extension_errors = self.form_valid_hook(form, unsaved_shipment) if has_extension_errors: return self.form_invalid(form) try: shipment = self.create_shipment( order=order, product_quantities=products_to_quantities, shipment=unsaved_shipment) except Problem as problem: messages.error(self.request, str(problem)) return self.form_invalid(form) except NoProductsToShipException as exc: messages.error(self.request, str(exc)) return self.form_invalid(form) except NoShippingAddressException: messages.error(self.request, _("Shipping address is not set.")) return self.form_invalid(form) else: messages.success(self.request, _("Shipment %s was created.") % shipment.id) return HttpResponseRedirect(self.get_success_url())