def _test_create_full_refund(browser, live_server, order): url = reverse("E-Commerce_admin:order.create-refund", kwargs={"pk": order.pk}) browser.visit("%s%s" % (live_server, url)) wait_until_condition(browser, lambda x: x.is_text_present("Refunded: %s" % format_money(order.shop.create_price("0.00")))) wait_until_condition(browser, lambda x: x.is_text_present("Remaining: %s" % format_money(order.taxful_total_price))) url = reverse("E-Commerce_admin:order.create-full-refund", kwargs={"pk": order.pk}) click_element(browser, "a[href='%s']" % url) wait_until_condition(browser, lambda x: x.is_text_present("Refund Amount: %s" % format_money(order.taxful_total_price))) click_element(browser, "#create-full-refund") _check_create_refund_link(browser, order, False) _check_order_details_visible(browser) order.refresh_from_db() assert not order.taxful_total_price assert order.is_paid() assert order.is_fully_shipped()
def __str__(self): text = super(Tax, self).__str__() if self.rate is not None: text += " ({})".format(format_percent(self.rate, digits=3)) if self.amount is not None: text += " ({})".format(format_money(self.amount)) return text
def add_data(self, name, data, chart_type): """ Add data to this chart :param name: the name of the dataset :type name: str :param data: the list of data :type data: list[int|float|Decimal] :param chart_type: the chart type - tells how data should be rendered. This data type must be available in the `supported_chart_type` attribute of this instance :type chart_type: ChartType """ assert chart_type in self.supported_chart_types formatted_data = [] # format value for each data point if self.data_type == ChartDataType.CURRENCY: for value in data: formatted_data.append(format_money(Money(value, currency=self.currency).as_rounded())) elif self.data_type == ChartDataType.PERCENT: for value in data: formatted_data.append(format_percent(value, locale=self.locale)) # self.data_type == ChartDataType.NUMBER else: for value in data: formatted_data.append(format_decimal(value, locale=self.locale)) self.datasets.append({"type": chart_type, "label": name, "data": data, "formatted_data": formatted_data})
def test_mixed_chart(): labels = ["One", "Two", "Three"] locale = "pt_br" currency = "BRL" chart = MixedChart("ma biultiful xart", labels, data_type=ChartDataType.CURRENCY, locale=locale, currency=currency) dataset1 = OrderedDict({"type": ChartType.BAR, "label": "some bars #1", "data": [1, 2, 3]}) dataset2 = OrderedDict({"type": ChartType.BAR, "label": "some bars #2", "data": [2, 3, 4]}) dataset3 = OrderedDict({"type": ChartType.LINE, "label": "some lines #1", "data": [5, 6, 7]}) dataset4 = OrderedDict({"type": ChartType.LINE, "label": "some lines #2", "data": [8, 9, 10]}) datasets = [dataset1, dataset2, dataset3, dataset4] for dataset in datasets: chart.add_data(dataset["label"], dataset["data"], dataset["type"]) chart_config = chart.get_config() assert chart_config["type"] == "mixed" assert chart_config["labels"] == labels for i in range(len(chart_config["data"])): for j in range(len(chart_config["data"][i]["data"])): assert chart_config["data"][i]["data"][j] == datasets[i]["data"][j] formatted_data = chart_config["data"][i]["formatted_data"][j] assert formatted_data == format_money(Money(datasets[i]["data"][j], currency=currency).as_rounded())
def get_validation_errors(cls, order_source): # check for the minimum sum of order total min_total = configuration.get(order_source.shop, ORDER_MIN_TOTAL_CONFIG_KEY, Decimal(0)) if order_source.shop.prices_include_tax: total = order_source.taxful_total_price.value else: total = order_source.taxless_total_price.value if total < min_total: min_total_price = format_money(order_source.shop.create_price(min_total)) msg = _("The total should be greater than {} to be ordered.").format(min_total_price) yield ValidationError(msg, code="order_total_too_low")
def test_order_creator_customer_details(rf, admin_user): shop = get_default_shop() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) company = create_random_company() group = get_default_customer_group() contact.groups.add(group) contact.company_memberships.add(company) contact.save() product = create_product( sku=printable_gibberish(), supplier=get_default_supplier(), shop=shop ) order = create_random_order(contact, products=[product]) request = apply_request_middleware(rf.get("/", { "command": "customer_details", "id": contact.id }), user=admin_user) response = OrderEditView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert "customer_info" in data assert "order_summary" in data assert "recent_orders" in data assert data["customer_info"]["name"] == contact.full_name assert data["customer_info"]["phone_no"] == contact.phone assert data["customer_info"]["email"] == contact.email assert company.full_name in data["customer_info"]["companies"] assert group.name in data["customer_info"]["groups"] assert data["customer_info"]["merchant_notes"] == contact.merchant_notes assert len(data["order_summary"]) == 1 assert data["order_summary"][0]["year"] == order.order_date.year assert data["order_summary"][0]["total"] == format_money(order.taxful_total_price) assert len(data["recent_orders"]) == 1 assert data["recent_orders"][0]["status"] == order.get_status_display() assert data["recent_orders"][0]["total"] == format_money(order.taxful_total_price) assert data["recent_orders"][0]["payment_status"] == force_text(order.payment_status.label) assert data["recent_orders"][0]["shipment_status"] == force_text(order.shipping_status.label)
def get_price_ranges(shop, min_price, max_price, range_step): if range_step == 0: return ranges = [] min_price_value = format_money(shop.create_price(min_price)) ranges.append(("-%s" % min_price, _("Under %(min_limit)s") % {"min_limit": min_price_value})) for range_min in range(min_price, max_price, range_step): range_min_price = format_money(shop.create_price(range_min)) range_max = range_min + range_step if range_max < max_price: range_max_price = format_money(shop.create_price(range_max)) ranges.append( ( "%s-%s" % (range_min, range_max), _("%(min)s to %(max)s") % dict(min=range_min_price, max=range_max_price) ) ) max_price_value = format_money(shop.create_price(max_price)) ranges.append(("%s-" % max_price, _("%(max_limit)s & Above") % {"max_limit": max_price_value})) return ranges
def _test_refund_view(browser, live_server, order): url = reverse("E-Commerce_admin:order.create-refund", kwargs={"pk": order.pk}) browser.visit("%s%s" % (live_server, url)) wait_until_condition(browser, lambda x: x.is_text_present("Refunded: %s" % format_money(order.shop.create_price("0.00")))) assert len(browser.find_by_css("#id_form-0-line_number option")) == 12 # blank + arbitrary amount + num lines try: click_element(browser, "#select2-id_form-0-line_number-container") wait_until_appeared(browser, "input.select2-search__field") except selenium.common.exceptions.TimeoutException as e: # For some reason first click happen before the element is not ready so # let's re-click when timeout happens. The actual functionality seem # to work nicely. click_element(browser, "#select2-id_form-0-line_number-container") wait_until_appeared(browser, "input.select2-search__field") wait_until_appeared(browser, ".select2-results__option[aria-selected='false']") browser.execute_script('$($(".select2-results__option")[1]).trigger({type: "mouseup"})') # select arbitrary amount wait_until_condition(browser, lambda x: len(x.find_by_css("#id_form-0-text"))) wait_until_condition(browser, lambda x: len(x.find_by_css("#id_form-0-amount"))) browser.find_by_css("#id_form-0-text").first.value = "test" browser.find_by_css("#id_form-0-amount").first.value = "900" move_to_element(browser, "#add-refund") click_element(browser, "#add-refund") # New line starts here... move_to_element(browser, "#add-refund") click_element(browser, "#select2-id_form-1-line_number-container") wait_until_appeared(browser, "input.select2-search__field") elem = browser.find_by_css("input.select2-search__field").first elem._element.send_keys("line 1") elem._element.send_keys(Keys.RETURN) assert decimal.Decimal(browser.find_by_css("#id_form-1-amount").first.value) == decimal.Decimal("100.00") assert int(decimal.Decimal(browser.find_by_css("#id_form-1-quantity").first.value)) == 10 click_element(browser, "button[form='create_refund']") _check_create_refund_link(browser, order, True) # can still refund quantity _check_order_details_visible(browser) order.refresh_from_db() assert not order.taxful_total_price assert order.is_paid() assert not order.is_fully_shipped()
def check_different_types(self, value): if isinstance(value, ProductMedia): return "<img src='/media/%s'>" % value.get_thumbnail() if isinstance(value, Image): thumbnailer = get_thumbnailer(value) options = {"size": (64, 64)} thumbnail = thumbnailer.get_thumbnail(options, generate=True) return "<img src='%s'>" % thumbnail.url if isinstance(value, bool): value = yesno(value) if isinstance(value, Manager): value = ", ".join("%s" % x for x in value.all()) return value if isinstance(value, datetime.datetime): return get_locally_formatted_datetime(value) if isinstance(value, Money): return escape(format_money(value))
def format_taxful_total_price(self, instance, *args, **kwargs): if not instance.taxful_total_price: return "" return escape(format_money(instance.taxful_total_price))
def test_format_money(): assert format_money(Money("3.6", "EUR"), locale="fi") == "3,60\xa0\u20ac" assert format_money(Money("3.6", "EUR"), widen=2, locale="fi") == "3,6000\xa0\u20ac" assert format_money(Money("3.6", "EUR"), digits=0, locale="fi") == "4\xa0\u20ac"
def test_happy_hour_prices_expiration(rf): with override_settings( CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'test_happy_hour_prices_bump', } } ): happy_hour = init_test() # it is now: 2018-01-01 09:00 AM before_happy_hour = datetime.datetime(2018, 1, 1, 9, 0, tzinfo=pytz.UTC) # 09:00 AM inside_happy_hour = datetime.datetime(2018, 1, 1, 10, 30, tzinfo=pytz.UTC) # 10:30 AM after_happy_hours = datetime.datetime(2018, 1, 1, 11, 20, tzinfo=pytz.UTC) # 11:30 AM # Create condition from 10am to 11am hour_start = datetime.datetime(2018, 1, 1, 10, 0, tzinfo=pytz.UTC).time() # 10:00 AM hour_end = datetime.datetime(2018, 1, 1, 11, 0, tzinfo=pytz.UTC).time() # 11:00 AM set_valid_times_condition(happy_hour, hour_start, hour_end, str(before_happy_hour.weekday())) shop = happy_hour.shops.first() discount = happy_hour.discounts.first() product = discount.product shop_product = product.get_shop_instance(shop) assert shop_product.default_price_value == 10 assert discount.discounted_price_value == 6 def get_request(): return apply_request_middleware(rf.get("/")) price_template = engines["jinja2"].from_string("{{ product|price }}") is_discounted_template = engines["jinja2"].from_string("{{ product|is_discounted }}") discount_percent_template = engines["jinja2"].from_string("{{ product|discount_percent }}") # we start with time being before happy hour with patch("django.utils.timezone.now", new=lambda: before_happy_hour): # mock also time.time so the cache timeout will be calculated correctly with patch("time.time", new=lambda: to_timestamp(before_happy_hour)): # check that product price is still the orignal (€10.00) # run twice to make sure caches are being used for cache_test in range(2): context = dict(product=product, request=get_request()) assert price_template.render(context) == format_money(shop_product.default_price) assert is_discounted_template.render(context) == "False" assert discount_percent_template.render(context) == "0%" if cache_test == 1: assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier()) # now we are inside happy hour range with patch("django.utils.timezone.now", new=lambda: inside_happy_hour): # mock also time.time so the cache timeout will be calculated correctly with patch("time.time", new=lambda: to_timestamp(inside_happy_hour)): # check that product price is the discounted one (€6.00) # run twice to make sure caches are being used for cache_test in range(2): context = dict(product=product, request=get_request()) assert price_template.render(context) == format_money( shop.create_price(discount.discounted_price_value) ) assert is_discounted_template.render(context) == "True" assert discount_percent_template.render(context) == "40%" if cache_test == 1: assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier()) # we change the discounted price from $6 to $7 # cached should be bumped discount.discounted_price_value = 7 discount.save() for cache_test in range(2): context = dict(product=product, request=get_request()) assert price_template.render(context) == format_money( shop.create_price(discount.discounted_price_value) ) assert is_discounted_template.render(context) == "True" assert discount_percent_template.render(context) == "30%" if cache_test == 1: assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier()) # now we are inside happy hour range with patch("django.utils.timezone.now", new=lambda: after_happy_hours): # mock also time.time so the cache timeout will be calculated correctly with patch("time.time", new=lambda: to_timestamp(after_happy_hours)): # check that product price is the orignal (€10.00) # run twice to make sure caches are being used for cache_test in range(2): context = dict(product=product, request=get_request()) assert price_template.render(context) == format_money(shop_product.default_price) assert is_discounted_template.render(context) == "False" assert discount_percent_template.render(context) == "0%" if cache_test == 1: assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier())