premiere iteration de la gestion des documents
CI - Security, Lint & Tests / validate (push) Failing after 1m20s
CI - Security, Lint & Tests / validate (push) Failing after 1m20s
This commit is contained in:
+68
-3
@@ -28,6 +28,59 @@ def _setting(name):
|
||||
return value
|
||||
|
||||
|
||||
def root_folder_id():
|
||||
"""The configured Drive folder that holds every application document.
|
||||
|
||||
This is the folder the owner set up (the ``signés`` folder); new
|
||||
documents land directly in it and signed copies are filed in nested
|
||||
folders below it.
|
||||
"""
|
||||
return _setting(GOOGLE_DRIVE_FOLDER_ID_ENV)
|
||||
|
||||
|
||||
def get_or_create_folder(name, parent_id):
|
||||
"""Return the Drive folder named *name* under *parent_id*, creating it.
|
||||
|
||||
Drive has no ``mkdir -p``; creating a folder is a write, so a signed
|
||||
copy filed under ``<team>/<role>/<username>`` walks the chain once per
|
||||
upload. A folder already present is reused rather than duplicated.
|
||||
|
||||
Args:
|
||||
name: Folder name to find or create.
|
||||
parent_id: Drive id of the parent folder.
|
||||
|
||||
Returns:
|
||||
str: The Drive id of the folder.
|
||||
"""
|
||||
try:
|
||||
service = _drive_service()
|
||||
query = (
|
||||
f"name='{name}' and mimeType='application/vnd.google-apps.folder' "
|
||||
f"and '{parent_id}' in parents and trashed=false"
|
||||
)
|
||||
response = service.files().list(q=query, fields='files(id, name)', pageSize=10).execute()
|
||||
existing = response.get('files', [])
|
||||
if existing:
|
||||
return existing[0]['id']
|
||||
folder = (
|
||||
service.files()
|
||||
.create(
|
||||
body={
|
||||
'name': name,
|
||||
'mimeType': 'application/vnd.google-apps.folder',
|
||||
'parents': [parent_id],
|
||||
},
|
||||
fields='id',
|
||||
)
|
||||
.execute()
|
||||
)
|
||||
return folder['id']
|
||||
except GoogleDriveStorageError:
|
||||
raise
|
||||
except Exception as exc: # Google client exceptions share no stable base class.
|
||||
raise GoogleDriveStorageError('Google Drive could not create the document folder.') from exc
|
||||
|
||||
|
||||
def _drive_service():
|
||||
"""Build an authorized Drive client from the owner's refresh token."""
|
||||
try:
|
||||
@@ -86,8 +139,17 @@ def list_folder_files():
|
||||
raise GoogleDriveStorageError('Google Drive could not list folder contents.') from exc
|
||||
|
||||
|
||||
def upload_file(*, stream, filename, mimetype):
|
||||
"""Upload a PDF to the configured owner folder and return its Drive ID."""
|
||||
def upload_file(*, stream, filename, mimetype, parents=None):
|
||||
"""Upload a PDF to the configured owner folder and return its Drive ID.
|
||||
|
||||
Args:
|
||||
stream: The binary stream to upload.
|
||||
filename: Name used for the uploaded file.
|
||||
mimetype: MIME type of the file.
|
||||
parents: Drive ids of the parent folders. Defaults to the root
|
||||
folder when omitted, which is how the legacy contract flow
|
||||
still works.
|
||||
"""
|
||||
try:
|
||||
from googleapiclient.http import MediaIoBaseUpload
|
||||
|
||||
@@ -97,7 +159,10 @@ def upload_file(*, stream, filename, mimetype):
|
||||
_drive_service()
|
||||
.files()
|
||||
.create(
|
||||
body={'name': filename, 'parents': [_setting(GOOGLE_DRIVE_FOLDER_ID_ENV)]},
|
||||
body={
|
||||
'name': filename,
|
||||
'parents': parents or [root_folder_id()],
|
||||
},
|
||||
media_body=media,
|
||||
fields='id',
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user