Statistics
| Branch: | Tag: | Revision:

root / snf-django-lib / snf_django / lib / db / fields.py @ 0f66865f

History | View | Annotate | Download (3.2 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.core import exceptions
35
from django.db.models import DecimalField, SubfieldBase
36
from django import forms
37
from django.utils.translation import ugettext_lazy as _
38
from south.modelsinspector import add_introspection_rules
39
import decimal
40

    
41
DECIMAL_DIGITS = 38
42

    
43

    
44
class IntDecimalField(DecimalField):
45

    
46
    __metaclass__ = SubfieldBase
47

    
48
    description = _("Integer number as decimal")
49

    
50
    def to_python(self, value):
51
        if value is None:
52
            return value
53
        try:
54
            return long(value)
55
        except (ValueError, TypeError):
56
            raise exceptions.ValidationError(self.error_messages['invalid'])
57

    
58
    def _to_decimal(self, value):
59
        if value is None:
60
            return value
61
        try:
62
            return decimal.Decimal(value)
63
        except decimal.InvalidOperation:
64
            raise exceptions.ValidationError(self.error_messages['invalid'])
65

    
66
    def get_db_prep_save(self, value, connection):
67
        return connection.ops.value_to_db_decimal(
68
            self._to_decimal(value), self.max_digits, self.decimal_places)
69

    
70
    def get_prep_value(self, value):
71
        return self._to_decimal(value)
72

    
73
    def formfield(self, **kwargs):
74
        defaults = {'form_class': forms.IntegerField}
75
        defaults.update(kwargs)
76
        return super(IntDecimalField, self).formfield(**defaults)
77

    
78
add_introspection_rules(
79
    [], ["^snf_django\.lib\.db\.fields\.IntDecimalField"])
80

    
81

    
82
def intDecimalField(verbose_name=None, name=None, **kwargs):
83
    # decimal_places is set here instead of the object constructor
84
    # in order to convince south
85
    return IntDecimalField(verbose_name, name,
86
                           max_digits=DECIMAL_DIGITS, decimal_places=0,
87
                           **kwargs)