Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: support oauth email account send email #699

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions dtable_events/automations/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,8 @@ def per_update_notify(self):
})
logger.debug('send_info: %s', send_info)
try:
sender = EmailSender(self.account_id, db_session=self.auto_rule.db_session)
sender.send(send_info, 'automation-rules')
sender = EmailSender(self.account_id, 'automation-rules', db_session=self.auto_rule.db_session)
sender.send(send_info)
except Exception as e:
logger.error('send email error: %s', e)

Expand All @@ -1371,8 +1371,8 @@ def cron_notify(self):
send_info['image_cid_url_map'] = self.image_cid_url_map
send_info.pop('message', None)
try:
sender = EmailSender(self.account_id, db_session=self.auto_rule.db_session)
sender.send(send_info, 'automation-rules')
sender = EmailSender(self.account_id, 'automation-rules', db_session=self.auto_rule.db_session)
sender.send(send_info)
except Exception as e:
logger.error('send email error: %s', e)

Expand Down Expand Up @@ -1433,8 +1433,8 @@ def condition_cron_notify(self):
step = 10
for i in range(0, len(send_info_list), step):
try:
sender = EmailSender(self.account_id, db_session=self.auto_rule.db_session)
sender.batch_send(send_info_list[i: i+step], 'automation-rules')
sender = EmailSender(self.account_id, 'automation-rules', db_session=self.auto_rule.db_session)
sender.batch_send(send_info_list[i: i+step])
except Exception as e:
logger.error('batch send email error: %s', e)

Expand Down Expand Up @@ -3220,8 +3220,8 @@ def send_email_cb(self, pdf_content):
'file_contents': {file_name: pdf_content}
}
try:
sender = EmailSender(account_id, conver_page_to_pdf_manager.config)
sender.send(send_info, 'automation-rules')
sender = EmailSender(account_id, 'automation-rules', conver_page_to_pdf_manager.config)
sender.send(send_info)
except Exception as e:
logger.exception('rule: %s dtable: %s page: %s send email: %s error: %s', self.auto_rule.rule_id, self.auto_rule.dtable_uuid, self.page_id, send_info, e)

Expand Down
4 changes: 2 additions & 2 deletions dtable_events/automations/general_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ def do_action_with_row(self, converted_row):
}
try:
send_info['message'] = self.generate_real_msg(self.msg, sql_row)
sender = EmailSender(self.account_id, db_session=self.context.db_session)
sender.send(send_info, self.send_from)
sender = EmailSender(self.account_id, self.send_from, db_session=self.context.db_session)
sender.send(send_info)
except Exception as e:
logger.exception(e)
logger.error('send email error: %s send_info: %s', e, send_info)
Expand Down
46 changes: 29 additions & 17 deletions dtable_events/automations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@

logger = logging.getLogger(__name__)

ENCRYPT_KEYS = ['password', 'webhook_url', 'api_key', 'secret_key', 'repo_api_token', 'client_secret']
def _encrypt_detail(detail):
detail_clone = deepcopy(detail)
cryptor = AESPasswordHasher()
try:
encrypted_details = {
key: cryptor.encode(detail_clone[key])
for key in ENCRYPT_KEYS if key in detail_clone and detail_clone[key]
}
detail_clone.update(encrypted_details)
return json.dumps(detail_clone)
except Exception as e:
logger.error(e)
return None

def _decrypt_detail(detail):
detail_clone = deepcopy(detail)
cryptor = AESPasswordHasher()
try:
if 'password' in detail_clone.keys():
password = detail_clone.get('password')
if password:
detail_clone.update({'password': cryptor.decode(password)})
if 'webhook_url' in detail.keys():
webhook_url = detail.get('webhook_url')
if webhook_url:
detail_clone.update({'webhook_url': cryptor.decode(webhook_url)})
if 'api_key' in detail.keys():
api_key = detail.get('api_key')
if api_key:
detail_clone.update({'api_key': cryptor.decode(api_key)})
if 'secret_key' in detail.keys():
secret_key = detail.get('secret_key')
if secret_key:
detail_clone.update({'secret_key': cryptor.decode(secret_key)})
decrypted_details = {
key: cryptor.decode(detail_clone[key])
for key in ENCRYPT_KEYS if key in detail_clone and detail_clone[key]
}
detail_clone.update(decrypted_details)
return detail_clone
except Exception as e:
logger.error(e)
return None


class BoundThirdPartyAccounts(Base):
__tablename__ = 'bound_third_party_accounts'

Expand Down Expand Up @@ -67,3 +69,13 @@ def get_third_party_account(session, account_id):
else:
logger.warning("Third party account %s does not exists." % account_id)
return None

def update_third_party_account_detail(session, account_id, new_detail):
stmt = select(BoundThirdPartyAccounts).where(BoundThirdPartyAccounts.id == account_id).limit(1)
account = session.scalars(stmt).first()
if account:
account.detail = _encrypt_detail(new_detail)
session.commit()
else:
logger.warning("Third party account %s does not exists." % account_id)
return None
4 changes: 2 additions & 2 deletions dtable_events/dtable_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ def plugin_email_send_email(context, config=None):
thread_table_name = table_info.get('thread_table_name')

# send email
sender = EmailSender(account_id, config)
result = sender.send(email_info, username)
sender = EmailSender(account_id, username, config)
result = sender.send(email_info)

if result.get('err_msg'):
dtable_plugin_email_logger.error('plugin email send failed, email account id: %s, username: %s', account_id, username)
Expand Down
2 changes: 0 additions & 2 deletions dtable_events/dtable_io/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,6 @@ def add_email_sending_task():
image_cid_url_map = data.get('image_cid_url_map', {})
if image_cid_url_map and not isinstance(image_cid_url_map, dict):
image_cid_url_map = json.loads(image_cid_url_map)

auth_type = data.get('auth_type')

account_id = data.get('account_id')

Expand Down
Loading