Browse Source

normalize errors, and use a global var for board manager...

I cannot figure out how to make this work w/ dep injection
due to needing to use asyncio to do DB accesses...  This will
work reliably, and is easy to understand...
main
John-Mark Gurney 4 years ago
parent
commit
66757740c6
1 changed files with 27 additions and 8 deletions
  1. +27
    -8
      bitelab/__init__.py

+ 27
- 8
bitelab/__init__.py View File

@@ -198,12 +198,31 @@ def get_data(settings: config.Settings = Depends(get_settings)):
d = make_orm(database)
return d

@unhashable_lru()
def sync_get_boardmanager(settings):
return BoardManager(settings)
async def real_get_boardmanager(settings, data):
brdmgr = BoardManager(settings)

# Clean up the database
# XXX - This isn't a complete fix, we need a better solution.
all = await data.BoardStatus.objects.all()
await asyncio.gather(*(x.delete() for x in all))

return brdmgr

_global_lock = asyncio.Lock()
_global_brdmgr = None

async def get_boardmanager(settings: config.Settings = Depends(get_settings),
data: data.DataWrapper = Depends(get_data)):
global _global_brdmgr

async def get_boardmanager(settings: config.Settings = Depends(get_settings)):
return sync_get_boardmanager(settings)
if _global_brdmgr is not None:
return _global_brdmgr

async with _global_lock:
if _global_brdmgr is None:
_global_brdmgr = await real_get_boardmanager(settings, data)

return _global_brdmgr

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/nonexistent')

@@ -231,7 +250,7 @@ async def validate_board_params(board_id, token, data, brdmgr):
brduser = await data.BoardStatus.objects.get(board=board_id)
except orm.exceptions.NoMatch:
raise BITEError(
status_code=HTTP_403_FORBIDDEN,
status_code=HTTP_400_BAD_REQUEST,
errobj=Error(error='Board not reserved.',
board=Board.from_orm(brd)))

@@ -750,8 +769,8 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
auth=BiteAuth('thisisanapikey'),
json=attrs)

# that it is forbidden
self.assertEqual(res.status_code, HTTP_403_FORBIDDEN)
# that it is a bad request
self.assertEqual(res.status_code, HTTP_400_BAD_REQUEST)

# that the cora-1 board is reserved
brd = self.brdmgr.boards['cora-1']


Loading…
Cancel
Save