def get_dashboard_blocks(self, request): if not self.check_demo_optin(request): return locale = get_current_babel_locale() n = now() weekday = format_date(n, "EEEE", locale=locale) today = format_date(n, locale=locale) yield DashboardValueBlock( id="test-x", color="blue", title="Happy %s!" % weekday, value=today, icon="fa fa-calendar" ) yield DashboardNumberBlock( id="test-x", color="red", title="Visitors Yesterday", value=random.randint(2200, 10000), icon="fa fa-globe" ) yield DashboardNumberBlock(id="test-x", color="gray", title="Registered Users", value=1240, icon="fa fa-user") yield DashboardNumberBlock(id="test-x", color="orange", title="Orders", value=32, icon="fa fa-inbox") yield DashboardMoneyBlock( id="test-x", color="green", title="Open Orders Value", value=32000, currency="USD", icon="fa fa-line-chart" ) yield DashboardNumberBlock(id="test-x", color="yellow", title="Current Visitors", value=6, icon="fa fa-users") yield DashboardMoneyBlock( id="test-x", color="none", title="Sales this week", value=430.30, currency="USD", icon="fa fa-dollar" ) yield DashboardValueBlock( id="test-1", value="\u03C0", title="The most delicious number", color="purple", icon="fa fa-smile-o" )
def __init__(self, id, value, title, currency=None, **kwargs): self.currency = (currency or settings.SHOOP_HOME_CURRENCY) value = parse_decimal_string(value) value = format_currency(value, currency=self.currency, locale=get_current_babel_locale()) super(DashboardMoneyBlock, self).__init__(id, value, title, **kwargs)
def __init__(self, id, value, title, currency, **kwargs): self.currency = currency value = parse_decimal_string(value) value = format_currency(value, currency=self.currency, locale=get_current_babel_locale()) super(DashboardMoneyBlock, self).__init__(id, value, title, **kwargs)
def percent(value, ndigits=3): locale = get_current_babel_locale() if not ndigits: return format_percent(value, locale=locale) else: format = locale.percent_formats.get(None) new_fmt = format.pattern.replace("0", "0." + (ndigits * "#")) return format_percent(value, format=new_fmt, locale=locale)
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): locale = get_current_babel_locale() translated_choices = [ (code, locale.languages.get(code, code)) for (code, _) in super( LanguageField, self).get_choices(include_blank, blank_choice) ] translated_choices.sort(key=lambda pair: pair[1].lower()) return translated_choices
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): locale = get_current_babel_locale() translated_choices = [ (code, locale.languages.get(code, code)) for (code, _) in super(LanguageField, self).get_choices(include_blank, blank_choice) ] translated_choices.sort(key=lambda pair: pair[1].lower()) return translated_choices
def encode_line(line): return { "sku": line.sku, "text": line.text, "quantity": format_decimal(line.quantity, locale=get_current_babel_locale()), "unitPrice": format_money(line.base_unit_price.amount), "discountAmount": format_money(line.discount_amount.amount), "taxlessTotal": format_money(line.taxless_price.amount), "taxPercentage": format_percent(line.tax_rate, 2), "taxfulTotal": format_money(line.taxful_price.amount) }
def datetime(value, kind="datetime", format="medium", tz=True): """ Format a datetime for human consumption. The currently active locale's formatting rules are used. The output of this function is probably not machine-parseable. :param value: datetime object to format :type value: datetime.datetime :param kind: Format as 'datetime', 'date' or 'time' :type kind: str :param format: Format specifier or one of 'full', 'long', 'medium' or 'short' :type format: str :param tz: Convert to current or given timezone. Accepted values are: True (default) convert to currently active timezone (as reported by :func:`django.utils.timezone.get_current_timezone`) False (or other false value like empty string) do no convert to any timezone (use UTC) Other values (as str) convert to given timezone (e.g. ``"US/Hawaii"``) :type tz: bool|str """ locale = get_current_babel_locale() if type( value ) is date: # Not using isinstance, since `datetime`s are `date` too. # We can't do any TZ manipulation for dates, so just use `format_date` always return format_date(value, format=format, locale=locale) if tz: value = localtime(value, (None if tz is True else tz)) if kind == "datetime": return format_datetime(value, format=format, locale=locale) elif kind == "date": return format_date(value, format=format, locale=locale) elif kind == "time": return format_time(value, format=format, locale=locale) else: raise ValueError("Unknown `datetime` kind: %r" % kind)
def get_dashboard_blocks(self, request): if not self.check_demo_optin(request): return locale = get_current_babel_locale() n = now() weekday = format_date(n, "EEEE", locale=locale) today = format_date(n, locale=locale) yield DashboardValueBlock(id="test-x", color="blue", title="Happy %s!" % weekday, value=today, icon="fa fa-calendar") yield DashboardNumberBlock(id="test-x", color="red", title="Visitors Yesterday", value=random.randint(2200, 10000), icon="fa fa-globe") yield DashboardNumberBlock(id="test-x", color="gray", title="Registered Users", value=1240, icon="fa fa-user") yield DashboardNumberBlock(id="test-x", color="orange", title="Orders", value=32, icon="fa fa-inbox") yield DashboardMoneyBlock(id="test-x", color="green", title="Open Orders Value", value=32000, currency="USD", icon="fa fa-line-chart") yield DashboardNumberBlock(id="test-x", color="yellow", title="Current Visitors", value=6, icon="fa fa-users") yield DashboardMoneyBlock(id="test-x", color="none", title="Sales this week", value=430.30, currency="USD", icon="fa fa-dollar") yield DashboardValueBlock(id="test-1", value="\u03C0", title="The most delicious number", color="purple", icon="fa fa-smile-o")
def datetime(value, kind="datetime", format="medium", tz=True): """ Format a datetime for human consumption. The currently active locale's formatting rules are used. The output of this function is probably not machine-parseable. :param value: datetime object to format :type value: datetime.datetime :param kind: Format as 'datetime', 'date' or 'time' :type kind: str :param format: Format specifier or one of 'full', 'long', 'medium' or 'short' :type format: str :param tz: Convert to current or given timezone. Accepted values are: True (default) convert to currently active timezone (as reported by :func:`django.utils.timezone.get_current_timezone`) False (or other false value like empty string) do no convert to any timezone (use UTC) Other values (as str) convert to given timezone (e.g. ``"US/Hawaii"``) :type tz: bool|str """ locale = get_current_babel_locale() if type(value) is date: # Not using isinstance, since `datetime`s are `date` too. # We can't do any TZ manipulation for dates, so just use `format_date` always return format_date(value, format=format, locale=locale) if tz: value = localtime(value, (None if tz is True else tz)) if kind == "datetime": return format_datetime(value, format=format, locale=locale) elif kind == "date": return format_date(value, format=format, locale=locale) elif kind == "time": return format_time(value, format=format, locale=locale) else: raise ValueError("Unknown `datetime` kind: %r" % kind)
def as_string_list(self, locale=None): locale = locale or get_current_babel_locale() country = self.country.code.upper() base_lines = [ self.company_name, self.full_name, self.name_ext, self.street, self.street2, self.street3, "%s %s %s" % (self.region_code, self.postal_code, self.city), self.region, locale.territories.get(country, country) if not self.is_home else None ] stripped_lines = [ force_text(line).strip() for line in base_lines if line ] return [s for s in stripped_lines if (s and len(s) > 1)]
def get_chart(self): aggregate_data = group_by_period( Order.objects.valid().since(days=365), "order_date", "month", sum=Sum("taxful_total_price") ) locale = get_current_babel_locale() bar_chart = BarChart(title=_("Sales per Month (last year)"), labels=[ format_date(k, format=get_year_and_month_format(locale), locale=locale) for k in aggregate_data ]) bar_chart.add_data( _("Sales (%(currency)s)") % {"currency": settings.SHOOP_HOME_CURRENCY}, [bankers_round(v["sum"], settings.SHOOP_ORDER_TOTAL_DECIMALS) for v in aggregate_data.values()] ) return bar_chart
def as_string_list(self, locale=None): locale = locale or get_current_babel_locale() country = self.country.code.upper() base_lines = [ self.company_name, self.full_name, self.name_ext, self.street, self.street2, self.street3, "%s %s %s" % (self.region_code, self.postal_code, self.city), self.region, locale.territories.get(country, country) if not self.is_home else None ] stripped_lines = [force_text(line).strip() for line in base_lines if line] return [s for s in stripped_lines if (s and len(s) > 1)]
def get_chart(self): orders = get_orders_by_currency(self.currency) aggregate_data = group_by_period(orders.valid().since(days=365), "order_date", "month", sum=Sum("taxful_total_price_value")) locale = get_current_babel_locale() bar_chart = BarChart(title=_("Sales per Month (last year)"), labels=[ format_date( k, format=get_year_and_month_format(locale), locale=locale) for k in aggregate_data ]) bar_chart.add_data( _("Sales (%(currency)s)") % {"currency": self.currency}, [ bankers_round(v["sum"], 2) # TODO: To be fixed in SHOOP-1912 for v in aggregate_data.values() ]) return bar_chart
def get_chart(self): orders = get_orders_by_currency(self.currency) aggregate_data = group_by_period( orders.valid().since(days=365), "order_date", "month", sum=Sum("taxful_total_price_value") ) locale = get_current_babel_locale() bar_chart = BarChart(title=_("Sales per Month (last year)"), labels=[ format_date(k, format=get_year_and_month_format(locale), locale=locale) for k in aggregate_data ]) bar_chart.add_data( _("Sales (%(currency)s)") % {"currency": self.currency}, [ bankers_round(v["sum"], 2) # TODO: To be fixed in SHOOP-1912 for v in aggregate_data.values() ] ) return bar_chart
def format_order_date(self, instance, *args, **kwargs): return format_datetime(localtime(instance.order_date), locale=get_current_babel_locale())
def home_currency(value): return format_home_currency(value, locale=get_current_babel_locale())
def format_end_date(self, instance, *args, **kwargs): return format_date(instance.end_date, locale=get_current_babel_locale())
def __init__(self, id, value, title, **kwargs): value = parse_decimal_string(value) if int(value) == value: value = int(value) value = format_number(value, locale=get_current_babel_locale()) super(DashboardNumberBlock, self).__init__(id, value, title, **kwargs)
def number(value): return format_decimal(value, locale=get_current_babel_locale())
def _formatted_datetime(self, dt): return format_datetime(localtime(dt), locale=get_current_babel_locale())