def post(self, *args, **kwargs):
        """Add / create relationship(s)"""
        json_data = request.get_json() or {}

        relationship_field, model_relationship_field, related_type_, related_id_field = self._get_relationship_data()

        if 'data' not in json_data:
            raise BadRequest('You must provide data with a "data" route node', source={'pointer': '/data'})
        if isinstance(json_data['data'], dict):
            if 'type' not in json_data['data']:
                raise BadRequest('Missing type in "data" node', source={'pointer': '/data/type'})
            if 'id' not in json_data['data']:
                raise BadRequest('Missing id in "data" node', source={'pointer': '/data/id'})
            if json_data['data']['type'] != related_type_:
                raise InvalidType('The type field does not match the resource type', source={'pointer': '/data/type'})
        if isinstance(json_data['data'], list):
            for obj in json_data['data']:
                if 'type' not in obj:
                    raise BadRequest('Missing type in "data" node', source={'pointer': '/data/type'})
                if 'id' not in obj:
                    raise BadRequest('Missing id in "data" node', source={'pointer': '/data/id'})
                if obj['type'] != related_type_:
                    raise InvalidType('The type provided does not match the resource type',
                                      source={'pointer': '/data/type'})

        self.before_post(args, kwargs, json_data=json_data)

        obj_, updated = self._data_layer.create_relationship(json_data,
                                                             model_relationship_field,
                                                             related_id_field,
                                                             kwargs)

        status_code = 200
        result = {'meta': {'message': 'Relationship successfully created'}}

        if updated is False:
            result = ''
            status_code = 204

        final_result = self.after_post(result, status_code)

        return final_result
def post ( self , * args , ** kwargs ) : json_data = request . get_json ( ) or { } relationship_field , model_relationship_field , related_type_ , related_id_field = self . _get_relationship_data ( ) if 'data' not in json_data : raise BadRequest ( 'You must provide data with a "data" route node' , source = { 'pointer' : '/data' } ) if isinstance ( json_data [ 'data' ] , dict ) : if 'type' not in json_data [ 'data' ] : raise BadRequest ( 'Missing type in "data" node' , source = { 'pointer' : '/data/type' } ) if 'id' not in json_data [ 'data' ] : raise BadRequest ( 'Missing id in "data" node' , source = { 'pointer' : '/data/id' } ) if json_data [ 'data' ] [ 'type' ] != related_type_ : raise InvalidType ( 'The type field does not match the resource type' , source = { 'pointer' : '/data/type' } ) if isinstance ( json_data [ 'data' ] , list ) : for obj in json_data [ 'data' ] : if 'type' not in obj : raise BadRequest ( 'Missing type in "data" node' , source = { 'pointer' : '/data/type' } ) if 'id' not in obj : raise BadRequest ( 'Missing id in "data" node' , source = { 'pointer' : '/data/id' } ) if obj [ 'type' ] != related_type_ : raise InvalidType ( 'The type provided does not match the resource type' , source = { 'pointer' : '/data/type' } ) self . before_post ( args , kwargs , json_data = json_data ) obj_ , updated = self . _data_layer . create_relationship ( json_data , model_relationship_field , related_id_field , kwargs ) status_code = 200 result = { 'meta' : { 'message' : 'Relationship successfully created' } } if updated is False : result = '' status_code = 204 final_result = self . after_post ( result , status_code ) return final_result
Add / create relationship(s)