async def wrapper(request): title = request.headers.get(HEADER_TITLE, None) if not title: return json_response(data={'status': Status.ERROR}, status=HTTPStatus.FORBIDDEN) return await handler(request)
async def favorites(request): screen_type = request.match_info.get('screen') with db_manager.master_session() as db: query = db.query(Image).options(subqueryload( Image.screen_type), subqueryload(Image.tags)).join( ScreenType, Image.screen_type_id == ScreenType.id).join( Category, Image.category_id == Category.id).filter( Category.is_live == False).filter( Category.active == True) if screen_type: query = query.filter(ScreenType.title == screen_type) query = query.filter(Image.active == True).order_by(Image.likes.desc()) images_raw = query.all() images = {} for image in images_raw: if image.origin_id not in images: images[image.origin_id] = {'urls': {}} if not image.image in images[image.origin_id]['urls']: images[image.origin_id]['urls'][image.screen_type.screen_type] = { 'image': image.image, 'video': image.video, 'likes': image.likes, 'tags': [tag.title for tag in image.tags] if image.tags else [] } images_list = [{ 'id': img_id, 'urls': img_data['urls'] } for img_id, img_data in images.items()] page = int(request.query.get('page', DEFAULT_PAGE)) page_size = int(request.query.get('page_size', DEFAULT_PAGE_SIZE)) if screen_type: result = [] for img in images_list: if screen_type in img['urls']: result.append(img) images_list = result total = len(images_list) total_pages = ceil(total / page_size) start = (page - 1) * page_size data = images_list[start:start + page_size] return json_response({ 'page': page, 'pages': total_pages, 'page_size': page_size, 'total': total, 'data': data })
async def live(request): screen_type = request.query.get('screen_type') with db_manager.master_session() as db: query = db.query(Image).options(subqueryload( Image.screen_type), subqueryload(Image.tags)).join( ScreenType, Image.screen_type_id == ScreenType.id).join( Category, Image.category_id == Category.id).filter( Category.is_live == True).filter( Category.active == True) if screen_type: query = query.filter(ScreenType.title == screen_type) query = query.filter(Image.active == True).order_by(Image.likes.desc()) images_raw = query.all() images = [] for image in images_raw: images.append({ 'screen_type': image.screen_type.screen_type, 'image': image.image, 'video': image.video, 'likes': image.likes }) page = int(request.query.get('page', DEFAULT_PAGE)) page_size = int(request.query.get('page_size', DEFAULT_PAGE_SIZE)) total = len(images) total_pages = ceil(total / page_size) start = (page - 1) * page_size data = images[start:start + page_size] return json_response({ 'page': page, 'pages': total_pages, 'page_size': page_size, 'total': total, 'data': data })
async def check(request): """Check the API is alive.""" status = 'fail' response_status = 200 try: async with ClientSession( connector=TCPConnector(verify_ssl=False), raise_for_status=True ) as session: async with session.get('https://subchat.site/api/v1/ping/') as response: data = await response.read() data = ujson.loads(data.decode('utf-8')) if data['status'] == Status.OK: status = 'alive' response_status = response.status except Exception as e: status = 'error' return json_response(status=response_status, data={'status': status})
async def register(request): data = await request.json() required_parameters = ['first_name', 'last_name', 'login', 'password'] for field in required_parameters: if field not in data: raise KeyError() data['password'] = get_password(data['password']) async with request.app.db.acquire() as connection: query = select(['*' ]).select_from(User).where(User.login == data['login']) user = await asyncio.wait_for(connection.fetchrow(query), timeout=None) if not user: insert_q = insert(User, data).on_conflict_do_nothing().returning(User.id) user_id = await asyncio.shield(connection.fetchval(insert_q)) else: user_id = user['id'] return json_response(data={'status': Status.OK, 'user_id': user_id})
async def ping(request): """Check the API is alive.""" return json_response(data={'status': Status.OK})
async def ping(request): return json_response({'status': Status.OK})