44# Copyright (c) 2019 Ingram Micro. All Rights Reserved.
55
66from marshmallow import Schema , fields , post_load
7+ import six
78
89
910class BaseSchema (Schema ):
@@ -138,9 +139,18 @@ def make_object(self, data):
138139 return DownloadLink (** data )
139140
140141
142+ class UserSchema (BaseSchema ):
143+ name = fields .Str ()
144+
145+ @post_load
146+ def make_object (self , data ):
147+ from connect .models import User
148+ return User (** data )
149+
150+
141151class EventInfoSchema (BaseSchema ):
142152 at = fields .DateTime (allow_none = True )
143- by = fields .Nested (CompanySchema , allow_none = True )
153+ by = fields .Nested (UserSchema , allow_none = True )
144154
145155 @post_load
146156 def make_object (self , data ):
@@ -216,25 +226,36 @@ def make_object(self, data):
216226 return Renewal (** data )
217227
218228
229+ class QuantityField (fields .Field ):
230+ def _deserialize (self , value , attr , obj , ** kwargs ):
231+ if isinstance (value , six .string_types ):
232+ if value == 'unlimited' :
233+ return - 1
234+ else :
235+ try :
236+ float_val = float (value )
237+ int_val = int (float_val )
238+ return int_val if int_val == float_val else float_val
239+ except ValueError :
240+ raise ValueError ({
241+ attr : [u'Not a valid string encoded number nor "unlimited".' ]
242+ })
243+ elif isinstance (value , (int , float )):
244+ return value
245+ else :
246+ raise ValueError ({attr : [u'Not a valid int, float or string.' ]})
247+
248+
219249class ItemSchema (BaseSchema ):
220250 mpn = fields .Str ()
221- quantity = fields . Str ()
222- old_quantity = fields . Str (allow_none = True )
251+ quantity = QuantityField ()
252+ old_quantity = QuantityField (allow_none = True )
223253 renewal = fields .Nested (RenewalSchema , allow_none = True )
224254 global_id = fields .Str ()
225255
226256 @post_load
227257 def make_object (self , data ):
228258 from connect .models import Item
229- params = ('quantity' , 'old_quantity' )
230- for param in params :
231- if param in data :
232- if data [param ] != 'unlimited' :
233- float_val = float (data [param ])
234- int_val = int (float_val )
235- data [param ] = int_val if float_val == int_val else float_val
236- else :
237- data [param ] = - 1
238259 return Item (** data )
239260
240261
@@ -261,7 +282,7 @@ class AgreementSchema(BaseSchema):
261282 updated = fields .DateTime ()
262283 owner = fields .Nested (CompanySchema )
263284 stats = fields .Nested (AgreementStatsSchema , allow_none = True )
264- author = fields .Nested (CompanySchema , allow_none = True )
285+ author = fields .Nested (UserSchema , allow_none = True )
265286 version = fields .Int ()
266287 active = fields .Bool ()
267288 link = fields .Str ()
@@ -285,13 +306,13 @@ class ContractSchema(BaseSchema):
285306 agreement = fields .Nested (AgreementSchema , only = ('id' , 'name' ))
286307 marketplace = fields .Nested (MarketplaceSchema , only = ('id' , 'name' ), allow_none = True )
287308 owner = fields .Nested (CompanySchema , only = ('id' , 'name' ), allow_none = True )
288- creator = fields .Nested (CompanySchema , only = ('id' , 'name' ))
309+ creator = fields .Nested (UserSchema , only = ('id' , 'name' ))
289310 created = fields .DateTime ()
290311 updated = fields .DateTime ()
291312 enrolled = fields .DateTime (allow_none = True )
292313 version_created = fields .DateTime ()
293314 activation = fields .Nested (ActivationSchema )
294- signee = fields .Nested (CompanySchema , only = ('id' , 'name' ), allow_none = True )
315+ signee = fields .Nested (UserSchema , only = ('id' , 'name' ), allow_none = True )
295316
296317 @post_load
297318 def make_object (self , data ):
@@ -471,7 +492,7 @@ class TierConfigRequestSchema(BaseSchema):
471492 configuration = fields .Nested (TierConfigSchema )
472493 events = fields .Nested (EventsSchema , allow_none = True )
473494 params = fields .Nested (ParamSchema , many = True )
474- assignee = fields .Nested (CompanySchema , allow_none = True )
495+ assignee = fields .Nested (UserSchema , allow_none = True )
475496 template = fields .Nested (TemplateSchema , allow_none = True )
476497 reason = fields .Str (allow_none = True )
477498 activation = fields .Nested (ActivationSchema , allow_none = True )
0 commit comments