Quellcode durchsuchen

Make child products searchable in the dashboard (#3642)

master
Orinda Harrison(Mitch) vor 4 Jahren
Ursprung
Commit
eaccba15dc
Es ist kein Account mit der E-Mail-Adresse des Committers verbunden

+ 8
- 6
src/oscar/apps/dashboard/catalogue/views.py Datei anzeigen

@@ -139,14 +139,15 @@ class ProductListView(PartnerProductFilterMixin, SingleTableView):
139 139
 
140 140
         data = self.form.cleaned_data
141 141
 
142
-        if data.get('upc'):
142
+        upc = data.get('upc')
143
+        if upc:
143 144
             # Filter the queryset by upc
144 145
             # For usability reasons, we first look at exact matches and only return
145 146
             # them if there are any. Otherwise we return all results
146 147
             # that contain the UPC.
147 148
 
148 149
             # Look up all matches (child products, products not allowed to access) ...
149
-            matches_upc = Product.objects.filter(upc__iexact=data['upc'])
150
+            matches_upc = Product.objects.filter(Q(upc__iexact=upc) | Q(children__upc__iexact=upc))
150 151
 
151 152
             # ... and use that to pick all standalone or parent products that the user is
152 153
             # allowed to access.
@@ -158,14 +159,15 @@ class ProductListView(PartnerProductFilterMixin, SingleTableView):
158 159
                 queryset = qs_match
159 160
             else:
160 161
                 # No direct UPC match. Let's try the same with an icontains search.
161
-                matches_upc = Product.objects.filter(upc__icontains=data['upc'])
162
+                matches_upc = Product.objects.filter(Q(upc__icontains=upc) | Q(children__upc__icontains=upc))
162 163
                 queryset = queryset.filter(
163 164
                     Q(id__in=matches_upc.values('id')) | Q(id__in=matches_upc.values('parent_id')))
164 165
 
165
-        if data.get('title'):
166
-            queryset = queryset.filter(title__icontains=data['title'])
166
+        title = data.get('title')
167
+        if title:
168
+            queryset = queryset.filter(Q(title__icontains=title) | Q(children__title__icontains=title))
167 169
 
168
-        return queryset
170
+        return queryset.distinct()
169 171
 
170 172
 
171 173
 class ProductCreateRedirectView(generic.RedirectView):

+ 36
- 0
tests/integration/dashboard/test_catalogue_views.py Datei anzeigen

@@ -0,0 +1,36 @@
1
+from django.test import TestCase
2
+
3
+from oscar.apps.dashboard.catalogue.views import ProductListView
4
+from oscar.core.loading import get_model
5
+from oscar.test.factories import create_product
6
+from oscar.test.utils import RequestFactory
7
+
8
+Product = get_model('catalogue', 'Product')
9
+
10
+
11
+class ProductListViewTestCase(TestCase):
12
+    def test_searching_child_product_by_title_returns_parent_product(self):
13
+        self.parent_product = create_product(structure=Product.PARENT, title='Parent', upc='PARENT_UPC')
14
+        create_product(structure=Product.CHILD, title='Child', parent=self.parent_product, upc='CHILD_UPC')
15
+        view = ProductListView(request=RequestFactory().get('/?title=Child'))
16
+        assert list(view.get_queryset()) == [self.parent_product]
17
+
18
+    def test_searching_child_product_by_title_returns_1_parent_product_if_title_is_partially_shared(self):
19
+        self.parent_product = create_product(structure=Product.PARENT, title='Shared', upc='PARENT_UPC')
20
+        create_product(structure=Product.CHILD, title='Shared', parent=self.parent_product, upc='CHILD_UPC')
21
+        create_product(structure=Product.CHILD, title='Shared1', parent=self.parent_product, upc='CHILD_UPC1')
22
+        view = ProductListView(request=RequestFactory().get('/?title=Shared'))
23
+        assert list(view.get_queryset()) == [self.parent_product]
24
+
25
+    def test_searching_child_product_by_upc_returns_parent_product(self):
26
+        self.parent_product = create_product(structure=Product.PARENT, title='Parent', upc='PARENT_UPC')
27
+        create_product(structure=Product.CHILD, title='Child', parent=self.parent_product, upc='CHILD_UPC')
28
+        view = ProductListView(request=RequestFactory().get('/?upc=CHILD_UPC'))
29
+        assert list(view.get_queryset()) == [self.parent_product]
30
+
31
+    def test_searching_child_product_by_upc_returns_1_parent_product_if_upc_is_partially_shared(self):
32
+        self.parent_product = create_product(structure=Product.PARENT, title='Parent', upc='PARENT_UPC')
33
+        create_product(structure=Product.CHILD, title='Child', parent=self.parent_product, upc='CHILD_UPC')
34
+        create_product(structure=Product.CHILD, title='Child1', parent=self.parent_product, upc='CHILD_UPC1')
35
+        view = ProductListView(request=RequestFactory().get('/?upc=CHILD'))
36
+        assert list(view.get_queryset()) == [self.parent_product]

Laden…
Abbrechen
Speichern