Commit 10f970be authored by Pavel Kuzmenko's avatar Pavel Kuzmenko
Browse files

feat: add get_auction_by_id on databridge

parent fe90e75d
import os
from aiohttp import web
import ujson
import aiojobs
from pymongo.errors import DuplicateKeyError
from distutils.util import strtobool
import prozorro_sale
from prozorro_sale import tools
......@@ -25,7 +27,7 @@ async def version(request):
Returns:
aiohttp.web.Response: Response object.
"""
return web.json_response({'api_version': prozorro_sale.version})
return web.json_response({'api_version': prozorro_sale.version}, dumps=ujson.dumps)
async def ping(request):
......@@ -38,7 +40,7 @@ async def ping(request):
Returns:
aiohttp.web.Response: Response object.
"""
return web.json_response({'text': 'pong'})
return web.json_response({'text': 'pong'}, dumps=ujson.dumps)
async def create_auction(request):
......@@ -64,10 +66,10 @@ async def create_auction(request):
else:
auction = await db.read_auction(procedure['auctionId'])
auction_id = auction.id
return web.json_response({'auction_id': auction_id}, status=409)
return web.json_response({'auction_id': auction_id}, status=409, dumps=ujson.dumps)
finally:
AUCTION_ID.set(auction_id)
return web.json_response({'auction_id': auction_id}, status=201)
return web.json_response({'auction_id': auction_id}, status=201, dumps=ujson.dumps)
@utils.expects({errors.ForbiddenStateError: (410, messages.CANNOT_POST_BID)})
......@@ -89,8 +91,8 @@ async def add_bid(request):
auction.add_bid(bid)
except errors.ApiValueError as ex:
LOG.error(str(ex))
return web.json_response({'message': str(ex)}, status=409)
return web.json_response({'bidder_id': bid['id']}, status=201)
return web.json_response({'message': str(ex)}, status=409, dumps=ujson.dumps)
return web.json_response({'bidder_id': bid['id']}, status=201, dumps=ujson.dumps)
@utils.expects({errors.ForbiddenStateError: (409, messages.CANNOT_CANCEL_AUCTION)})
......@@ -108,7 +110,7 @@ async def cancel_auction(request):
auction_id = request.match_info['auction_id']
async with db.read_and_update(auction_id) as auction:
auction.cancel_auction()
return web.json_response({'auction_id': auction_id}, status=200)
return web.json_response({'auction_id': auction_id}, status=200, dumps=ujson.dumps)
@utils.expects({
......@@ -128,7 +130,7 @@ async def pause_auction(request):
auction_id = request.match_info['auction_id']
async with db.read_and_update(auction_id) as auction:
auction.pause_auction()
return web.json_response({'auction_id': auction_id}, status=200)
return web.json_response({'auction_id': auction_id}, status=200, dumps=ujson.dumps)
async def protect_auction(request):
......@@ -146,7 +148,24 @@ async def protect_auction(request):
auction_id = request.match_info['auction_id']
async with db.read_and_update(auction_id) as auction:
auction._protected = data.get("protect", True)
return web.json_response({'auction_id': auction_id}, status=200)
return web.json_response({'auction_id': auction_id}, status=200, dumps=ujson.dumps)
async def get_auction_by_id(request):
"""
Get auction by provided id.
Args:
request (object): Request.
Returns:
aiohttp.web.Response: Response object.
"""
auction_id = request.match_info['auction_id']
protected = strtobool(request.query.get('protect', "1"))
_auction = await db.read_auction(auction_id)
return web.json_response(_auction.to_primitive(role='public', app_data={"protect": protected}),
status=200, dumps=ujson.dumps)
async def on_shutdown(app):
......@@ -179,6 +198,7 @@ def configure_routes(app):
"""
app.router.add_get('/api', version)
app.router.add_get('/api/ping', ping)
app.router.add_get('/api/auctions/{auction_id}', get_auction_by_id)
app.router.add_post('/api/auctions/{auction_type}', create_auction)
app.router.add_post('/api/auctions/{auction_id}/bid', add_bid)
app.router.add_put('/api/auctions/{auction_id}/cancel', cancel_auction)
......
......@@ -341,7 +341,7 @@ class Auction(state_machine.StateMachine, Model):
if role == 'bidder' and self._auction.bidsInfo[context.bidder_id]._tempBid:
app_data['temp_bid'] = self._auction.bidsInfo[context.bidder_id]._tempBid
primitive_data = super().to_primitive(role, app_data, **kwargs)
if role == 'public' and hasattr(self, 'protect_config') and self.protect_config:
if role == 'public' and getattr(self, 'protect_config', None) and app_data.get("protect", True):
return self.protect_config.protect(primitive_data)
return primitive_data
......
......@@ -3,7 +3,7 @@ import bson
from prozorro_sale.auction import db, databridge
from test.utils import AUCTIONS_URL
from test.utils import AUCTIONS_URL, set_auction_state
class TestDatabridge:
......@@ -191,3 +191,26 @@ class TestDatabridge:
resp = await client.put(f'{AUCTIONS_URL}/{auction.id}/cancel',
json=procedure_fixture)
assert resp.status == 200
async def test_get_auction_by_id(self, client, procedure_fixture):
response = await client.post(f'{AUCTIONS_URL}/english', json=procedure_fixture)
assert response.status == 201
auction = await db.read_auction_by_procedure_object_id(procedure_fixture['_id'])
await set_auction_state(auction.id, 'done')
resp = await client.put(f'{AUCTIONS_URL}/{auction.id}/protect',
json={'protect': True})
assert resp.status == 200
resp = await client.get(f'{AUCTIONS_URL}/{auction.id}')
assert resp.status == 200
data = await resp.json()
for bid_id, bid_info in data.get('bidsInfo').items():
for bidder in bid_info.get('bidders'):
assert '*****' in bidder['name']['uk_UA']
resp = await client.get(f'{AUCTIONS_URL}/{auction.id}?protect=false')
assert resp.status == 200
data = await resp.json()
for bid_id, bid_info in data.get('bidsInfo').items():
for bidder in bid_info.get('bidders'):
assert '*****' not in bidder['name']['uk_UA']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment