Exemplo n.º 1
0
        def _get_resources():
            results = self.db_session.query(model).all()

            if not extractfor_resources:

                _list_data_exp = ({
                    key: val
                    for key, val in vars(r).items() if not key.startswith('_')
                } for r in results)
                #inject the URI to the data
                _list_data = list({
                    **adict, 'uri':
                    '/%s/%s' % (model.__tablename__, adict[_primary_key])
                } for adict in _list_data_exp)
                #if after request if not not then call the predicate
                if before_response_for_resources:
                    before_response_for_resources(_list_data)

                return json_records_envelop(_list_data)
            else:
                _extractfor_resources = list(extractfor_resources)

                _list_data = []
                for result in results:
                    _adict = {
                        key: val
                        for key, val in vars(result).items()
                        if not key.startswith('_')
                    }
                    adict = {
                        **_adict, 'uri':
                        '/%s/%s' % (model.__tablename__, _adict[_primary_key])
                    }
                    #nod for each extract with the many to one relationship,
                    for relationship in _extractfor_resources:
                        _rel_val = getattr(result, relationship)
                        if not _rel_val:
                            adict[relationship] = None
                            continue
                        if not isinstance(_rel_val, collections.Iterable):
                            adict[relationship] = {
                                key: val
                                for key, val in vars(_rel_val).items()
                                if not key.startswith('_')
                            }
                            continue

                        adict[relationship] = list({
                            key: val
                            for key, val in vars(_r_val).items()
                            if not key.startswith('_')
                        } for _r_val in _rel_val)

                    #finally add to the list
                    _list_data.append(adict)
                return json_records_envelop(_list_data)
Exemplo n.º 2
0
                def _get_resources_by_parent(id):
                    request_target = request.full_path.split("/")[-1].replace(
                        "?", "")
                    sub_pks = id.split("+")
                    db_session = self.Session()
                    try:
                        children = db_session.query(
                            mappers[request_target]).join(model)
                        for i in range(len(model.__mapper__.primary_key)):
                            children = children.filter(
                                model.__mapper__.primary_key[i] == sub_pks[i])
                        children = children.all()
                    except NoResultFound:
                        return record_notfound_envelop()
                    else:
                        _list = list({
                            key: val
                            for key, val in vars(data).items()
                            if not key.startswith('_')
                        } for data in children)
                        _primary_keys = [
                            x.name for x in mappers[request_target].class_.
                            __mapper__.primary_key
                        ]
                        # inject the URI to the data
                        if self.uri_prefix:
                            _list = list({
                                **adict, 'uri':
                                '/%s/%s/%s' %
                                (self.uri_prefix,
                                 str(mappers[request_target].mapped_table.name
                                     ),
                                 urllib.parse.quote_plus('+'.join([
                                     str(adict[pkey]) for pkey in _primary_keys
                                 ])))
                            } for adict in _list)
                        else:
                            _list = list({
                                **adict, 'uri':
                                '/%s/%s' % (str(
                                    mappers[request_target].mapped_table.name),
                                            urllib.parse.quote_plus('+'.join([
                                                str(adict[pkey])
                                                for pkey in _primary_keys
                                            ])))
                            } for adict in _list)

                        # if after request if not not then call the predicate
                        if before_response_for_resources:
                            before_response_for_resources(_list)
                        return json_records_envelop(_list)
                    finally:
                        db_session.close()
                        self.Session.remove()
Exemplo n.º 3
0
 def _get_resources_by_parent(id):
     try:
         parent = self.db_session.query(model).\
                     filter(getattr(model, _primary_key) == id).one()
     except NoResultFound:
         return record_notfound_envelop()
     else:
         _results = getattr(parent, _prop)
         _list = list({
             key: val
             for key, val in vars(data).items()
             if not key.startswith('_')
         } for data in _results)
         return json_records_envelop(_list)
