Repository: shuup/shuup

Issue:
Make sure all Dashboard blocks are filtering by the current shop
One case is here:
- https://github.com/shuup/shuup/blob/master/shuup/admin/modules/sales_dashboard/dashboard.py#L278
- https://github.com/shuup/shuup/blob/master/shuup/admin/modules/sales_dashboard/dashboard.py#L277


Discussion:

Resolving patch:
diff --git a/shuup/admin/modules/sales_dashboard/dashboard.py b/shuup/admin/modules/sales_dashboard/dashboard.py
--- a/shuup/admin/modules/sales_dashboard/dashboard.py
+++ b/shuup/admin/modules/sales_dashboard/dashboard.py
@@ -274,8 +274,8 @@ def get_shop_overview_block(request, currency, for_date=None):
 
     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(
+    ytd = get_order_overview_for_date_range(currency, start_of_year, end, shop=shop)
+    totals = get_orders_by_currency(currency).complete().filter(shop=shop).aggregate(
         num_orders=Count("id"),
         num_customers=Count("customer", distinct=True),
         sales=Sum("taxful_total_price_value")


Test patch:
diff --git a/shuup_tests/admin/test_sales_dashboard_module.py b/shuup_tests/admin/test_sales_dashboard_module.py
--- a/shuup_tests/admin/test_sales_dashboard_module.py
+++ b/shuup_tests/admin/test_sales_dashboard_module.py
@@ -9,16 +9,16 @@
 
 import pytest
 from bs4 import BeautifulSoup
-from django.utils import html
+from django.test import override_settings
 
 from shuup.admin.modules.sales_dashboard.dashboard import (
     get_recent_orders_block, get_shop_overview_block,
     OrderValueChartDashboardBlock
 )
-from shuup.core.models import OrderStatus
+from shuup.core.models import OrderStatus, ShopStatus
 from shuup.testing.factories import (
-    create_product, create_random_order, create_random_person,
-    DEFAULT_CURRENCY, get_default_product, get_default_shop
+    create_random_order, create_random_person,
+    DEFAULT_CURRENCY, get_default_product, get_default_shop, get_shop
 )
 from shuup.testing.utils import apply_request_middleware
 from shuup.utils.dates import to_aware
@@ -54,35 +54,61 @@ def test_order_chart_works(rf, admin_user):
     (date(2020, 2, 29), 2, 3, 4),
 ])
 def test_shop_overview_block(rf, data, admin_user):
-    (today, expected_today, expected_mtd, expected_ytd) = data
-    product = get_default_product()
-    sp = product.get_shop_instance(get_default_shop())
-    sp.default_price_value = "10"
-    sp.save()
-    get_order_for_date(today, product)
-    o = get_order_for_date(today, product)
-    o.customer = None
-    o.save()
-    get_order_for_date(date(today.year - 1, 12, 31), product)
-    get_order_for_date(date(today.year, 1, 1), product)
-    get_order_for_date(date(today.year, today.month, 1), product)
-    request = apply_request_middleware(rf.get("/"), user=admin_user)
-    block = get_shop_overview_block(request, currency=DEFAULT_CURRENCY, for_date=today)
-    soup = BeautifulSoup(block.content)
-    _, today_sales, mtd, ytd, totals = soup.find_all("tr")
-    assert today_sales.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_today)
-    assert today_sales.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_today)
-    assert mtd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_mtd)
-    assert mtd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_mtd)
-    assert ytd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_ytd)
-    assert ytd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_ytd)
-    assert totals.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "5"
-    assert totals.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "5"
+    with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True):
+        shop1 = get_default_shop()
+        shop2 = get_shop(identifier="shop2", status=ShopStatus.ENABLED, name="Shop2")
+
+        (today, expected_today, expected_mtd, expected_ytd) = data
+        product = get_default_product()
+        sp = product.get_shop_instance(shop1)
+        sp.default_price_value = "10"
+        sp.save()
+        get_order_for_date(today, product)
+        o = get_order_for_date(today, product)
+        o.customer = None
+        o.save()
+        get_order_for_date(date(today.year - 1, 12, 31), product)
+        get_order_for_date(date(today.year, 1, 1), product)
+        get_order_for_date(date(today.year, today.month, 1), product)
+
+        for shop in [shop1, shop2]:
+            request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop)
+            block = get_shop_overview_block(request, currency=DEFAULT_CURRENCY, for_date=today)
+            soup = BeautifulSoup(block.content)
+            _, today_sales, mtd, ytd, totals = soup.find_all("tr")
+
+            if shop == shop1:
+                assert today_sales.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_today)
+                assert today_sales.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_today)
+                assert mtd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_mtd)
+                assert mtd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_mtd)
+                assert ytd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == str(expected_ytd)
+                assert ytd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == str(expected_ytd)
+                assert totals.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "5"
+                assert totals.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "5"
+            else:
+                assert today_sales.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "0"
+                assert today_sales.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "0"
+                assert mtd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "0"
+                assert mtd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "0"
+                assert ytd.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "0"
+                assert ytd.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "0"
+                assert totals.find_all("td")[NUM_ORDERS_COLUMN_INDEX].string == "0"
+                assert totals.find_all("td")[NUM_CUSTOMERS_COLUMN_INDEX].string == "0"
 
 
 @pytest.mark.django_db
 def test_recent_orders_block(rf, admin_user):
-    order = create_random_order(customer=create_random_person(), products=[get_default_product()])
-    request = apply_request_middleware(rf.get("/"), user=admin_user)
-    block = get_recent_orders_block(request)
-    assert order.customer.name in block.content
+    with override_settings(SHUUP_ENABLE_MULTIPLE_SHOPS=True):
+        shop1 = get_default_shop()
+        shop2 = get_shop(identifier="shop2", status=ShopStatus.ENABLED, name="Shop2")
+        order = create_random_order(customer=create_random_person(), products=[get_default_product()])
+
+        for shop in [shop1, shop2]:
+            request = apply_request_middleware(rf.get("/"), user=admin_user, shop=shop)
+            block = get_recent_orders_block(request)
+
+            if shop == shop1:
+                assert order.customer.name in block.content
+            else:
+                assert order.customer.name not in block.content