Repository: fidals/shopelectro

Issue:
tests_selenium_admin.py:146-149: Resurrect admin price...
The puzzle `822-138e7a1d` from #822 has to be resolved: 

https://github.com/fidals/shopelectro/blob/c89b807552a45099cad752b4121f40d8836eb024/shopelectro/tests/tests_selenium_admin.py#L146-L149

The puzzle was created by duker33 on 30-Apr-19. 

  

If you have any technical questions, don't ask me, submit new tickets instead. The task will be \"done\" when the problem is fixed and the text of the puzzle is _removed_ from the source code. Here is more about [PDD](http://www.yegor256.com/2009/03/04/pdd.html) and [about me](http://www.yegor256.com/2017/04/05/pdd-in-action.html). 



Discussion:
@artemiyds2 @duker33 please pay attention to this new issue.

Resolving patch:
diff --git a/shopelectro/admin.py b/shopelectro/admin.py
--- a/shopelectro/admin.py
+++ b/shopelectro/admin.py
@@ -12,7 +12,7 @@
 from ecommerce import mailer
 from ecommerce.models import Position
 from pages.models import CustomPage, FlatPage, PageTemplate
-from generic_admin import inlines, mixins, models, sites
+from generic_admin import inlines, mixins, models, sites, filters
 
 from shopelectro import models as se_models
 from shopelectro.views.admin import TableEditor
@@ -34,6 +34,11 @@ def prepare_has_filter_queryset(value, db_table, queryset):
     return queryset.filter(**{query: value != 'yes'})
 
 
+class ProductPriceFilter(filters.PriceRange):
+
+    price_lookup = 'shopelectro_product__price'
+
+
 class HasTagsFilter(admin.SimpleListFilter):
 
     product_model = se_models.Product
@@ -155,7 +160,13 @@ class ProductPageAdmin(models.ProductPageAdmin):
     add = False
     delete = False
     category_page_model = se_models.CategoryPage
-    list_filter = [*models.ProductPageAdmin.list_filter, HasTagsFilter, HasCategoryFilter]
+    list_filter = [
+        *models.ProductPageAdmin.list_filter,
+        ProductPriceFilter,
+        HasTagsFilter,
+        HasCategoryFilter,
+    ]
+    list_display = ['model_id', 'name', 'custom_parent', 'price', 'links', 'is_active']
     inlines = [ProductInline, inlines.ImageInline]
     search_fields = [
         'shopelectro_product__vendor_code', 'name', 'slug',
@@ -167,10 +178,16 @@ def model_id(self, obj):
     model_id.short_description = _('Vendor code')
     model_id.admin_order_field = 'shopelectro_product__vendor_code'
 
+    def price(self, obj):
+        return obj.model.price
+
+    price.short_description = _('Price')
+    price.admin_order_field = '_product_price'
+
     def get_queryset(self, request):
+        qs = super().get_queryset(request)
         return (
-            super(ProductPageAdmin, self)
-            .get_queryset(request)
+            self.add_reference_to_field_on_related_model(qs, _product_price='price')
             .select_related('shopelectro_product')
         )
 


Test patch:
diff --git a/shopelectro/tests/tests_selenium_admin.py b/shopelectro/tests/tests_selenium_admin.py
--- a/shopelectro/tests/tests_selenium_admin.py
+++ b/shopelectro/tests/tests_selenium_admin.py
@@ -59,6 +59,10 @@ def setUp(self):
 class AdminPage(AdminSeleniumTestCase):
     """Selenium-based tests for Admin page UI."""
 
+    # @todo #835:60m Fix admin filter tests
+    #  Tests rely upon order of html markup.
+    #  It is fragile and always breaks after admin filter modifications.
+
     @classmethod
     def setUpClass(cls):
         super().setUpClass()
@@ -67,11 +71,11 @@ def setUpClass(cls):
         cls.title_text = 'Shopelectro administration'
         cls.active_products = '//*[@id="changelist-filter"]/ul[1]/li[2]/a'
         cls.inactive_products = '//*[@id="changelist-filter"]/ul[1]/li[3]/a'
-        cls.price_filter = '//*[@id="changelist-filter"]/ul[2]/li[3]/a'
-        cls.filter_by_has_content = '//*[@id="changelist-filter"]/ul[3]/li[2]/a'
-        cls.filter_by_has_not_content = '//*[@id="changelist-filter"]/ul[3]/li[3]/a'
-        cls.filter_by_has_image = '//*[@id="changelist-filter"]/ul[4]/li[2]/a'
-        cls.filter_by_has_not_image = '//*[@id="changelist-filter"]/ul[4]/li[3]/a'
+        cls.price_filter = '//*[@id="changelist-filter"]/ul[4]/li[3]/a'
+        cls.filter_by_has_content = '//*[@id="changelist-filter"]/ul[2]/li[2]/a'
+        cls.filter_by_has_not_content = '//*[@id="changelist-filter"]/ul[2]/li[3]/a'
+        cls.filter_by_has_image = '//*[@id="changelist-filter"]/ul[3]/li[2]/a'
+        cls.filter_by_has_not_image = '//*[@id="changelist-filter"]/ul[3]/li[3]/a'
         cls.is_active_img = 'field-is_active'
         cls.autocomplete_text = 'Prod'
         cls.jstree_children = 'jstree-children'
@@ -143,11 +147,6 @@ def test_login(self):
         admin_title = self.browser.find_element_by_id('site-name')
         self.assertIn(self.title_text, admin_title.text)
 
-    # @todo #822:30m  Resurrect admin price filter.
-    #  It's failing after fix changing `PriceRange.price_lookup` value.
-    #  Fix was here: https://github.com/fidals/refarm-site/pull/325
-    #  Just disable this filter for STB. It's not relevant there
-    @unittest.expectedFailure
     def test_product_price_filter(self):
         """
         Price filter is able to filter products by set range.
@@ -164,7 +163,7 @@ def test_product_price_filter(self):
         ))
         product_price = int(float(product.text.replace(',', '.')))
 
-        self.assertTrue(product_price >= 1000)
+        self.assertTrue(product_price >= 1000 and product_price < 2000)
 
     def test_image_filter(self):
         """Image filter is able to filter pages by the presence of the image."""