Compare commits

..

5 Commits

  1. 10
      adminpanel/settings.py
  2. 3
      adminpanel/urls.py
  3. 66
      adminpanelapp/admin.py
  4. 26
      adminpanelapp/models.py
  5. 33
      adminpanelapp/templates/send_telegram_message.html
  6. 11
      adminpanelapp/views.py
  7. 215
      bot_modules/all_orders.py
  8. 33
      bot_modules/messages.py
  9. 9
      bot_modules/mod_simple_message.py
  10. 168
      bot_modules/mod_table_operate.py
  11. 80
      bot_modules/orders.py
  12. 2
      bot_modules/orders_cat.py
  13. 143
      bot_modules/user_message.py
  14. 13
      bot_modules/users.py
  15. 10
      bot_sys/bd_table.py
  16. 1
      bot_sys/bot_bd.py
  17. 42
      bot_sys/bot_messages.py
  18. 5
      bot_sys/config.py
  19. 8
      bot_sys/interfaces.py
  20. 7
      main.py
  21. 3
      requirements.txt
  22. 31
      template/bd_item_edit.py
  23. 2
      template/simple_message.py
  24. 4
      template/sql_request.py

10
adminpanel/settings.py

@ -24,13 +24,14 @@ load_dotenv()
SECRET_KEY = os.getenv('PLATFORM_ADMINPANEL_SECRET_KEY') SECRET_KEY = os.getenv('PLATFORM_ADMINPANEL_SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'django_admin_listfilter_dropdown',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
@ -38,7 +39,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'adminpanelapp', 'adminpanelapp',
'rangefilter',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -115,7 +116,7 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = '/static/'
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
@ -123,4 +124,5 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
ASGI_APPLICATION = 'adminpanelapp.asgi.application' ASGI_APPLICATION = 'adminpanelapp.asgi.application'
ASYNC_MODE = 'django' ASYNC_MODE = 'django'

3
adminpanel/urls.py

@ -1,8 +1,7 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.views.generic import RedirectView from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),

66
adminpanelapp/admin.py

@ -1,15 +1,51 @@
from datetime import timedelta, datetime
from django.contrib.auth.models import User, Group
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from .models import Orders from django_admin_listfilter_dropdown.filters import DropdownFilter
from .models import Orders, Users
from django.contrib import admin, messages from django.contrib import admin, messages
from django.urls import reverse from django.urls import reverse
from rangefilter.filters import DateRangeFilter
class OrdersAdmin(admin.ModelAdmin): class OrdersAdmin(admin.ModelAdmin):
list_display = ('orderName', 'orderCreateDateTime', 'orderDesc', 'orderAddress', 'show_photo', 'show_photopay')
actions =['send_message'] list_display = (
'orderName', 'orderCreateDateTime', 'orderDesc', 'orderAddress',
'catID', 'userName', 'orderStatus', 'show_photo', 'show_photopay',
)
actions = ['send_message']
exclude = ['orderAccess', 'userID', 'orderPhoto', 'orderPhotoPay'] exclude = ['orderAccess', 'userID', 'orderPhoto', 'orderPhotoPay']
search_fields = ['orderName__regex', 'orderCreateDateTime__regex', 'orderDesc__regex',
'orderAddress__regex', 'catID__regex', 'orderStatus__regex'
]
list_filter = [
('orderName', DropdownFilter),
('orderAddress', DropdownFilter),
('orderDesc', DropdownFilter),
('catID', DropdownFilter),
('orderStatus', DropdownFilter),
('orderCreateDateTime', DateRangeFilter),
]
def userName(self, obj):
user = Users.objects.get(user_id=obj.userID)
return user.userName
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['show_delete'] = False
extra_context['show_save'] = False
extra_context['show_save_and_continue'] = False
return super().changeform_view(request, object_id, form_url, extra_context)
def has_add_permission(self, request):
return False
def show_photo(self, obj): def show_photo(self, obj):
html = obj.get_photo_html() html = obj.get_photo_html()
@ -37,4 +73,26 @@ class OrdersAdmin(admin.ModelAdmin):
send_message.short_description = 'Отправка сообщения' send_message.short_description = 'Отправка сообщения'
class CustomUserAdmin(admin.ModelAdmin):
# Переопределение метода, чтобы запретить добавление пользователей
def has_add_permission(self, request):
return False
def has_delete_permission(self, request, obj=None):
return False
def has_view_or_change_permission(self, request, obj=None):
return False
class CustomGroupAdmin(admin.ModelAdmin):
#Переопределение метода, чтобы запретить добавление групп
def has_add_permission(self, request):
return False
admin.site.unregister(User) # Снятие регистрации стандартного класса администратора пользователей
admin.site.unregister(Group) # Снятие регистрации стандартного класса администратора групп
admin.site.register(User, CustomUserAdmin) # Регистрация настроенного класса администратора пользователей
admin.site.register(Group, CustomGroupAdmin) # Регистрация настроенного класса администратора групп
admin.site.register(Orders, OrdersAdmin) admin.site.register(Orders, OrdersAdmin)

26
adminpanelapp/models.py

@ -1,11 +1,26 @@
import time
from urllib.parse import quote from urllib.parse import quote
import requests import requests
from django.db import models from django.db import models
from bot_sys.config import GetTelegramBotApiToken from bot_sys.config import GetTelegramBotApiToken
class Users(models.Model):
user_id = models.CharField(max_length=100, primary_key=True)
userName = models.CharField(max_length=100, verbose_name='Имя пользователя')
userFirstName = models.CharField(max_length=100, verbose_name='Имя')
userLastName = models.CharField(max_length=100, verbose_name='Фамилия')
userIsBot = models.BooleanField(verbose_name='Является ботом')
userLanguageCode = models.CharField(max_length=100, verbose_name='Код языка')
userPhoto = models.ImageField(verbose_name='Фото пользователя')
userAccess = models.CharField(max_length=100, verbose_name='Доступ')
createDateTime = models.DateTimeField(auto_now_add=True, verbose_name='Дата и время создания')
class Meta:
verbose_name_plural = 'Заказы'
managed = False
db_table = 'users'
class Orders(models.Model): class Orders(models.Model):
orderID = models.AutoField(primary_key=True, verbose_name='id заказа') orderID = models.AutoField(primary_key=True, verbose_name='id заказа')
userID = models.CharField(max_length=100, verbose_name='id пользователя в tg', null=True) userID = models.CharField(max_length=100, verbose_name='id пользователя в tg', null=True)
@ -17,6 +32,7 @@ class Orders(models.Model):
orderAccess = models.CharField(max_length=100, verbose_name='доступ', blank=True, null=True) orderAccess = models.CharField(max_length=100, verbose_name='доступ', blank=True, null=True)
orderCreateDateTime = models.DateTimeField(auto_now_add=True, null=True, verbose_name='дата и время создания') orderCreateDateTime = models.DateTimeField(auto_now_add=True, null=True, verbose_name='дата и время создания')
orderStatus = models.CharField(max_length=100, verbose_name='статус заказа', blank=True, null=True) orderStatus = models.CharField(max_length=100, verbose_name='статус заказа', blank=True, null=True)
catID = models.CharField(max_length=100, verbose_name='категория', blank=True, null=True)
def get_photo_html(self, width=100, height=100, large_width=400, large_height=400): def get_photo_html(self, width=100, height=100, large_width=400, large_height=400):
file_id = self.orderPhoto file_id = self.orderPhoto
@ -139,4 +155,6 @@ class Orders(models.Model):
verbose_name_plural = 'Заказы' verbose_name_plural = 'Заказы'
managed = False managed = False
db_table = 'orders' db_table = 'orders'

33
adminpanelapp/templates/send_telegram_message.html

@ -1,8 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>Отправка сообщений</title>
</head>
<body>
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<div><label for="message">Сообщение:</label></div> <div>
<div><pre> <label for="message">Сообщение:</label>
<textarea id="message" name="message" cols="50" rows="10" placeholder="Введите сообщение"></textarea> </div>
</pre></div> <div>
<div><button type="submit">Отправить сообщение</button></div> <textarea id="message" name="message" cols="50" rows="10" placeholder="Введите сообщение" class="markdownx"></textarea>
</form> </div>
<div>
<button type="submit">Отправить сообщение</button>
</div>
</form>
<p>
Пример разметки<br>
<br>
<strong>• *Жирный текст* </strong><br>
<code>• `номер карты` При нажатии пользователем на текст, заключенный в ``, автоматически копируется</code><br>
<i>• _курсив_</i><br>
</p>
</body>
</html>

11
adminpanelapp/views.py

@ -10,9 +10,16 @@ def send_telegram_message(request, chat_id):
message = request.POST.get('message') message = request.POST.get('message')
bot_token = GetTelegramBotApiToken() bot_token = GetTelegramBotApiToken()
url = f'https://api.telegram.org/bot{bot_token}/sendMessage?text={message}&chat_id={chat_id}' url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
payload = {
'chat_id': chat_id,
'text': message,
'parse_mode': 'markdown'
}
response = requests.post(url, json=payload)
response = requests.get(url)
if response.status_code == 200: if response.status_code == 200:
messages.success(request, "Сообщение успешно отправлено") messages.success(request, "Сообщение успешно отправлено")
back_url = reverse('') back_url = reverse('')

215
bot_modules/all_orders.py

