Esempio n. 1
0
async def get_file(request: web.Request):
    user_id = id_validator(request.match_info['user_id'], 'User')
    file_id = id_validator(request.match_info['file_id'], 'File')

    user_table = get_model_by_name('user')
    user_exists = await request.app['pg'].fetchval(
        select([exists().where(user_table.c.user_id == user_id)]))

    if not user_exists:
        raise web.HTTPNotFound(body=json.dumps({'error': 'User not found'}),
                               content_type='application/json')

    file_table = get_model_by_name('file')
    file_exists = await request.app['pg'].fetchval(
        select([
            exists().where(
                and_(file_table.c.id == file_id,
                     file_table.c.user_id == user_id))
        ]))

    if not file_exists:
        raise web.HTTPNotFound(body=json.dumps({'error': 'File not found'}),
                               content_type='application/json')

    file = await request.app['pg'].fetchrow(
        file_table.select().where(file_table.c.id == file_id))
    file = row_to_dict(file, 'file')

    return web.FileResponse(path=file['path'], status=200)
Esempio n. 2
0
def filter_items():
    comment = f"%{request.json.get('comment')}%"
    title = f"%{request.json.get('title')}%"
    query = session.query(Item).filter(Item.comment.like(comment),
                                       Item.title.like(title)).all()
    result = [row_to_dict(row) for row in query]
    return (jsonify(result))
Esempio n. 3
0
def update_item(item_id):
    comment = request.json.get("comment")
    if not comment:
        return (jsonify({}), 400)
    new_item = session.query(Item).filter(Item.id == item_id).update(
        {'comment': comment})
    result = row_to_dict(
        session.query(Item).filter(Item.id == item_id).first())
    session.commit()
    return ((jsonify(result), 201))
Esempio n. 4
0
    async def post(self):
        user_id = id_validator(self.request.match_info['user_id'], 'User')

        if self.request.content_type != 'multipart/form-data' or self.request.content_length == 0:
            return web.json_response(data=[])

        user_table = get_model_by_name('user')
        file_table = get_model_by_name('file')
        user_exists = await self.request.app['pg'].fetchval(
            select([exists().where(user_table.c.user_id == user_id)]))

        if not user_exists:
            await self.request.app['pg'].fetchrow(
                user_table.insert().values(**{'user_id': user_id}))

        reader = await self.request.multipart()
        upload_folder = self.request.app['config']['UPLOAD_FOLDER']
        data = []
        while not reader.at_eof():
            image = await reader.next()

            if not image:
                break

            file_name, ext = get_ext(image.filename)
            generated_file_name = '{}.{}'.format(uuid.uuid4(), ext)
            full_path = os.path.abspath(
                os.path.join(upload_folder, generated_file_name))
            size = 0

            with open(full_path, 'wb') as f:
                while True:
                    chunk = await image.read_chunk()
                    if not chunk:
                        break
                    size += len(chunk)
                    f.write(chunk)

            body = {
                'user_id': user_id,
                'name': image.filename,
                'path': full_path,
                'size': size
            }

            file = await self.request.app['pg'].fetchrow(
                file_table.insert().values(**body).returning(
                    literal_column('*')))
            file = row_to_dict(file, 'file')
            data.append(dict(file))

        return web.json_response(data=data)
Esempio n. 5
0
 def _wrap(self, data):
     summary = utils.row_to_dict(data)
     del summary['uuid']
     return summary
Esempio n. 6
0
def list_all_items():
    result = []
    query = session.query(Item).all()
    result = [row_to_dict(row) for row in query]
    return (jsonify(result))
Esempio n. 7
0
def get_item_by_id(item_id):
    query = session.query(Item).filter(Item.id == item_id).first()
    return (row_to_dict(query))