def hydrate_m2m(self, bundle): ci = bundle.obj CIRelation.objects.filter( Q(child_id=ci.id) | Q(parent_id=ci.id) ).delete() for relation_data in bundle.data.get('related', []): relation = CIRelation() try: type_id = CI_RELATION_TYPES.id_from_name(relation_data['type']) except ValueError: raise tastypie.exceptions.BadRequest( 'No such relation type {}'.format(relation_data['type']) ) relation.type = type_id other_ci = CI.objects.get(id=relation_data['id']) if relation_data['dir'] == 'OUTGOING': relation.parent = ci relation.child = other_ci elif relation_data['dir'] == 'INCOMING': relation.parent = other_ci relation.child = ci else: raise tastypie.exceptions.BadRequest( 'dir should be OUTGOING or INCOMING' ) relation.save() return []
def dehydrate(self, bundle, **kwargs): result = [] id_ = bundle.obj.id for q, dir_name, other in ( (Q(parent_id=id_), 'OUTGOING', 'child'), (Q(child_id=id_), 'INCOMING', 'parent'), ): for relation in CIRelation.objects.filter(q).select_related(other): other_ci = getattr(relation, other) result.append({ 'type': CI_RELATION_TYPES.name_from_id(relation.type), 'dir': dir_name, 'id': other_ci.id, 'resource_uri': CIResourceV010( api_name=self.api_name).get_resource_uri(other_ci), 'name': other_ci.name, 'ci_type': other_ci.type_id, }) return result
def dehydrate(self, bundle, **kwargs): result = [] id_ = bundle.obj.id for q, dir_name, other in ((Q(parent_id=id_), "OUTGOING", "child"), (Q(child_id=id_), "INCOMING", "parent")): for relation in CIRelation.objects.filter(q).select_related(other): other_ci = getattr(relation, other) result.append( { "type": CI_RELATION_TYPES.name_from_id(relation.type), "dir": dir_name, "id": other_ci.id, "resource_uri": CIResourceV010(api_name=self.api_name).get_resource_uri(other_ci), "name": other_ci.name, "ci_type": other_ci.type_id, } ) return result
def dehydrate(self, bundle, **kwargs): result = [] id_ = bundle.obj.id for q, dir_name, other in ( (Q(parent_id=id_), 'OUTGOING', 'child'), (Q(child_id=id_), 'INCOMING', 'parent'), ): for relation in CIRelation.objects.filter(q).select_related(other): other_ci = getattr(relation, other) result.append( { 'type': CI_RELATION_TYPES.name_from_id(relation.type), 'dir': dir_name, 'id': other_ci.id, 'resource_uri': CIResourceV010( api_name=self.api_name ).get_resource_uri(other_ci), 'name': other_ci.name, 'ci_type': other_ci.type_id, } ) return result
def hydrate_m2m(self, bundle): ci = bundle.obj CIRelation.objects.filter(Q(child_id=ci.id) | Q(parent_id=ci.id)).delete() for relation_data in bundle.data.get('related', []): relation = CIRelation() try: type_id = CI_RELATION_TYPES.id_from_name(relation_data['type']) except ValueError: raise tastypie.exceptions.BadRequest( 'No such relation type {}'.format(relation_data['type'])) relation.type = type_id other_ci = CI.objects.get(id=relation_data['id']) if relation_data['dir'] == 'OUTGOING': relation.parent = ci relation.child = other_ci elif relation_data['dir'] == 'INCOMING': relation.parent = other_ci relation.child = ci else: raise tastypie.exceptions.BadRequest( 'dir should be OUTGOING or INCOMING') relation.save() return []