class LoggedActionRecentChangesFilter(django_filters.FilterSet): """ For filtering the recent changes page """ class Meta: model = LoggedAction fields = ["action_type", "flagged_type", "edit_type", "username"] flagged_type = django_filters.ChoiceFilter( choices=[ (t.type, t.label) for t in REVIEW_TYPES if not t.type.startswith("no_review_") ], widget=LinkWidget(), ) edit_type = django_filters.ChoiceFilter( choices=[(edit_type.name, edit_type.value) for edit_type in EditType], widget=LinkWidget(), ) action_type = django_filters.AllValuesMultipleFilter( choices=get_action_types ) username = django_filters.CharFilter( label="User name", method="filter_username" ) def filter_username(self, queryset, name, value): return queryset.filter(user__username=value)
def test_widget(self): choices = ( ('test-val1', 'test-label1'), ('test-val2', 'test-label2'), ) w = LinkWidget(choices=choices) self.assertEqual(len(w.choices), 2) self.assertHTMLEqual(w.render('price', ''), """ <ul> <li><a href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""") self.assertHTMLEqual(w.render('price', None), """ <ul> <li><a href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""") self.assertHTMLEqual(w.render('price', 'test-val1'), """ <ul> <li><a class="selected" href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""")
def test_widget(self): choices = ( ('test-val1', 'test-label1'), ('test-val2', 'test-label2'), ) w = LinkWidget(choices=choices) self.assertEqual(len(w.choices), 2) self.assertHTMLEqual( w.render('price', ''), """ <ul> <li><a href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""") self.assertHTMLEqual( w.render('price', None), """ <ul> <li><a href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""") self.assertHTMLEqual( w.render('price', 'test-val1'), """ <ul> <li><a class="selected" href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""")
class BallotFilter(django_filters.FilterSet): def lock_status(self, queryset, name, value): """ Unlocked ballots with a document but no lock suggestion """ kwargs = {} if value == "locked": kwargs["candidates_locked"] = True elif value == "suggestion": kwargs["suggestedpostlock__isnull"] = False kwargs["candidates_locked"] = False elif value == "unlocked": kwargs["suggestedpostlock__isnull"] = True kwargs["candidates_locked"] = False return queryset.filter(**kwargs) def has_sopn_filter(self, queryset, name, value): if int(value): return queryset.exclude(officialdocument=None) else: return queryset.filter(officialdocument=None) def election_type_filter(self, queryset, name, value): return queryset.filter(election__slug__startswith=value) review_required = django_filters.ChoiceFilter( field_name="review_required", method="lock_status", widget=LinkWidget(), label="Lock status", choices=[ ("locked", "Locked"), ("suggestion", "Lock suggestion"), ("unlocked", "Unlocked"), ], ) has_sopn = django_filters.ChoiceFilter( field_name="has_sopn", method="has_sopn_filter", widget=LinkWidget(), label="Has SoPN", choices=[(1, "Yes"), (0, "No")], ) election_type = CurrentElectionTypesFilter( widget=LinkWidget(), method="election_type_filter", label="Election Type", ) class Meta: model = PostExtraElection fields = ["review_required", "has_sopn"]
def __init__(self, *args, **kwargs): """ Initialiser le filtre """ super(FlagFilterSet, self).__init__(*args, **kwargs) self.filters['type'].widget = LinkWidget(attrs={'class': ''}) self.filters['status'].widget = LinkWidget(attrs={'class': ''}) self.filters['automatic'].widget = LinkWidget(attrs={'class': ''}) for _name, field in self.filters.items(): if isinstance(field, ChoiceFilter): field.extra['choices'] = tuple([("", _("All"))] + list(field.extra['choices']))
def test_widget_with_blank_choice(self): choices = ( ('', '---------'), ('test-val1', 'test-label1'), ('test-val2', 'test-label2'), ) w = LinkWidget(choices=choices) self.assertHTMLEqual(w.render('price', ''), """ <ul> <li><a class="selected" href="?price=">All</a></li> <li><a href="?price=test-val1">test-label1</a></li> <li><a href="?price=test-val2">test-label2</a></li> </ul>""")
class ProductionOrderFilter(StateOrderFilter): from mrp.models import ProductionType production_type = django_filters.ModelChoiceFilter( label='状态', queryset=ProductionType.objects.all(), widget=LinkWidget(attrs={'class': 'inline-ul'})) class Meta: model = ProductionOrder fields = ('state', 'production_type', 'partner')
class StateOrderFilter(django_filters.FilterSet): state = django_filters.ChoiceFilter( label='状态', choices=( ('draft', '草稿'), ('confirm', '确认'), ('done', '完成'), ('cancel', '取消'), ), widget=LinkWidget(attrs={'class': 'inline-ul'}))
class CurrentOrFutureBallotFilter(BaseBallotFilter): """ Same as Ballot Filter, but only present options related to current elections """ election_type = django_filters.ChoiceFilter( widget=LinkWidget(), method="election_type_filter", choices=current_election_types_choices, label="Election Type", )
def test_widget_with_option_groups(self): choices = ( ('Audio', ( ('vinyl', 'Vinyl'), ('cd', 'CD'), )), ('Video', ( ('vhs', 'VHS Tape'), ('dvd', 'DVD'), )), ('unknown', 'Unknown'), ) w = LinkWidget(choices=choices) self.assertHTMLEqual(w.render('media', ''), """ <ul> <li><a href="?media=vinyl">Vinyl</a></li> <li><a href="?media=cd">CD</a></li> <li><a href="?media=vhs">VHS Tape</a></li> <li><a href="?media=dvd">DVD</a></li> <li><a href="?media=unknown">Unknown</a></li> </ul>""")
class PaymentFilter(django_filters.FilterSet): state = django_filters.ChoiceFilter( label='状态', field_name='type', choices=TYPE_CHOICES, widget=LinkWidget(attrs={'class': 'inline-ul'})) confirm = django_filters.BooleanFilter(label='确认收款') class Meta: model = Payment fields = ('state', 'confirm') def __init__(self, data=None, *args, **kwargs): if not data: data = {'state': '1', 'confirm': False} super().__init__(data=data, *args, **kwargs)
def test_widget_without_choices(self): w = LinkWidget() self.assertEqual(len(w.choices), 0) self.assertHTMLEqual(w.render('price', ''), """<ul />""")
def test_widget_value_from_datadict(self): w = LinkWidget() data = {'price': 'test-val1'} result = w.value_from_datadict(data, {}, 'price') self.assertEqual(result, 'test-val1')
class BaseBallotFilter(django_filters.FilterSet): def lock_status(self, queryset, name, value): """ Unlocked ballots with a document but no lock suggestion """ kwargs = {} if value == "locked": kwargs["candidates_locked"] = True elif value == "suggestion": kwargs["suggestedpostlock__isnull"] = False kwargs["candidates_locked"] = False elif value == "unlocked": kwargs["suggestedpostlock__isnull"] = True kwargs["candidates_locked"] = False return queryset.filter(**kwargs) def has_sopn_filter(self, queryset, name, value): if int(value): return queryset.exclude(officialdocument=None) else: return queryset.filter(officialdocument=None) def election_type_filter(self, queryset, name, value): return queryset.filter(election__slug__startswith=value) review_required = django_filters.ChoiceFilter( field_name="review_required", method="lock_status", widget=LinkWidget(), label="Lock status", help_text="One of `locked`, `suggestion` or `unlocked`.", choices=[ ("locked", "Locked"), ("suggestion", "Lock suggestion"), ("unlocked", "Unlocked"), ], ) has_sopn = django_filters.ChoiceFilter( field_name="has_sopn", method="has_sopn_filter", widget=LinkWidget(), label="Has SoPN", help_text="""Boolean, `1` for ballots that have a SOPN uploaded or `0` for ballots without SOPNs""", choices=[(1, "Yes"), (0, "No")], ) election_type = django_filters.ChoiceFilter( widget=LinkWidget(), method="election_type_filter", choices=election_types_choices, label="Election Type", help_text= "A valid [election type](https://elections.democracyclub.org.uk/election_types/)", ) class Meta: model = Ballot fields = ["review_required", "has_sopn"]