async def handle_order_create(request): body = await request.json() try: validate( body, { 'sender_name': field.String(empty=False), 'amount': field.Integer(positive=True), 'receiver_name': field.String(empty=False), 'receiver_phone': field.Integer(), 'receiver_addr': field.String(empty=False), }) except InvalidInputError as e: print('fuckfuck') print(e) return web.Response(status=400) order_number = uuid.uuid4().hex new_order = { 'order_number': order_number, 'order_type': OrderType.APPLEJUICE, # Default order type 'sender_name': body['sender_name'], 'amount': body['amount'], 'receiver_name': body['receiver_name'], 'receiver_phone': body['receiver_phone'], 'receiver_addr': body['receiver_addr'], } query = (order.insert().values(new_order)) result = request.app['db_engine'].execute(query) order_id = result.inserted_primary_key if order_id is None: return web.Response(status=500) return web.json_response({'id': order_id[0], 'order_number': order_number})
async def handle_signin(request): body = await request.json() try: validate(body, { 'id': field.String(), 'password': field.String(), }) except InvalidInputError as e: return web.Response(text=str(e), status=400) query = (sa.select('*').select_from(user).where( (user.c.id == body['id']) & (user.c.password == body['password']))) result = request.app['db_engine'].execute(query) row = result.first() if not row: return web.Response(status=401) _, user_id, nickname, password = row # TODO: get rid of password field from jwt token. jwt_byte = _jwt.encode( { 'id': user_id, 'nickname': nickname, 'password': password }, 'applejuice-backend-jwt-secret-key', algorithm='HS256') jwt = jwt_byte.decode('utf-8') return web.json_response({'id': user_id, 'nickname': nickname, 'jwt': jwt})
async def handle_article_create(request): body = await request.json() try: validate( body, { 'title': field.String(), 'board': field.Integer(nonnegative=True), 'content': field.String(), 'created_by': field.Integer(nonnegative=True) }) except InvalidInputError as e: return web.Response(text=str(e), status=400) # TODO: validate inputs. new_article = { 'title': body['title'], 'board': body['board'], 'content': body['content'], 'created_by': body['created_by'], } query = (article.insert().values(new_article)) result = request.app['db_engine'].execute(query) article_id = result.inserted_primary_key if not article_id: return web.Response(status=500) return web.json_response({'id': article_id[0]})
def test_validate(): validation_spec = { 'username': field.String(length=32), 'password': field.String(length=32), 'age': field.Integer(positive=True), 'description': field.String(nullable=True), } body = { 'username': '******', 'password': '******', 'age': 23, } assert validate(body, validation_spec) wrong_body = { 'password': '******', 'age': 23, } with pytest.raises(InvalidInputError): validate(wrong_body, validation_spec)
async def handle_signup(request): body = await request.json() try: validate( body, { 'id': field.String(length=30), 'nickname': field.String(length=32), 'password': field.String(length=32), }) except InvalidInputError as e: return web.Response(text=str(e), status=400) # TODO: check length of id, nickname, password. # TODO: check if duplicate. # TODO: encrypt password. new_user = { 'id': body['id'], 'nickname': body['nickname'], 'password': body['password'], } query = (user.insert().values(new_user)) request.app['db_engine'].execute(query) return web.Response(status=204)