current_principal = fastapi.Depends(auth.get_current_principal)) -> uploads.Upload:
# Create a new upload
try:
- async with backend.db:
+ async with backend.db as session:
upload = await backend.uploads.create(
filename = request.filename,
size = request.size,
# Stream the payload
try:
- async with backend.db:
+ async with backend.db as session:
async with upload.stream() as f:
async for chunk in request.stream():
await f.write(chunk)
@router.delete("/{id}")
async def delete(upload: uploads.Upload = fastapi.Depends(get_upload)):
# Delete the upload
- async with backend.db():
+ async with backend.db() as session:
await upload.delete()
# Send 204
#
# It can be used as follows:
#
- # async with backend.db:
+ # async with backend.db as session:
# ...
async def __aenter__(self):
return await self.session()
async def __aexit__(self, type, exception, traceback):
- # This method will be called when the block is being excited and it will
- # release the database session. Usually that means that there will be a commit.
- task = asyncio.current_task()
-
- # Immediately release the session
- await self.__release_session(task)
+ # If we leave the block, we automatically commit, but we keep the session
+ # alive so that any objects that we are using won't expire and require refresh.
+ if exception is None:
+ await self.commit()
async def session(self):
"""