Hello,
I migrated to the latest version of SeaTable self-hosted enterprise edition, version 5.
Since then, my Python scripts no longer work.
Is it because of the API update?
Thank you in advance.
My Setup:
- SeaTable Edition: Enterprise
- SeaTable Version: 5.0.7
Description of the Problem:
I migrated to the latest version of SeaTable self-hosted enterprise edition, version 5.
Since then, my Python scripts no longer work.
Is it because of the API update?
Error Messages:
Here is my Python script that sends an email with data from certain columns of my table:
import smtplib
from urllib.parse import unquote
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
from seatable_api import Base, context
import os
import markdown
import base64
import re
# Initializing the SeaTable API
base = Base(context.api_token, context.server_url)
base.auth()
# Retrieving the active row
active_row = context.current_row
if not active_row:
print("No active row detected.")
exit()
# Email parameters
sender = "Client.VPN@gifi.fr"
subject = active_row.get('Sujet', 'Default Subject')
# Email recipient
receiver_email = active_row.get('Mail')
if not receiver_email:
print("Email address missing in the active row.")
exit()
# Preparing the message
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8').encode()
msg['From'] = sender
msg['To'] = receiver_email
# Email body
markdown_content = active_row.get('Contenue Mail', 'Default content if not found')
html_content = markdown.markdown(markdown_content)
# Function to download and encode images in base64
def download_and_encode_image(url):
save_path = "/tmp/" + unquote(url.split('/')[-1])
base.download_file(url, save_path)
with open(save_path, "rb") as img_file:
b64_string = base64.b64encode(img_file.read()).decode('utf-8')
os.remove(save_path)
return b64_string
# Replacing image URLs with base64 encoded images in the HTML content
image_urls = re.findall(r'!\[.*?\]\((.*?)\)', markdown_content)
for url in image_urls:
b64_string = download_and_encode_image(url)
html_content = html_content.replace(url, f"data:image/png;base64,{b64_string}")
msg.attach(MIMEText(html_content, 'html', 'utf-8'))
# Creating the attachment from the "Config VPN" column
config_vpn_content = active_row.get('Config VPN', '')
user = active_row.get('User', 'unknown')
attachment_filename = f"ClientVPN_{user}.vpn"
attachment_path = f"/tmp/{attachment_filename}"
with open(attachment_path, 'w', encoding='utf-8') as f:
f.write(config_vpn_content)
with open(attachment_path, 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=attachment_filename)
msg.attach(attachment)
os.remove(attachment_path)
# Sending the email and updating the columns based on the result
try:
with smtplib.SMTP('myservermailhost', 25) as server:
server.sendmail(sender, receiver_email, msg.as_string())
base.update_row(context.current_table, active_row['_id'], {"Envoi email": True, "Logs envoie de mail": "Email sent successfully."})
print("Email sent successfully.")
except Exception as e:
log_message = f"Error sending email: {str(e)}"
base.update_row(context.current_table, active_row['_id'], {"Envoi email": False, "Logs envoie de mail": log_message})
print(log_message)
Here are the logs:
Traceback (most recent call last):
File "/scripts/index.py", line 80, in <module>
base.update_row(context.current_table, active_row['_id'], {"Envoi email": True, "Logs envoie de mail": "Email sent successfully."})
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 26, in wrapper
return func(obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 33, in wrapper
return getattr(new_obj, func.__name__)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/api_gateway.py", line 305, in update_row
return parse_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/utils.py", line 213, in parse_response
raise ConnectionError(response.status_code, response.text)
ConnectionError: [Errno 502] <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/scripts/index.py", line 84, in <module>
base.update_row(context.current_table, active_row['_id'], {"Envoi email": False, "Logs envoie de mail": log_message})
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 26, in wrapper
return func(obj, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/main.py", line 33, in wrapper
return getattr(new_obj, func.__name__)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/api_gateway.py", line 305, in update_row
return parse_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/lib/python3.11/site-packages/seatable_api/utils.py", line 213, in parse_response
raise ConnectionError(response.status_code, response.text)
ConnectionError: [Errno 502] <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>
This script works on version 4.