def reset_option(name, value, user=None): """ Removes all definitions of an option and sets its value once again. Use this function in case of OptionDuplicated error, e.g. # get value of a global option and make sure it's not duplicated try: value = get_option(option_name) except OptionDuplicated: reset_option(option_name, default_value) value = default_value """ CustomOption.objects.filter(name=name, user=user).delete() option = CustomOption(name=name, value=value, user=user) option.save() return option
def test_unset_option(self): option = CustomOption(name='opt', value='val') option.save() unset_option('opt') try: option = CustomOption.objects.get(name='opt', value='val') except CustomOption.DoesNotExist: option = None self.assertEqual(option, None) option = CustomOption(name='opt', value='val', user=self.user) option.save() unset_option('opt', self.user) try: option = CustomOption.objects.get(name='opt', value='val', user=self.user) except CustomOption.DoesNotExist: option = None self.assertEqual(option, None)