Exemple #1
0
    async def query_insert(self, user, query, insert):
        visor_name = query['visor']
        visor = self._get_visor(visor_name)
        # if table not in self.tables:
        #     return GrozaResponse({'errors':
        #     [f'Table '{table}' is not handled']})
        async with self._storage.session() as session:
            visor_instance = visor()
            result = await visor_instance.insert(insert=insert,
                                                 user=user,
                                                 session=session)

        if not result:
            return GrozaResponse({'status': 'error', 'message': 'No result'})

        return GrozaResponse({
            'status': 'ok',
            visor.primary_key: result[visor.primary_key]
        })
Exemple #2
0
    async def handle_request(self, request):
        resp = {
            'responseQueryId': request['queryId'],
        }

        if request.get('type') not in ('login', 'sub', 'auth', 'register',
                                       'update', 'insert', 'delete'):
            return {
                'status': 'error',
                'message': 'Invalid type: %s' % request.get('type')
            }

        push_request = GrozaRequest(request)

        req_type = request['type']
        if req_type == 'login':
            handle_resp = await self.handler.login(push_request)
            p = 0
        elif req_type == 'register':
            handle_resp = await self.handler.register(push_request)
            p = 0
        elif req_type == 'auth':
            token = request['token']
            handle_resp = await self.handler.auth(push_request)
            if handle_resp.data.get('status') == 'ok':
                self.user.auth_token = token
                self.user.user_id = handle_resp.data['userId']
        elif req_type == 'sub':
            if 'sub' not in request or not isinstance(request['sub'], dict):
                return GrozaResponse({
                    'status': 'error',
                    'message': 'Invalid not dict sub'
                })
            self.all_sub = request['sub']
            handle_resp = await self.handler.fetch_sub(self.user, self.all_sub)
            self.last_sub = handle_resp.data['sub']
        elif req_type == 'update':
            update = request['update']
            handle_resp = await self.handler.query_update(self.user, update)
        elif req_type == 'insert':
            query = request['query']
            insert = request['insert']
            handle_resp = await self.handler.query_insert(
                self.user, query, insert)
        elif req_type == 'delete':
            delete = request['delete']
            handle_resp = await self.handler.query_delete(self.user, delete)
        else:
            raise RuntimeError('Unhandled type %s' % req_type)

        resp.update(handle_resp.data)

        return resp
Exemple #3
0
    async def query_delete(self, user, delete):
        for cnt, delete_item in enumerate(delete):
            visor_name = delete_item['visor']
            _ = self._get_visor(visor_name)

        async with self._storage.session() as session:
            async with session.transaction():
                for cnt, delete_item in enumerate(delete):
                    visor_name = delete_item['visor']
                    visor = self._get_visor(visor_name)

                    visor_instance = visor()
                    result = await visor_instance.delete(delete=delete_item,
                                                         user=user,
                                                         session=session)

        return GrozaResponse({'status': 'ok'})
Exemple #4
0
    async def query_update(self, user, update):
        for cnt, (query, upd) in enumerate(update):
            visor_name = query['visor']
            _ = self._get_visor(visor_name)

        async with self._storage.session() as session:
            async with session.transaction():
                for cnt, (query, upd) in enumerate(update):
                    visor_name = query['visor']
                    visor = self._get_visor(visor_name)

                    visor_instance = visor()
                    result = await visor_instance.update(update=(query, upd),
                                                         user=user,
                                                         session=session)

        return GrozaResponse({'status': 'ok'})
Exemple #5
0
    async def fetch_sub(self, user, all_sub):
        resp = {}
        data = {}
        errors = []

        sub_resp = {}

        async with self._storage.session() as session:
            for sub, sub_desc in all_sub.items():
                visor_name = sub_desc['visor']
                # if table not in self.tables:
                #     errors.append(f'Table '{table}' is not handled')
                #     continue
                #
                # sub_table = self.tables[table]

                visor = self._get_visor(visor_name)

                sub_desc_from = sub_desc.get('fromSub')
                add_data, link_field = await session.query(
                    visor=visor,
                    from_sub=sub_desc_from,
                    where=sub_desc.get('where'),
                    order=sub_desc.get('order'),
                    all_sub=all_sub,
                    sub_resp=sub_resp,
                )

                primary_key_field = visor.primary_key

                def make_key(key):
                    if isinstance(key, UUID):
                        key = str(key)
                    return key

                table = visor.table

                data.setdefault(table, {})
                data[table].update(add_data)

                ids = []
                recursive_field, recursive_inject = sub_desc.get(
                    'recursive', (None, None))

                assert (recursive_field is None and recursive_inject is None
                        or recursive_field is not None
                        and recursive_inject is not None)

                if recursive_field:
                    for key in add_data.keys():
                        add_data[key].setdefault(recursive_inject, [])

                for key, item in add_data.items():
                    if recursive_field and item.get(recursive_field):
                        inject_to = data[table][item[recursive_field]]
                        inject_to.setdefault(recursive_inject, [])
                        inject_to[recursive_inject].append(
                            item[primary_key_field])
                        continue

                    ids.append(make_key(item[primary_key_field]))

                from_sub = {}
                if sub_desc_from is not None:
                    for key, item in add_data.items():
                        lf = item[link_field]

                        from_sub.setdefault(lf, [])
                        from_sub[lf].append(item[primary_key_field])

                sub_resp[sub] = {
                    'status': 'ok',
                    'dataField': table,
                    'ids': ids,
                }

                sub_resp[sub]['fromSub'] = from_sub

        resp['type'] = 'data'
        resp['data'] = data
        resp['sub'] = sub_resp

        if errors:
            resp['errors'] = errors

        return GrozaResponse(resp)
Exemple #6
0
 async def auth(self, request: GrozaRequest) -> GrozaResponse:
     # user = self.auth.register(request)
     return GrozaResponse({}, request=request)
Exemple #7
0
 async def login(self, request: GrozaRequest) -> GrozaResponse:
     user = self._auth.login(request)
     return GrozaResponse({}, request=request)