def test_mptt_filter(rf, admin_user): parent_category = Category.objects.create(name="parent") child_category = Category.objects.create(name="child") parent_category.children.add(child_category) columns = [ Column("name", "name", filter_config=MPTTFilter(choices=Category.objects.all(), filter_field="id")) ] pico = get_pico(rf, admin_user, model=Category, columns=columns) data = pico.get_data({ "perPage": 100, "page": 1, "filters": { "id": parent_category.id } }) assert len(data["items"]) == 2 data = pico.get_data({ "perPage": 100, "page": 1, "filters": { "name": child_category.id } }) assert len(data["items"]) == 1
class CategoryListView(PicotableListView): model = Category default_columns = [ Column("image", _("Image"), sortable=False, linked=True, raw=True), Column("name", _(u"Name"), sortable=False, display="format_name", linked=True, filter_config=MPTTFilter(choices="get_name_filter_choices", filter_field="id")), Column("status", _(u"Status"), filter_config=ChoicesFilter( choices=CategoryStatus.choices, default=CategoryStatus.VISIBLE.value)), Column( "visibility", _(u"Visibility"), filter_config=ChoicesFilter(choices=CategoryVisibility.choices)), ] def get_name_filter_choices(self): choices = [] shop = self.request.shop for c in Category.objects.all_except_deleted(shop=shop): name = self.format_name(c) choices.append((c.pk, name)) return choices def get_queryset(self): return Category.objects.all_except_deleted(shop=get_shop(self.request)) def format_name(self, instance, *args, **kwargs): level = getattr(instance, instance._mptt_meta.level_attr) return ('---' * level) + ' ' + instance.name def get_object_abstract(self, instance, item): return [ { "text": "%s" % instance.name, "class": "header" }, { "title": _(u"Status"), "text": item.get("status") }, ] def get_toolbar(self): toolbar = super(CategoryListView, self).get_toolbar() toolbar.append( URLActionButton(url=reverse("shuup_admin:category.organize"), text=_("Organize"), tooltip=_("Organize categories"), icon="fa fa-sitemap", extra_css_class="btn-default")) return toolbar
class CategoryListView(PicotableListView): model = Category category_status_choices = [(status.value, status) for status in CategoryStatus if status != CategoryStatus.DELETED] default_columns = [ Column("image", _("Image"), sortable=False, linked=True, raw=True), Column("name", _(u"Name"), sort_field="translations__name", display="format_name", linked=True, allow_highlight=False, filter_config=MPTTFilter(choices="get_name_filter_choices", filter_field="id")), Column("status", _(u"Status"), filter_config=ChoicesFilter( choices=category_status_choices, default=CategoryStatus.VISIBLE.value)), Column( "visibility", _(u"Visibility"), filter_config=ChoicesFilter(choices=CategoryVisibility.choices)), ] toolbar_buttons_provider_key = "category_list_toolbar_provider" mass_actions_provider_key = "category_list_mass_actions_provider" def get_name_filter_choices(self): choices = [] shop = self.request.shop for c in Category.objects.all_except_deleted(shop=shop): name = self.format_name(c) choices.append((c.pk, name)) return choices def get_queryset(self): return Category.objects.all_except_deleted(shop=get_shop(self.request)) def format_name(self, instance, *args, **kwargs): level = getattr(instance, instance._mptt_meta.level_attr) return ('---' * level) + ' ' + instance.name def get_object_abstract(self, instance, item): return [ { "text": "%s" % instance.name, "class": "header" }, { "title": _(u"Status"), "text": item.get("status") }, ]
class CategoryListView(PicotableListView): model = Category default_columns = [ Column("image", _("Image"), sortable=False, linked=True, raw=True), Column("name", _(u"Name"), sortable=False, display="format_name", linked=True, filter_config=MPTTFilter(choices="get_name_filter_choices", filter_field="id")), Column("status", _(u"Status"), filter_config=ChoicesFilter( choices=CategoryStatus.choices, default=CategoryStatus.VISIBLE.value)), Column( "visibility", _(u"Visibility"), filter_config=ChoicesFilter(choices=CategoryVisibility.choices)), ] def get_name_filter_choices(self): choices = [] for c in Category.objects.all_except_deleted(): name = self.format_name(c) choices.append((c.pk, name)) return choices def get_queryset(self): return Category.objects.all_except_deleted() def format_name(self, instance, *args, **kwargs): level = getattr(instance, instance._mptt_meta.level_attr) return ('---' * level) + ' ' + instance.name def get_object_abstract(self, instance, item): return [ { "text": "%s" % instance, "class": "header" }, { "title": _(u"Status"), "text": item.get("status") }, ]