class InstrumentsInformationFilter(django_filters.FilterSet): site = django_filters.MultipleChoiceFilter( choices=configdb.get_site_tuples(), label='Site code') enclosure = django_filters.MultipleChoiceFilter( choices=configdb.get_enclosure_tuples(), label='Enclosure code') telescope_class = django_filters.MultipleChoiceFilter( choices=configdb.get_telescope_class_tuples(), label='Telescope class') telescope = django_filters.MultipleChoiceFilter( choices=configdb.get_telescope_tuples(), label='Telescope code')
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['site'] = forms.ChoiceField( choices=configdb.get_site_tuples(include_blank=True)) self.fields['enclosure'] = forms.ChoiceField( choices=configdb.get_enclosure_tuples(include_blank=True)) self.fields['telescope'] = forms.ChoiceField( choices=configdb.get_telescope_tuples(include_blank=True)) self.fields['telescope_class'] = forms.ChoiceField( choices=configdb.get_telescope_class_tuples())
class LocationSerializer(serializers.ModelSerializer): site = serializers.ChoiceField(choices=configdb.get_site_tuples(), required=False) enclosure = serializers.ChoiceField( choices=configdb.get_enclosure_tuples(), required=False) telescope = serializers.ChoiceField( choices=configdb.get_telescope_tuples(), required=False) telescope_class = serializers.ChoiceField( choices=configdb.get_telescope_class_tuples(), required=True) class Meta: model = Location exclude = Location.SERIALIZER_EXCLUDE def validate(self, data): if 'enclosure' in data and 'site' not in data: raise serializers.ValidationError( _("Must specify a site with an enclosure.")) if 'telescope' in data and 'enclosure' not in data: raise serializers.ValidationError( _("Must specify an enclosure with a telescope.")) site_data_dict = { site['code']: site for site in configdb.get_site_data() } if 'site' in data: if data['site'] not in site_data_dict: msg = _('Site {} not valid. Valid choices: {}').format( data['site'], ', '.join(site_data_dict.keys())) raise serializers.ValidationError(msg) enc_set = site_data_dict[data['site']]['enclosure_set'] enc_dict = {enc['code']: enc for enc in enc_set} if 'enclosure' in data: if data['enclosure'] not in enc_dict: raise serializers.ValidationError( _(f'Enclosure {data["enclosure"]} not valid. Valid choices: {", ".join(enc_dict.keys())}' )) tel_set = enc_dict[data['enclosure']]['telescope_set'] tel_list = [tel['code'] for tel in tel_set] if 'telescope' in data and data['telescope'] not in tel_list: msg = _( 'Telescope {} not valid. Valid choices: {}').format( data['telescope'], ', '.join(tel_list)) raise serializers.ValidationError(msg) return data def to_representation(self, instance): """ This method is overridden to remove blank fields from serialized output. We could put this into a subclassed ModelSerializer if we want it to apply to all our Serializers. """ rep = super().to_representation(instance) return {key: val for key, val in rep.items() if val}
class LastChangedFilter(django_filters.FilterSet): telescope_class = django_filters.MultipleChoiceFilter( choices=configdb.get_telescope_class_tuples())