Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#835 admin price filter #848

Merged
merged 5 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ generate-production-static-data deploy


# ---------------------- Dev section ----------------------
repl:
$(dc) exec app python manage.py shell

bash:
$(dc) exec app bash

migrate:
$(dc) run --rm app python manage.py migrate

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ sorl-thumbnail==12.5.0
python-telegram-bot==11.1.0
sentry-sdk==0.7.2
https://github.com/selwin/django-user_agents/archive/master.zip
https://github.com/fidals/refarm-site/archive/0.5.11.zip
https://github.com/fidals/refarm-site/archive/0.5.12.zip
25 changes: 21 additions & 4 deletions shopelectro/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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')
)

Expand Down
21 changes: 10 additions & 11 deletions shopelectro/tests/tests_selenium_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'
Expand Down Expand Up @@ -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.
Expand All @@ -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."""
Expand Down