Пример #1
0
    def test_signals(self):
        value = 'signal fired'

        handler = MagicMock()
        setting = loading.get_setting(MODULE_NAME, 'Unpopulated', 'string')
        signals.setting_changed.connect(handler, sender=setting)
        Unpopulated.settings.string = value
        handler.assert_called_once_with(signal=signals.setting_changed,
                                        sender=setting,
                                        value=value)

        handler = MagicMock()
        setting = loading.get_setting(MODULE_NAME, 'Populated', 'string')
        signals.setting_changed.connect(handler, sender=setting)
        Populated.settings.string = value
        handler.assert_called_once_with(signal=signals.setting_changed,
                                        sender=setting,
                                        value=value)

        handler = MagicMock()
        setting = loading.get_setting(MODULE_NAME, 'Combined', 'string')
        signals.setting_changed.connect(handler, sender=setting)
        Combined.settings.string = value
        handler.assert_called_once_with(signal=signals.setting_changed,
                                        sender=setting,
                                        value=value)

        handler = MagicMock()
        setting = loading.get_setting(MODULE_NAME, 'Unpopulated', 'integer')
        signals.setting_changed.connect(handler, sender=setting)
        Unpopulated.settings.integer = 43
        handler.assert_called_once_with(signal=signals.setting_changed,
                                        sender=setting,
                                        value=43)

        # If value has not changed, then the signal is not sent.
        new_handler = MagicMock()
        signals.setting_changed.connect(new_handler, sender=setting)
        Unpopulated.settings.integer = 43
        new_handler.assert_not_called()
Пример #2
0
def app_settings(request, app_label, template='dbsettings/app_settings.html'):
    # Determine what set of settings this editor is used for
    if app_label is None:
        settings = loading.get_all_settings()
        title = _('Site settings')
    else:
        settings = loading.get_app_settings(app_label)
        title = _('%(app)s settings') % {'app': capfirst(app_label)}

    # Create an editor customized for the current user
    editor = forms.customized_editor(request.user, settings)

    if request.method == 'POST':
        # Populate the form with user-submitted data
        form = editor(request.POST.copy(), request.FILES)
        if form.is_valid():
            form.full_clean()

            for name, value in form.cleaned_data.items():
                key = forms.RE_FIELD_NAME.match(name).groups()
                setting = loading.get_setting(*key)
                try:
                    storage = loading.get_setting_storage(*key)
                    current_value = setting.to_python(storage.value)
                except:
                    current_value = None

                if current_value != setting.to_python(value):
                    args = key + (value, )
                    loading.set_setting_value(*args)

                    # Give user feedback as to which settings were changed
                    if setting.class_name:
                        location = setting.class_name
                    else:
                        location = setting.module_name
                    update_msg = (_('Updated %(desc)s on %(location)s') % {
                        'desc': str(setting.description),
                        'location': location
                    })
                    messages.add_message(request, messages.INFO, update_msg)

            return HttpResponseRedirect(request.path)
    else:
        # Leave the form populated with current setting values
        form = editor()

    return render(request, template, {
        'title': title,
        'no_settings': len(settings) == 0,
        'form': form,
    })
Пример #3
0
def app_settings(request, app_label, template='dbsettings/app_settings.html'):
    # Determine what set of settings this editor is used for
    if app_label is None:
        settings = loading.get_all_settings()
        title = _('Site settings')
    else:
        settings = loading.get_app_settings(app_label)
        title = _('%(app)s settings') % {'app': capfirst(app_label)}

    # Create an editor customized for the current user
    editor = forms.customized_editor(request.user, settings)

    if request.method == 'POST':
        # Populate the form with user-submitted data
        form = editor(request.POST.copy(), request.FILES)
        if form.is_valid():
            form.full_clean()

            for name, value in form.cleaned_data.items():
                key = forms.RE_FIELD_NAME.match(name).groups()
                setting = loading.get_setting(*key)
                try:
                    storage = loading.get_setting_storage(*key)
                    current_value = setting.to_python(storage.value)
                except:
                    current_value = None

                if current_value != setting.to_python(value):
                    args = key + (value,)
                    loading.set_setting_value(*args)

                    # Give user feedback as to which settings were changed
                    if setting.class_name:
                        location = setting.class_name
                    else:
                        location = setting.module_name
                    update_msg = (_('Updated %(desc)s on %(location)s') %
                                  {'desc': six.text_type(setting.description),
                                   'location': location})
                    messages.add_message(request, messages.INFO, update_msg)

            # Problem with settings not updated in form fields on save causing user errors. Sending to admin dash instead
            return HttpResponseRedirect(reverse_lazy('admin:index'))
    else:
        # Leave the form populated with current setting values
        form = editor()

    return render_to_response(template, {
        'title': title,
        'form': form,
    }, context_instance=RequestContext(request))