@ -5,11 +5,7 @@
from bot_sys import bot_bd, keyboard, user_access, bd_table from bot_sys import bot_bd, keyboard, user_access, bd_table
from bot_modules import mod_table_operate, mod_simple_message, orders, access_utils from bot_modules import mod_table_operate, mod_simple_message, orders, access_utils
from bot_modules import users, user_message from template import bd_item_select, bd_item_view, bd_item
from template import bd_item_select, bd_item_view, bd_item, bd_item_edit
from enum import Enum
from enum import auto
# --------------------------------------------------------- # ---------------------------------------------------------
# БД # БД
@ -22,23 +18,14 @@ init_access = f'{user_access.user_access_group_new}=-'
# --------------------------------------------------------- # ---------------------------------------------------------
# Сообщения и кнопки # Сообщения и кнопки
class ButtonNames(Enum):
SHOW_USER_INFO = auto()
SEND_USER_MESSAGE = auto()
ADD_TO_DESC = auto()
button_names = {} button_names = {}
button_names.update(orders.button_names) button_names.update(orders.button_names)
button_names.pop(mod_table_operate.ButtonNames.ADD) button_names.pop(mod_table_operate.ButtonNames.ADD)
cur_button_names = { cur_button_names = {
mod_simple_message.ButtonNames.START: "🛒 Все заказы", mod_simple_message.ButtonNames.START: "🛒 Все заказы",
mod_table_operate.ButtonNames.SHOW: "☐ Открыть заказ",
mod_table_operate.ButtonNames.LIST: "📃 Список текущих заказов", mod_table_operate.ButtonNames.LIST: "📃 Список текущих заказов",
orders.ButtonNames.LIST_ALL: "📃 Список всех заказов", orders.ButtonNames.LIST_ALL: "📃 Список всех заказов",
ButtonNames.SHOW_USER_INFO: "👤 Информация о пользователе",
ButtonNames.SEND_USER_MESSAGE: "📨 Отправить сообщение пользователю",
ButtonNames.ADD_TO_DESC: "𝌴 Добавить к описанию",
mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать заказ", mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать заказ",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO_PAY): "☐ Загрузить чек по оплате заказа", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO_PAY): "☐ Загрузить чек по оплате заказа",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в заказе", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в заказе",
@ -69,8 +56,6 @@ cur_messages = {
Введите новый статус заказа: Введите новый статус заказа:
''', ''',
mod_table_operate.Messages.OPEN: f''' mod_table_operate.Messages.OPEN: f'''
<b>Номер заказа: #{orders.key_name} </b>
<b>Заказ: #{orders.name_field}</b> <b>Заказ: #{orders.name_field}</b>
<b>Описание и состав заказа:</b> #{orders.desc_field} <b>Описание и состав заказа:</b> #{orders.desc_field}
@ -83,29 +68,16 @@ cur_messages = {
<b>Время создания:</b> #{orders.create_datetime_field} <b>Время создания:</b> #{orders.create_datetime_field}
''', ''',
mod_table_operate.InlineMessage(ButtonNames.ADD_TO_DESC): f'''
Текущее описание заказа:
<code>#{orders.desc_field}</code>
Введите дополение к описанию заказа:
''',
} }
messages_notification = { messages_notification = {
mod_table_operate.NotificationMessage(orders.OrderStatus.NEW): f'''Статус заказа "#{orders.name_field}" изменён на - ожидает модерации''', mod_table_operate.NotificationMessage(orders.OrderStatus.NEW): f'''Статус заказа "#{orders.name_field}" изменён на - ожидает модерации''',
mod_table_operate.NotificationMessage(orders.OrderStatus.PAY): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ ожидает оплаты. mod_table_operate.NotificationMessage(orders.OrderStatus.PAY): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ ожидает оплаты.
<b>Описание заказа:</b> Оплатите заказ и прикрепите чек об оплате к заказу. Для этого проследуйте по пути "Заказы"->"Редактировать мой заказ"->"Загрузить чек по оплате моего заказа"''',
#{orders.desc_field}
Оплатите заказ и прикрепите чек об оплате к заказу. Для этого проследуйте по пути "Главное меню"->"Заказы"->"Загрузить чек по оплате моего заказа"
Или воспользуйтесь кнопкой:''',
mod_table_operate.NotificationMessage(orders.OrderStatus.ADDRESS): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ ожидает указания адреса доставки. mod_table_operate.NotificationMessage(orders.OrderStatus.ADDRESS): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ ожидает указания адреса доставки.
<b>Описание заказа:</b> Для этого проследуйте по пути "Заказы"->"Редактировать мой заказ"->"Изменить адрес в моём заказе"''',
#{orders.desc_field}
Для этого проследуйте по пути "Главное меню"->"Заказы"->"Редактировать мой заказ"->"Изменить адрес в моём заказе"''',
mod_table_operate.NotificationMessage(orders.OrderStatus.FINISH): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ выполнен''', mod_table_operate.NotificationMessage(orders.OrderStatus.FINISH): f'''Статус заказа "#{orders.name_field}" изменён на - Заказ выполнен''',
} }
@ -157,32 +129,12 @@ class ModuleAllOrders(orders.ModuleOrders):
return DBItemForUserSelectSource(self.m_Bot, self.m_Table.GetName(), parent_id_field, a_PrevPrefix, a_ButtonName) return DBItemForUserSelectSource(self.m_Bot, self.m_Table.GetName(), parent_id_field, a_PrevPrefix, a_ButtonName)
def GetButtonNameAndKeyValueAndAccess(self, a_Item): def GetButtonNameAndKeyValueAndAccess(self, a_Item):
user_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_ID) parent_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
n, k, a = super().GetButtonNameAndKeyValueAndAccess(a_Item) n, k, a = super().GetButtonNameAndKeyValueAndAccess(a_Item)
return n + "," + str(a_Item[user_field_id]), k, a return n + ":" + str(a_Item[parent_field_id]), k, a
def GetPayInlineKeyboardFunc(self, a_ItemID, a_Destiny):
def PayInlineKeyboard(a_Message, a_UserGroups):
#print('PayInlineKeyboard', a_Message, a_UserGroups, a_ItemID)
cur_buttons = []
orders_mod = self.GetModule(orders.module_name)
dst = a_Destiny
if orders_mod:
prefix = orders_mod.m_EditPrefix.get(dst, None)
if prefix:
access = orders_mod.GetAccessForEditKeyboardButtons(orders_mod.m_Table.GetFieldByDestiny(dst))
cur_buttons += [
keyboard.InlineButtonWithAccess(orders_mod.GetButton(mod_table_operate.EditButton(dst)), prefix, a_ItemID, orders_mod.GetAccess(), access),
]
#print('cur_buttons', cur_buttons, a_Message, a_UserGroups, a_ItemID)
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)
return PayInlineKeyboard
async def OnChangeField(self, a_Field, a_ItemID, a_ItemData, a_EditUserID): async def OnChangeField(self, a_Field, a_ItemID, a_ItemData, a_EditUserID):
await super().OnChangeField(a_Field, a_ItemID, a_ItemData, a_EditUserID) super().OnChangeField(a_Field, a_ItemID, a_ItemData, a_EditUserID)
if a_Field.m_Destiny == bd_table.TableFieldDestiny.STATUS: if a_Field.m_Destiny == bd_table.TableFieldDestiny.STATUS:
key_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY) key_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY)
item = bd_item.GetBDItemsTemplate(self.m_Bot, self.m_Table.GetName(), key_field)(a_ItemID) item = bd_item.GetBDItemsTemplate(self.m_Bot, self.m_Table.GetName(), key_field)(a_ItemID)
@ -192,159 +144,20 @@ class ModuleAllOrders(orders.ModuleOrders):
item = item[0] item = item[0]
user_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_ID) user_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_ID)
parent_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
status_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.STATUS) status_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.STATUS)
#print('OnChangeField', item, user_field_id, status_field_id) print('OnChangeField', item, user_field_id, status_field_id)
owner_id = item[user_field_id] owner_id = item[user_field_id]
new_status = item[status_field_id] new_status = item[status_field_id]
parent_id = item[parent_field_id]
msg = self.GetMessage(mod_table_operate.NotificationMessage(new_status)) msg = self.GetMessage(mod_table_operate.NotificationMessage(new_status))
parent_id_msg = None
if msg:
name = self.GetMessageNameWithTableFieldDestinyAndValue(mod_table_operate.NotificationMessage(new_status), bd_table.TableFieldDestiny.PARENT_ID, parent_id)
#print('name', name, msg.GetLanguage())
parent_id_msg = msg.FindMessageForLang(name, msg.GetLanguage())
if parent_id_msg:
msg = parent_id_msg
if not msg: if not msg:
self.m_Log.Error(f'Не удалось оповестить по заказу №{a_ItemID}. Пустое сообщение для нового статуса {new_status}') self.m_Log.Error(f'Не удалось оповестить по заказу №{a_ItemID}. Пустое сообщение для нового статуса {new_status}')
return return
msg = self.UpdateMessage(msg, None, item) msg = self.UpdateMessage(msg, None, item)
inline_keyboard = None await self.m_Bot.SendMessage(
#print('new_status', str(orders.OrderStatus.PAY), new_status) owner_id,
if new_status == str(orders.OrderStatus.PAY): msg.GetDesc(),
inline_keyboard = self.GetPayInlineKeyboardFunc(a_ItemID, bd_table.TableFieldDestiny.PHOTO_PAY) msg.GetPhotoID(),
elif new_status == str(orders.OrderStatus.ADDRESS): None,
inline_keyboard = self.GetPayInlineKeyboardFunc(a_ItemID, bd_table.TableFieldDestiny.ADDRESS) None
)
await self.SendMessageToUser(msg, owner_id, a_GetInlineButtonsFunc = inline_keyboard)
def GetStartButtons(self, a_Message, a_UserGroups):
return [
[mod_table_operate.ButtonNames.LIST, user_access.AccessMode.VIEW],
[mod_table_operate.ButtonNames.EDIT, user_access.AccessMode.EDIT],
[mod_table_operate.ButtonNames.ADD, user_access.AccessMode.ADD],
[mod_table_operate.ButtonNames.DEL, user_access.AccessMode.DELETE],
]
def GetViewItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID):
table_name = self.m_Table.GetName()
key_name = self.GetKeyFieldName()
cur_item = mod_table_operate.GetCurItem(self.m_Bot, table_name, key_name, a_ItemID)
user_id = None
user_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_ID)
key_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)
if user_id_field_index and cur_item and cur_item[user_id_field_index]:
user_id = cur_item[user_id_field_index]
if not user_id:
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, [], a_UserGroups)
cur_buttons = []
for dst in bd_table.TableFieldDestiny.DESC, bd_table.TableFieldDestiny.STATUS:
access = self.GetAccessForEditKeyboardButtons(self.m_Table.GetFieldByDestiny(dst))
btn = mod_table_operate.EditButton(dst)
prefix = self.m_EditPrefix.get(dst, None)
if prefix:
cur_buttons += [
keyboard.InlineButtonWithAccess(self.GetButton(btn), prefix, cur_item[key_field_index], self.GetAccess(), access),
]
access = self.GetAccessForEditKeyboardButtons(self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC))
a_Button = ButtonNames.ADD_TO_DESC
prefix = self.m_EditPrefix.get(a_Button, None)
cur_buttons += [
keyboard.InlineButtonWithAccess(self.GetButton(a_Button), prefix, cur_item[key_field_index], self.GetAccess(), access),
]
user_message_mod = self.GetModule(user_message.module_name)
if user_message_mod:
cur_buttons += [
keyboard.InlineButtonWithAccess(self.GetButton(ButtonNames.SEND_USER_MESSAGE), user_message_mod.GetAddPrefix(), user_id, user_message_mod.GetAccess(), user_access.AccessMode.ADD),
]
users_mod = self.GetModule(users.module_name)
if users_mod:
cur_buttons += [
keyboard.InlineButtonWithAccess(self.GetButton(ButtonNames.SHOW_USER_INFO), users_mod.GetShowPrefix(), user_id, users_mod.GetAccess(), user_access.AccessMode.VIEW),
]
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)
def RegisterAddToDesc(self):
a_AccessMode = user_access.AccessMode.EDIT
a_Button = ButtonNames.ADD_TO_DESC
a_ButtonName = self.GetButton(a_Button)
a_EditMessage = self.GetMessage(mod_table_operate.InlineMessage(a_Button))
a_Field = self.m_Table.GetFieldByDestiny(bd_table.TableFieldDestiny.DESC)
a_FieldName = a_Field.m_Name
a_FieldType = bd_item.FieldType.text
if a_Field.m_Type == bd_table.TableFieldType.PHOTO:
a_FieldType = bd_item.FieldType.photo
if not a_ButtonName or not a_EditMessage:
return
async def OnChange(a_ItemID, a_ItemData, a_EditUserID):
await self.OnChangeField(a_Field, a_ItemID, a_ItemData, a_EditUserID)
return self.OnChange()
table_name = self.m_Table.GetName()
key_name = self.GetKeyFieldName()
edit_keyboard_func = self.m_GetEditKeyboardButtonsFunc
GetButtonNameAndKeyValueAndAccess = self.m_GetButtonNameAndKeyValueAndAccessFunc
GetAccess = self.m_GetAccessFunc
async def EditBDItemFunc(a_ItemData, a_UserID):
a_KeyName = key_name
a_TableName = table_name
item_id = a_ItemData[a_KeyName]
field_value = a_ItemData[a_FieldName]
cur_item = mod_table_operate.GetCurItem(self.m_Bot, table_name, key_name, item_id)
desc = ""
desc_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.DESC)
if desc_field_index and cur_item and cur_item[desc_field_index]:
desc = cur_item[desc_field_index]
new_value = desc + "\n" + field_value
res, error = bd_item.EditBDItemInTableTemplate(self.m_Bot, a_TableName, a_KeyName, a_FieldName)(item_id, new_value)
if error:
self.m_Bot.GetLog().Error(f'Пользователю {a_UserID} не удалось изменить поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={field_value}. Ошибка: {error}')
else:
self.m_Bot.GetLog().Success(f'Пользователь {a_UserID} добавил в поле в таблице {a_TableName} ключу {a_KeyName}={item_id}. Новое значение поля {a_FieldName}={new_value}. Старое {desc}.')
await OnChange(item_id, a_ItemData, a_UserID)
return res, error
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True)
a_Prefix = bd_item_edit.CustomEditBDItemRegisterHandlers(self.m_Bot, \
EditBDItemFunc,
self.SelectSourceTemplate(a_Prefix, a_ButtonName), \
mod_table_operate.MakeFSMForAddAndEdit(self.GetName(), a_FieldName), \
self.GetMessage(mod_table_operate.Messages.SELECT_TO_EDIT), \
self.ShowMessageTemplate(a_EditMessage), \
self.ShowMessageTemplate(self.GetMessage(mod_table_operate.Messages.SUCCESS_EDIT)), \
table_name, \
key_name, \
a_FieldName, \
self.PostProccessingForFieldForEditTemplate(a_Field),\
GetButtonNameAndKeyValueAndAccess, \
GetAccess, \
self.AdditionalKeyboardForEditTemplate(a_Field),\
edit_keyboard_func, \
access_mode = a_AccessMode, \
field_type = a_FieldType\
)
self.m_EditPrefix.update({a_Button: a_Prefix})
def RegisterHandlers(self):
super().RegisterHandlers()
self.RegisterAddToDesc()

33
bot_modules/messages.py

@ -46,11 +46,11 @@ button_names = {
mod_simple_message.ButtonNames.START: "✉ Сообщения", mod_simple_message.ButtonNames.START: "✉ Сообщения",
mod_table_operate.ButtonNames.LIST: "📃 Список сообщений", mod_table_operate.ButtonNames.LIST: "📃 Список сообщений",
mod_table_operate.ButtonNames.ADD: "☑ Добавить сообщение", mod_table_operate.ButtonNames.ADD: "☑ Добавить сообщение",
mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать сообщение бота", mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать сообщение",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение у сообщения", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение у сообщения",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.NAME): " Изменить название сообщения", mod_table_operate.EditButton(bd_table.TableFieldDestiny.NAME): "≂ Изменить название у сообщения",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание сообщения", mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание у сообщения",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.ACCESS): " Изменить доступ к сообщению", mod_table_operate.EditButton(bd_table.TableFieldDestiny.ACCESS): "✋ Изменить доступ к сообщению",
mod_table_operate.ButtonNames.DEL: "❌ Удалить сообщение", mod_table_operate.ButtonNames.DEL: "❌ Удалить сообщение",
} }
@ -72,12 +72,9 @@ messages = {
Время создания: #{create_datetime_field} Время создания: #{create_datetime_field}
''', ''',
mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.NAME): f''' mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.NAME): '''
Создание сообщения. Шаг 1 Создание сообщения. Шаг 1
Вы можете добавить номер родителя ID в таком формате: ИмяСообщения{'<code>?' + str(bd_table.TableFieldDestiny.PARENT_ID) + '=</code>ID'}
, чтобы сообщение было только для него
Введите название сообщения: Введите название сообщения:
''', ''',
mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): ''' mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): '''
@ -149,7 +146,12 @@ class ModuleMessages(mod_table_operate.TableOperateModule):
return self.m_Bot.SQLRequest(f'INSERT OR IGNORE INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})', return self.m_Bot.SQLRequest(f'INSERT OR IGNORE INTO {table_name}({photo_field}, {name_field}, {desc_field}, {access_field}, {parent_id_field}, {create_datetime_field}) VALUES(?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()})',
commit = True, return_error = True, param = (a_Message.m_PhotoID, a_Message.m_MessageName, a_Message.m_MessageDesc, access_utils.GetItemDefaultAccessForModule(self.m_Bot, module_name), lang_id)) commit = True, return_error = True, param = (a_Message.m_PhotoID, a_Message.m_MessageName, a_Message.m_MessageDesc, access_utils.GetItemDefaultAccessForModule(self.m_Bot, module_name), lang_id))
def LoadAllMessages(self): def FlushMessages(self):
msg = self.m_BotMessages.GetMessages()
for lang, msg_dict in msg.items():
for msg_name, message in msg_dict.items():
self.AddOrIgnoreMessage(message)
table_name = self.m_Table.GetName() table_name = self.m_Table.GetName()
msgs_bd = bd_item.GetAllItemsTemplate(self.m_Bot, table_name)() msgs_bd = bd_item.GetAllItemsTemplate(self.m_Bot, table_name)()
if msgs_bd: if msgs_bd:
@ -161,20 +163,7 @@ class ModuleMessages(mod_table_operate.TableOperateModule):
lang_name = self.GetModule(self.m_ParentModName).GetLangName(lang_id) lang_name = self.GetModule(self.m_ParentModName).GetLangName(lang_id)
self.m_BotMessages.CreateMessage(name, desc, self.m_Log.GetTimeNow(), a_MessagePhotoID = photo_id, a_MessageLang = lang_name) self.m_BotMessages.CreateMessage(name, desc, self.m_Log.GetTimeNow(), a_MessagePhotoID = photo_id, a_MessageLang = lang_name)
def FlushMessages(self):
msg = self.m_BotMessages.GetMessages()
for lang, msg_dict in msg.items():
for msg_name, message in msg_dict.items():
self.AddOrIgnoreMessage(message)
self.LoadAllMessages()
self.m_BotMessages.UpdateSignal(self.m_Log.GetTimeNow()) self.m_BotMessages.UpdateSignal(self.m_Log.GetTimeNow())
def OnChange(self): def OnChange(self):
self.FlushMessages() self.FlushMessages()
async def PostDelete(self, a_CallbackQuery, a_ItemID):
self.LoadAllMessages() # Сначала загружаем, т.к. одно сообщение удалилось
await super().PostDelete(a_CallbackQuery, a_ItemID)

9
bot_modules/mod_simple_message.py

@ -74,11 +74,10 @@ class SimpleMessageModule(mod_interface.IModule):
if len(a_ButtonDesc) >= 41: if len(a_ButtonDesc) >= 41:
print('ButtonDesc:', a_ButtonDesc) print('ButtonDesc:', a_ButtonDesc)
assert False # Телеграм не поддерживает больше assert False # Телеграм не поддерживает больше
assert a_ButtonDesc[0] != ' ', f'ButtonDesc:{a_ButtonDesc}, Телеграм не поддерживает пробелы в начале' assert a_ButtonDesc[0] != ' ' # Телеграм не поддерживает пробелы в начале
assert a_ButtonDesc[-1:] != ' ', f'ButtonDesc:{a_ButtonDesc}, Телеграм не поддерживает пробелы в конце' assert a_ButtonDesc[-1:] != ' ' # Телеграм не поддерживает пробелы в конце
btn_name = f'{self.GetName()} {str(a_ButtonName).replace("ButtonNames.", "")}' # TODO добавить проверку, что все кнопки (a_ButtonDesc) разные
assert not self.m_BotButtons.CheckDescExist(a_ButtonDesc), f'ButtonDesc:{a_ButtonDesc}, повтор имени кнопки, все кнопки должны быть разными' btn = self.m_BotButtons.CreateMessage(f'{self.GetName()} {str(a_ButtonName).replace("ButtonNames.", "")}', a_ButtonDesc, self.m_Log.GetTimeNow())
btn = self.m_BotButtons.CreateMessage(btn_name, a_ButtonDesc, self.m_Log.GetTimeNow())
return btn return btn
def GetModule(self, a_ModName): def GetModule(self, a_ModName):

168
bot_modules/mod_table_operate.py

@ -4,7 +4,7 @@
# Модуль для редактирования и просмотра таблицы в БД # Модуль для редактирования и просмотра таблицы в БД
from bot_sys import keyboard, user_access, bd_table, bot_bd, bot_subscribes from bot_sys import keyboard, user_access, bd_table, bot_bd, bot_subscribes
from bot_modules import access_utils, mod_simple_message, groups_utils from bot_modules import access_utils, mod_simple_message
from template import simple_message, bd_item, bd_item_select, bd_item_view, bd_item_delete, bd_item_add, bd_item_edit from template import simple_message, bd_item, bd_item_select, bd_item_view, bd_item_delete, bd_item_add, bd_item_edit
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
@ -22,9 +22,6 @@ def EnumButton(a_EnumItem):
def EditMessage(a_BDTableDestiny): def EditMessage(a_BDTableDestiny):
return 'edit ' + str(a_BDTableDestiny) return 'edit ' + str(a_BDTableDestiny)
def InlineMessage(a_ButtonName):
return 'inline ' + str(a_ButtonName)
def CreateMessage(a_BDTableDestiny): def CreateMessage(a_BDTableDestiny):
return 'create ' + str(a_BDTableDestiny) return 'create ' + str(a_BDTableDestiny)
@ -38,7 +35,6 @@ def SubscribeMessage(a_EnumItem):
return 'subscribe ' + str(a_EnumItem) return 'subscribe ' + str(a_EnumItem)
class ButtonNames(Enum): class ButtonNames(Enum):
SHOW = auto()
LIST = auto() LIST = auto()
ADD = auto() ADD = auto()
EDIT = auto() EDIT = auto()
@ -92,20 +88,6 @@ def MakeFSMForEdit(a_ModName, a_FieldName):
exec(cmd, globals(), _locals) exec(cmd, globals(), _locals)
return _locals['fsm'] return _locals['fsm']
add_and_edit_fsm_cmd = '''
class FSMAddAndEdit{a_ModName}_{a_FieldName}_Item(StatesGroup):
item_field = State()
fsm = FSMAddAndEdit{a_ModName}_{a_FieldName}_Item
'''
def MakeFSMForAddAndEdit(a_ModName, a_FieldName):
cmd = add_and_edit_fsm_cmd.replace("{a_ModName}", a_ModName).replace("{a_FieldName}", a_FieldName)
_locals = locals()
exec(cmd, globals(), _locals)
return _locals['fsm']
class TableOperateModule(mod_simple_message.SimpleMessageModule): class TableOperateModule(mod_simple_message.SimpleMessageModule):
def __init__(self, a_Table, a_Messages, a_Buttons, a_ParentModName, a_ChildModName, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log): def __init__(self, a_Table, a_Messages, a_Buttons, a_ParentModName, a_ChildModName, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log):
super().__init__(a_Messages, a_Buttons, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log) super().__init__(a_Messages, a_Buttons, a_InitAccess, a_DefInitAccess, a_ChildModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log)
@ -114,7 +96,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
self.m_ChildModName = a_ChildModName self.m_ChildModName = a_ChildModName
self.m_ParentModName = a_ParentModName self.m_ParentModName = a_ParentModName
self.m_SelectPrefix = '' self.m_SelectPrefix = ''
self.m_EditPrefix = {}
def GetEditKeyboardButtons(a_Message, a_UserGroups): def GetEditKeyboardButtons(a_Message, a_UserGroups):
return self.GetEditKeyboardButtons(a_Message, a_UserGroups) return self.GetEditKeyboardButtons(a_Message, a_UserGroups)
@ -171,11 +152,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
cur_buttons += [keyboard.ButtonWithAccess(self.GetButton(EditButton(f.m_Destiny)), access, self.GetAccess()),] cur_buttons += [keyboard.ButtonWithAccess(self.GetButton(EditButton(f.m_Destiny)), access, self.GetAccess()),]
return mod_buttons + keyboard.MakeButtons(self.m_Bot, cur_buttons, a_UserGroups) return mod_buttons + keyboard.MakeButtons(self.m_Bot, cur_buttons, a_UserGroups)
def GetShowItemInlineKeyboardTemplate(self, a_ItemID):
def GetShowItemInlineKeyboard(a_Message, a_UserGroups):
return self.GetShowItemInlineKeyboard(a_Message, a_UserGroups, a_ItemID)
return GetShowItemInlineKeyboard
def GetViewItemInlineKeyboardTemplate(self, a_ItemID): def GetViewItemInlineKeyboardTemplate(self, a_ItemID):
def GetViewItemInlineKeyboard(a_Message, a_UserGroups): def GetViewItemInlineKeyboard(a_Message, a_UserGroups):
return self.GetViewItemInlineKeyboard(a_Message, a_UserGroups, a_ItemID) return self.GetViewItemInlineKeyboard(a_Message, a_UserGroups, a_ItemID)
@ -184,27 +160,12 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
def GetSelectPrefix(self): def GetSelectPrefix(self):
return self.m_SelectPrefix return self.m_SelectPrefix
def GetDelPrefix(self):
return self.m_DelPrefix
def GetAddPrefix(self):
return self.m_AddPrefix
def GetShowPrefix(self):
return self.m_ShowPrefix
def GetShowItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID):
cur_buttons = [
keyboard.InlineButtonWithAccess(self.GetButton(ButtonNames.SHOW), self.GetShowPrefix(), a_ItemID, self.GetAccess(), user_access.AccessMode.VIEW),
]
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)
def GetViewItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID): def GetViewItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID):
if not self.m_ChildModName: if not self.m_ChildModName:
return None return None
child_mod = self.GetModule(self.m_ChildModName) child_mod = self.GetModule(self.m_ChildModName)
cur_buttons = [ cur_buttons = [
keyboard.InlineButtonWithAccess(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, child_mod.GetAccess(), user_access.AccessMode.VIEW), keyboard.InlineButtonWithAccess(child_mod.GetButton(ButtonNames.LIST), child_mod.GetSelectPrefix(), a_ItemID, self.GetAccess(), user_access.AccessMode.VIEW),
] ]
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups) return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)
@ -253,49 +214,23 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_Msg.UpdatePhotoID(','.join(photos)) a_Msg.UpdatePhotoID(','.join(photos))
return a_Msg return a_Msg
def GetParentID(self, a_Item, a_ItemDict, a_TableName):
parent_id = None
parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if parent_id_field and a_ItemDict and parent_id_field in a_ItemDict:
parent_id = str(a_ItemDict[parent_id_field])
parent_id_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if not parent_id and parent_id_field_id != None and a_Item and len(a_Item) == self.m_Table.GetFieldsCount() and self.m_Table.GetName() == a_TableName:
parent_id = str(a_Item[parent_id_field_id])
if self.m_ParentModName:
parent_mod = self.GetModule(self.m_ParentModName)
if parent_mod:
key_field_id = parent_mod.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)
if not parent_id and key_field_id != None and a_Item and len(a_Item) == parent_mod.m_Table.GetFieldsCount() and parent_mod.m_Table.GetName() == a_TableName:
parent_id = str(a_Item[key_field_id])
return parent_id
def GetMessageNameWithTableFieldDestinyAndValue(self, a_NameWithoutDestiny, a_Destiny, a_Value):
return a_NameWithoutDestiny + '?' + str(a_Destiny) + '=' + str(a_Value)
def ShowMessageTemplate(self, a_Message, Inline_keyboard_template_func = None, a_EnablePhoto = False): def ShowMessageTemplate(self, a_Message, Inline_keyboard_template_func = None, a_EnablePhoto = False):
async def ShowMessage(a_CallbackQuery, a_Item, a_ItemDict, table_name = self.m_Table.GetName()): async def ShowMessage(a_CallbackQuery, a_Item, a_ItemDict, table_name = self.m_Table.GetName()):
lang = str(a_CallbackQuery.from_user.language_code) msg = a_Message.StaticCopy()
msg = a_Message
# Подменяем сообщение, если оно уже есть для PARENT_ID
parent_id = self.GetParentID(a_Item, a_ItemDict, table_name)
if parent_id:
name = self.GetMessageNameWithTableFieldDestinyAndValue(msg.GetName(), bd_table.TableFieldDestiny.PARENT_ID, parent_id)
parent_id_msg = a_Message.FindMessageForLang(name, lang)
if parent_id_msg:
msg = parent_id_msg
msg = msg.GetMessageForLang(lang).StaticCopy()
# TODO: добавить поддержку языков в a_MessageName # TODO: добавить поддержку языков в a_MessageName
Inline_keyboard_func = None Inline_keyboard_func = None
item_access = None item_access = None
if a_ItemDict: if a_ItemDict:
lang = str(a_CallbackQuery.from_user.language_code)
msg = msg.GetMessageForLang(lang).StaticCopy()
msg = self.UpdateMessageByDict(msg, lang, a_ItemDict, a_EnablePhoto = a_EnablePhoto) msg = self.UpdateMessageByDict(msg, lang, a_ItemDict, a_EnablePhoto = a_EnablePhoto)
if a_Item and self.m_Table.GetName() == table_name: if a_Item and self.m_Table.GetName() == table_name:
if len(a_Item) < self.m_Table.GetFieldsCount() - 1: # Для проектов это нужно. Там на 1 меньше поле. TODO разделить отправку сообщений item_access и Inline_keyboard_func if len(a_Item) < self.m_Table.GetFieldsCount() - 1: # Для проектов это нужно. Там на 1 меньше поле. TODO разделить отправку сообщений item_access и Inline_keyboard_func
return simple_message.WorkFuncResult(self.GetMessage(Messages.ERROR_FIND)) return simple_message.WorkFuncResult(self.GetMessage(Messages.ERROR_FIND))
elif len(a_Item) == self.m_Table.GetFieldsCount(): elif len(a_Item) == self.m_Table.GetFieldsCount():
lang = str(a_CallbackQuery.from_user.language_code)
msg = msg.GetMessageForLang(lang).StaticCopy()
msg = self.UpdateMessage(msg, lang, a_Item, a_EnablePhoto = a_EnablePhoto) msg = self.UpdateMessage(msg, lang, a_Item, a_EnablePhoto = a_EnablePhoto)
item_access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)] item_access = a_Item[self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)]
if Inline_keyboard_template_func: if Inline_keyboard_template_func:
@ -312,34 +247,34 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
async def PostDelete(self, a_CallbackQuery, a_ItemID): async def PostDelete(self, a_CallbackQuery, a_ItemID):
user_id = a_CallbackQuery.from_user.id user_id = a_CallbackQuery.from_user.id
self.m_Log.Success(f'Элемент{a_ItemID} была удалена пользователем {user_id}.') self.m_Log.Success(f'Задача{a_ItemID} была удалена пользователем {user_id}.')
#TODO: удалить вложенные #TODO: удалить вложенные
self.OnChange() self.OnChange()
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_DEL subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_DEL
item_id = -1 item_id = -1
await self.SendSubscribe(subscribe_type, item_id, user_id, item_id) await self.SendSubscribe(subscribe_type, item_id, user_id)
subscribe_type = bot_subscribes.SubscribeType.ITEM_DEL subscribe_type = bot_subscribes.SubscribeType.ITEM_DEL
item_id = a_ItemID item_id = a_ItemID
await self.SendSubscribe(subscribe_type, item_id, user_id, item_id) await self.SendSubscribe(subscribe_type, item_id, user_id)
table_name = self.m_Table.GetName() table_name = self.m_Table.GetName()
key_name = self.GetKeyFieldName() key_name = self.GetKeyFieldName()
cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_ItemID) cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_ItemID)
print(cur_item)
parent_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID) parent_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if parent_id_field_index and cur_item and cur_item[parent_id_field_index]: if parent_id_field_index and cur_item and cur_item[parent_id_field_index]:
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT
item_id = cur_item[parent_id_field_index] item_id = cur_item[parent_id_field_index]
await self.SendSubscribe(subscribe_type, item_id, user_id, item_id) await self.SendSubscribe(subscribe_type, item_id, user_id)
return simple_message.WorkFuncResult(self.GetMessage(Messages.SUCCESS_DELETE)) return simple_message.WorkFuncResult(self.GetMessage(Messages.SUCCESS_DELETE))
async def AddBDItemFunc(self, a_ItemData, a_UserID): async def AddBDItemFunc(self, a_ItemData, a_UserID):
table_name = self.m_Table.GetName() table_name = self.m_Table.GetName()
key_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.KEY)
name_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.NAME) name_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.NAME)
photo_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PHOTO) photo_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PHOTO)
desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC) desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC)
@ -369,8 +304,8 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
values += ['?'] values += ['?']
param += (a_ItemData[n], ) param += (a_ItemData[n], )
request = f'INSERT INTO {table_name}({",".join(fields)}) VALUES({",".join(values)})' # RETURNING {key_field} request = f'INSERT INTO {table_name}({",".join(fields)}) VALUES({",".join(values)})'
#print('request', request, param) print('request', request, param)
res, error = self.m_Bot.SQLRequest(request, commit = True, return_error = True, param = param) res, error = self.m_Bot.SQLRequest(request, commit = True, return_error = True, param = param)
self.OnChange() self.OnChange()
@ -379,56 +314,36 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
else: else:
self.m_Log.Success(f'Пользователь {a_UserID}. Добавлена запись в таблицу {request} {param}.') self.m_Log.Success(f'Пользователь {a_UserID}. Добавлена запись в таблицу {request} {param}.')
res_id, error_id = self.m_Bot.SQLRequest(f'SELECT rowid from {table_name} order by ROWID DESC limit 1', commit = False, return_error = True)
table_item_id = -1
if not error_id and res_id and len(res_id) == 1 and len(res_id[0]) == 1:
table_item_id = str(res_id[0][0])
#print('res_id', res, error, table_item_id)
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_ADD subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_ADD
item_id = -1 item_id = -1
await self.SendSubscribe(subscribe_type, item_id, a_UserID, table_item_id) await self.SendSubscribe(subscribe_type, item_id, a_UserID)
if parent_id_field and a_ItemData[parent_id_field]: if parent_id_field and a_ItemData[parent_id_field]:
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT
item_id = a_ItemData[parent_id_field] item_id = a_ItemData[parent_id_field]
await self.SendSubscribe(subscribe_type, item_id, a_UserID, table_item_id) await self.SendSubscribe(subscribe_type, item_id, a_UserID)
return res, error return res, error
async def SendMessageToUser(self, a_Message, a_UserID, a_GetButtonsFunc = None, a_GetInlineButtonsFunc = None, parse_mode = None): async def SendSubscribe(self, a_Type, a_ItemID, a_OwnerUserID):
a_Message = a_Message.StaticCopy()
a_MessageFromUSer = None
user_groups = groups_utils.GetUserGroupData(self.m_Bot, a_UserID)
try:
#print('SendMessage', self.m_Bot, a_Message, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_MessageFromUSer, user_groups, parse_mode)
await simple_message.SendMessage(self.m_Bot, a_Message, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_MessageFromUSer, user_groups, parse_mode=parse_mode)
return True
except:
return False
async def SendSubscribe(self, a_Type, a_ItemID, a_OwnerUserID, a_TableItem):
table_name = self.m_Table.GetName()
key_name = self.GetKeyFieldName()
cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_TableItem)
for s_user_id in self.m_BotSubscribes.GetUserIDs(self.GetName(), a_Type, a_ItemID = a_ItemID): for s_user_id in self.m_BotSubscribes.GetUserIDs(self.GetName(), a_Type, a_ItemID = a_ItemID):
a_BotMessage = self.GetMessage(SubscribeMessage(a_Type)) a_BotMessage = self.GetMessage(SubscribeMessage(a_Type))
#print('s_user_id', s_user_id, a_BotMessage, a_OwnerUserID, a_TableItem, cur_item)
if not a_BotMessage: if not a_BotMessage:
continue continue
if s_user_id == a_OwnerUserID: if s_user_id == a_OwnerUserID:
continue continue
a_UserID = s_user_id
a_BotMessage = a_BotMessage.StaticCopy() a_BotMessage = a_BotMessage.StaticCopy()
a_BotMessage.UpdateDesc(a_BotMessage.GetDesc().replace("#item_id", str(a_TableItem))) a_BotMessage.UpdateDesc(a_BotMessage.GetDesc().replace("#item_id", str(a_ItemID)))
a_GetButtonsFunc = None
inline_keyboard_func = None a_GetInlineButtonsFunc = None
if cur_item: a_UserID = s_user_id
a_BotMessage.UpdateDesc(self.m_Table.ReplaceAllFieldTags(a_BotMessage.GetDesc(), cur_item)) a_Message = None
inline_keyboard_func = self.GetShowItemInlineKeyboardTemplate(a_TableItem) user_groups = None
parse_mode = None
await self.SendMessageToUser(a_BotMessage, a_UserID, a_GetInlineButtonsFunc = inline_keyboard_func) try:
await simple_message.SendMessage(self.m_Bot, a_BotMessage, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_UserID, a_Message, user_groups, parse_mode=parse_mode)
except:
return
def SelectSourceTemplate(self, a_PrevPrefix, a_ButtonName): def SelectSourceTemplate(self, a_PrevPrefix, a_ButtonName):
parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
@ -474,23 +389,24 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
async def OnChangeField(self, a_Field, a_ItemID, a_ItemData, a_EditUserID): async def OnChangeField(self, a_Field, a_ItemID, a_ItemData, a_EditUserID):
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_EDIT subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_EDIT
item_id = -1 item_id = -1
await self.SendSubscribe(subscribe_type, item_id, a_EditUserID, a_ItemID) await self.SendSubscribe(subscribe_type, item_id, a_EditUserID)
subscribe_type = bot_subscribes.SubscribeType.ITEM_EDIT subscribe_type = bot_subscribes.SubscribeType.ITEM_EDIT
item_id = a_ItemID item_id = a_ItemID
await self.SendSubscribe(subscribe_type, item_id, a_EditUserID, a_ItemID) await self.SendSubscribe(subscribe_type, item_id, a_EditUserID)
table_name = self.m_Table.GetName() table_name = self.m_Table.GetName()
key_name = self.GetKeyFieldName() key_name = self.GetKeyFieldName()
cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_ItemID) cur_item = GetCurItem(self.m_Bot, table_name, key_name, a_ItemID)
#print(cur_item) print(cur_item)
parent_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID) parent_id_field_index = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if parent_id_field_index and cur_item and cur_item[parent_id_field_index]: if parent_id_field_index and cur_item and cur_item[parent_id_field_index]:
subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT subscribe_type = bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT
item_id = cur_item[parent_id_field_index] item_id = cur_item[parent_id_field_index]
await self.SendSubscribe(subscribe_type, item_id, a_EditUserID, a_ItemID) await self.SendSubscribe(subscribe_type, item_id, a_EditUserID)
def RegisterEdit(self, a_Field, a_AccessMode = user_access.AccessMode.EDIT): def RegisterEdit(self, a_Field, a_AccessMode = user_access.AccessMode.EDIT):
a_ButtonName = self.GetButton(EditButton(a_Field.m_Destiny)) a_ButtonName = self.GetButton(EditButton(a_Field.m_Destiny))
@ -516,7 +432,7 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True) a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True)
a_Prefix = bd_item_edit.EditBDItemRegisterHandlers(self.m_Bot, \ bd_item_edit.EditBDItemRegisterHandlers(self.m_Bot, \
self.SelectSourceTemplate(a_Prefix, a_ButtonName), \ self.SelectSourceTemplate(a_Prefix, a_ButtonName), \
MakeFSMForEdit(self.GetName(), a_FieldName), \ MakeFSMForEdit(self.GetName(), a_FieldName), \
self.GetMessage(Messages.SELECT_TO_EDIT), \ self.GetMessage(Messages.SELECT_TO_EDIT), \
@ -535,10 +451,8 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
field_type = a_FieldType\ field_type = a_FieldType\
) )
self.m_EditPrefix.update({a_Field.m_Destiny: a_Prefix}) def GetAddFields(self):
add_destiny = (
def GetAddFieldDestinys(self):
return (
bd_table.TableFieldDestiny.NAME, bd_table.TableFieldDestiny.NAME,
bd_table.TableFieldDestiny.DESC, bd_table.TableFieldDestiny.DESC,
bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldDestiny.PHOTO,
@ -552,9 +466,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
bd_table.TableFieldDestiny.USER_CONTACTS, bd_table.TableFieldDestiny.USER_CONTACTS,
bd_table.TableFieldDestiny.USER_CONFIRM, bd_table.TableFieldDestiny.USER_CONFIRM,
) )
def GetAddFields(self):
add_destiny = self.GetAddFieldDestinys()
fields = [] fields = []
for f in self.m_Table.GetFields(): for f in self.m_Table.GetFields():
if f.m_Destiny in add_destiny: if f.m_Destiny in add_destiny:
@ -651,7 +562,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
GetAccess,\ GetAccess,\
access_mode = user_access.AccessMode.VIEW\ access_mode = user_access.AccessMode.VIEW\
) )
self.m_ShowPrefix = a_Prefix
bd_item_view.ShowBDItemRegisterHandlers(self.m_Bot,\ bd_item_view.ShowBDItemRegisterHandlers(self.m_Bot,\
a_Prefix,\ a_Prefix,\
table_name,\ table_name,\
@ -666,8 +576,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_ButtonName = self.GetButton(ButtonNames.DEL) a_ButtonName = self.GetButton(ButtonNames.DEL)
if a_ButtonName: if a_ButtonName:
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW) a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW)
self.m_DelPrefix = a_Prefix
bd_item_delete.DeleteBDItemRegisterHandlers(self.m_Bot, \ bd_item_delete.DeleteBDItemRegisterHandlers(self.m_Bot, \
a_Prefix, \ a_Prefix, \
table_name, \ table_name, \
@ -682,7 +590,6 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
a_ButtonName = self.GetButton(ButtonNames.ADD) a_ButtonName = self.GetButton(ButtonNames.ADD)
if a_ButtonName: if a_ButtonName:
a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True) a_Prefix = self.RegisterSelect(a_ButtonName, user_access.AccessMode.VIEW, only_parent = True)
self.m_AddPrefix = a_Prefix
check_func = bd_item.GetCheckForTextFunc(a_ButtonName) check_func = bd_item.GetCheckForTextFunc(a_ButtonName)
if a_Prefix: if a_Prefix:
@ -714,6 +621,9 @@ class TableOperateModule(mod_simple_message.SimpleMessageModule):
bd_item.GetCheckForTextFunc(a_ButtonName)\ bd_item.GetCheckForTextFunc(a_ButtonName)\
) )
address_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.ADDRESS)
status_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.STATUS)
for f in self.m_Table.GetFields(): for f in self.m_Table.GetFields():
self.RegisterEdit(f) self.RegisterEdit(f)

80
bot_modules/orders.py

@ -33,15 +33,13 @@ access_field = 'orderAccess'
create_datetime_field = 'orderCreateDateTime' create_datetime_field = 'orderCreateDateTime'
parent_id_field = 'catID' parent_id_field = 'catID'
pay_field = bd_table.TableField(photo_pay_field, bd_table.TableFieldDestiny.PHOTO_PAY, bd_table.TableFieldType.PHOTO)
table = bd_table.Table(table_name, [ table = bd_table.Table(table_name, [
bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT), bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
bd_table.TableField(user_id_field, bd_table.TableFieldDestiny.USER_ID, bd_table.TableFieldType.INT), bd_table.TableField(user_id_field, bd_table.TableFieldDestiny.USER_ID, bd_table.TableFieldType.INT),
bd_table.TableField(name_field, bd_table.TableFieldDestiny.NAME, bd_table.TableFieldType.STR), 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(desc_field, bd_table.TableFieldDestiny.DESC, bd_table.TableFieldType.STR),
bd_table.TableField(photo_field, bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldType.PHOTO), bd_table.TableField(photo_field, bd_table.TableFieldDestiny.PHOTO, bd_table.TableFieldType.PHOTO),
pay_field, bd_table.TableField(photo_pay_field, bd_table.TableFieldDestiny.PHOTO_PAY, bd_table.TableFieldType.PHOTO),
bd_table.TableField(status_field, bd_table.TableFieldDestiny.STATUS, bd_table.TableFieldType.ENUM, a_Enum = OrderStatus), bd_table.TableField(status_field, bd_table.TableFieldDestiny.STATUS, bd_table.TableFieldType.ENUM, a_Enum = OrderStatus),
bd_table.TableField(address_field, bd_table.TableFieldDestiny.ADDRESS, bd_table.TableFieldType.STR), bd_table.TableField(address_field, bd_table.TableFieldDestiny.ADDRESS, bd_table.TableFieldType.STR),
bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR), bd_table.TableField(access_field, bd_table.TableFieldDestiny.ACCESS, bd_table.TableFieldType.STR),
@ -59,12 +57,11 @@ class ButtonNames(Enum):
button_names = { button_names = {
mod_simple_message.ButtonNames.START: "🛒 Заказы", mod_simple_message.ButtonNames.START: "🛒 Заказы",
mod_table_operate.ButtonNames.SHOW: "☐ Открыть заказ",
mod_table_operate.ButtonNames.LIST: "📃 Список моих текущих заказов", mod_table_operate.ButtonNames.LIST: "📃 Список моих текущих заказов",
ButtonNames.LIST_ALL: "📃 Список всех моих заказов", ButtonNames.LIST_ALL: "📃 Список всех моих заказов",
mod_table_operate.ButtonNames.ADD: "✅ Добавить заказ", mod_table_operate.ButtonNames.ADD: "✅ Добавить заказ",
mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать мой заказ", mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать мой заказ",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO_PAY): "🧾 Загрузить чек по оплате моего заказа", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO_PAY): " Загрузить чек по оплате моего заказа",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в моём заказе", mod_table_operate.EditButton(bd_table.TableFieldDestiny.PHOTO): "☐ Изменить изображение в моём заказе",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.NAME): "≂ Изменить название в моём заказе", mod_table_operate.EditButton(bd_table.TableFieldDestiny.NAME): "≂ Изменить название в моём заказе",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание в моём заказе", mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание в моём заказе",
@ -85,9 +82,7 @@ messages = {
Ошибка, заказ не найден Ошибка, заказ не найден
''', ''',
mod_table_operate.Messages.OPEN: f''' mod_table_operate.Messages.OPEN: f'''
<b>Номер заказа: #{key_name} </b> <b>Заказ: #{name_field}</b>
<b>Имя заказа: #{name_field}</b>
<b>Описание и состав заказа:</b> #{desc_field} <b>Описание и состав заказа:</b> #{desc_field}
@ -110,8 +105,8 @@ messages = {
mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.PHOTO): ''' mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.PHOTO): '''
Создание заказа. Шаг 3 Создание заказа. Шаг 3
Загрузите фотографию для заказа. Фото будет отображаться в его описании. Загрузите обложку для заказа (Фото):
(Если фотографии нет, то нажмите "⏩ Пропустить") Она будет отображаться в его описании.
''', ''',
mod_table_operate.Messages.SUCCESS_CREATE: '''✅ Заказ успешно добавлен!''', mod_table_operate.Messages.SUCCESS_CREATE: '''✅ Заказ успешно добавлен!''',
mod_table_operate.Messages.START_EDIT: ''' mod_table_operate.Messages.START_EDIT: '''
@ -135,7 +130,7 @@ messages = {
''', ''',
mod_table_operate.EditMessage(bd_table.TableFieldDestiny.DESC): f''' mod_table_operate.EditMessage(bd_table.TableFieldDestiny.DESC): f'''
Текущее описание заказа: Текущее описание заказа:
<code>#{desc_field}</code> #{desc_field}
Введите новое описание заказа: Введите новое описание заказа:
''', ''',
@ -176,18 +171,8 @@ messages_subscribes = {
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL):f'''Заказ удалён''', mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL):f'''Заказ удалён''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_EDIT):f'''Заказ отредактирован #item_id''', mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_EDIT):f'''Заказ отредактирован #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_DEL):f'''Заказ удалён #item_id''', mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_DEL):f'''Заказ удалён #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT):f'''Заказ создан mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT):f'''Заказ создан''',
<b>Номер заказа: #{key_name} </b> mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT):f'''Заказ отредактирован''',
<b>Имя заказа: #{name_field}</b>
<b>Описание и состав заказа:</b> #{desc_field}''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT):f'''Заказ отредактирован
<b>Номер заказа: #{key_name} </b>
<b>Имя заказа: #{name_field}</b>
<b>Описание и состав заказа:</b> #{desc_field}''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT):f'''Заказ удалён''', mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT):f'''Заказ удалён''',
} }
@ -232,52 +217,12 @@ class ModuleOrders(mod_table_operate.TableOperateModule):
def AddBDItemFunc(self, a_ItemData, a_UserID): def AddBDItemFunc(self, a_ItemData, a_UserID):
parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID) parent_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
a_ItemData[name_field] = ''
a_ItemData[photo_field] = ''
a_ItemData[user_id_field] = a_UserID a_ItemData[user_id_field] = a_UserID
a_ItemData[status_field] = str(OrderStatus.NEW) a_ItemData[status_field] = str(OrderStatus.NEW)
a_ItemData[address_field] = '' a_ItemData[address_field] = ''
a_ItemData[photo_pay_field] = '0' a_ItemData[photo_pay_field] = '0'
return super().AddBDItemFunc(a_ItemData, a_UserID) return super().AddBDItemFunc(a_ItemData, a_UserID)
def GetModuleButtons(self):
buttons = super().GetModuleButtons();
add_order_button = [
keyboard.ButtonWithAccess(self.GetButton(mod_table_operate.ButtonNames.ADD), user_access.AccessMode.ADD, self.GetAccess()),
]
return add_order_button + buttons
def GetNameFromDesc(self, a_Desc):
name = ''
lines = str(a_Desc).splitlines()
for l in lines:
name = l.strip();
if len(name) > 0:
break;
return name[0:16]
def GetButtonNameAndKeyValueAndAccess(self, a_Item):
user_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.USER_ID)
key_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.KEY)
desc_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.DESC)
n, k, a = super().GetButtonNameAndKeyValueAndAccess(a_Item)
return str(a_Item[key_field_id]) + ":" + str(self.GetNameFromDesc(a_Item[desc_field_id])), k, a
def GetAddFieldDestinys(self):
return (
bd_table.TableFieldDestiny.DESC,
)
def GetStartButtons(self, a_Message, a_UserGroups):
pay_access = self.GetAccessForEditKeyboardButtons(pay_field)
return [
[mod_table_operate.ButtonNames.ADD, user_access.AccessMode.ADD],
[mod_table_operate.EditButton(pay_field.m_Destiny), pay_access],
[mod_table_operate.ButtonNames.EDIT, user_access.AccessMode.EDIT],
[mod_table_operate.ButtonNames.LIST, user_access.AccessMode.VIEW],
[mod_table_operate.ButtonNames.DEL, user_access.AccessMode.DELETE],
]
def GetStartKeyboardButtons(self, a_Message, a_UserGroups): def GetStartKeyboardButtons(self, a_Message, a_UserGroups):
parent_buttons = super().GetStartKeyboardButtons(a_Message, a_UserGroups) parent_buttons = super().GetStartKeyboardButtons(a_Message, a_UserGroups)
cur_buttons = [ cur_buttons = [
@ -325,12 +270,3 @@ class ModuleUserOrders(ModuleOrders):
def GetName(self): def GetName(self):
return module_name return module_name
def GetShowItemInlineKeyboard(self, a_Message, a_UserGroups, a_ItemID):
all_orders_mod = self.GetModule('all_orders')
if not all_orders_mod:
return None
cur_buttons = [
keyboard.InlineButtonWithAccess(all_orders_mod.GetButton(mod_table_operate.ButtonNames.SHOW), all_orders_mod.GetShowPrefix(), a_ItemID, all_orders_mod.GetAccess(), user_access.AccessMode.VIEW),
]
return keyboard.MakeInlineKeyboardButtons(self.m_Bot, cur_buttons, a_UserGroups)

2
bot_modules/orders_cat.py

@ -1,7 +1,7 @@
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> # Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Категории для заказов # Проекты
from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes
from bot_modules import mod_table_operate, mod_simple_message from bot_modules import mod_table_operate, mod_simple_message

143
bot_modules/user_message.py

@ -1,143 +0,0 @@
# -*- coding: utf8 -*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Сообщения пользователю
from bot_sys import bot_bd, keyboard, user_access, bd_table, bot_subscribes, bot_messages, interfaces
from bot_modules import mod_table_operate, mod_simple_message, users
# ---------------------------------------------------------
# БД
module_name = 'user_messge'
table_name = module_name
key_name = 'userMessageID'
desc_field = 'messageText'
access_field = 'catAccess'
create_datetime_field = 'catCreateDateTime'
parent_id_field = users.key_name
table = bd_table.Table(table_name, [
bd_table.TableField(key_name, bd_table.TableFieldDestiny.KEY, bd_table.TableFieldType.INT),
bd_table.TableField(desc_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),
bd_table.TableField(parent_id_field, bd_table.TableFieldDestiny.PARENT_ID, bd_table.TableFieldType.INT),
])
init_access = f'{user_access.user_access_group_new}=-'
# ---------------------------------------------------------
# Сообщения и кнопки
button_names = {
mod_simple_message.ButtonNames.START: "📩 Сообщения для польователей",
mod_table_operate.ButtonNames.LIST: "📃 Список отправленных сообщений",
mod_table_operate.ButtonNames.ADD: "📨 Отправить сообщение",
mod_table_operate.ButtonNames.EDIT: "🛠 Редактировать сообщение",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.DESC): "𝌴 Изменить описание у сообщения",
mod_table_operate.EditButton(bd_table.TableFieldDestiny.ACCESS): "✋ Изменить доступ к сообщению",
mod_table_operate.ButtonNames.DEL: "❌ Удалить отправленное сообщение",
}
messages = {
mod_simple_message.Messages.START: f'''
<b>{button_names[mod_simple_message.ButtonNames.START]}</b>
''',
mod_table_operate.Messages.SELECT: '''
Пожалуйста, выберите сообщение:
''',
mod_table_operate.Messages.ERROR_FIND: '''
Ошибка, сообщение не найдено
''',
mod_table_operate.Messages.OPEN: f'''
<b>Категория: </b>
Пользоватеель: #{parent_id_field}
Текст сообщения: #{desc_field}
Время создания: #{create_datetime_field}
''',
mod_table_operate.CreateMessage(bd_table.TableFieldDestiny.DESC): '''
Отправка сообщения
Введите текст сообщения:
''',
mod_table_operate.Messages.SUCCESS_CREATE: f'''✅ Сообщение успешно отправлено пользователю!''',
mod_table_operate.Messages.START_EDIT: '''
Пожалуйста, выберите действие:
''',
mod_table_operate.Messages.SELECT_TO_EDIT: '''
Выберите проект, который вы хотите отредактировать.
''',
mod_table_operate.EditMessage(bd_table.TableFieldDestiny.DESC): f'''
Текущий текст сообщения:
#{desc_field}
Введите новый текст:
''',
mod_table_operate.EditMessage(bd_table.TableFieldDestiny.ACCESS): f'''
Текущий доступ к сообщению:
#{access_field}
{user_access.user_access_readme}
Введите новую строку доступа:
''',
mod_table_operate.Messages.SUCCESS_EDIT: '''✅ Сообщение успешно отредактировано!''',
mod_table_operate.Messages.SELECT_TO_DELETE: '''
Выберите категорию, которую вы хотите удалить.
''',
mod_table_operate.Messages.SUCCESS_DELETE: '''✅ Сообщение успешно удалёно!''',
}
messages_subscribes = {
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD):f'''Сообщение создано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT):f'''Сообщение отредактировано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL):f'''Сообщение удалено''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_EDIT):f'''Сообщение отредактировано #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ITEM_DEL):f'''Сообщение удалено #item_id''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_ADD_WITH_PARENT):f'''Сообщение создано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_EDIT_WITH_PARENT):f'''Сообщение отредактировано''',
mod_table_operate.SubscribeMessage(bot_subscribes.SubscribeType.ANY_ITEM_DEL_WITH_PARENT):f'''Сообщение удалено''',
}
messages.update(messages_subscribes)
class ModuleUserMessage(mod_table_operate.TableOperateModule):
def __init__(self, a_ParentModName, a_ChildModName, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log):
super().__init__(table, messages, button_names, a_ParentModName, a_ChildModName, init_access, init_access, a_ChildModuleNameList, a_EditModuleNameList, a_Bot, a_ModuleAgregator, a_BotMessages, a_BotButtons, a_BotSubscribes, a_Log)
def GetName(self):
return module_name
async def AddBDItemFunc(self, a_ItemData, a_UserID):
res, error = await super().AddBDItemFunc(a_ItemData, a_UserID)
desc_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.DESC)
user_id_field = self.m_Table.GetFieldNameByDestiny(bd_table.TableFieldDestiny.PARENT_ID)
if not await self.SendMessageToUser(bot_messages.MakeBotMessage(a_ItemData[desc_field]), a_ItemData[user_id_field], parse_mode=interfaces.ParseMode.HTML.value):
return None, '❌ Ошибка отправки сообщения'
return res, error
def GetStartButtons(self, a_Message, a_UserGroups):
return [
[mod_table_operate.ButtonNames.ADD, user_access.AccessMode.ADD],
[mod_table_operate.ButtonNames.LIST, user_access.AccessMode.VIEW],
[mod_table_operate.ButtonNames.EDIT, user_access.AccessMode.EDIT],
[mod_table_operate.ButtonNames.DEL, user_access.AccessMode.DELETE],
]
def GetButtonNameAndKeyValueAndAccess(self, a_Item):
key_name_id = self.GetKeyFieldID()
date_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.CREATE_DATE)
access_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)
assert key_name_id != None
assert date_field_id != None
assert access_field_id != None
return \
a_Item[date_field_id],\
a_Item[key_name_id],\
a_Item[access_field_id]

