def test_validators(self): settings.max_processes = 24 self.assertFalse(settings.is_valid()) self.assertRaises(ValueError, settings.save) settings.max_processes = 4 settings.hosts_file = 'something' self.assertFalse(settings.is_valid()) self.assertRaises(ValueError, settings.save) settings.max_processes = 4 settings.hosts_file = '/etc/hosts.new' settings.hourly_rate = Decimal('0.5') self.assertFalse(settings.is_valid()) self.assertRaises(ValueError, settings.save) settings.hourly_rate = Decimal('12.725') self.assertFalse(settings.is_valid()) self.assertRaises(ValueError, settings.save) settings.hourly_rate = Decimal('1200.50') self.assertFalse(settings.is_valid()) self.assertRaises(ValueError, settings.save) settings.hourly_rate = Decimal('10.00') self.assertTrue(settings.is_valid()) settings.save()
def main(available_settings=None): available_settings = available_settings or settings.available_settings app_name = available_settings.app_name if not app_name: print('Found %d available setting(s)' % len(available_settings)) for mixed in available_settings: if is_settings_container(mixed): main(mixed) else: setting = mixed values = getattr(settings, app_name) if app_name else settings old_value = getattr(values, setting.name) print('\n%s (%s)' % (setting.label, setting.help_text)) new_value = raw_input('Enter new value, old value is %r: ' % \ old_value) if not new_value: new_value = old_value setattr(values, setting.name, new_value) if not available_settings.app_name: if settings.is_valid(): settings.save() print('\nSettings were saved! Exit...') else: print('\nCannot save settings, error:\n%s' % settings.error)
def test_workflow(self): settings.max_processes = 4 settings.hosts_file = '/etc/hosts.new' settings.hourly_rate = Decimal(10) settings.testapp.debug = True settings.save() self.assertTrue(os.path.isfile(self.filename)) handler = open(self.filename, 'r') content = handler.read() handler.close() if self.format == 'ini': self.assertIn('max_processes = 4', content) self.assertIn('hosts_file = /etc/hosts.new', content) self.assertIn('hourly_rate = 10', content) self.assertIn('[testapp]', content) self.assertIn('debug = True', content) elif self.format == 'json': self.assertIn('"max_processes": 4', content) self.assertIn('"hosts_file": "/etc/hosts.new"', content) self.assertIn('"hourly_rate": "10"', content) self.assertIn('"testapp": {"debug": true}', content) # Re-check backend data self.assertEqual(settings.max_processes, 4) self.assertEqual(settings.hosts_file, '/etc/hosts.new') self.assertEqual(settings.hourly_rate, Decimal(10)) self.assertTrue(settings.testapp.debug)
def revert(request): """ Revert settings to default values. This view uses same permission rules as "Edit Settings" view. """ redirect_to = request.GET.get('next') if not redirect_to: redirect_to = reverse('setman_edit') if not auth_permitted(request.user): return render(request, 'setman/edit.html', {'auth_forbidden': True}, status=403) settings.revert() settings.save() messages.success( request, _('Settings have been reverted to default values.') ) return redirect('%s?%d' % (redirect_to, randint(1000, 9999)))
def test_convert_value(self): settings.max_processes = '16' settings.save() self.assertEqual(settings.max_processes, 16) settings.max_processes = 'abc' self.assertFalse(settings.is_valid())
def save(self): """ Save all settings to the database. """ cd = self.cleaned_data for key, value in cd.items(): setattr(settings, key, value) settings.save()
def test_app_settings(self): self.assertRaises(SettingDoesNotExist, getattr, settings, 'auth') self.assertTrue(hasattr(settings, 'testapp')) self.assertTrue(is_settings_container(settings.testapp)) self.assertEqual(settings.testapp.app_setting, None) self.assertEqual(settings.testapp.setting_to_redefine, 0) settings.testapp.app_setting = 'string' settings.testapp.setting_to_redefine = 60 settings.save()
def test_restore(self): settings.max_processes = 4 settings.hosts_file = '/etc/hosts.new' settings.hourly_rate = Decimal(10) settings.testapp.debug = True settings.save() settings.revert() self.assertEqual(settings.max_processes, 2) self.assertEqual(settings.hosts_file, '/etc/hosts') self.assertEqual(settings.hourly_rate, Decimal(15)) self.assertFalse(settings.testapp.debug)
def save(self): """ Save all settings to the database. """ cd = self.cleaned_data for key, value in cd.items(): if '.' in key: app_name, key = key.split('.', 1) setattr(getattr(settings, app_name), key, value) else: setattr(settings, key, value) settings.save()
def test_save_settings(self): instance = Settings.objects.create(data=TEST_SETTINGS) settings.BOOLEAN_SETTING = True settings.save() self.assertTrue(settings.BOOLEAN_SETTING) instance = Settings.objects.get() self.assertTrue(instance.BOOLEAN_SETTING) instance.BOOLEAN_SETTING = False instance.save() self.assertFalse(instance.BOOLEAN_SETTING)
def save_form_fields(self, data): """ """ from setman import settings sep = self.field_name_separator for key, value in data.items(): if sep in key: app_name, key = key.split(sep, 1) setattr(getattr(settings, app_name), key, value) else: setattr(settings, key, value) settings.save()
def test_app_settings(self): self.assertRaises(AttributeError, getattr, settings, 'auth') self.assertTrue(hasattr(settings, 'core')) self.assertTrue(is_settings_container(settings.core)) self.assertEqual(settings.core.app_setting, None) self.assertEqual(settings.core.setting_to_redefine, 0) settings.core.app_setting = 'string' settings.core.setting_to_redefine = 60 settings.save() settings._clear() cache.clear() self.assertEqual(settings.core.app_setting, 'string') self.assertEqual(settings.core.setting_to_redefine, 60)
def revert(request): """ Revert settings to default values. This view uses same permission rules as "Edit Settings" view. """ redirect_to = request.GET.get('next') if not redirect_to: redirect_to = reverse('setman_edit') if not auth_permitted(request.user): return render(request, 'setman/edit.html', {'auth_forbidden': True}, status=403) settings.revert() settings.save() messages.success(request, _('Settings have been reverted to default values.')) return redirect('%s?%d' % (redirect_to, randint(1000, 9999)))