root / snf-django-lib / snf_django / lib / db / fields.py @ 0f66865f
History | View | Annotate | Download (3.2 kB)
1 | 0275cd41 | Giorgos Korfiatis | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 0275cd41 | Giorgos Korfiatis | #
|
3 | 0275cd41 | Giorgos Korfiatis | # Redistribution and use in source and binary forms, with or
|
4 | 0275cd41 | Giorgos Korfiatis | # without modification, are permitted provided that the following
|
5 | 0275cd41 | Giorgos Korfiatis | # conditions are met:
|
6 | 0275cd41 | Giorgos Korfiatis | #
|
7 | 0275cd41 | Giorgos Korfiatis | # 1. Redistributions of source code must retain the above
|
8 | 0275cd41 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
9 | 0275cd41 | Giorgos Korfiatis | # disclaimer.
|
10 | 0275cd41 | Giorgos Korfiatis | #
|
11 | 0275cd41 | Giorgos Korfiatis | # 2. Redistributions in binary form must reproduce the above
|
12 | 0275cd41 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
13 | 0275cd41 | Giorgos Korfiatis | # disclaimer in the documentation and/or other materials
|
14 | 0275cd41 | Giorgos Korfiatis | # provided with the distribution.
|
15 | 0275cd41 | Giorgos Korfiatis | #
|
16 | 0275cd41 | Giorgos Korfiatis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 0275cd41 | Giorgos Korfiatis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 0275cd41 | Giorgos Korfiatis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 0275cd41 | Giorgos Korfiatis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 0275cd41 | Giorgos Korfiatis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 0275cd41 | Giorgos Korfiatis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 0275cd41 | Giorgos Korfiatis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 0275cd41 | Giorgos Korfiatis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 0275cd41 | Giorgos Korfiatis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 0275cd41 | Giorgos Korfiatis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 0275cd41 | Giorgos Korfiatis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 0275cd41 | Giorgos Korfiatis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 0275cd41 | Giorgos Korfiatis | #
|
29 | 0275cd41 | Giorgos Korfiatis | # The views and conclusions contained in the software and
|
30 | 0275cd41 | Giorgos Korfiatis | # documentation are those of the authors and should not be
|
31 | 0275cd41 | Giorgos Korfiatis | # interpreted as representing official policies, either expressed
|
32 | 0275cd41 | Giorgos Korfiatis | # or implied, of GRNET S.A.
|
33 | 0275cd41 | Giorgos Korfiatis | |
34 | c11dc0ce | Giorgos Korfiatis | from django.core import exceptions |
35 | c11dc0ce | Giorgos Korfiatis | from django.db.models import DecimalField, SubfieldBase |
36 | 3b3baf88 | Giorgos Korfiatis | from django import forms |
37 | c11dc0ce | Giorgos Korfiatis | from django.utils.translation import ugettext_lazy as _ |
38 | 0275cd41 | Giorgos Korfiatis | from south.modelsinspector import add_introspection_rules |
39 | c11dc0ce | Giorgos Korfiatis | import decimal |
40 | c11dc0ce | Giorgos Korfiatis | |
41 | 0275cd41 | Giorgos Korfiatis | DECIMAL_DIGITS = 38
|
42 | 0275cd41 | Giorgos Korfiatis | |
43 | 0275cd41 | Giorgos Korfiatis | |
44 | c11dc0ce | Giorgos Korfiatis | class IntDecimalField(DecimalField): |
45 | c11dc0ce | Giorgos Korfiatis | |
46 | c11dc0ce | Giorgos Korfiatis | __metaclass__ = SubfieldBase |
47 | c11dc0ce | Giorgos Korfiatis | |
48 | c11dc0ce | Giorgos Korfiatis | description = _("Integer number as decimal")
|
49 | c11dc0ce | Giorgos Korfiatis | |
50 | c11dc0ce | Giorgos Korfiatis | def to_python(self, value): |
51 | c11dc0ce | Giorgos Korfiatis | if value is None: |
52 | c11dc0ce | Giorgos Korfiatis | return value
|
53 | c11dc0ce | Giorgos Korfiatis | try:
|
54 | c11dc0ce | Giorgos Korfiatis | return long(value) |
55 | c11dc0ce | Giorgos Korfiatis | except (ValueError, TypeError): |
56 | c11dc0ce | Giorgos Korfiatis | raise exceptions.ValidationError(self.error_messages['invalid']) |
57 | c11dc0ce | Giorgos Korfiatis | |
58 | c11dc0ce | Giorgos Korfiatis | def _to_decimal(self, value): |
59 | c11dc0ce | Giorgos Korfiatis | if value is None: |
60 | c11dc0ce | Giorgos Korfiatis | return value
|
61 | c11dc0ce | Giorgos Korfiatis | try:
|
62 | c11dc0ce | Giorgos Korfiatis | return decimal.Decimal(value)
|
63 | c11dc0ce | Giorgos Korfiatis | except decimal.InvalidOperation:
|
64 | c11dc0ce | Giorgos Korfiatis | raise exceptions.ValidationError(self.error_messages['invalid']) |
65 | c11dc0ce | Giorgos Korfiatis | |
66 | c11dc0ce | Giorgos Korfiatis | def get_db_prep_save(self, value, connection): |
67 | 0275cd41 | Giorgos Korfiatis | return connection.ops.value_to_db_decimal(
|
68 | 0275cd41 | Giorgos Korfiatis | self._to_decimal(value), self.max_digits, self.decimal_places) |
69 | c11dc0ce | Giorgos Korfiatis | |
70 | c11dc0ce | Giorgos Korfiatis | def get_prep_value(self, value): |
71 | c11dc0ce | Giorgos Korfiatis | return self._to_decimal(value) |
72 | c11dc0ce | Giorgos Korfiatis | |
73 | c11dc0ce | Giorgos Korfiatis | def formfield(self, **kwargs): |
74 | c11dc0ce | Giorgos Korfiatis | defaults = {'form_class': forms.IntegerField}
|
75 | c11dc0ce | Giorgos Korfiatis | defaults.update(kwargs) |
76 | 3b3baf88 | Giorgos Korfiatis | return super(IntDecimalField, self).formfield(**defaults) |
77 | c11dc0ce | Giorgos Korfiatis | |
78 | 0275cd41 | Giorgos Korfiatis | add_introspection_rules( |
79 | b052f360 | Giorgos Korfiatis | [], ["^snf_django\.lib\.db\.fields\.IntDecimalField"])
|
80 | c11dc0ce | Giorgos Korfiatis | |
81 | c11dc0ce | Giorgos Korfiatis | |
82 | 0f0dd7df | Giorgos Korfiatis | def intDecimalField(verbose_name=None, name=None, **kwargs): |
83 | c11dc0ce | Giorgos Korfiatis | # decimal_places is set here instead of the object constructor
|
84 | c11dc0ce | Giorgos Korfiatis | # in order to convince south
|
85 | 0275cd41 | Giorgos Korfiatis | return IntDecimalField(verbose_name, name,
|
86 | 0275cd41 | Giorgos Korfiatis | max_digits=DECIMAL_DIGITS, decimal_places=0,
|
87 | 0275cd41 | Giorgos Korfiatis | **kwargs) |