コード例 #1
0
class ExperimentListView(FilterView):
    context_object_name = "experiments"
    filterset_class = ExperimentFilterset
    model = Experiment
    template_name = "experiments/list.html"
    paginate_by = settings.EXPERIMENTS_PAGINATE_BY
    queryset = Experiment.objects.get_prefetched()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ordering_form = None

    def get_filterset_kwargs(self, *args, **kwargs):
        kwargs = super().get_filterset_kwargs(*args, **kwargs)

        # Always pass in request.GET otherwise the
        # filterset form will be unbound and our custom
        # validation won't kick in
        kwargs["data"] = self.request.GET
        return kwargs

    def get_ordering(self):
        self.ordering_form = ExperimentOrderingForm(self.request.GET)

        if self.ordering_form.is_valid():
            return self.ordering_form.cleaned_data["ordering"]

        return self.ordering_form.ORDERING_CHOICES[0][0]

    def get_context_data(self, *args, **kwargs):
        return super().get_context_data(ordering_form=self.ordering_form,
                                        *args,
                                        **kwargs)
コード例 #2
0
 def test_rejects_invalid_ordering(self):
     form = ExperimentOrderingForm({"ordering": "invalid ordering"})
     self.assertFalse(form.is_valid())
コード例 #3
0
 def test_accepts_valid_ordering(self):
     ordering = ExperimentOrderingForm.ORDERING_CHOICES[1][0]
     form = ExperimentOrderingForm({"ordering": ordering})
     self.assertTrue(form.is_valid())