Skip to content

Commit

Permalink
ex-props support folder (#296)
Browse files Browse the repository at this point in the history
* ex-props support folder

* opt change dir ex props for sub dirs/files
  • Loading branch information
AlexCXC authored Aug 30, 2023
1 parent 48b80dd commit 2b572b3
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 55 deletions.
120 changes: 120 additions & 0 deletions events/change_extended_props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import os
import logging

from seafevents.app.config import DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, EX_PROPS_TABLE
from seafevents.utils.seatable_api import SeaTableAPI

logger = logging.getLogger(__name__)


class ChangeExtendedPropsHandler:

def __init__(self):
self.need_change_ex_props = all((DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, EX_PROPS_TABLE))

def change_file_ex_props(self, repo_id, path, new_path):
if not self.need_change_ex_props:
return
try:
self._change_file_ex_props(repo_id, path, new_path)
except Exception as e:
logger.warning('change file ex-props repo: %s path: %s new_path: %s error: %s', repo_id, path, new_path, e)

def _change_file_ex_props(self, repo_id, path, new_path):
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return
file_name = os.path.basename(new_path)
sql = f"UPDATE `{EX_PROPS_TABLE}` SET `Path`='{new_path}', `File`='{file_name}' WHERE `Repo ID`='{repo_id}' AND `Path`='{path}'"
logger.info('sql: %s', sql)
try:
seatable_api.query(sql)
except Exception as e:
logger.error('update ex-props repo: %s path: %s new_path: %s', repo_id, path, new_path)

def delete_file_ex_props(self, repo_id, path):
if not self.need_change_ex_props:
return
try:
self._delete_file_ex_props(repo_id, path)
except Exception as e:
logger.warning('delete file ex-props repo: %s path: %s error: %s', repo_id, path, e)

def _delete_file_ex_props(self, repo_id, path):
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return
sql = f"DELETE FROM `{EX_PROPS_TABLE}` WHERE `Repo ID`='{repo_id}' AND `Path`='{path}'"
logger.info('sql: %s', sql)
try:
seatable_api.query(sql)
except Exception as e:
logger.error('update ex-props repo: %s path: %s', repo_id, path)

def change_dir_ex_props(self, repo_id, path, new_path):
if not self.need_change_ex_props:
return
try:
self._change_dir_ex_props(repo_id, path, new_path)
except Exception as e:
logger.warning('change dir ex-props repo: %s path: %s new_path: %s error: %s', repo_id, path, new_path, e)

def _change_dir_ex_props(self, repo_id, path, new_path):
self._change_file_ex_props(repo_id, path, new_path)
sql_temp = f"SELECT `_id`, `Path` FROM `{EX_PROPS_TABLE}` WHERE `Repo ID`='{repo_id}' AND `Path` LIKE '{path}/%'"
path_column = None
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
step = 1000
metadata = None
while True:
sql = sql_temp + f' LIMIT {step}'
resp_json = seatable_api.query(sql)
results = resp_json['results']
metadata = resp_json['metadata']
if not path_column:
for column in metadata:
if column['name'] == 'Path':
path_column = column
break
if not path_column: # It usually doesn't run here
logger.error('No Path column found!')
return
for row in results:
row_id, row_path = row['_id'], row[path_column['key']]
new_row_path = row_path.replace(path, new_path, 1)
update_sql = f"UPDATE `{EX_PROPS_TABLE}` SET `Path`='{new_row_path}' WHERE `_id`='{row_id}'"
logger.info('update_sql: %s', update_sql)
seatable_api.query(update_sql)
if len(results) < step:
break
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return

def delete_dir_ex_props(self, repo_id, path):
if not self.need_change_ex_props:
return
try:
self._delete_dir_ex_props(repo_id, path)
except Exception as e:
logger.warning('delete dir ex-props repo: %s path: %s error: %s', repo_id, path, e)

def _delete_dir_ex_props(self, repo_id, path):
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return
sql = f"DELETE FROM `{EX_PROPS_TABLE}` WHERE `Repo ID`='{repo_id}' AND `Path`='{path}'"
sub_items_sql = f"DELETE FROM `{EX_PROPS_TABLE}` WHERE `Repo ID`='{repo_id}' AND `Path` LIKE '{path}/%'"
logger.info('sql: %s sub_items_sql: %s', sql, sub_items_sql)
try:
seatable_api.query(sql)
seatable_api.query(sub_items_sql)
except Exception as e:
logger.error('update ex-props repo: %s path: %s', repo_id, path)
51 changes: 0 additions & 51 deletions events/change_file_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

from sqlalchemy.sql import text

from seafevents.app.config import DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, EX_PROPS_TABLE
from seafevents.utils.seatable_api import SeaTableAPI


logger = logging.getLogger(__name__)


class ChangeFilePathHandler(object):
def __init__(self, session):
self.session = session
self.need_change_ex_props = all((DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, EX_PROPS_TABLE))

def update_db_records(self, dst_repo_id, path, new_path, is_dir, src_repo_id=None):
if not dst_repo_id or not path or not new_path:
Expand Down Expand Up @@ -164,50 +160,3 @@ def _change_upload_share_file_path(self, repo_id, path, new_path, is_dir, src_re
{'new_repo_id': repo_id, 'new_path': new_path_value,
'repo_id': src_repo_id if src_repo_id else repo_id, 'path': row[0]})
self.session.commit()

def change_file_ex_props(self, repo_id, path, new_path):
if not self.need_change_ex_props:
return
try:
self._change_file_ex_props(repo_id, path, new_path)
except Exception as e:
logger.warning('change file ex-props repo: %s path: %s new_path: %s error: %s', repo_id, path, new_path, e)

def _change_file_ex_props(self, repo_id, path, new_path):
if not self.need_change_ex_props:
return
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return
file_name = os.path.basename(new_path)
sql = f"UPDATE `{EX_PROPS_TABLE}` SET `Path`='{new_path}', `File`='{file_name}' WHERE `Repo ID`='{repo_id}' AND `Path`='{path}'"
logger.info('sql: %s', sql)
try:
seatable_api.query(sql)
except Exception as e:
logger.error('update ex-props repo: %s path: %s new_path: %s', repo_id, path, new_path)

def delete_file_ex_props(self, repo_id, path):
if not self.need_change_ex_props:
return
try:
self._delete_file_ex_props(repo_id, path)
except Exception as e:
logger.warning('delete file ex-props repo: %s path: %s error: %s', repo_id, path, e)

def _delete_file_ex_props(self, repo_id, path):
if not self.need_change_ex_props:
return
try:
seatable_api = SeaTableAPI(SEATABLE_EX_PROPS_BASE_API_TOKEN, DTABLE_WEB_SERVER)
except Exception as e:
logger.error('DTABLE_WEB_SERVER: %s, SEATABLE_EX_PROPS_BASE_API_TOKEN: %s auth error: %s', DTABLE_WEB_SERVER, SEATABLE_EX_PROPS_BASE_API_TOKEN, e)
return
sql = f"DELETE FROM `{EX_PROPS_TABLE}` WHERE `Repo ID`='{repo_id}' AND `Path`='{path}'"
logger.info('sql: %s', sql)
try:
seatable_api.query(sql)
except Exception as e:
logger.error('update ex-props repo: %s path: %s', repo_id, path)
14 changes: 10 additions & 4 deletions events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from seafevents.app.config import TIME_ZONE
from seafevents.utils import get_opt_from_conf_or_env
from .change_file_path import ChangeFilePathHandler
from .change_extended_props import ChangeExtendedPropsHandler
from .models import Activity
from seafevents.batch_delete_files_notice.utils import get_deleted_files_count, save_deleted_files_msg
from seafevents.batch_delete_files_notice.db import get_deleted_files_total_count, save_deleted_files_count
Expand Down Expand Up @@ -53,20 +54,25 @@ def RepoUpdateEventHandler(config, session, msg):
added_files, deleted_files, added_dirs, deleted_dirs, modified_files,\
renamed_files, moved_files, renamed_dirs, moved_dirs = differ.diff()

if renamed_files or renamed_dirs or moved_files or moved_dirs or deleted_files:
if renamed_files or renamed_dirs or moved_files or moved_dirs or deleted_files or deleted_dirs:
changer = ChangeFilePathHandler(session)
ex_props_changer = ChangeExtendedPropsHandler()
for r_file in renamed_files:
changer.update_db_records(repo_id, r_file.path, r_file.new_path, 0)
changer.change_file_ex_props(repo_id, r_file.path, r_file.new_path)
ex_props_changer.change_file_ex_props(repo_id, r_file.path, r_file.new_path)
for r_dir in renamed_dirs:
changer.update_db_records(repo_id, r_dir.path, r_dir.new_path, 1)
ex_props_changer.change_dir_ex_props(repo_id, r_dir.path, r_dir.new_path)
for m_file in moved_files:
changer.update_db_records(repo_id, m_file.path, m_file.new_path, 0)
changer.change_file_ex_props(repo_id, m_file.path, m_file.new_path)
ex_props_changer.change_file_ex_props(repo_id, m_file.path, m_file.new_path)
for m_dir in moved_dirs:
changer.update_db_records(repo_id, m_dir.path, m_dir.new_path, 1)
ex_props_changer.change_dir_ex_props(repo_id, m_dir.path, m_dir.new_path)
for d_file in deleted_files:
changer.delete_file_ex_props(repo_id, d_file.path)
ex_props_changer.delete_file_ex_props(repo_id, d_file.path)
for d_dir in deleted_dirs:
ex_props_changer.delete_dir_ex_props(repo_id, d_dir.path)

users = []
org_id = get_org_id_by_repo_id(repo_id)
Expand Down

0 comments on commit 2b572b3

Please sign in to comment.