13
bot_modules/users.py

@ -66,7 +66,7 @@ messages = {
<b>Пользователь: #{name_field}</b> <b>Пользователь: #{name_field}</b>
<b>ID:</b> #{key_name} <b>ID:</b> #{key_name}
<b>Имя:</b> @#{name_field} <b>Имя:</b> #{name_field}
<b>Имя1:</b> #{name1_field} <b>Имя1:</b> #{name1_field}
<b>Имя2:</b> #{name2_field} <b>Имя2:</b> #{name2_field}
<b>Код языка:</b> #{language_code_field} <b>Код языка:</b> #{language_code_field}
@ -133,17 +133,6 @@ class ModuleUsers(mod_table_operate.TableOperateModule):
def GetName(self): def GetName(self):
return module_name return module_name
def GetButtonNameAndKeyValueAndAccess(self, a_Item):
key_name_id = self.GetKeyFieldID()
name_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.NAME)
access_field_id = self.m_Table.GetFieldIDByDestiny(bd_table.TableFieldDestiny.ACCESS)
assert key_name_id != None
assert name_field_id != None
assert access_field_id != None
return \
a_Item[name_field_id] + '(' + str(a_Item[key_name_id]) + ')',\
a_Item[key_name_id],\
a_Item[access_field_id]
# Работа с базой данных пользователей # Работа с базой данных пользователей

