···11-# -*- coding: utf-8 -*-
22-33-41import six
52from django.conf import settings
33+from django.core.exceptions import ValidationError
64from django.db import models
75from django.utils.translation import ugettext_lazy as _
8699-from options import STRING, TYPE_CHOICES, INT, FLOAT
77+from options import STRING, TYPE_CHOICES, CONVERTER
88+from options.helpers import convert_value
109from options.managers import OptionManager, UserOptionManager
11101211···3433 def __str__(self):
3534 return "%s" % self.public_name
36353737- def _convert_value(self, value, type):
3838- converter = {INT: int, FLOAT: float, STRING: six.text_type}
3939- default_values = {INT: 0, FLOAT: 1.0, STRING: ""}
4040- try:
4141- option_value = converter.get(self.type, six.text_type)(self.value)
4242- except ValueError:
4343- option_value = default_values.get(self.type)
4444- return option_value
4545-4636 def get_value(self):
4737 """Gets the value with the proper type. If the type is not
4838 valid it would return the default value for the field, to avoid
4939 problems with manual database modifications"""
50405141 if not self.is_list:
5252- return self._convert_value(self.value, self.type)
4242+ return convert_value(self.value, self.type)
5343 else:
5444 values = self.value.split(",")
5555- return [self._convert_value(self.type, item) for item in values]
4545+ return [convert_value(self.type, value) for value in values]
56465747 def clean(self):
5858- from django.core.exceptions import ValidationError
5959-6060- converter = {INT: int, FLOAT: float, STRING: six.text_type}
4848+ """Calls to the converter to check the type conversion. Added exception
4949+ for lists, to check all values."""
6150 try:
6262- converter.get(self.type, six.text_type)(self.value)
5151+ values = [self.value] if not self.is_list else self.value.split(",")
5252+ [CONVERTER.get(self.type, six.text_type)(value) for value in values]
6353 except ValueError:
6454 raise ValidationError(_("Invalid value for this type."))
6555
+1-5
options/settings.py
···11-# -*- coding: utf-8 -*-
22-from __future__ import unicode_literals, print_function, division, absolute_import
33-41from django.conf import settings
55-6273# Set on settings the default options for this project. These will be created
84# on the post migrate signal handler.
···1915DEFAULT_OPTIONS = getattr(settings, "CONFIGURATION_DEFAULT_OPTIONS", {})
20162117# Set the list of options that the user can't customize.
2222-DEFAULT_EXCLUDE_USER_OPTIONS = getattr(settings, "EXCLUDE_USER_OPTIONS", tuple())1818+DEFAULT_EXCLUDE_USER_OPTIONS = getattr(settings, "EXCLUDE_USER_OPTIONS", tuple())