Attributes¶
-
class
fhirbug.models.attributes.
Attribute
(getter=None, setter=None, searcher=None, search_regex=None, audit_get=None, audit_set=None)[source]¶ The base class for declaring db to fhir mappings. Accepts three positional arguments, a getter, a setter and a searcher.
The getter parameter can be a string, a tuple, a callable or type const.
- Using a string:
>>> from types import SimpleNamespace as SN >>> class Bla: ... _model = SN(column_name=12) ... p = Attribute('column_name') ... >>> b = Bla() >>> b.p 12
- Strings can also be properties:
>>> class Model: ... column_name = property(lambda x: 13) >>> class Bla: ... _model = Model() ... p = Attribute('column_name') ... >>> b = Bla() >>> b.p 13
- Callables will be called:
>>> class Bla: ... _model = SN(column_name=12) ... def get_col(self): ... return 'test' ... p = Attribute(get_col) ... >>> b = Bla() >>> b.p 'test'
- As a shortcut, a tuple (col_name, callable) can be passed. The result will be callable(_model.col_name)
>>> import datetime >>> class Bla: ... _model = SN(date='2012') ... p = Attribute(('date', int)) ... >>> b = Bla() >>> b.p 2012
The setter parameter can be a string, a tuple, a callable or type const.
- Using a string:
>>> class Bla: ... _model = SN(date='2012') ... p = Attribute(setter='date') ... >>> b = Bla() >>> b.p = '2013' >>> b._model.date '2013'
- Again, the string can point to a property with a setter:
>>> class Model: ... b = 12 ... def set_b(self, value): ... self.b = value ... column_name = property(lambda self: self.b, set_b) >>> class Bla: ... _model = Model() ... p = Attribute(getter='column_name', setter='column_name') ... >>> b = Bla() >>> b.p = 13 >>> b.p == b._model.b == 13 True
- Callables will be called:
>>> class Bla: ... _model = SN(column_name=12) ... def set_col(self, value): ... self._model.column_name = value ... p = Attribute(setter=set_col) ... >>> b = Bla() >>> b.p = 'test' >>> b._model.column_name 'test'
- Two-tuples contain a column name and a callable or const. Set the column to the result of the callable or const
>>> def add(column, value): ... return column + value
>>> class Bla: ... _model = SN(column_name=12) ... p = Attribute(setter=('column_name', add)) ... >>> b = Bla() >>> b.p = 3 >>> b._model.column_name 15
-
class
fhirbug.models.attributes.
BooleanAttribute
(*args, save_true_as=1, save_false_as=0, default=None, truthy_values=['true', 'True', 1, '1'], falsy_values=['false', 'False', '0', 0], **kwargs)[source]¶ Used for attributes representing boolean types.
truthy_values
andfalsy_values
are used to determine which possible values from the database we should consider as True and False. Values that are not in any of the lists are mapped todefault
and if that is None, a MappingValidationError is thrown.Parameters: - save_true_as – How do we save True in the database
- save_false_as – How do we save Fasle in the database
- deafult – If we read a value that is not in
truthy_values
orfalsy_values
, it will default to ths value. - truthy_values (list) – Which values, when read from the database should be mapped to True
- falsy_values (list) – Which values, when read from the database should be mapped to False
-
class
fhirbug.models.attributes.
EmbeddedAttribute
(*args, type=None, **kwargs)[source]¶ An attribute representing a BackboneElement that is described by a model and is stored using an ORM relationship, usually a ForeignKeyField or an embedded mongo document.
-
class
fhirbug.models.attributes.
NameAttribute
(family_getter=None, given_getter=None, family_setter=None, given_setter=None, join_given_names=False, pass_given_names=False, getter=None, setter=None, searcher=None, given_join_separator=' ', audit_get=None, audit_set=None)[source]¶ NameAttribute is for used on fields that represnt a HumanName resource. The parameters can be any of the valid getter and setter types for simple
Attribute
Parameters: - family_getter – A getter type parameter for the family name.
- given_getter – A getter type parameter for the given name
- family_setter – A setter type parameter for the family name
- given_setter – A getter type parameter for the given name
-
class
fhirbug.models.attributes.
ReferenceAttribute
(cls, id, name, setter=None, force_display=False, searcher=None)[source]¶ A Reference to some other Resource that may be contained.
-
fhirbug.models.attributes.
audited
(func)[source]¶ A decorator that adds auditing functionality to the
__get__
and__set__
methods of descriptor Attributes. Attribute auditors, depending on the result of the audit, can returnTrue
, meaning access to the attribute has been granted orFalse
, meaning access has been denied but execution should continue normally. If execution should stop and an error returned to the requester, it should raise an exception.