def store_default_values(self, verbosity): """ Store default values to Settings instance. """ def store_values(settings, available_settings=None, prefix=None): available_settings = available_settings or AVAILABLE_SETTINGS for setting in available_settings: if is_settings_container(setting): store_values(settings, setting, setting.app_name) elif not prefix: setattr(settings, setting.name, setting.default) else: data = settings.data if not prefix in data: data[prefix] = {} data[prefix][setting.name] = setting.default try: settings = Settings.objects.get() except Settings.DoesNotExist: settings = Settings() if verbosity: print >> self.stdout, 'Will create new Settings instance.' else: if verbosity: print >> self.stdout, 'Settings instance already exist.' store_values(settings) settings.save() if verbosity: print >> self.stdout, 'Default values stored well!'
def store_default_values(self, verbosity): """ Store default values to Settings instance. """ try: settings = Settings.objects.get() except Settings.DoesNotExist: settings = Settings() if verbosity: print >> self.stdout, 'Will create new Settings instance.' else: if verbosity: print >> self.stdout, 'Settings instance already exist.' for setting in AVAILABLE_SETTINGS: setattr(settings, setting.name, setting.default) settings.save() if verbosity: print >> self.stdout, 'Default values stored well!'
def test_save(self): settings = Settings() for key, value in TEST_SETTINGS.items(): setattr(settings, key, value) settings.save() settings = Settings.objects.get() old_create_date = settings.create_date old_update_date = settings.update_date for key, value in TEST_SETTINGS.items(): self.assertEqual(getattr(settings, key), value) settings.save() self.assertEqual(settings.create_date, old_create_date) self.assertNotEqual(settings.update_date, old_update_date) settings = Settings.objects.get() self.assertEqual(settings.create_date, old_create_date) self.assertNotEqual(settings.update_date, old_update_date)