402 def __setattr__(self, par, val):
403 """Sets an object attribute or relationship.
404
405 This method overrides the default setattr method, so it can apply
406 existence and type verification on class attributes. If the attribute
407 to be set starts with '__', or the passed value is None,
408 no verification is performed. If the value to set an attribute is a
409 list, the type verification is performed in every component of that
410 list.
411
412 N.B.: This method takes a reference to the object being passed. It does
413 not copy the value, so, if you do a.b = c, and then you apply changes
414 to 'c', these changes will be also applied to 'a.b'.
415
416 Parameters:
417
418 par -- The name of the parameter (attribute or relationship)
419
420 val -- The value that will be attributed to 'par'.
421
422 Raises AttributeError if the parameter does not exist.
423
424 Raises ValueError if the value you passed cannot be coerced to a
425 compatible OKS python type for the attribute or relationship you are
426 trying to set.
427 """
428 from .schema import coerce, check_relation, check_cardinality
429 from . import dalproperty
430
431
432 if par[0:2] == '__':
433 self.__dict__[par] = val
434 return
435
436
437 if par == 'id':
438 if isinstance(val, str):
439 prop = getattr(self.__class__, par)
440 prop.__set__(self, val)
441 self.__reset_identity__()
442 return
443 else:
444 raise ValueError(
445 'The "id" attribute of a DAL object must be a string')
446
447 if par in list(self.__schema__['attribute'].keys()):
448
449
450 if val is None:
451 self.__dict__[par] = val
452
453 try:
454 if val is not None:
455 check_cardinality(val, self.__schema__['attribute'][par])
456 if self.__schema__['attribute'][par]['multivalue']:
457 result = \
458 [coerce(v, self.__schema__['attribute'][par])
459 for v in val]
460 else:
461 result = coerce(val, self.__schema__['attribute'][par])
462 else:
463 result = val
464
465 try:
466 prop = getattr(self.__class__, par)
467
468 except AttributeError:
469
470 prop = property(dalproperty._return_attribute(par),
471 dalproperty._assign_attribute(par))
472 setattr(self.__class__, par, prop)
473
474 prop.__set__(self, result)
475
476 except ValueError as e:
477 raise ValueError('Problems setting attribute "%s" '
478 'at object %s: %s' %
479 (par, self.fullName(), str(e)))
480
481 elif par in list(self.__schema__['relation'].keys()):
482
483 try:
484
485 if val is not None:
486 check_cardinality(val, self.__schema__['relation'][par])
487
488 tmpval = \
489 val if self.__schema__[
490 'relation'][par]['multivalue'] else [val]
491 for v in tmpval:
492 check_relation(v, self.__schema__['relation'][par])
493
494 try:
495 prop = getattr(self.__class__, par)
496
497 except AttributeError:
498
499 multi = self.__schema__['relation'][par]['multivalue']
500 prop = property(
501 dalproperty._return_relation(par, multi=multi),
502 dalproperty._assign_relation(par))
503 setattr(self.__class__, par, prop)
504
505 prop.__set__(self, val)
506 self.__touched__.append(par)
507
508 except ValueError as e:
509 raise ValueError('Problems setting relation "%s" at '
510 'object %s: %s' %
511 (par, self.fullName(), str(e)))
512
513 else:
514 raise AttributeError('Parameter "%s" is not ' % par +
515 'part of class "%s" or any of its '
516 'parent classes' %
517 (self.className()))
518
519