class HeavySelect2MultipleWidgetForm(forms.Form): genres = forms.MultipleChoiceField(widget=HeavySelect2MultipleWidget( data_view='heavy_data_1', choices=NUMBER_CHOICES)) featured_artists = forms.MultipleChoiceField( widget=HeavySelect2MultipleWidget(data_view='heavy_data_2', choices=NUMBER_CHOICES), required=False)
class Meta: if use_select2: widgets = { 'authors': HeavySelect2MultipleWidget(data_view='profile_select2', attrs={'style': 'width: 100%'}), 'banned_users': HeavySelect2MultipleWidget(data_view='profile_select2', attrs={'style': 'width: 100%'}), 'types': Select2MultipleWidget, 'group': Select2Widget, }
class HeavySelect2MultipleWidgetForm(forms.Form): title = forms.CharField(max_length=50) genres = forms.MultipleChoiceField(widget=HeavySelect2MultipleWidget( data_view='heavy_data_1', choices=NUMBER_CHOICES), choices=NUMBER_CHOICES) featured_artists = forms.MultipleChoiceField( widget=HeavySelect2MultipleWidget(data_view='heavy_data_2', choices=NUMBER_CHOICES), choices=NUMBER_CHOICES, required=False) def clean_title(self): if len(self.cleaned_data['title']) < 3: raise forms.ValidationError( "Title must have more than 3 characters.") return self.cleaned_data["title"]
class ContestTagForm(ModelForm): contests = ModelMultipleChoiceField( label=_('Included contests'), queryset=Contest.objects.all(), required=False, widget=HeavySelect2MultipleWidget(data_view='contest_select2') if use_select2 else FilteredSelectMultiple('contests', False))
class ProblemTypeForm(ModelForm): problems = ModelMultipleChoiceField( label=_('Included problems'), queryset=Problem.objects.all(), required=False, help_text=_('These problems are included in this type of problems'), widget=HeavySelect2MultipleWidget(data_view='problem_select2') if use_select2 else FilteredSelectMultiple('problems', False))
class LanguageForm(ModelForm): problems = ModelMultipleChoiceField( label=_('Disallowed problems'), queryset=Problem.objects.all(), required=False, help_text=_('These problems are NOT allowed to be submitted in this language'), widget=HeavySelect2MultipleWidget(data_view='problem_select2') if use_select2 else FilteredSelectMultiple('problems', False))
class HeavySelect2MultipleWidgetForm(forms.Form): title = forms.CharField(max_length=50) genres = forms.MultipleChoiceField( widget=HeavySelect2MultipleWidget( data_view="heavy_data_1", choices=NUMBER_CHOICES, attrs={"data-minimum-input-length": 0}, ), choices=NUMBER_CHOICES, ) featured_artists = forms.MultipleChoiceField( widget=HeavySelect2MultipleWidget( data_view="heavy_data_2", choices=NUMBER_CHOICES, attrs={"data-minimum-input-length": 0}, ), choices=NUMBER_CHOICES, required=False, ) def clean_title(self): if len(self.cleaned_data["title"]) < 3: raise forms.ValidationError("Title must have more than 3 characters.") return self.cleaned_data["title"]
# -*- coding: utf-8 -*- from django import forms from django.core.exceptions import ValidationError from django_select2.forms import HeavySelect2MultipleWidget from vidtrest.apps.categories.models import VideoCat from .models import Vid from .services import ExtractcombinedTagsCategoriesService SELECT_2_WIDGET = HeavySelect2MultipleWidget( data_view='categories:autocomplete') class AnyChoiceMultipleChoiceField(forms.MultipleChoiceField): """ Allows a choice of any value, for select2 """ def validate(self, value): if self.required and not value: raise ValidationError(self.error_messages['required'], code='required') class VidForm(forms.ModelForm): combined_tags = AnyChoiceMultipleChoiceField(widget=SELECT_2_WIDGET, required=False) categories = forms.ModelMultipleChoiceField( queryset=VideoCat.objects.all(), widget=forms.CheckboxSelectMultiple(),
# -*- coding: utf-8 -*- from django import forms from django.urls import reverse from django.utils.translation import ugettext_lazy as _ from django_select2.forms import HeavySelect2MultipleWidget from crispy_forms.helper import FormHelper from crispy_forms.layout import Layout, ButtonHolder, Field, Submit from vidtrest.apps.vid.forms import AnyChoiceMultipleChoiceField SELECT_2_WIDGET = HeavySelect2MultipleWidget( data_view='public:search_autocomplete') class SearchForm(forms.Form): query = AnyChoiceMultipleChoiceField(widget=SELECT_2_WIDGET, label='', required=True) def __init__(self, *args, **kwargs): super(SearchForm, self).__init__(*args, **kwargs) # Setup the initial values for the combined_tags field # @TODO clean this nastiness up move into form or widget self.fields['query'].widget.attrs.update( {'data-tags': 'true'}) # V important for taking multiple and creating new @property
class ItemFilter(django_filters.FilterSet): short_title = django_filters.Filter(lookup_expr='icontains', method='multiple_words_filter') lot = django_filters.Filter(field_name='lot__lot_as_listed_in_collection', lookup_expr='icontains') number_of_volumes = django_filters.Filter(lookup_expr='icontains') book_format = django_filters.ModelMultipleChoiceFilter( queryset=BookFormat.objects.all(), widget=Select2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, )) non_book = django_filters.BooleanFilter( label="Include non-book items", widget=CheckboxInput, method='include_non_book_items_filter') item_type = django_filters.ModelMultipleChoiceFilter( label="Item Type", queryset=ItemType.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=ItemType, search_fields=['name__icontains']), method='item_type_filter') collection = django_filters.ModelMultipleChoiceFilter( label="Collection", queryset=Collection.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Collection, search_fields=[ 'short_title__icontains', 'full_title__icontains', 'preface_and_paratexts__icontains' ]), method='collection_filter') collection_publication_year = django_filters.RangeFilter( label="Collection publication year", widget=RangeWidget(), field_name='lot__collection__year_of_publication') parisian_category = django_filters.ModelMultipleChoiceFilter( label="Parisian category", queryset=ParisianCategory.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=ParisianCategory, search_fields=['name__icontains', 'description__icontains']), method='parisian_category_filter') lot_isnull = django_filters.BooleanFilter(label="No associated lot", widget=CheckboxInput(), method='lot_isnull_filter') edition_isnull = django_filters.BooleanFilter( label="No associated edition", widget=CheckboxInput(), method='edition_isnull_filter') edition_isempty = django_filters.BooleanFilter( label="Associated edition is empty", widget=CheckboxInput(), method='edition_isempty_filter') edition_place = django_filters.ModelMultipleChoiceFilter( label="Stated place of publication", queryset=Place.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Place, search_fields=['name__icontains']), method='edition_place_filter') edition_year = django_filters.RangeFilter(label="Date of publication", widget=RangeWidget(), field_name='edition__year', method='year_filter') edition_year_tag = django_filters.Filter(label="Date of publication tag", field_name='edition__year_tag', lookup_expr='icontains') material_details = django_filters.ModelMultipleChoiceFilter( label="Material details", queryset=MaterialDetails.objects.all(), widget=Select2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, ), method='material_details_filter') tag = django_filters.ModelMultipleChoiceFilter( label='Tag', queryset=Tag.objects.all(), method='tag_filter', widget=ModelSelect2MultipleWidget( model=Tag, search_fields=['name__icontains'], ), ) person = django_filters.ModelMultipleChoiceFilter( label='Person', queryset=Person.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Person, search_fields=['short_name__icontains']), method='person_filter') role = django_filters.ModelMultipleChoiceFilter( label='Role', queryset=PersonItemRelationRole.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=PersonItemRelationRole, search_fields=['name__icontains']), method='role_filter') exclude_person = django_filters.ModelMultipleChoiceFilter( label='Not related person', queryset=Person.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Person, search_fields=['short_name__icontains']), method='exclude_person_filter') person_role = django_filters.MultipleChoiceFilter( label='Person and Role', choices=[], method='person_role_filter', widget=HeavySelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, data_view='personroleautoresponse')) # Override the field_class (with valid_value method) person_role.field_class = PersonRoleMultipleChoiceField owner_gender = django_filters.ChoiceFilter(label="Owner gender", choices=Person.SEX_CHOICES, method='owner_gender_filter') owner_country_of_birth = django_filters.ModelMultipleChoiceFilter( label="Owner country of birth", queryset=Country.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Country, search_fields=['name__icontains']), method='owner_country_of_birth_filter') owner_country_of_death = django_filters.ModelMultipleChoiceFilter( label="Owner country of death", queryset=Country.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Country, search_fields=['name__icontains']), method='owner_country_of_death_filter') owner_country_of_residence = django_filters.ModelMultipleChoiceFilter( label="Owner country of residence", queryset=Country.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Country, search_fields=['name__icontains']), method='owner_country_of_residence_filter') owner_profession = django_filters.ModelMultipleChoiceFilter( label="Owner profession", queryset=Profession.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Profession, search_fields=['name__icontains']), method='owner_profession_filter') owner_religion = django_filters.ModelMultipleChoiceFilter( label="Ovner religion", queryset=Religion.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Religion, search_fields=['name__icontains']), method='owner_religion_filter') publisher = django_filters.ModelMultipleChoiceFilter( label="Publisher", queryset=Person.objects.filter(publisher__isnull=False).distinct(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Person, queryset=Person.objects.filter(publisher__isnull=False).distinct(), search_fields=[ 'short_name__icontains', 'surname__icontains', 'first_names__icontains' ]), field_name='edition__publisher__publisher') language = django_filters.ModelMultipleChoiceFilter( label='Language', queryset=Language.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Language, search_fields=[ 'name__icontains', 'language_code_2char__iexact', 'language_code_3char__iexact' ]), method='language_filter') collection_country_of_publication = django_filters.ModelMultipleChoiceFilter( label="Collection country of publication", queryset=Country.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Country, search_fields=['name__icontains']), method='collection_country_of_publication_filter') collection_city_of_publication = django_filters.ModelMultipleChoiceFilter( label="Collection city of publication", queryset=Place.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Place, search_fields=['name__icontains']), method='collection_city_of_publication_filter') collection_tag = django_filters.ModelMultipleChoiceFilter( label="Collection tag", queryset=Tag.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Tag, search_fields=[ 'namespace__icontains', 'name__icontains', 'value__icontains' ]), method='collection_tag_filter') works = django_filters.ModelMultipleChoiceFilter( queryset=Work.objects.all(), widget=ModelSelect2MultipleWidget( attrs={'data-placeholder': "Select multiple"}, model=Work, search_fields=['title__icontains', 'viaf_id__icontains']), method='works_filter') class Meta: model = Item exclude = ['uuid'] fields = [ 'short_title', 'lot', 'number_of_volumes', 'book_format', 'non_book', 'item_type', 'collection', 'collection_publication_year', 'parisian_category', 'lot_isnull', 'edition_isnull', 'edition_isempty', 'edition_place', 'edition_year', 'edition_year_tag', 'material_details', 'tag', 'collection_country_of_publication', 'collection_city_of_publication', 'collection_tag', ] def __init__(self, data=None, *args, **kwargs): self.pass_selected_choices_to_person_role_filter(data) super().__init__(data, *args, **kwargs) def pass_selected_choices_to_person_role_filter(self, data): """Passes the selected choices are in field on the result page""" if data is not None: person_role_field = self.base_filters.get('person_role') person_roles = data.getlist('person_role') person_role_choices = [] for person_role in person_roles: person_id, role_id = person_role.split('|') person_name = Person.objects.get(uuid=person_id).short_name role_name = PersonItemRelationRole.objects.get( uuid=role_id).name person_role_choices.append( (person_role, "{} - {}".format(person_name, role_name))) person_role_field.extra['choices'] = person_role_choices def tag_filter(self, queryset, name, value): if value: return queryset.filter(tags__tag__in=value) return queryset def multiple_words_filter(self, queryset, name, value): return filter_multiple_words(self.filters[name].lookup_expr, queryset, name, value, wildcards=True) def material_details_filter(self, queryset, name, value): if value: return queryset.filter( itemmaterialdetailsrelation__material_details__in=value) else: return queryset def include_non_book_items_filter(self, queryset, name, value): """ If value is True Items with ItemType containing 'book' are included. Those Items are excluded by default. :param queryset: the filter queryset :param name: the name of the field :param value: the value of field :return: the filter queryset """ if value: return queryset return queryset.exclude(non_book=True) def item_type_filter(self, queryset, name, value): if value: return queryset.filter(itemitemtyperelation__type__name__in=value) return queryset def parisian_category_filter(self, queryset, name, value): if value: return queryset.filter(lot__category__parisian_category__in=value) return queryset def lot_isnull_filter(self, queryset, name, value): if value: return queryset.filter(lot__isnull=True) return queryset def edition_isnull_filter(self, queryset, name, value): if value: return queryset.filter(edition__isnull=True) return queryset def edition_isempty_filter(self, queryset, name, value): if value: return queryset.filter(edition__year__isnull=True, edition__place__isnull=True)\ .annotate(num_publishers=Count('edition__publisher')).filter(num_publishers=0) return queryset def edition_place_filter(self, queryset, name, value): if value: return queryset.filter(edition__place__in=value) return queryset def year_filter(self, queryset, name, value): if value: if value.start and not value.stop: q = Q(edition__year_start__gte=value.start) | Q( edition__year_end__gte=value.start) elif value.stop and not value.start: q = Q(edition__year_start__lte=value.stop) else: # value.start and value.end # Either year_start within value range # or year_end within value range # or value range inside range of year_start to year_end q = (Q(edition__year_start__gte=value.start) & Q(edition__year_start__lte=value.stop)) \ | (Q(edition__year_end__gte=value.start) & Q(edition__year_end__lte=value.stop)) \ | (Q(edition__year_start__lte=value.start) & Q(edition__year_end__gte=value.stop)) return queryset.filter(q) return queryset def collection_filter(self, queryset, name, value): if value: return queryset.filter(lot__collection__in=value) return queryset def person_filter(self, queryset, name, value): if value: return queryset.filter( personitemrelation__person__in=value).distinct() return queryset def role_filter(self, queryset, name, value): if value: return queryset.filter( personitemrelation__role__in=value).distinct() return queryset def exclude_person_filter(self, queryset, name, value): if value: return queryset.exclude( personitemrelation__person__in=value).distinct() return queryset def person_role_filter(self, queryset, name, value): if value: for person_role in value: person_id, role_id = person_role.split('|') queryset = queryset.filter( personitemrelation__person__uuid=person_id, personitemrelation__role__uuid=role_id) return queryset return queryset def language_filter(self, queryset, name, value): if value: return queryset.filter(languages__language__in=value) return queryset def owner_gender_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__sex=value)) return queryset def owner_country_of_birth_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__city_of_birth__country__in=value)) return queryset def owner_country_of_death_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__city_of_death__country__in=value)) return queryset def owner_country_of_residence_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__residence__place__country__in=value)) return queryset def owner_profession_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__personprofession__profession__in=value)) return queryset def owner_religion_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__personcollectionrelation__in= PersonCollectionRelation.objects.filter( role__name__iexact='owner', person__religiousaffiliation__religion__in=value)) return queryset def collection_country_of_publication_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__related_places__place__country__in=value ).distinct() return queryset def collection_city_of_publication_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__related_places__place__in=value).distinct() return queryset def collection_tag_filter(self, queryset, name, value): if value: return queryset.filter( lot__collection__tags__tag__in=value).distinct() return queryset def works_filter(self, queryset, name, value): if value: return queryset.filter(works__work__in=value).distinct() return queryset
def __init__(self, data_url, attrs={}, **kwargs): widget = HeavySelect2MultipleWidget(data_url=data_url, attrs=attrs) super().__init__(widget=widget, **kwargs)
class Meta: if use_select2: widgets = { 'admins': HeavySelect2MultipleWidget(data_view='profile_select2'), 'registrant': HeavySelect2Widget(data_view='profile_select2'), }
class Meta: if use_select2: widgets = { 'organizers': HeavySelect2MultipleWidget(data_view='profile_select2'), 'organizations': HeavySelect2MultipleWidget(data_view='organization_select2') }