| 26 | |
| 27 | |
| 28 | class SourceSchema(ma.Schema): |
| 29 | |
| 30 | id = ma.fields.String() |
| 31 | name = ma.fields.String() |
| 32 | facts = ma.fields.List(ma.fields.Nested(FactSchema)) |
| 33 | rules = ma.fields.List(ma.fields.Nested(RuleSchema)) |
| 34 | adjustments = ma.fields.List(ma.fields.Nested(AdjustmentSchema)) |
| 35 | relationships = ma.fields.List(ma.fields.Nested(RelationshipSchema)) |
| 36 | plugin = ma.fields.String(load_default=None) |
| 37 | |
| 38 | @ma.pre_load |
| 39 | def fix_adjustments(self, in_data, **_): |
| 40 | x = [] |
| 41 | raw_adjustments = in_data.pop('adjustments', {}) |
| 42 | if raw_adjustments and isinstance(raw_adjustments, dict): |
| 43 | for ability_id, adjustments in raw_adjustments.items(): |
| 44 | for trait, block in adjustments.items(): |
| 45 | for change in block: |
| 46 | x.append(dict(ability_id=ability_id, trait=trait, value=change.get('value'), |
| 47 | offset=change.get('offset'))) |
| 48 | in_data['adjustments'] = x |
| 49 | self._fix_loaded_object_origins(in_data) |
| 50 | return in_data |
| 51 | |
| 52 | @ma.post_load() |
| 53 | def build_source(self, data, **kwargs): |
| 54 | return None if kwargs.get('partial') is True else Source(**data) |
| 55 | |
| 56 | @staticmethod |
| 57 | def _fix_loaded_object_origins(input_data): |
| 58 | """ |
| 59 | Sort through input_data's facts and relationships, and patch them to include origin and references |
| 60 | :param input_data: A 'source' dictionary |
| 61 | :return: input_data with updated facts/relationships (patched in place) |
| 62 | """ |
| 63 | for y in input_data.get('facts', []): |
| 64 | y['origin_type'] = OriginType.IMPORTED.name |
| 65 | y['source'] = input_data['id'] |
| 66 | for y in input_data.get('relationships', []): |
| 67 | y['source']['origin_type'] = OriginType.IMPORTED.name |
| 68 | y['source']['source'] = input_data['id'] |
| 69 | if y.get('target'): |
| 70 | y['target']['origin_type'] = OriginType.IMPORTED.name |
| 71 | y['target']['source'] = input_data['id'] |
| 72 | |
| 73 | |
| 74 | class Source(FirstClassObjectInterface, BaseObject): |
no outgoing calls