You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.9 KiB
132 lines
4.9 KiB
1 year ago
|
import requests
|
||
|
from django.http import HttpResponseRedirect
|
||
|
from django.utils.html import format_html
|
||
|
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
||
|
|
||
|
from adminpanelapp.settings import g_telegram_bot_api_token
|
||
|
from .models import Orders
|
||
|
|
||
|
|
||
|
from django.contrib import admin, messages
|
||
|
from django.urls import reverse
|
||
|
|
||
|
|
||
|
|
||
|
class OrdersAdmin(admin.ModelAdmin):
|
||
|
list_display = ('name', 'description', 'time_create', 'adress', 'is_approved', 'orders_photo', 'cheque_image')
|
||
|
list_editable = ('is_approved',)
|
||
|
# exclude = ('param_id', 'send_message')
|
||
|
actions =['change_value_and_redirect',]
|
||
|
readonly_fields = ['order_photo', 'cheque']
|
||
|
|
||
|
|
||
|
def orders_photo(self, obj):
|
||
|
if obj.order_photo and obj.order_photo.url:
|
||
|
return format_html('<img src="{}" width="100" height="100" />', obj.order_photo.url)
|
||
|
return "-"
|
||
|
|
||
|
|
||
|
def cheque_image(self, obj):
|
||
|
if obj.cheque and obj.cheque.url:
|
||
|
return format_html('<img src="{}" width="100" height="100" />', obj.cheque.url)
|
||
|
return "-"
|
||
|
|
||
|
# image_tag.short_description = 'Cheque'
|
||
|
|
||
|
# def photo_link(self, obj):
|
||
|
# url = obj.get_absolute_url()
|
||
|
# return format_html('<a href="{}">{}</a>', url, obj.photo_link)
|
||
|
#
|
||
|
# photo_link.short_description = 'photo_link'
|
||
|
|
||
|
|
||
|
|
||
|
# def change_view(self, request, object_id, form_url='', extra_context=None):
|
||
|
# obj = self.get_object(request, object_id)
|
||
|
# extra_context = extra_context or {}
|
||
|
# extra_context['chat_id'] = obj.param_id
|
||
|
#
|
||
|
# if obj.send_message:
|
||
|
# obj.send_message = False
|
||
|
# obj.save()
|
||
|
# url = reverse('send_telegram_message')
|
||
|
# chat_id = obj.param_id
|
||
|
# url = f"{url}?param_id={chat_id}"
|
||
|
# print(chat_id)
|
||
|
# return HttpResponseRedirect(url)
|
||
|
#
|
||
|
#
|
||
|
# return super().change_view(request, object_id, form_url, extra_context)
|
||
|
|
||
|
|
||
|
# def change_view(self, request, object_id, form_url='', extra_context=None):
|
||
|
# obj = self.get_object(request, object_id)
|
||
|
# if obj.send_message:
|
||
|
# url = reverse('send_telegram_message')
|
||
|
#
|
||
|
# return HttpResponseRedirect(url)
|
||
|
#
|
||
|
# return super().change_view(request, object_id, form_url, extra_context)
|
||
|
|
||
|
# def change_value_and_redirect(modeladmin, request, queryset):
|
||
|
#
|
||
|
# # Получаем список выбранных объектов
|
||
|
# selected_objects = request.POST.getlist(ACTION_CHECKBOX_NAME)
|
||
|
# # Проверяем, что выбран только один объект
|
||
|
# if len(selected_objects) != 1:
|
||
|
# # Если выбрано несколько объектов или не выбран ни один, выбрасываем исключение или выводим сообщение пользователю
|
||
|
# raise ValueError("Выберите только один объект")
|
||
|
# # Получаем ID выбранного объекта
|
||
|
# selected_object_id = int(selected_objects[0])
|
||
|
# # Получаем объект по ID
|
||
|
# obj = queryset.get(id=selected_object_id)
|
||
|
# # Изменяем значение ячейки объекта на False
|
||
|
# obj.send_message = True
|
||
|
# obj.save()
|
||
|
# # Редирект на страницу send_tg_message
|
||
|
# url = reverse('send_telegram_message')
|
||
|
# return HttpResponseRedirect(url)
|
||
|
|
||
|
def change_value_and_redirect(modeladmin, request, queryset):
|
||
|
selected_objects = request.POST.getlist(ACTION_CHECKBOX_NAME)
|
||
|
if len(selected_objects) != 1:
|
||
|
messages.error(request, "Выберите только один объект")
|
||
|
return
|
||
|
|
||
|
selected_chat_id = int(selected_objects[0])
|
||
|
obj = queryset.get(id=selected_chat_id)
|
||
|
chat_id = obj.param_id
|
||
|
url = reverse('send_telegram_message', kwargs={'chat_id': chat_id})
|
||
|
return HttpResponseRedirect(url)
|
||
|
|
||
|
|
||
|
change_value_and_redirect.short_description = 'Отправка сообщения'
|
||
|
|
||
|
|
||
|
def save_model(self, request, obj, form, change):
|
||
|
super().save_model(request, obj, form, change)
|
||
|
|
||
|
def send_telegram_message(message):
|
||
|
bot_token = g_telegram_bot_api_token
|
||
|
|
||
|
chat_id = obj.param_id
|
||
|
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
||
|
params = {
|
||
|
'chat_id': chat_id,
|
||
|
'text': message
|
||
|
}
|
||
|
response = requests.get(url, params=params)
|
||
|
|
||
|
if response.status_code == 200:
|
||
|
print("Сообщение успешно отправлено!")
|
||
|
else:
|
||
|
print("Ошибка при отправке сообщения")
|
||
|
|
||
|
if obj.is_approved == True:
|
||
|
message2 = 'Ваш заказ принят. Ожидайте получения'
|
||
|
send_telegram_message(message2)
|
||
|
|
||
|
|
||
|
|
||
|
admin.site.register(Orders, OrdersAdmin,)
|