Beispiel #1
0
import django
from django.db import models
from django.conf import settings
from prices import Price

from . import forms

if django.VERSION < (1, 8):
    BaseField = models.SubfieldBase('BaseField', (models.DecimalField, ), {})
else:
    BaseField = models.DecimalField


class Creator(object):
    """
    A placeholder class that provides a way to set the attribute on the model.
    """
    def __init__(self, field):
        self.field = field

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        return obj.__dict__[self.field.name]

    def __set__(self, obj, value):
        obj.__dict__[self.field.name] = self.field.to_python(value)


class PriceField(BaseField):
Beispiel #2
0
    def get_db_prep_value(self, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_value(value)
        return value

    def value_to_string(self, obj):
        value = self.to_python(self._get_val_from_obj(obj))
        return str(value)

    def run_validators(self, value):
        return super(BaseSemVerField, self).run_validators(str(value))


# Py2 and Py3-compatible metaclass
SemVerField = models.SubfieldBase(
    str('SemVerField'), (BaseSemVerField, models.CharField), {})


class VersionField(SemVerField):
    default_error_messages = {
        'invalid': _("Enter a valid version number in X.Y.Z format."),
    }
    description = _("Version")

    def __init__(self, *args, **kwargs):
        self.partial = kwargs.pop('partial', False)
        self.coerce = kwargs.pop('coerce', False)
        super(VersionField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        """Converts any value to a base.Version field."""
Beispiel #3
0
            super(JSONField, self).validate(value, model_instance)
            try:
                json.loads(value)
            except Exception as err:
                raise ValidationError(str(err))

    def get_prep_value(self, value):
        """Convert value to JSON string before save"""
        try:
            return json.dumps(value)
        except Exception as err:
            raise ValidationError(str(err))

    def value_to_string(self, obj):
        """Return value from object converted to string properly"""
        return smart_text(self.get_prep_value(self._get_val_from_obj(obj)))

    def value_from_object(self, obj):
        """Return value dumped to string."""
        return self.get_prep_value(self._get_val_from_obj(obj))


JSONField = models.SubfieldBase('JSONField', (BaseJSONField, ), {})

try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules(
        [], ["^social\.apps\.django_app\.default\.fields\.JSONField"])
except:
    pass