10
bot_sys/bd_table.py

@ -109,9 +109,7 @@ class Table:
result = a_String result = a_String
for i in range(len(self.m_Fields)): for i in range(len(self.m_Fields)):
f = self.m_Fields[i] f = self.m_Fields[i]
name = f'#{f.m_Name}' result = result.replace(f'#{f.m_Name}', str(a_BDItem[i]))
result = result.replace('<code>' + name + '</code>', '<code>' + str(a_BDItem[i]).replace('\n', '</code>\n<code>') + '</code>')
result = result.replace(name, str(a_BDItem[i]))
return result return result
def ReplaceAllFieldTagsByDict(self, a_String, a_BDItemDict): def ReplaceAllFieldTagsByDict(self, a_String, a_BDItemDict):
@ -170,7 +168,5 @@ def Test():
print(table.GetInitTableRequest()) print(table.GetInitTableRequest())
assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT, f3 TEXT, f4 TEXT, UNIQUE(f1), UNIQUE(f2, f3));' assert table.GetInitTableRequest() == 'CREATE TABLE IF NOT EXISTS tname(f1 INTEGER PRIMARY KEY, f2 TEXT, f3 TEXT, f4 TEXT, UNIQUE(f1), UNIQUE(f2, f3));'
item = [10, 'i2\ni2.1', 'i3', 'i4'] item = [10, 'i2', 'i3', 'i4']
assert table.ReplaceAllFieldTags('#f1 #f2 #f3 #f4', item) == '10 i2\ni2.1 i3 i4' assert table.ReplaceAllFieldTags('#f1 #f2 #f3 #f4', item) == '10 i2 i3 i4'
assert table.ReplaceAllFieldTags('#f1 <code>#f2</code> <code>#f3</code> #f4', item) == '10 <code>i2</code>\n<code>i2.1</code> <code>i3</code> i4', table.ReplaceAllFieldTags('f1 <code>#f2</code> <code>#f3</code> #f4', item)