Exemplo n.º 4
0
        def _get_resource(r_id):
            db_session = self.Session()
            try:
                result = db_session.query(model)
                sub_pks = r_id.split("+")
                for i in range(len(model.__mapper__.primary_key)):
                    result = result.filter(
                        model.__mapper__.primary_key[i] == sub_pks[i])
                result = result.one()
                _data = {
                    key: val
                    for key, val in vars(result).items()
                    if not key.startswith('_')
                }

                if before_response_for_resource:
                    before_response_for_resource(result, _data)

                if extract:
                    for relationship in extract:
                        # get the attribute
                        children = getattr(result, relationship)
                        if not children:
                            _data[relationship] = None
                            continue
                        if not isinstance(children, collections.Iterable):
                            # that means it is on many to one side
                            _data[relationship] = {
                                key: val
                                for key, val in vars(children).items()
                                if not key.startswith('_')
                            }
                            continue

                        _data[relationship] = list({
                            key: val
                            for key, val in vars(child).items()
                            if not key.startswith('_')
                        } for child in children)

            except NoResultFound:
                return record_notfound_envelop()
            else:
                return json_records_envelop(_data)
            finally:
                db_session.close()
                self.Session.remove()
Exemplo n.º 5
0
        def _get_resource(r_id):
            try:
                result = self.db_session.query(model).\
                            filter(getattr(model, _primary_key) == r_id).one()
                _data = {
                    key: val
                    for key, val in vars(result).items()
                    if not key.startswith('_')
                }

                if before_response_for_resource:
                    before_response_for_resource(result, _data)

                if extract:
                    for relationship in extract:
                        #get the attribute
                        children = getattr(result, relationship)
                        if not children:
                            _data[relationship] = None
                            continue
                        if not isinstance(children, collections.Iterable):
                            #that means it is on many to one side
                            _data[relationship] = {
                                key: val
                                for key, val in vars(children).items()
                                if not key.startswith('_')
                            }
                            continue

                        _data[relationship] = list({
                            key: val
                            for key, val in vars(child).items()
                            if not key.startswith('_')
                        } for child in children)

            except NoResultFound:
                return record_notfound_envelop()
            else:

                return json_records_envelop(_data)
Exemplo n.º 6
0
        def _get_resources():
            db_session = self.Session()
            try:
                results = db_session.query(model).all()

                if not extractfor_resources:

                    _list_data_exp = ({
                        key: val
                        for key, val in vars(r).items()
                        if not key.startswith('_')
                    } for r in results)
                    # inject the URI to the data
                    if self.uri_prefix:
                        _list_data = list({
                            **adict, 'uri':
                            '/%s/%s/%s' %
                            (self.uri_prefix, model.__tablename__,
                             urllib.parse.quote_plus('+'.join(
                                 [str(adict[pkey])
                                  for pkey in _primary_keys])))
                        } for adict in _list_data_exp)
                    else:
                        _list_data = list({
                            **adict, 'uri':
                            '/%s/%s' %
                            (model.__tablename__,
                             urllib.parse.quote_plus('+'.join(
                                 [str(adict[pkey])
                                  for pkey in _primary_keys])))
                        } for adict in _list_data_exp)
                    # if after request if not not then call the predicate
                    if before_response_for_resources:
                        before_response_for_resources(_list_data)

                    return json_records_envelop(_list_data)
                else:
                    raise NotImplementedError(
                        "Untested code, proceed with care!")
                    _extractfor_resources = list(extractfor_resources)

                    _list_data = []
                    for result in results:
                        _adict = {
                            key: val
                            for key, val in vars(result).items()
                            if not key.startswith('_')
                        }
                        adict = {
                            **_adict, 'uri':
                            '/%s/%s' % (model.__tablename__,
                                        urllib.parse.quote_plus('+'.join([
                                            str(_adict[pkey])
                                            for pkey in _adict[_primary_keys]
                                        ])))
                        }
                        # nod for each extract with the many to one relationship,
                        for relationship in _extractfor_resources:
                            _rel_val = getattr(result, relationship)
                            if not _rel_val:
                                adict[relationship] = None
                                continue
                            if not isinstance(_rel_val, collections.Iterable):
                                adict[relationship] = {
                                    key: val
                                    for key, val in vars(_rel_val).items()
                                    if not key.startswith('_')
                                }
                                continue

                            adict[relationship] = list({
                                key: val
                                for key, val in vars(_r_val).items()
                                if not key.startswith('_')
                            } for _r_val in _rel_val)

                        # finally add to the list
                        _list_data.append(adict)
                    return json_records_envelop(_list_data)
            finally:
                db_session.close()
                self.Session.remove()