Пример #4
0
def app_settings(request, app_label, template='dbsettings/app_settings.html'):
    # Determine what set of settings this editor is used for
    if app_label is None:
        settings = loading.get_all_settings()
        title = 'Site settings'
    else:
        settings = loading.get_app_settings(app_label)
        title = '%s settings' % capfirst(app_label)

    # Create an editor customized for the current user
    editor = forms.customized_editor(request.user, settings)

    if request.method == 'POST':
        # Populate the form with user-submitted data
        form = editor(request.POST.copy(), request.FILES)
        if form.is_valid():
            form.full_clean()

            for name, value in form.cleaned_data.items():
                key = forms.re_field_name.match(name).groups()
                setting = loading.get_setting(*key)
                try:
                    storage = loading.get_setting_storage(*key)
                    current_value = setting.to_python(storage.value)
                except:
                    current_value = None

                if current_value != setting.to_python(value):
                    args = key + (value, )
                    loading.set_setting_value(*args)

                    # Give user feedback as to which settings were changed
                    if setting.class_name:
                        location = setting.class_name
                    else:
                        location = setting.module_name
                    update_msg = u'Updated %s on %s' % (unicode(
                        setting.description), location)
                    messages.add_message(request, messages.INFO, update_msg)

            return HttpResponseRedirect(request.path)
    else:
        # Leave the form populated with current setting values
        form = editor()

    return render_to_response(template, {
        'title': title,
        'form': form,
    },
                              context_instance=RequestContext(request))
Пример #5
0
def app_settings(request, app_label, template='dbsettings/app_settings.html'):
    # Determine what set of settings this editor is used for
    if app_label is None:
        settings = loading.get_all_settings()
        title = _('Site settings')
    else:
        settings = loading.get_app_settings(app_label)
        title = _('%(app)s settings') % {'app': capfirst(app_label)}

    # Create an editor customized for the current user
    editor = forms.customized_editor(request.user, settings)

    if request.method == 'POST':
        # Populate the form with user-submitted data
        form = editor(request.POST.copy(), request.FILES)
        if form.is_valid():
            form.full_clean()

            n_changed = 0
            for name, value in form.cleaned_data.items():
                key = forms.RE_FIELD_NAME.match(name).groups()
                setting = loading.get_setting(*key)
                try:
                    storage = loading.get_setting_storage(*key)
                    current_value = setting.to_python(storage.value)
                except Exception, e:
                    log.info("An exception occurred trying to convert %s, "
                        "with value %s, to python.", name, value, exc_info=True)
                    current_value = None

                if current_value != setting.to_python(value):
                    args = key + (value,)
                    loading.set_setting_value(*args)

                    # Give user feedback as to which settings were changed
                    if setting.class_name:
                        location = setting.class_name
                    else:
                        location = setting.module_name
                    update_msg = (_(u'Updated %(desc)s on %(location)s') %
                                  {'desc': unicode(setting.description), 'location': location})
                    messages.add_message(request, messages.INFO, update_msg)
                    n_changed += 1

            if not n_changed:
                no_update_msg = (_(u'Nothing was changed'))
                messages.add_message(request, messages.INFO, no_update_msg)

            return HttpResponseRedirect(request.path)
Пример #6
0
def app_settings(request, app_label, template='dbsettings/app_settings.html'):
    # Determine what set of settings this editor is used for
    if app_label is None:
        settings = loading.get_all_settings()
        title = 'Site settings'
    else:
        settings = loading.get_app_settings(app_label)
        title = '%s settings' % capfirst(app_label)

    # Create an editor customized for the current user
    editor = forms.customized_editor(request.user, settings)

    if request.method == 'POST':
        # Populate the form with user-submitted data
        form = editor(request.POST.copy(), request.FILES)
        if form.is_valid():
            form.full_clean()

            for name, value in form.cleaned_data.items():
                key = forms.re_field_name.match(name).groups()
                setting = loading.get_setting(*key)
                try:
                    storage = loading.get_setting_storage(*key)
                    current_value = setting.to_python(storage.value)
                except:
                    current_value = None

                if current_value != setting.to_python(value):
                    args = key + (value,)
                    loading.set_setting_value(*args)

                    # Give user feedback as to which settings were changed
                    if setting.class_name:
                        location = setting.class_name
                    else:
                        location = setting.module_name
                    update_msg = u'Updated %s on %s' % (unicode(setting.description), location)
                    messages.add_message(request, messages.INFO, update_msg )

            return HttpResponseRedirect(request.path)
    else:
        # Leave the form populated with current setting values
        form = editor()

    return render_to_response(template, {
        'title': title,
        'form': form,
    }, context_instance=RequestContext(request))
Пример #7
0
 def assertCorrectSetting(self, value_class, *key):
     from dbsettings import loading
     setting = loading.get_setting(*key)
     self.assertEqual(
         key, setting.key)  # Check if setting is registered with proper key
     self.assertTrue(isinstance(setting, value_class))
Пример #8
0
 def assertCorrectSetting(self, value_class, *key):
     from dbsettings import loading
     setting = loading.get_setting(*key)
     self.assertEqual(key, setting.key)  # Check if setting is registered with proper key
     self.assertTrue(isinstance(setting, value_class))