1
bot_sys/bot_bd.py

@ -21,7 +21,6 @@ def RequestSelectTemplate(a_Bot, a_TableName):
return SelectBD return SelectBD
def SQLRequest(a_Log, a_BDFileName, a_Request : str, commit = False, return_error = False, param = None): def SQLRequest(a_Log, a_BDFileName, a_Request : str, commit = False, return_error = False, param = None):
#print('SQLRequest', a_Request, commit)
db = sqlite3.connect(a_BDFileName) db = sqlite3.connect(a_BDFileName)
cursor = db.cursor() cursor = db.cursor()
result = [] result = []

42
bot_sys/bot_messages.py

@ -37,40 +37,23 @@ class BotMessage:
msg = self.GetMessageForLang(self.m_Language) msg = self.GetMessageForLang(self.m_Language)
return msg.GetDesc() return msg.GetDesc()
def GetCurLang(self, a_Language):
msg = self.m_BotMessages.GetMessages()
if not msg.get(a_Language, None):
a_Language = self.m_Language
if not msg.get(a_Language, None):
a_Language = self.m_BotMessages.a_DefaultLanguage
return a_Language
def FindMessageForLang(self, a_Name, a_Language):
if not self.m_BotMessages:
return None
msg = self.m_BotMessages.GetMessages()
a_Language = self.GetCurLang(a_Language)
if not msg.get(a_Language, None):
return None
new_msg = msg[a_Language].get(a_Name, None)
return new_msg
def GetMessageForLang(self, a_Language): def GetMessageForLang(self, a_Language):
if not self.m_BotMessages: if not self.m_BotMessages:
return self return self
last_update = self.m_BotMessages.m_LastUpdate last_update = self.m_BotMessages.m_LastUpdate
new_msg = self new_msg = self
if self.m_DateTime < last_update: if self.m_DateTime < last_update:
a_Language = self.GetCurLang(a_Language) msg = self.m_BotMessages.GetMessages()
new_msg = self.FindMessageForLang(self.m_MessageName, a_Language) if not msg.get(a_Language, None):
if not new_msg: a_Language = self.m_Language
new_msg = self if not msg.get(a_Language, None):
elif a_Language == self.m_Language: a_Language = self.m_BotMessages.a_DefaultLanguage
new_msg = msg[a_Language].get(self.m_MessageName, self)
if a_Language == self.m_Language:
self.m_MessageDesc = new_msg.m_MessageDesc self.m_MessageDesc = new_msg.m_MessageDesc
self.m_Language = new_msg.m_Language self.m_Language = new_msg.m_Language
self.m_PhotoID = new_msg.m_PhotoID self.m_PhotoID = new_msg.m_PhotoID
self.m_DateTime = last_update self.m_DateTime = new_msg.m_DateTime
return new_msg return new_msg
def MakeBotMessage(a_MessageDesc): def MakeBotMessage(a_MessageDesc):
@ -85,15 +68,6 @@ class BotMessages:
def GetMessages(self): def GetMessages(self):
return self.m_Messages return self.m_Messages
def CheckDescExist(self, a_MessageDesc):
msg = self.GetMessages()
for l, m in msg.items():
for name, cur_msg in m.items():
if cur_msg.GetDesc() == a_MessageDesc:
return True
return False
def UpdateSignal(self, a_DateTime): def UpdateSignal(self, a_DateTime):
self.m_LastUpdate = a_DateTime self.m_LastUpdate = a_DateTime

