class SecretRoleInfoForm(SelectRoleForm): conference = ModelMultipleChoiceField( queryset=Conference.objects.all().order_by('conference_name'), widget=MultipleHiddenInput(), required=True) roles = MultipleChoiceField(widget=MultipleHiddenInput(), required=True, choices=role_options)
class SecretBidderInfoForm(SelectBidderForm): conference = ModelMultipleChoiceField( queryset=Conference.objects.all().order_by('conference_name'), widget=MultipleHiddenInput(), required=True) bid_type = MultipleChoiceField(widget=MultipleHiddenInput(), required=True) state = MultipleChoiceField(choices=((('Draft', 'Draft'), ) + acceptance_states), widget=MultipleHiddenInput(), required=True)
class SelectEventForm(Form): required_css_class = 'required' error_css_class = 'error' events = ModelMultipleChoiceField(queryset=Event.objects.all(), widget=MultipleHiddenInput(), required=False) staff_areas = ModelMultipleChoiceField(queryset=StaffArea.objects.all(), widget=MultipleHiddenInput(), required=False) event_collections = MultipleChoiceField(required=False, widget=MultipleHiddenInput(), choices=event_collect_choices)
class HiddenSelectEventForm(Form): required_css_class = 'required' error_css_class = 'error' day = MultipleChoiceField( widget=MultipleHiddenInput(), required=False) calendar_type = MultipleChoiceField( choices=list(calendar_type_options.items()), widget=MultipleHiddenInput(), required=False) staff_area = ModelMultipleChoiceField( queryset=StaffArea.objects.all().order_by("conference", "slug"), widget=MultipleHiddenInput(), required=False)
class HiddenSelectEventForm(Form): required_css_class = 'required' error_css_class = 'error' day = MultipleChoiceField(widget=MultipleHiddenInput(), required=False) calendar_type = MultipleChoiceField(choices=calendar_type_options.items(), widget=MultipleHiddenInput(), required=False) staff_area = ModelMultipleChoiceField( queryset=StaffArea.objects.all().order_by("conference", "slug"), widget=MultipleHiddenInput(), required=False) volunteer_type = ModelMultipleChoiceField( queryset=AvailableInterest.objects.filter(visible=True), widget=MultipleHiddenInput(), required=False)
def test_render_attrs_constructor(self): widget = MultipleHiddenInput(attrs={"class": "fun"}) self.check_html(widget, "email", [], "") self.check_html( widget, "email", ["*****@*****.**"], html=( '<input type="hidden" class="fun" value="*****@*****.**" name="email">' ), ) self.check_html( widget, "email", ["*****@*****.**", "*****@*****.**"], html=( '<input type="hidden" class="fun" value="*****@*****.**" ' 'name="email">\n' '<input type="hidden" class="fun" value="*****@*****.**" ' 'name="email">' ), ) self.check_html( widget, "email", ["*****@*****.**"], attrs={"class": "special"}, html=( '<input type="hidden" class="special" value="*****@*****.**" ' 'name="email">' ), )
class Meta: model = Course fields = [ 'id', 'course_code', 'name', 'materials', 'teachers', 'updated_associations', 'updated_teacher', 'calendar_year', 'coordinator', 'period', *(['study_year'] if course_id is None else []), ] widgets = { 'id': HiddenInput(), "materials": AddAnotherWidgetWrapper( ModelSelect2MultipleWidget( queryset=MSP.objects.all(), search_fields=[ "mspline__materials__name__icontains", "mspline__materials__book__ISBN__icontains", "mspline__materials__book__author__icontains", "mspline__materials__book__year_of_publishing__icontains", "mspline__materials__scientificarticle__DOI__icontains", "mspline__materials__scientificarticle__author__icontains", "mspline__materials__scientificarticle__year_of_publishing__icontains", ]), reverse_lazy('boecie:msp.create', kwargs={'course': course_id})) if course_id else MultipleHiddenInput(), 'teachers': AddAnotherWidgetWrapper( ModelSelect2MultipleWidget(model=Teacher, search_fields=[ 'titles__icontains', 'initials__icontains', 'first_name__icontains', 'surname_prefix__icontains', 'last_name__icontains', 'email__icontains' ]), reverse_lazy('boecie:teacher.create')), 'coordinator': AddAnotherWidgetWrapper( ModelSelect2Widget(model=Teacher, search_fields=[ 'titles__icontains', 'initials__icontains', 'first_name__icontains', 'surname_prefix__icontains', 'last_name__icontains', 'email__icontains' ]), reverse_lazy('boecie:teacher.create')), }
def test_render_attrs_constructor(self): widget = MultipleHiddenInput(attrs={'class': 'fun'}) self.check_html(widget, 'email', [], '') self.check_html( widget, 'email', ['*****@*****.**'], html= '<input type="hidden" class="fun" value="*****@*****.**" name="email">', ) self.check_html( widget, 'email', ['*****@*****.**', '*****@*****.**'], html= ('<input type="hidden" class="fun" value="*****@*****.**" name="email">\n' '<input type="hidden" class="fun" value="*****@*****.**" name="email">' ), ) self.check_html( widget, 'email', ['*****@*****.**'], attrs={'class': 'special'}, html= '<input type="hidden" class="special" value="*****@*****.**" name="email">', )
def valid_layout(self): self.fields["method"].widget = MultipleHiddenInput() self.helper.layout = Layout( "method", HTML.h2("You answered..."), HTML.table(None, [("Methods selected:", self.get_method())]), Button("continue", "Continue"), )
class Meta: model = MSPLine fields = [ "type", "msp", "comment", "materials", ] widgets = { "msp": HiddenInput(), "comment": HiddenInput(), "materials": MultipleHiddenInput(), "type": HiddenInput(), }
class FineFileAdmin(admin.ModelAdmin): formfield_overrides = { models.FileField: { 'widget': MultipleHiddenInput(attrs={'admin': True, 'itemLimit': 1}) }, } class Media: js = ( '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', ) def fineuploader_setting(self, request): post_info = request.POST.dict() if type(self.formfield_overrides.get(FileField).get('widget')) is MultipleHiddenInput: model_fields = self.model._meta.fields file_fields_name = [] for field in model_fields: if field.get_internal_type() is 'FileField': file_fields_name.append(field.name) if request.method == 'POST': print(file_fields_name) for name in file_fields_name: file_uploader = cache.get(request.POST.get(name)) if file_uploader is None: return # raise forms.ValidationError("There is no file in the field.") file_path = file_uploader.storage.path(file_uploader.real_path) post_info[name] = SimpleUploadedFile( file_uploader.filename, open(file_path, 'rb').read() ) request.POST = post_info folder_path = file_uploader.storage.path(file_uploader.file_path) try: shutil.rmtree(folder_path) except OSError: pass # return post_info def add_view(self, request, form_url='', extra_context=None): self.fineuploader_setting(request) return super(type(self), self).add_view(request, form_url, extra_context) def change_view(self, request, object_id, form_url='', extra_context=None): self.fineuploader_setting(request) return super(type(self), self).change_view(request, object_id, form_url, extra_context)
class Meta: model = PositionDetail fields = [ 'position', 'sources', 'chapter', 'verse_start', 'verse_end', 'usage', 'contents', ] widgets = { 'chapter': NumberInput(attrs={'style': 'width: 50px'}), 'verse_start': NumberInput(attrs={'style': 'width: 50px'}), 'verse_end': NumberInput(attrs={'style': 'width: 50px'}), 'usage': HiddenInput(), 'sources': MultipleHiddenInput(), }
class RemoveUserFromGroupForm(ModelForm): user_set = forms.ModelMultipleChoiceField(queryset=MrMapUser.objects.all(), label=_('Members'), widget=MultipleHiddenInput()) class Meta: model = MrMapGroup fields = () def __init__(self, user, *args, **kwargs): super(RemoveUserFromGroupForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance') if instance: self.fields['user_set'].initial = instance.user_set.exclude(username=user.username) else: raise ImproperlyConfigured("RemoveUserFromGroupForm without instance kw isn't possible") def save(self, commit=True): self.instance.user_set.clear() self.instance.user_set.add(*self.cleaned_data['user_set']) return self.instance
class MultipleHiddenInputTest(WidgetTest): widget = MultipleHiddenInput() def test_render_single(self): self.check_html( self.widget, 'email', ['*****@*****.**'], html='<input type="hidden" name="email" value="*****@*****.**" />', ) def test_render_multiple(self): self.check_html( self.widget, 'email', ['*****@*****.**', '*****@*****.**'], html=( '<input type="hidden" name="email" value="*****@*****.**" />\n' '<input type="hidden" name="email" value="*****@*****.**" />' ), ) def test_render_attrs(self): self.check_html( self.widget, 'email', ['*****@*****.**'], attrs={'class': 'fun'}, html='<input type="hidden" name="email" value="*****@*****.**" class="fun" />', ) def test_render_attrs_multiple(self): self.check_html( self.widget, 'email', ['*****@*****.**', '*****@*****.**'], attrs={'class': 'fun'}, html=( '<input type="hidden" name="email" value="*****@*****.**" class="fun" />\n' '<input type="hidden" name="email" value="*****@*****.**" class="fun" />' ), ) def test_render_attrs_constructor(self): widget = MultipleHiddenInput(attrs={'class': 'fun'}) self.check_html(widget, 'email', [], '') self.check_html( widget, 'email', ['*****@*****.**'], html='<input type="hidden" class="fun" value="*****@*****.**" name="email" />', ) self.check_html( widget, 'email', ['*****@*****.**', '*****@*****.**'], html=( '<input type="hidden" class="fun" value="*****@*****.**" name="email" />\n' '<input type="hidden" class="fun" value="*****@*****.**" name="email" />' ), ) self.check_html( widget, 'email', ['*****@*****.**'], attrs={'class': 'special'}, html='<input type="hidden" class="special" value="*****@*****.**" name="email" />', ) def test_render_empty(self): self.check_html(self.widget, 'email', [], '') def test_render_none(self): self.check_html(self.widget, 'email', None, '') def test_render_increment_id(self): """ Each input should get a separate ID. """ self.check_html( self.widget, 'letters', ['a', 'b', 'c'], attrs={'id': 'hideme'}, html=( '<input type="hidden" name="letters" value="a" id="hideme_0" />\n' '<input type="hidden" name="letters" value="b" id="hideme_1" />\n' '<input type="hidden" name="letters" value="c" id="hideme_2" />' ), )
class Meta: model = Set exclude = ('order', 'exerciseday') widgets = { 'exercises': MultipleHiddenInput(), }
class _Form(BulkRenameForm): pk = ModelMultipleChoiceField( queryset=self.queryset, widget=MultipleHiddenInput() )
def render_hidden(name, value): """ render as hidden widget """ if isinstance(value, list): return MultipleHiddenInput().render(name, value) return HiddenInput().render(name, value)
class MultipleHiddenInputTest(WidgetTest): widget = MultipleHiddenInput() def test_render_single(self): self.check_html( self.widget, "email", ["*****@*****.**"], html='<input type="hidden" name="email" value="*****@*****.**">', ) def test_render_multiple(self): self.check_html( self.widget, "email", ["*****@*****.**", "*****@*****.**"], html=( '<input type="hidden" name="email" value="*****@*****.**">\n' '<input type="hidden" name="email" value="*****@*****.**">' ), ) def test_render_attrs(self): self.check_html( self.widget, "email", ["*****@*****.**"], attrs={"class": "fun"}, html=( '<input type="hidden" name="email" value="*****@*****.**" ' 'class="fun">' ), ) def test_render_attrs_multiple(self): self.check_html( self.widget, "email", ["*****@*****.**", "*****@*****.**"], attrs={"class": "fun"}, html=( '<input type="hidden" name="email" value="*****@*****.**" ' 'class="fun">\n' '<input type="hidden" name="email" value="*****@*****.**" class="fun">' ), ) def test_render_attrs_constructor(self): widget = MultipleHiddenInput(attrs={"class": "fun"}) self.check_html(widget, "email", [], "") self.check_html( widget, "email", ["*****@*****.**"], html=( '<input type="hidden" class="fun" value="*****@*****.**" name="email">' ), ) self.check_html( widget, "email", ["*****@*****.**", "*****@*****.**"], html=( '<input type="hidden" class="fun" value="*****@*****.**" ' 'name="email">\n' '<input type="hidden" class="fun" value="*****@*****.**" ' 'name="email">' ), ) self.check_html( widget, "email", ["*****@*****.**"], attrs={"class": "special"}, html=( '<input type="hidden" class="special" value="*****@*****.**" ' 'name="email">' ), ) def test_render_empty(self): self.check_html(self.widget, "email", [], "") def test_render_none(self): self.check_html(self.widget, "email", None, "") def test_render_increment_id(self): """ Each input should get a separate ID. """ self.check_html( self.widget, "letters", ["a", "b", "c"], attrs={"id": "hideme"}, html=( '<input type="hidden" name="letters" value="a" id="hideme_0">\n' '<input type="hidden" name="letters" value="b" id="hideme_1">\n' '<input type="hidden" name="letters" value="c" id="hideme_2">' ), ) def test_fieldset(self): class TestForm(Form): template_name = "forms_tests/use_fieldset.html" composers = MultipleChoiceField( choices=[("J", "John Lennon"), ("P", "Paul McCartney")], widget=MultipleHiddenInput, ) form = TestForm(MultiValueDict({"composers": ["J", "P"]})) self.assertIs(self.widget.use_fieldset, False) self.assertHTMLEqual( '<input type="hidden" name="composers" value="J" id="id_composers_0">' '<input type="hidden" name="composers" value="P" id="id_composers_1">', form.render(), )