import operator
from functools import reduce

from django.db.models import Q
from django.forms import MultipleChoiceField


class MultipleValueField(MultipleChoiceField):
    def __init__(self, *args, field_class, **kwargs):
        self.inner_field = field_class()
        super().__init__(*args, **kwargs)

    def valid_value(self, value):
        return self.inner_field.validate(value)

    def clean(self, values):
        return values and [self.inner_field.clean(value) for value in values]


class DisplayMultipleChoiceField(MultipleChoiceField):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.choices = [(v, k) for k, v in self.choices]

    def clean(self, value):
        """
        Validate the given value and return its "cleaned" value as an
        appropriate Python object. Raise ValidationError for any errors.
        """
        value = super().clean(value)
        string_to_value_map = dict(self.choices)
        return [string_to_value_map.get(val) for val in value]
