def get_shop_overview_block(request, currency, for_date=None): end = to_aware(for_date, time=time.max) if for_date else local_now() start_of_day = to_aware(end.date(), time=time.min) start_of_month = start_of_day.replace(day=1) start_of_year = start_of_day.replace(month=1, day=1) daily = get_order_overview_for_date_range(currency, start_of_day, end) mtd = get_order_overview_for_date_range(currency, start_of_month, end) ytd = get_order_overview_for_date_range(currency, start_of_year, end) totals = get_orders_by_currency(currency).complete().aggregate( num_orders=Count("id"), num_customers=Count("customer", distinct=True), sales=Sum("taxful_total_price_value") ) anon_orders = get_orders_by_currency(currency).complete().filter(customer__isnull=True).aggregate( num_orders=Count("id")) totals["num_customers"] += anon_orders["num_orders"] totals["sales"] = TaxfulPrice(totals["sales"] or 0, currency) block = DashboardContentBlock.by_rendering_template( "store_overview", request, "shuup/admin/sales_dashboard/_store_overview_dashboard_block.jinja", { "daily": daily, "mtd": mtd, "ytd": ytd, "totals": totals }) block.size = "small" return block
def get_shop_overview_block(request, currency, for_date=None): end = to_aware(for_date, time=time.max) if for_date else local_now() start_of_day = to_aware(end.date(), time=time.min) start_of_month = start_of_day.replace(day=1) start_of_year = start_of_day.replace(month=1, day=1) shop = request.shop if not currency: currency = shop.currency daily = get_order_overview_for_date_range(currency, start_of_day, end, shop=shop) mtd = get_order_overview_for_date_range(currency, start_of_month, end, shop=shop) ytd = get_order_overview_for_date_range(currency, start_of_year, end) totals = get_orders_by_currency(currency).complete().aggregate( num_orders=Count("id"), num_customers=Count("customer", distinct=True), sales=Sum("taxful_total_price_value") ) anon_orders = get_orders_by_currency(currency).complete().filter(customer__isnull=True, shop=shop).aggregate( num_orders=Count("id")) totals["num_customers"] += anon_orders["num_orders"] totals["sales"] = TaxfulPrice(totals["sales"] or 0, currency) block = DashboardContentBlock.by_rendering_template( "store_overview", request, "shuup/admin/sales_dashboard/_store_overview_dashboard_block.jinja", { "daily": daily, "mtd": mtd, "ytd": ytd, "totals": totals }) block.size = "medium" return block
def test_dst_safe_aware(): random_date = date(2018, 11, 4) sao_paulo = to_aware(random_date, tz=pytz.timezone("America/Sao_Paulo")) assert sao_paulo.hour == 0 assert sao_paulo.minute == 0 assert sao_paulo.tzinfo._dst.seconds == 3600 # 1hr madrid = to_aware(random_date, tz=pytz.timezone("Europe/Madrid")) assert madrid.hour == 0 assert madrid.minute == 0 assert madrid.tzinfo._dst.seconds == 0
def in_date_range(self, start, end): """ Limit to orders is given date range. :type start: datetime.datetime|None :param start: Start time, inclusive. :type end: datetime.datetime|None :param end: End time, inclusive. """ result = self if start: result = result.filter(order_date__gte=to_aware(start)) if end: result = result.filter(order_date__lte=to_aware(end)) return result
def get_order_for_date(dt, product): order = create_random_order(customer=create_random_person(), products=[product]) order.order_date = to_aware(dt) order.status = OrderStatus.objects.get_default_complete() order.save() return order
def get_connection_info(shop): ts = configuration.get(shop, MC_LAST_CHECK) if ts: ts = to_aware(datetime.datetime.fromtimestamp(ts)) return { "mailchimp_last_check": ts, "mailchimp_check_success": configuration.get(shop, MC_CHECK_SUCCESS), }
def get_best_selling_product_info(shop_ids, cutoff_days=30): shop_ids = sorted(map(int, shop_ids)) cutoff_date = datetime.date.today() - datetime.timedelta(days=cutoff_days) cache_key = "best_sellers:%r_%s" % (shop_ids, cutoff_date) sales_data = cache.get(cache_key) if sales_data is None: sales_data = (OrderLine.objects.filter( order__shop_id__in=shop_ids, order__order_date__gte=to_aware(cutoff_date), type=OrderLineType.PRODUCT).values("product").annotate( n=Sum("quantity")).order_by("-n")[:100].values_list( "product", "product__variation_parent_id", "n")) cache.set(cache_key, sales_data, 3 * 60 * 60) # three hours return sales_data
def get_best_selling_product_info(shop_ids, cutoff_days=30, supplier=None, orderable_only=True, quantity=100): shop_ids = sorted(map(int, shop_ids)) cutoff_date = datetime.date.today() - datetime.timedelta(days=cutoff_days) cache_key = "best_sellers:%r_%s_%s_%s_%s" % ( shop_ids, cutoff_date, (supplier.pk if supplier else ""), (1 if orderable_only else 0), quantity, ) sales_data = cache.get(cache_key) if sales_data is None: valid_products = ProductCatalogPrice.objects.filter(shop__in=shop_ids) if orderable_only: valid_products = valid_products.filter(is_available=True) if supplier: valid_products = valid_products.filter(supplier=supplier) queryset = OrderLine.objects.filter( order__shop_id__in=shop_ids, order__order_date__gte=to_aware(cutoff_date), type=OrderLineType.PRODUCT, product_id__in=valid_products.values_list("product_id", flat=True).distinct(), ) if supplier: queryset = queryset.filter(supplier=supplier) sales_data = ( queryset.annotate( group_product_id=Case( When(product__variation_parent_id__isnull=False, then=F("product__variation_parent_id")), default=F("product_id"), ) ) .values("group_product_id") .annotate(total=Sum("quantity")) .order_by("-total")[:quantity] .values_list("group_product_id", "total") ) cache.set(cache_key, sales_data, 3 * 60 * 60) # three hours return sales_data
def get_best_selling_product_info(shop_ids, cutoff_days=30): shop_ids = sorted(map(int, shop_ids)) cutoff_date = datetime.date.today() - datetime.timedelta(days=cutoff_days) cache_key = "best_sellers:%r_%s" % (shop_ids, cutoff_date) sales_data = cache.get(cache_key) if sales_data is None: sales_data = ( OrderLine.objects .filter( order__shop_id__in=shop_ids, order__order_date__gte=to_aware(cutoff_date), type=OrderLineType.PRODUCT ) .values("product") .annotate(n=Sum("quantity")) .order_by("-n")[:100] .values_list("product", "product__variation_parent_id", "n") ) cache.set(cache_key, sales_data, 3 * 60 * 60) # three hours return sales_data
def since(self, days, tz=None): since_date = (local_now(tz) - datetime.timedelta(days=days)).date() since = to_aware(since_date, tz=tz) return self.in_date_range(since, None)
def get_order_for_date(dt, product): order = create_random_order(customer=create_random_person(), products=[product]) order.order_date = to_aware(dt) order.change_status(get_completed_order_status(), save=False) order.save() return order