Browse Source

Исправлена работа с документами

auth_koop
Alexei 1 year ago
parent
commit
2ada0a2508
  1. BIN
      auth_docs/test_doc.odt
  2. 20
      bot_modules/authorize.py
  3. 4
      bot_modules/mod_table_operate.py
  4. 25
      template/docs_message.py

BIN
auth_docs/test_doc.odt

Binary file not shown.

20
bot_modules/authorize.py

@ -5,7 +5,7 @@
from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes, config
from bot_modules import mod_table_operate, mod_simple_message
from template import docs_message, bd_item
from template import docs_message, bd_item, bd_item_select
from enum import Enum
from enum import auto
@ -53,6 +53,7 @@ init_access = f'{user_access.user_access_group_new}=va'
def GetAuthorizeItem(a_Bot, a_UserID):
items = bd_item.GetBDItemsTemplate(a_Bot, table_name, user_id_field)(a_UserID)
print('GetAuthorizeItem', items)
if len(items) == 1:
return items[0]
return None
@ -274,7 +275,7 @@ class ModuleAuthorize(mod_table_operate.TableOperateModule):
assert fam_name_field_id != None
assert access_field_id != None
return \
a_Item[name_field_id] + ' ' + a_Item[fam_name_field_id] + '(' + a_Item[key_name_id] +')',\
a_Item[name_field_id] + ' ' + a_Item[fam_name_field_id] + '(' + str(a_Item[key_name_id]) +')',\
a_Item[key_name_id],\
a_Item[access_field_id]
@ -283,8 +284,9 @@ class ModuleAuthorize(mod_table_operate.TableOperateModule):
user_id = str(a_Message.from_user.id)
item = GetAuthorizeItem(self.m_Bot, user_id)
user_confirm_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_CONFIRM)
print('GetStartKeyboardButtons', item, user_confirm_field_id)
cur_buttons = []
if item and item[user_confirm_field_id] == str(ConfirmStatus.YES):
if item and item[user_confirm_field_id] == self.GetMessage(mod_table_operate.EnumMessageForView(ConfirmStatus.YES)).GetDesc():
cur_buttons += [
keyboard.ButtonWithAccess(self.GetButton(ButtonNames.LIST_AUTH_DOCS), user_access.AccessMode.VIEW, self.GetAccess()),
]
@ -298,7 +300,8 @@ class ModuleAuthorize(mod_table_operate.TableOperateModule):
cur_dict = GetReplaceDictFunc(self.m_Bot, a_user_id)
result = {}
for f in files:
result.update({f, cur_dict})
result.update({f: cur_dict})
print(cur_dict)
return result
button_name = self.GetButton(ButtonNames.LIST_AUTH_DOCS)
@ -323,18 +326,21 @@ class ModuleAuthorize(mod_table_operate.TableOperateModule):
return super().AddBDItemFunc(a_ItemData, a_UserID)
def GetReplaceDictFunc(a_Bot, a_user_id):
item = GetAuthorizeItem(self.m_Bot, user_id)
item = GetAuthorizeItem(a_Bot, a_user_id)
if not item:
return None
result = {}
i = 0
for f in table:
result.update({str(f.m_Destiny), item[i]})
for f in table.GetFields():
result.update({str(f.m_Destiny): item[i]})
i += 1
print(result)
return result
auth_docs_file_name = 'config_auth_docs'
g_auth_docs = []
def GetAuthDocs():
global g_auth_docs
if len(g_auth_docs) == 0:

4
bot_modules/mod_table_operate.py

@ -406,7 +406,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
)
def GetAddFields(self):
good_destiny = (
add_destiny = (
bd_table.TableFieldDestiny.NAME,
bd_table.TableFieldDestiny.DESC,
bd_table.TableFieldDestiny.PHOTO,
@ -422,7 +422,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
)
fields = []
for f in self.m_Table.GetFields():
if f.m_Destiny in good_destiny:
if f.m_Destiny in add_destiny:
fields += [f]
return fields

25
template/docs_message.py

@ -6,7 +6,7 @@
from bot_sys import log, config, user_access
from bot_modules import groups_utils
from template import simple_message
import odf
#import odf
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT):
async def DocFiles(a_Message):
@ -23,11 +23,11 @@ def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetBu
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
return
for file_path, dict_replace in files.values():
for file_path, dict_replace in files.items():
if not dict_replace:
continue
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id)
document = await GetFile(a_Bot, file_path)
document = await GetFile(a_Bot, new_file)
if document is None:
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups)
else:
@ -40,20 +40,25 @@ def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetBu
)
return DocFiles
from odf import opendocument, text, teletype
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
document = odf.opendocument.load(a_FilePath)
print ('MakeDocFile', a_FilePath)
#FixBadZipfile(a_FilePath)
document = opendocument.load(a_FilePath) # odf.opendocuement.
if document == None:
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.')
return None
for element in document.getElementsByType(text.Span):
extracted_text = odf.teletype.extractText(element)
extracted_text = teletype.extractText(element)
print('MakeDocFile', extracted_text)
for file_path, dict_replace in a_DictReplace.values():
if extracted_text.find('Replace this') != -1:
extracted_text = extracted_text.replace('Replace this', 'to this')
for rep_this, to_this in a_DictReplace.items():
if extracted_text.find(rep_this) != -1:
extracted_text = extracted_text.replace(rep_this, to_this)
new_element = odf.text.Span()
new_element = text.Span()
new_element.setAttribute('stylename', element.getAttribute('stylename'))
new_element.addText(extracted_text)
@ -61,7 +66,7 @@ async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
element.parentNode.removeChild(element)
new_file_path = a_FilePath[:-4]
new_file_path += f"{a_user_id}.odt"
new_file_path += f"_{a_user_id}.odt"
document.save(new_file_path)
return new_file_path

Loading…
Cancel
Save