- finished add_document on Zapier interface

This commit is contained in:
Josako
2024-12-13 10:40:57 +01:00
parent 46c60b36a0
commit 3176b95323
2 changed files with 29 additions and 18 deletions

View File

@@ -69,7 +69,7 @@ class AddDocument(Resource):
@document_ns.response(500, 'Internal Server Error') @document_ns.response(500, 'Internal Server Error')
def post(self): def post(self):
""" """
Add a new document Add a new document by providing the content of a file (Multipart/form-data).
""" """
tenant_id = get_jwt_identity() tenant_id = get_jwt_identity()
current_app.logger.info(f'Adding document for tenant {tenant_id}') current_app.logger.info(f'Adding document for tenant {tenant_id}')
@@ -120,7 +120,7 @@ class AddDocument(Resource):
add_document_through_url = document_ns.model('AddDocumentThroughURL', { add_document_through_url = document_ns.model('AddDocumentThroughURL', {
'catalog_id': fields.Integer(required=True, description='ID of the catalog the URL needs to be added to'), 'catalog_id': fields.Integer(required=True, description='ID of the catalog the URL needs to be added to'),
'temp_url': fields.String(required=True, description='Temporary URL of the document to add'), 'temp_url': fields.String(required=True, description='Temporary URL of the document to add'),
'name': fields.String(required=False, description='Name of the document'), 'name': fields.String(required=True, description='Name of the document'),
'language': fields.String(required=True, description='Language of the document'), 'language': fields.String(required=True, description='Language of the document'),
'user_context': fields.String(required=False, description='User context for the document'), 'user_context': fields.String(required=False, description='User context for the document'),
'valid_from': fields.String(required=False, description='Valid from date for the document'), 'valid_from': fields.String(required=False, description='Valid from date for the document'),
@@ -163,18 +163,26 @@ class AddDocumentThroughURL(Resource):
current_app.logger.error(f"Exception type: {type(e)}") current_app.logger.error(f"Exception type: {type(e)}")
raise raise
file_url = args['temp_url']
current_app.logger.info(f"Downloading file from URL: {file_url}")
try: try:
response = requests.get(file_url, stream=True) # Step 1: Download from stashed URL
stashed_url = args['temp_url']
current_app.logger.info(f"Downloading stashed file from URL: {stashed_url}")
response = requests.get(stashed_url, stream=True)
response.raise_for_status() response.raise_for_status()
hydration_url = response.text.strip()
current_app.logger.info(f"Downloading actual file from URL: {hydration_url}")
# Step 2: Download from hydration URL
actual_file_response = requests.get(hydration_url, stream=True)
actual_file_response.raise_for_status()
hydrated_file_content = actual_file_response.content
# Get filename from URL or use provided name # Get filename from URL or use provided name
filename = secure_filename(args.get('name') or file_url.split('/')[-1]) filename = secure_filename(args.get('name'))
extension = filename.rsplit('.', 1)[1].lower() if '.' in filename else '' extension = filename.rsplit('.', 1)[1].lower() if '.' in filename else ''
# Create FileStorage object from downloaded content # Create FileStorage object from downloaded content
file_content = io.BytesIO(response.content) file_content = io.BytesIO(hydrated_file_content)
file = FileStorage( file = FileStorage(
stream=file_content, stream=file_content,
filename=filename, filename=filename,
@@ -249,7 +257,8 @@ class AddURL(Resource):
@document_ns.response(500, 'Internal Server Error') @document_ns.response(500, 'Internal Server Error')
def post(self): def post(self):
""" """
Add a new document from URL Add a new document from URL. The URL in this case is stored and can be used to refresh the document.
As a consequence, this must be a permanent and accessible URL.
""" """
tenant_id = get_jwt_identity() tenant_id = get_jwt_identity()
current_app.logger.info(f'Adding document from URL for tenant {tenant_id}') current_app.logger.info(f'Adding document from URL for tenant {tenant_id}')
@@ -328,7 +337,7 @@ class DocumentResource(Resource):
@document_ns.response(404, 'Document not found') @document_ns.response(404, 'Document not found')
@document_ns.response(500, 'Internal Server Error') @document_ns.response(500, 'Internal Server Error')
def put(self, document_id): def put(self, document_id):
"""Edit a document""" """Edit a document. The content of the document will not be refreshed!"""
try: try:
current_app.logger.debug(f'Editing document {document_id}') current_app.logger.debug(f'Editing document {document_id}')
data = request.json data = request.json
@@ -347,7 +356,8 @@ class DocumentResource(Resource):
@document_ns.doc('refresh_document') @document_ns.doc('refresh_document')
@document_ns.response(200, 'Document refreshed successfully') @document_ns.response(200, 'Document refreshed successfully')
def post(self, document_id): def post(self, document_id):
"""Refresh a document""" """Refresh a document. In this case, the content of the document will be refreshed! This requires the document
version to have a permanent and accessible URL!"""
tenant_id = get_jwt_identity() tenant_id = get_jwt_identity()
new_version, result = refresh_document(document_id, tenant_id) new_version, result = refresh_document(document_id, tenant_id)
if new_version: if new_version:
@@ -398,7 +408,8 @@ class RefreshDocument(Resource):
@document_ns.response(404, 'Document not found') @document_ns.response(404, 'Document not found')
def post(self, document_id): def post(self, document_id):
""" """
Refresh a document without additional information Refresh a document without additional information. In this case, the content of the document will be refreshed!
This requires the document version to have a permanent and accessible URL!
""" """
tenant_id = get_jwt_identity() tenant_id = get_jwt_identity()
current_app.logger.info(f'Refreshing document {document_id} for tenant {tenant_id}') current_app.logger.info(f'Refreshing document {document_id} for tenant {tenant_id}')
@@ -431,7 +442,7 @@ class RefreshDocumentWithInfo(Resource):
@document_ns.response(404, 'Document not found') @document_ns.response(404, 'Document not found')
def post(self, document_id): def post(self, document_id):
""" """
Refresh a document with new information Refresh a document with new version information.
""" """
tenant_id = get_jwt_identity() tenant_id = get_jwt_identity()
current_app.logger.info(f'Refreshing document {document_id} with info for tenant {tenant_id}') current_app.logger.info(f'Refreshing document {document_id} with info for tenant {tenant_id}')

View File

@@ -35,7 +35,7 @@ module.exports = {
label: 'Document Name', label: 'Document Name',
type: 'string', type: 'string',
helpText: 'The name you want to give the Document.', helpText: 'The name you want to give the Document.',
required: false, required: true,
list: false, list: false,
altersDynamicFields: false, altersDynamicFields: false,
}, },
@@ -136,16 +136,16 @@ module.exports = {
Object.assign(baseMetadata, bundle.inputData.additional_metadata); Object.assign(baseMetadata, bundle.inputData.additional_metadata);
} }
// Get the file content // Get the file URL from Zapier
const filePromise = z.stashFile(bundle.inputData.file); const tempFileUrl = await z.stashFile(bundle.inputData.file);
const file = await filePromise; // Log the temporary URL for debugging
// const temp_url = z.stashFile(bundle.inputData.file); z.console.log('Temporary URL created:', tempFileUrl);
// Create request data as an object // Create request data as an object
const requestData = { const requestData = {
catalog_id: bundle.inputData.catalog_id, catalog_id: bundle.inputData.catalog_id,
language: bundle.inputData.language, language: bundle.inputData.language,
temp_url: file, // This will be handled by z.request automatically temp_url: tempFileUrl,
user_metadata: JSON.stringify(baseMetadata), user_metadata: JSON.stringify(baseMetadata),
}; };