# Category filter -- restricted categories accordingly to CategoryUserRoles and categories filtered via AdminSettings. # custom registered DateField filter. Filter is inserted to the beginning of filter chain. category_lookup = lambda fspec: '%s__%s__exact' % (fspec.f.name, fspec.f.rel.get_related_field().name) @filter_spec(lambda field: is_category_fk(field), category_lookup) def category_field_filter(fspec): qs = Category.objects.filter(pk__in=applicable_categories(fspec.user)) for cat in user_category_filter(qs, fspec.user): lookup_var = '%s__%s__exact' % (fspec.f.name, fspec.f.rel.to._meta.pk.name) link = ( cat, {lookup_var: cat.pk}) fspec.links.append(link) return True site_lookup = lambda fspec: '%s__%s__exact' % (fspec.f.name, fspec.f.rel.get_related_field().name) @filter_spec(lambda field: is_site_fk(field), site_lookup) def site_field_filter(fspec): category_ids = get_user_config(fspec.user, CATEGORY_FILTER) if not category_ids: if not fspec.user.is_superuser: category_ids = m.DenormalizedCategoryUserRole.objects.root_categories_by_user(fspec.user) else: category_ids = Category.objects.filter(tree_parent=None) qs = Category.objects.filter(pk__in=category_ids) sites = map(lambda c: c.site, qs) for site in sites: #category__site__id__exact=1 lookup_var = '%s__%s__exact' % (fspec.f.name, fspec.f.rel.get_related_field().name) link = ( site, {lookup_var: site.pk}) fspec.links.append(link) return True
return lk @filter_spec(lambda field: is_category_fk(field), category_field_lookup) def category_field_filter(fspec): #qs = Category.objects.filter(pk__in=applicable_categories(fspec.user)) print 'Categories %d' % fspec.model_admin.queryset(fspec.request).count() #print 'self.params %s' % fspec.params #print 'self.result_list %s' % fspec.result_list qs = Category.objects.filter(pk__in=applicable_categories(fspec.user)) for cat in user_category_filter(qs, fspec.user): lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name) link = ( cat, {lookup_var: cat.pk}) fspec.links.append(link) return True @filter_spec(lambda field: is_site_fk(field)) def site_field_filter(fspec): category_ids = get_user_config(fspec.user, CATEGORY_FILTER) if not category_ids: if not fspec.user.is_superuser: category_ids = m.DenormalizedCategoryUserRole.objects.root_categories_by_user(fspec.user) else: category_ids = Category.objects.filter(tree_parent=None) qs = Category.objects.filter(pk__in=category_ids) sites = map(lambda c: c.site, qs) for site in sites: #category__site__id__exact=1 lookup_var = '%s__%s__exact' % (fspec.field_path, fspec.f.rel.to._meta.pk.name) link = ( site, {lookup_var: site.pk}) fspec.links.append(link) return True