def render(self, name, value, attrs=None): # THIS IS A COPY OF django.forms.widgets.MultiWidget.render() # (except for the last line) # value is a list of values, each corresponding to a widget # in self.widgets. site_choices = get_site_choices() page_choices = get_page_choices() self.site_choices = site_choices self.choices = page_choices self.widgets = (Select(choices=site_choices ), Select(choices=[('', '----')]), Select(choices=self.choices, attrs={'style': "display:none;"} ), ) if not isinstance(value, list): value = self.decompress(value) output = [] final_attrs = self.build_attrs(attrs) id_ = final_attrs.get('id', None) for i, widget in enumerate(self.widgets): try: widget_value = value[i] except IndexError: widget_value = None if id_: final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) output.append(widget.render(name + '_%s' % i, widget_value, final_attrs)) output.append(r'''<script type="text/javascript"> (function($) { var handleSiteChange = function(site_name, selected_id) { $("#id_%(name)s_1 optgroup").remove(); var myOptions = $("#id_%(name)s_2 optgroup[label='" + site_name + "']").clone(); $("#id_%(name)s_1").append(myOptions); $("#id_%(name)s_1").change(); }; var handlePageChange = function(page_id) { if (page_id) { $("#id_%(name)s_2 option").attr('selected', false); $("#id_%(name)s_2 option[value=" + page_id + "]").attr('selected', true); } else { $("#id_%(name)s_2 option[value=]").attr('selected', true); }; }; $("#id_%(name)s_0").change(function(){ var site_label = $("#id_%(name)s_0").children(":selected").text(); handleSiteChange( site_label ); }); $("#id_%(name)s_1").change(function(){ var page_id = $(this).find('option:selected').val(); handlePageChange( page_id ); }); $(function(){ handleSiteChange( $("#id_%(name)s_0").children(":selected").text() ); $("#add_id_%(name)s").hide(); }); })(django.jQuery); </script>''' % {'name': name}) return mark_safe(self.format_output(output))
def render(self, name, value, attrs=None): # THIS IS A COPY OF django.forms.widgets.MultiWidget.render() # (except for the last line) # value is a list of values, each corresponding to a widget # in self.widgets. site_choices = get_site_choices() page_choices = get_page_choices() self.site_choices = site_choices self.choices = page_choices self.widgets = ( Select(choices=site_choices), Select(choices=[('', '----')]), Select(choices=self.choices, attrs={'style': "display:none;"}), ) if not isinstance(value, list): value = self.decompress(value) output = [] final_attrs = self.build_attrs(attrs) id_ = final_attrs.get('id', None) for i, widget in enumerate(self.widgets): try: widget_value = value[i] except IndexError: widget_value = None if id_: final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) output.append( widget.render(name + '_%s' % i, widget_value, final_attrs)) output.append(r'''<script type="text/javascript"> (function($) { var handleSiteChange = function(site_name, selected_id) { $("#id_%(name)s_1 optgroup").remove(); var myOptions = $("#id_%(name)s_2 optgroup[label='" + site_name + "']").clone(); $("#id_%(name)s_1").append(myOptions); $("#id_%(name)s_1").change(); }; var handlePageChange = function(page_id) { if (page_id) { $("#id_%(name)s_2 option").attr('selected', false); $("#id_%(name)s_2 option[value=" + page_id + "]").attr('selected', true); } else { $("#id_%(name)s_2 option[value=]").attr('selected', true); }; }; $("#id_%(name)s_0").change(function(){ var site_label = $("#id_%(name)s_0").children(":selected").text(); handleSiteChange( site_label ); }); $("#id_%(name)s_1").change(function(){ var page_id = $(this).find('option:selected').val(); handlePageChange( page_id ); }); $(function(){ handleSiteChange( $("#id_%(name)s_0").children(":selected").text() ); $("#add_id_%(name)s").hide(); }); })(django.jQuery); </script>''' % {'name': name}) return mark_safe(self.format_output(output))
def test_get_page_choices(self): result = get_page_choices() self.assertEqual(result, [('', '----')])
def test_superlazy_iterator_behaves_properly_for_pages(self): normal_result = get_page_choices() lazy_result = SuperLazyIterator(get_page_choices) self.assertEqual(normal_result, list(lazy_result))