Skip to content

Commit

Permalink
Merge 15cde98 into 41d4288
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav committed Oct 12, 2017
2 parents 41d4288 + 15cde98 commit 3942f85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions openregistry/api/models/ocds.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from uuid import uuid4

from schematics.types import (StringType, FloatType, URLType, IntType,
BooleanType, BaseType, EmailType, MD5Type)
from schematics.exceptions import ValidationError
BooleanType, BaseType, EmailType, MD5Type,
DecimalType as BaseDecimalType)
from schematics.exceptions import ValidationError, ConversionError
from schematics.types.compound import ModelType, ListType
from schematics.types.serializable import serializable

from decimal import Decimal, InvalidOperation, ROUND_HALF_UP

from openregistry.api.constants import (DEFAULT_CURRENCY,
DEFAULT_ITEM_CLASSIFICATION, ITEM_CLASSIFICATIONS, DOCUMENT_TYPES,
IDENTIFIER_CODES, DEBTOR_TYPES
Expand Down Expand Up @@ -147,6 +150,22 @@ class Identifier(Model):
uri = URLType() # A URI to identify the organization.


class DecimalType(BaseDecimalType):

def __init__(self, precision=3, min_value=None, max_value=None, **kwargs):
self.min_value = min_value
self.max_value = max_value
self.precision = Decimal('1E{:d}'.format(precision))
super(DecimalType, self).__init__(**kwargs)

def to_native(self, value):
try:
value = Decimal(value).quantize(self.precision, rounding=ROUND_HALF_UP).normalize()
except (TypeError, InvalidOperation):
raise ConversionError(self.messages['number_coerce'].format(value))
return value


class Item(Model):
"""A good, service, or work to be contracted."""
id = StringType(required=True, min_length=1, default=lambda: uuid4().hex)
Expand All @@ -156,7 +175,7 @@ class Item(Model):
classification = ModelType(ItemClassification)
additionalClassifications = ListType(ModelType(Classification), default=list())
unit = ModelType(Unit) # Description of the unit which the good comes in e.g. hours, kilograms
quantity = IntType() # The number of units required
quantity = DecimalType() # The number of units required
address = ModelType(Address)
location = ModelType(Location)

Expand Down
2 changes: 1 addition & 1 deletion openregistry/api/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def test_Item_model(self):
"name": u"item",
"code": u"39513200-3"
},
"quantity": 5,
"quantity": u'0',
"address": {
"countryName": u"Україна",
"postalCode": "79000",
Expand Down

0 comments on commit 3942f85

Please sign in to comment.