Exemple #1
0
    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))
Exemple #2
0
 def _build_widgets(self):
     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;"} ),
     )
Exemple #3
0
 def _build_widgets(self):
     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;"}),
                     )
Exemple #4
0
 def test_get_site_choices_without_moderator_with_superuser(self):
     # boilerplate (creating a page)
     user_super = User(username="******", is_staff=True, is_active=True,
         is_superuser=True)
     user_super.set_password("super")
     user_super.save()
     with self.login_user_context(user_super):
         create_page("home", "nav_playground.html", "en", created_by=user_super)
         # The proper test
         result = get_site_choices()
         self.assertEquals(result, [(1, 'example.com')])
Exemple #5
0
 def test_get_site_choices_without_moderator_with_superuser(self):
     # boilerplate (creating a page)
     user_super = User(username="******", is_staff=True, is_active=True,
         is_superuser=True)
     user_super.set_password("super")
     user_super.save()
     with self.login_user_context(user_super):
         create_page("home", "nav_playground.html", "en", created_by=user_super)
         # The proper test
         result = get_site_choices()
         self.assertEquals(result, [(1, 'example.com')])
Exemple #6
0
 def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
              required=True, widget=None, to_field_name=None, *args, **kwargs):
     errors = self.default_error_messages.copy()
     if 'error_messages' in kwargs:
         errors.update(kwargs['error_messages'])
     site_choices, page_choices = get_site_choices(), get_page_choices()
     kwargs['required']=required
     fields = (
         forms.ChoiceField(choices=site_choices, required=False, error_messages={'invalid': errors['invalid_site']}),
         forms.ChoiceField(choices=page_choices, required=False, error_messages={'invalid': errors['invalid_page']}),
     )
     super(PageSelectFormField, self).__init__(fields, *args, **kwargs)
Exemple #7
0
 def test_04_get_site_choices_without_moderator(self):
     with SettingsOverride(CMS_MODERATOR=False):
         # boilerplate (creating a page)
         user_super = User(username="******", is_staff=True, is_active=True, 
             is_superuser=True)
         user_super.set_password("super")
         user_super.save()
         self.login_user(user_super)
         self.create_page(title="home", user=user_super)
         # The proper test
         result = get_site_choices()
         self.assertEquals(result, [(1,'example.com')])
Exemple #8
0
 def __init__(self, site_choices=None, page_choices=None, attrs=None):
     if attrs is not None:
         self.attrs = attrs.copy()
     else:
         self.attrs = {}
     if site_choices is None or page_choices is None:
         site_choices, page_choices = get_site_choices(), get_page_choices()
     self.site_choices = site_choices
     self.choices = page_choices
     widgets = (Select(choices=site_choices ),
                Select(choices=[('', '----')]),
                Select(choices=self.choices, attrs={'style': "display:none;"} ),
     )
     super(PageSelectWidget, self).__init__(widgets, attrs)
Exemple #9
0
 def test_04_get_site_choices_without_moderator(self):
     with SettingsOverride(CMS_MODERATOR=False):
         # boilerplate (creating a page)
         user_super = User(username="******",
                           is_staff=True,
                           is_active=True,
                           is_superuser=True)
         user_super.set_password("super")
         user_super.save()
         self.login_user(user_super)
         self.create_page(title="home", user=user_super)
         # The proper test
         result = get_site_choices()
         self.assertEquals(result, [(1, 'example.com')])
Exemple #10
0
 def __init__(self, site_choices=None, page_choices=None, attrs=None):
     if attrs is not None:
         self.attrs = attrs.copy()
     else:
         self.attrs = {}
     if site_choices is None or page_choices is None:
         site_choices, page_choices = get_site_choices(), get_page_choices()
     self.site_choices = site_choices
     self.choices = page_choices
     widgets = (Select(choices=site_choices ),
                Select(choices=[('', '----')]),
                Select(choices=self.choices, attrs={'style': "display:none;"} ),
     )
     super(PageSelectWidget, self).__init__(widgets, attrs)
Exemple #11
0
    def test_get_site_choices_without_moderator_with_superuser(self):
        # boilerplate (creating a page)
        User = get_user_model()

        fields = dict(is_staff=True, is_active=True, is_superuser=True, email="*****@*****.**")

        if User.USERNAME_FIELD != 'email':
            fields[User.USERNAME_FIELD] = "super"

        user_super = User(**fields)
        user_super.set_password(getattr(user_super, User.USERNAME_FIELD))
        user_super.save()
        with self.login_user_context(user_super):
            create_page("home", "nav_playground.html", "en", created_by=user_super)
            # The proper test
            result = get_site_choices()
            self.assertEqual(result, [(1, 'example.com')])
Exemple #12
0
    def test_get_site_choices_without_moderator_with_superuser(self):
        # boilerplate (creating a page)
        User = get_user_model()

        fields = dict(is_staff=True, is_active=True, is_superuser=True, email="*****@*****.**")

        if User.USERNAME_FIELD != 'email':
            fields[User.USERNAME_FIELD] = "super"

        user_super = User(**fields)
        user_super.set_password(getattr(user_super, User.USERNAME_FIELD))
        user_super.save()
        with self.login_user_context(user_super):
            create_page("home", "nav_playground.html", "en", created_by=user_super)
            # The proper test
            result = get_site_choices()
            self.assertEqual(result, [(1, 'example.com')])
Exemple #13
0
    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">
            var CMS = window.CMS || {};

            CMS.Widgets = CMS.Widgets || {};
            CMS.Widgets._pageSelectWidgets = CMS.Widgets._pageSelectWidgets || [];
            CMS.Widgets._pageSelectWidgets.push({
                name: '%(name)s'
            });
        </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">
            var CMS = window.CMS || {};

            CMS.Widgets = CMS.Widgets || {};
            CMS.Widgets._pageSelectWidgets = CMS.Widgets._pageSelectWidgets || [];
            CMS.Widgets._pageSelectWidgets.push({
                name: '%(name)s'
            });
        </script>''' % {'name': name})
        return mark_safe(self.format_output(output))
Exemple #15
0
 def test_get_site_choices_without_moderator(self):
     with SettingsOverride(CMS_MODERATOR=False):
         result = get_site_choices()
         self.assertEquals(result, [])
Exemple #16
0
 def test_get_site_choices(self):
     result = get_site_choices()
     self.assertEquals(result, [])
Exemple #17
0
 def test_superlazy_iterator_behaves_properly_for_sites(self):
     normal_result = get_site_choices()
     lazy_result = SuperLazyIterator(get_site_choices)
     
     self.assertEquals(normal_result, list(lazy_result))
Exemple #18
0
 def test_get_site_choices_without_moderator(self):
     result = get_site_choices()
     self.assertEquals(result, [])
Exemple #19
0
 def test_get_site_choices(self):
     result = get_site_choices()
     self.assertEquals(result, [])
Exemple #20
0
    def test_superlazy_iterator_behaves_properly_for_sites(self):
        normal_result = get_site_choices()
        lazy_result = SuperLazyIterator(get_site_choices)

        self.assertEquals(normal_result, list(lazy_result))
Exemple #21
0
 def test_get_site_choices_without_moderator(self):
     result = get_site_choices()
     self.assertEqual(result, [])
Exemple #22
0
 def test_get_site_choices_without_moderator(self):
     with SettingsOverride(CMS_MODERATOR=False):
         result = get_site_choices()
         self.assertEquals(result, [])