Skip to content

Commit

Permalink
Merge pull request #276 from oleksiyVeretiuk/ssp_openprocurement
Browse files Browse the repository at this point in the history
Ssp openprocurement
  • Loading branch information
leits committed Mar 30, 2018
2 parents ae57acd + c178cf5 commit cd5b4cd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'rfc6266',
'setuptools',
'tzlocal',
'isodate'
]
test_requires = requires + [
'webtest',
Expand Down
13 changes: 10 additions & 3 deletions src/openprocurement/api/registry_models/ocds.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,26 @@ def validate_id(self, data, code):
raise ValidationError(BaseType.MESSAGES['choices'].format(unicode(available_codes)))


class Unit(Model):
class BaseUnit(Model):
"""
Description of the unit which the good comes in e.g. hours, kilograms.
Made up of a unit name, and the value of a single unit.
Made up of a unit name of a single unit.
"""

name = StringType()
name_en = StringType()
name_ru = StringType()
value = ModelType(Value)
code = StringType(required=True)


class Unit(BaseUnit):
"""
Extends BaseUnit adding value field to it.
"""

value = ModelType(Value)


class Address(Model):

streetAddress = StringType()
Expand Down
1 change: 0 additions & 1 deletion src/openprocurement/api/registry_models/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
'revisions': document_revisions_role,
}


organization_roles = {
'embedded': schematics_embedded_role,
'view': schematics_default_role,
Expand Down
23 changes: 22 additions & 1 deletion src/openprocurement/api/registry_models/schematics_extender.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import datetime, timedelta
from iso8601 import parse_date, ParseError
from isodate.duration import Duration
from isodate import parse_duration, ISO8601Error, duration_isoformat
from hashlib import algorithms, new as hash_new
from schematics.exceptions import ConversionError, ValidationError

Expand Down Expand Up @@ -35,6 +37,25 @@ def to_primitive(self, value, context=None):
return value.isoformat()


class IsoDurationType(BaseType):
MESSAGES = {
'parse': u'Could not parse {0}. Should be ISO8601 duration format.',
}

def to_native(self, value, context=None):
if isinstance(value, (timedelta, Duration)):
return value
try:
return parse_duration(value)
except (ISO8601Error, TypeError):
raise ConversionError(self.messages['parse'].format(value))
except OverflowError as e:
raise ConversionError(e.message)

def to_primitive(self, value, context=None):
return duration_isoformat(value)


class HashType(StringType):

MESSAGES = {
Expand Down
1 change: 0 additions & 1 deletion src/openprocurement/api/tests/blanks/json_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from copy import deepcopy


test_organization = {
"name": u"Державне управління справами",
"identifier": {
Expand Down
30 changes: 29 additions & 1 deletion src/openprocurement/api/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import mock
from copy import deepcopy
from datetime import datetime, timedelta
from isodate.duration import Duration
from schematics.exceptions import ConversionError, ValidationError, ModelValidationError
from decimal import Decimal

from openprocurement.api.utils import get_now

from openprocurement.api.registry_models.roles import blacklist
from openprocurement.api.registry_models.schematics_extender import (
IsoDateTimeType, HashType)
IsoDateTimeType, HashType, IsoDurationType)
from openprocurement.api.registry_models.ocds import (
Organization, ContactPoint, Identifier, Address,
Item, Location, Unit, Value, ItemClassification, Classification,
Expand Down Expand Up @@ -94,6 +95,33 @@ def test_HashType_model(self):
result = 'md5:{}'.format(uuid4().hex)
self.assertEqual(hash.to_native(result), result)

def test_IsoDuration_model(self):
dt = IsoDurationType()

duration = Duration(days=5, hours=6)
value = dt.to_primitive(duration)
self.assertEqual(duration, dt.to_native(duration))
self.assertEqual(duration, dt.to_native(value))

duration_string = "P1DT12H"
self.assertEqual(Duration(days=1, hours=12), dt.to_native(duration_string))
self.assertEqual("P1DT12H", dt.to_primitive(Duration(days=1, hours=12)))

duration_string = "P3Y1DT12H"
self.assertEqual(Duration(years=3, days=1, hours=12), dt.to_native(duration_string))
self.assertEqual("P3Y1DT12H", dt.to_primitive(Duration(years=3 ,days=1, hours=12)))

# Iso8601Error
for duration in (None, '', 2017, "X1DT12H"):
with self.assertRaisesRegexp(ConversionError,
u'Could not parse %s. Should be ISO8601 duration format.' % duration):
dt.to_native(duration)
# OverflowError
# for date in (Duration(12), datetime.min):
# self.assertEqual(date, dt.to_native(date))
# with self.assertRaises(ConversionError):
# dt.to_native(dt.to_primitive(date))


class DummyOCDSModelsTest(unittest.TestCase):
""" Test Case for testing openprocurement.api.registry_models'
Expand Down

0 comments on commit cd5b4cd

Please sign in to comment.