5
bot_sys/config.py

@ -1,7 +1,7 @@
#-*-coding utf-8-*- #-*-coding utf-8-*-
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru> # Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Настнойка основных параметров системы # Настройка основных параметров системы
# --------------------------------------------------------- # ---------------------------------------------------------
# API токен телеграмм бота. Создаётся с помощью @BotFather # API токен телеграмм бота. Создаётся с помощью @BotFather
@ -57,5 +57,4 @@ def GetRootIDs():
for i in root_ids: for i in root_ids:
g_root_ids += [ClearReadLine(i)] g_root_ids += [ClearReadLine(i)]
return g_root_ids return g_root_ids

8
bot_sys/interfaces.py

@ -3,14 +3,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from enum import Enum
from enum import auto
# Способ форматирования
class ParseMode(Enum):
HTML = "HTML"
Markdown ='Markdown'
class IBot(ABC): class IBot(ABC):
@abstractmethod @abstractmethod
def GetRootIDs(self): def GetRootIDs(self):

7
main.py

@ -7,7 +7,6 @@ from bot_sys import config, log, aiogram_bot, bot_messages, bd_table, user_acces
from bot_modules import mod_agregator, start, profile, backup, users_groups_agregator, access, projects, tasks, needs, comments from bot_modules import mod_agregator, start, profile, backup, users_groups_agregator, access, projects, tasks, needs, comments
from bot_modules import languages, messages, buttons, users, groups, user_in_groups from bot_modules import languages, messages, buttons, users, groups, user_in_groups
from bot_modules import orders_cat, orders, all_orders, authorize from bot_modules import orders_cat, orders, all_orders, authorize
from bot_modules import user_message
from bot_modules import bd_version from bot_modules import bd_version
from bot_sys import bot_subscribes from bot_sys import bot_subscribes
@ -44,7 +43,6 @@ mod_user_in_groups_name = user_in_groups.module_name
mod_orders_cat_name = orders_cat.module_name mod_orders_cat_name = orders_cat.module_name
mod_orders_name = orders.module_name mod_orders_name = orders.module_name
mod_all_orders_name = all_orders.module_name mod_all_orders_name = all_orders.module_name
mod_user_message_name = user_message.module_name
start_mod_list = [mod_start_name] start_mod_list = [mod_start_name]
mod_access = access.ModuleAccess(start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) mod_access = access.ModuleAccess(start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
@ -53,9 +51,6 @@ g_ModuleAgregator.AddModule(mod_access)
mod_users = users.ModuleUsers(None, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) mod_users = users.ModuleUsers(None, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_users) g_ModuleAgregator.AddModule(mod_users)
mod_user_message = user_message.ModuleUserMessage(mod_users_name, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_user_message)
child_mod_name_list = [mod_start_name, mod_users_name, mod_user_in_groups_name] child_mod_name_list = [mod_start_name, mod_users_name, mod_user_in_groups_name]
mod_groups = groups.ModuleGroups(None, mod_user_in_groups_name, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) mod_groups = groups.ModuleGroups(None, mod_user_in_groups_name, child_mod_name_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_groups) g_ModuleAgregator.AddModule(mod_groups)
@ -63,7 +58,7 @@ g_ModuleAgregator.AddModule(mod_groups)
mod_user_in_groups = user_in_groups.ModuleUserInGroups(mod_groups_name, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) mod_user_in_groups = user_in_groups.ModuleUserInGroups(mod_groups_name, None, start_mod_list, start_mod_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_user_in_groups) g_ModuleAgregator.AddModule(mod_user_in_groups)
child_mod_name_list = [mod_start_name, mod_users_name, mod_groups_name, mod_user_in_groups_name, mod_user_message_name] child_mod_name_list = [mod_start_name, mod_users_name, mod_groups_name, mod_user_in_groups_name]
mod_users_groups_agregator = users_groups_agregator.ModuleUsersGroupsAgregator(child_mod_name_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log) mod_users_groups_agregator = users_groups_agregator.ModuleUsersGroupsAgregator(child_mod_name_list, g_Bot, g_ModuleAgregator, g_BotMessages, g_BotButtons, g_BotSubscribes, g_Log)
g_ModuleAgregator.AddModule(mod_users_groups_agregator) g_ModuleAgregator.AddModule(mod_users_groups_agregator)

3
requirements.txt

@ -5,4 +5,5 @@ Django==2.2.1
python-dotenv==0.21.1 python-dotenv==0.21.1
requests==2.31.0 requests==2.31.0
urllib3==1.25.11 urllib3==1.25.11
django-admin-list-filter-dropdown==1.0.3
django-admin-rangefilter==0.11.2

31
template/bd_item_edit.py

@ -18,23 +18,7 @@ error_photo_type_message = '''
def StartEditBDItemTemplate(a_Bot, a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT): def StartEditBDItemTemplate(a_Bot, a_FSM, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT):
return bd_item_add.StartAddBDItemTemplate(a_Bot, a_FSM, a_FSM.item_field, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = access_mode) return bd_item_add.StartAddBDItemTemplate(a_Bot, a_FSM, a_FSM.item_field, a_MessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, a_ButtonFunc, a_FinishButtonFunc, access_mode = access_mode)
def FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text): def FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
return bd_item_add.FinishAddBDItemTemplate(a_Bot, a_FSM, a_EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type)
def CustomEditBDItemRegisterHandlers(a_Bot, a_EditBDItemFunc, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
keyboard = bd_item.MixKeyboardFuncTemplate(a_AdditionalButtonFunc, bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode))
a_Prefix = bd_item_select.SelectRegisterHandlers(a_Bot, a_SelectSource, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = access_mode)
a_Bot.RegisterCallbackHandler(StartEditBDItemTemplate(a_Bot, a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard, a_FinishButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix))
if field_type == bd_item.FieldType.photo:
a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field)
else:
a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_EditBDItemFunc, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, access_mode, field_type = field_type), state = a_FSM.item_field)
return a_Prefix
def EditBDItemRegisterHandlers(a_Bot, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
async def EditBDItemFunc(a_ItemData, a_UserID): async def EditBDItemFunc(a_ItemData, a_UserID):
item_id = a_ItemData[a_KeyName] item_id = a_ItemData[a_KeyName]
field_value = a_ItemData[a_FieldName] field_value = a_ItemData[a_FieldName]
@ -46,5 +30,16 @@ def EditBDItemRegisterHandlers(a_Bot, a_SelectSource, a_FSM, a_StartMessage, a_E
await a_OnChangeFunc(item_id, a_ItemData, a_UserID) await a_OnChangeFunc(item_id, a_ItemData, a_UserID)
return res, error return res, error
return bd_item_add.FinishAddBDItemTemplate(a_Bot, a_FSM, EditBDItemFunc, a_TableName, a_KeyName, a_FieldName, a_MessageFunc, a_PostProcessFunc, a_AccessFunc, a_ButtonFunc, access_mode = access_mode, field_type = field_type)
return CustomEditBDItemRegisterHandlers(a_Bot, EditBDItemFunc, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, access_mode = access_mode, field_type = field_type) def EditBDItemRegisterHandlers(a_Bot, a_SelectSource, a_FSM, a_StartMessage, a_EditMessageFunc, a_FinishMessageFunc, a_TableName, a_KeyName, a_FieldName, a_PostProcessFunc, a_GetButtonNameAndKeyValueAndAccessFunc, a_AccessFunc, a_AdditionalButtonFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode = user_access.AccessMode.EDIT, field_type = bd_item.FieldType.text):
keyboard = bd_item.MixKeyboardFuncTemplate(a_AdditionalButtonFunc, bd_item.GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, access_mode))
a_Prefix = bd_item_select.SelectRegisterHandlers(a_Bot, a_SelectSource, a_GetButtonNameAndKeyValueAndAccessFunc, a_StartMessage, a_AccessFunc, access_mode = access_mode)
a_Bot.RegisterCallbackHandler(StartEditBDItemTemplate(a_Bot, a_FSM, a_EditMessageFunc, a_TableName, a_KeyName, a_Prefix, a_AccessFunc, keyboard, a_FinishButtonFunc, access_mode), bd_item.GetCheckForPrefixFunc(a_Prefix))
if field_type == bd_item.FieldType.photo:
a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), content_types = ['photo', 'text'], state = a_FSM.item_field)
else:
a_Bot.RegisterMessageHandler(FinishEditBDItemTemplate(a_Bot, a_FSM, a_TableName, a_KeyName, a_FieldName, a_FinishMessageFunc, a_PostProcessFunc, a_AccessFunc, a_FinishButtonFunc, a_OnChangeFunc, access_mode, field_type = field_type), state = a_FSM.item_field)

2
template/simple_message.py

@ -53,8 +53,6 @@ def SimpleMessageTemplate(a_Bot, a_WorkFunc, a_GetButtonsFunc, a_GetInlineButton
if res is None: if res is None:
return return
user_groups = groups_utils.GetUserGroupData(a_Bot, user_id) # При самом первом запуске a_WorkFunc добавляет пользователя
Inline_keyboard_func = a_GetInlineButtonsFunc Inline_keyboard_func = a_GetInlineButtonsFunc
if res.Inline_keyboard_func: if res.Inline_keyboard_func:
Inline_keyboard_func = res.Inline_keyboard_func Inline_keyboard_func = res.Inline_keyboard_func

4
template/sql_request.py

@ -3,7 +3,7 @@
# Сообщения для работы с sql запросами # Сообщения для работы с sql запросами
from bot_sys import user_access, keyboard, bot_messages, interfaces from bot_sys import user_access, keyboard, bot_messages
from bot_modules import groups_utils from bot_modules import groups_utils
from template import bd_item, simple_message from template import bd_item, simple_message
@ -31,7 +31,7 @@ def RequestToBDTemplate(a_Bot, a_StartMessage, a_GetButtonsFunc, a_AccessFunc, a
await a_FSM.sqlRequest.set() await a_FSM.sqlRequest.set()
print ('a_FSM.sqlRequest.set()', a_StartMessage) print ('a_FSM.sqlRequest.set()', a_StartMessage)
await simple_message.SendMessage(a_Bot, a_StartMessage, GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, a_AccessMode), None, user_id, a_Message, user_groups, parse_mode=interfaces.ParseMode.Markdown.value) await simple_message.SendMessage(a_Bot, a_StartMessage, GetCancelKeyboardButtonsTemplate(a_Bot, a_AccessFunc, a_AccessMode), None, user_id, a_Message, user_groups, parse_mode='Markdown')
return RequestToBDStart return RequestToBDStart
def RequestToBDFinishTemplate(a_Bot, a_GetButtonsFunc, a_AccessFunc, a_AccessMode): def RequestToBDFinishTemplate(a_Bot, a_GetButtonsFunc, a_AccessFunc, a_AccessMode):

Loading…
Cancel
Save