diff --git a/bot_modules/access.py b/bot_modules/access.py
index b413b54..a0c331d 100644
--- a/bot_modules/access.py
+++ b/bot_modules/access.py
@@ -4,7 +4,7 @@
# Права пользователей
from bot_sys import keyboard, user_access, bot_bd
-from bot_modules import mod_simple_message
+from bot_modules import mod_simple_message, access_utils
from template import simple_message, sql_request, bd_item
from aiogram.dispatcher import FSMContext
@@ -24,10 +24,10 @@ class FSMEditDefaultAccessItem(StatesGroup):
# БД
module_name = 'access'
-table_name = 'module_access'
-mod_name_field = 'modName'
-moduleaccess_field = 'modAccess'
-mod_default_access_field = 'itemDefaultAccess'
+table_name = access_utils.table_name
+mod_name_field = access_utils.mod_name_field
+moduleaccess_field = access_utils.moduleaccess_field
+mod_default_access_field = access_utils.mod_default_access_field
#TODO: Автоматическое создание init_bd_cmds, необходимо table_name, mod_name_field ... объединить в объект
@@ -109,14 +109,14 @@ class ModuleAccess(mod_simple_message.SimpleMessageModule):
)
def GetInitBDCommands(self):
- return super(). GetInitBDCommands() + [
+ return [
f"""CREATE TABLE IF NOT EXISTS {table_name}(
{mod_name_field} TEXT,
{moduleaccess_field} TEXT,
{mod_default_access_field} TEXT,
UNIQUE({mod_name_field})
);""",
- ]
+ ] + super().GetInitBDCommands()
def GetName(self):
return module_name
diff --git a/bot_modules/profile.py b/bot_modules/profile.py
index 5ed35e7..0a9c798 100644
--- a/bot_modules/profile.py
+++ b/bot_modules/profile.py
@@ -3,8 +3,8 @@
# Профиль пользователя
-from bot_sys import user_access, bot_bd
-from bot_modules import mod_simple_message, groups, access, access_utils
+from bot_sys import user_access, bot_bd, bd_table
+from bot_modules import mod_simple_message, groups, access, access_utils, groups_utils
from template import bd_item, simple_message
# ---------------------------------------------------------
@@ -21,6 +21,17 @@ language_code_field = 'userLanguageCode'
access_field = 'userAccess'
create_datetime_field = 'createDateTime'
+table = bd_table.Table(table_name, [
+ bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
+ bd_table.TableField(name_field, bd_table.TableFieldDestiny.NAME, bd_table.TableFieldType.STR),
+ bd_table.TableField(name1_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
+ bd_table.TableField(name2_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
+ bd_table.TableField(is_bot_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
+ bd_table.TableField(language_code_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
+ bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR),
+ bd_table.TableField(create_datetime_field, bd_table.TableFieldDestiny.CREATE_DATE, bd_table.TableFieldType.STR),
+ ])
+
start_message = f'''
📰 Профиль:
@@ -42,17 +53,7 @@ class ModuleProfile(mod_simple_message.SimpleMessageModule):
def GetInitBDCommands(self):
return super(). GetInitBDCommands() + [
- f"""CREATE TABLE IF NOT EXISTS {table_name}(
- {key_name} INTEGER,
- {name_field} TEXT,
- {name1_field} TEXT,
- {name2_field} TEXT,
- {is_bot_field} TEXT,
- {language_code_field} TEXT,
- {access_field} TEXT,
- {create_datetime_field} TEXT,
- UNIQUE({key_name})
- );""",
+ table.GetInitTableRequest(),
]
def GetName(self):
@@ -62,19 +63,11 @@ class ModuleProfile(mod_simple_message.SimpleMessageModule):
async def StartMessageHandler(self, a_Message, state = None):
user_info = GetUserInfo(self.m_Bot, a_Message.from_user.id)
lang = str(a_Message.from_user.language_code)
- msg = self.m_StartMessage
- msg = msg.GetMessageForLang(lang).StaticCopy()
if not user_info is None:
- msg.m_MessageDesc = msg.GetDesc().\
- replace(f'#{key_name}', str(user_info[0])).\
- replace(f'#{name_field}', str(user_info[1])).\
- replace(f'#{name1_field}', str(user_info[2])).\
- replace(f'#{name2_field}', str(user_info[3])).\
- replace(f'#{is_bot_field}', str(user_info[4])).\
- replace(f'#{language_code_field}', str(user_info[5])).\
- replace(f'#{access_field}', str(user_info[6])).\
- replace(f'#{create_datetime_field}', str(user_info[7]))
- return simple_message.WorkFuncResult(msg, item_access = str(user_info[6]))
+ msg = self.m_StartMessage
+ msg = msg.GetMessageForLang(lang).StaticCopy()
+ msg.m_MessageDesc = table.ReplaceAllFieldTags(msg.GetDesc(), user_info)
+ return simple_message.WorkFuncResult(msg, item_access = str(user_info[table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)]))
return await super().StartMessageHandler(a_Message, state)
@@ -84,8 +77,8 @@ class ModuleProfile(mod_simple_message.SimpleMessageModule):
def AddUser(a_Bot, a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot, a_LanguageCode):
a_Bot.SQLRequest(f"INSERT OR IGNORE INTO users ({key_name}, {name_field}, {name1_field}, {name2_field}, {is_bot_field}, {language_code_field}, {access_field}, {create_datetime_field}) VALUES (?, ?, ?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()});",
commit=True, param = (a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot, a_LanguageCode, access_utils.GetItemDefaultAccessForModule(a_Bot, module_name)))
-'''
- user_groups = access_utils.GetUserGroupData(a_Bot, a_UserID)
+
+ user_groups = groups_utils.GetUserGroupData(a_Bot, a_UserID)
# Если пользователь не состоит ни в одной группе, то добавляем его в группу user_access.user_access_group_new
if len(user_groups.group_names_list) == 0:
new_group_id = a_Bot.SQLRequest(f'SELECT {groups.key_table_groups_name} FROM {groups.table_groups_name} WHERE {groups.name_table_groups_field} = ?',
@@ -93,7 +86,7 @@ def AddUser(a_Bot, a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot,
if new_group_id and new_group_id[0]:
a_Bot.SQLRequest(f"INSERT OR IGNORE INTO {groups.table_user_in_groups_name} ({groups.user_id_field}, {groups.key_table_groups_name}, {groups.access_field}, {groups.create_datetime_field}) VALUES (?, ?, ?, {bot_bd.GetBDDateTimeNow()});",
commit=True, param = (a_UserID, new_group_id[0][0], access_utils.GetItemDefaultAccessForModule(a_Bot, module_name)))
-'''
+
def GetUserInfo(a_Bot, a_UserID):
user_info = a_Bot.SQLRequest('SELECT * FROM users WHERE user_id = ?', param = [a_UserID])
if len(user_info) != 0:
diff --git a/bot_modules/tasks.py b/bot_modules/tasks.py
index 386f472..b3ed5f8 100644
--- a/bot_modules/tasks.py
+++ b/bot_modules/tasks.py
@@ -3,7 +3,7 @@
# Задачи
-from bot_sys import bot_bd, log, keyboard, user_access
+from bot_sys import bot_bd, bd_table, keyboard, user_access
from bot_modules import start, access, groups, projects, needs, comments
from template import bd_item_view, simple_message, bd_item_delete, bd_item_edit, bd_item, bd_item_add, bd_item_select
@@ -47,6 +47,16 @@ access_field = 'taskAccess'
create_datetime_field = 'taskCreateDateTime'
parent_id_field = 'projectID'
+table = bd_table.Table(table_name, [
+ bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
+ bd_table.TableField(name_field, bd_table.TableFieldDestiny.NAME, bd_table.TableFieldType.STR),
+ bd_table.TableField(desc_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
+ bd_table.TableField(photo_field, bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldType.STR),
+ bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR),
+ bd_table.TableField(create_datetime_field, bd_table.TableFieldDestiny.CREATE_DATE, bd_table.TableFieldType.STR),
+ bd_table.TableField(parent_id_field, bd_table.TableFieldDestiny.PARENT_ID, bd_table.TableFieldType.INT),
+ ])
+
init_bd_cmds = [f'''CREATE TABLE IF NOT EXISTS {table_name}(
{key_name} INTEGER PRIMARY KEY,
{name_field} TEXT,
diff --git a/bot_sys/bd_table.py b/bot_sys/bd_table.py
new file mode 100644
index 0000000..280894e
--- /dev/null
+++ b/bot_sys/bd_table.py
@@ -0,0 +1,97 @@
+#-*-coding utf-8-*-
+# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov)
+
+from enum import Enum
+from enum import auto
+
+# Тип поля в таблице
+class TableFieldType(Enum):
+ INT = 'INTEGER'
+ STR = 'TEXT'
+
+# Предназначение поля в таблице
+class TableFieldDestiny(Enum):
+ KEY = auto()
+ NAME = auto()
+ DESC = auto()
+ PHOTO = auto()
+ ACCESS = auto()
+ CREATE_DATE = auto()
+ PARENT_ID = auto()
+ OTHER = auto()
+
+class TableField:
+ def __init__(self, a_Name, a_Destiny : TableFieldDestiny, a_Type : TableFieldType):
+ self.m_Name = a_Name
+ self.m_Destiny = a_Destiny
+ self.m_Type = a_Type
+
+class Table:
+ def __init__(self, a_TableName, a_Fields : [TableField]):
+ self.m_TableName = a_TableName
+ self.m_Fields = a_Fields
+
+ def GetName(self):
+ return self.m_TableName
+
+ def GetFields(self):
+ return self.TableFieldType
+
+ def GetFieldByDestiny(self, a_Destiny):
+ for f in self.m_Fields:
+ if f.m_Destiny == a_Destiny:
+ return f
+ return None
+
+ def GetFieldIDByDestiny(self, a_Destiny):
+ for i in range(len(self.m_Fields)):
+ f = self.m_Fields[i]
+ if f.m_Destiny == a_Destiny:
+ return i
+ return None
+
+ def GetInitTableRequest(self):
+ request = f'CREATE TABLE IF NOT EXISTS {self.GetName()}('
+ fields = []
+ for f in self.m_Fields:
+ key_str = ''
+ if f.m_Destiny == TableFieldDestiny.KEY:
+ key_str = 'PRIMARY KEY'
+ fields += [' '.join([f.m_Name, str(f.m_Type.value), key_str])]
+ return request + ', '.join(fields) + ');'
+
+ def ReplaceAllFieldTags(self, a_String, a_BDItem):
+ result = a_String
+ for i in range(len(self.m_Fields)):
+ f = self.m_Fields[i]
+ result = result.replace(f'#{f.m_Name}', str(a_BDItem[i]))
+ return result
+
+def Test():
+ table = Table('tname', [
+ TableField('f1', TableFieldDestiny.KEY, TableFieldType.INT),
+ TableField('f2', TableFieldDestiny.NAME, TableFieldType.STR),
+ TableField('f3', TableFieldDestiny.DESC, TableFieldType.STR),
+ ])
+ assert table.GetName() == 'tname'
+ assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Name == 'f1'
+ assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Destiny == TableFieldDestiny.KEY
+ assert table.GetFieldByDestiny(TableFieldDestiny.KEY).m_Type == TableFieldType.INT
+ assert table.GetFieldIDByDestiny(TableFieldDestiny.KEY) == 0
+ assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Name == 'f2'
+ assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Destiny == TableFieldDestiny.NAME
+ assert table.GetFieldByDestiny(TableFieldDestiny.NAME).m_Type == TableFieldType.STR
+ assert table.GetFieldIDByDestiny(TableFieldDestiny.NAME) == 1
+ assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Name == 'f3'
+ assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Destiny == TableFieldDestiny.DESC
+ assert table.GetFieldByDestiny(TableFieldDestiny.DESC).m_Type == TableFieldType.STR
+ assert table.GetFieldIDByDestiny(TableFieldDestiny.DESC) == 2
+
+ assert table.GetFieldByDestiny(TableFieldDestiny.PHOTO) == None
+ assert table.GetFieldIDByDestiny(TableFieldDestiny.PHOTO) == None
+
+ assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT , f3 TEXT );'
+
+ item = [10, 'i1', 'i2']
+ print( table.ReplaceAllFieldTags('#f1 #f2 #f3', item))
+ assert table.ReplaceAllFieldTags('#f1 #f2 #f3', item) == '10 i1 i2'
diff --git a/main.py b/main.py
index f3271f3..ec5c5e7 100644
--- a/main.py
+++ b/main.py
@@ -5,7 +5,7 @@ log_start_message = 'Бот успешно запущен!'
import os
-from bot_sys import config, log, bot_bd, user_access, aiogram_bot, bot_messages
+from bot_sys import config, log, bot_bd, user_access, aiogram_bot, bot_messages, bd_table
from bot_modules import mod_agregator, start, profile, backup, groups, access #, projects, , access, , tasks, needs, comments, messages, , languages
g_Log = log
@@ -20,18 +20,18 @@ g_ModuleAgregator = mod_agregator.ModuleAgregator()
mod_start_name = 'start'
+mod_access = access.ModuleAccess([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
+g_ModuleAgregator.AddModule(mod_access)
+
+mod_groups = groups.ModuleGroups([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
+g_ModuleAgregator.AddModule(mod_groups)
+
mod_profile = profile.ModuleProfile([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_profile)
mod_backup = backup.ModuleBackup([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
g_ModuleAgregator.AddModule(mod_backup)
-mod_groups = groups.ModuleGroups([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
-g_ModuleAgregator.AddModule(mod_groups)
-
-mod_access = access.ModuleAccess([mod_start_name], g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_Log)
-g_ModuleAgregator.AddModule(mod_access)
-
start_mod_name_list = [#, 'projects', 'groups', 'access', , 'languages']
mod_profile.GetName(),
mod_backup.GetName(),
@@ -64,7 +64,7 @@ for m in modules:
m.RegisterHandlers()
# Юнит тесты модулей и файлов
-test_mods = [user_access]
+test_mods = [user_access, bd_table]
for m in test_mods:
m.Test()