diff --git a/adminpanel/settings.py b/adminpanel/settings.py index 5719139..e94cc72 100644 --- a/adminpanel/settings.py +++ b/adminpanel/settings.py @@ -37,7 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'adminpanelapp' + 'adminpanelapp', + ] MIDDLEWARE = [ @@ -129,6 +130,3 @@ ASYNC_MODE = 'django' g_telegram_bot_api_token = '6212211018:AAEwcEN0NdjbhqDiClUk8vZkE_vfRUxsReU' -MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') - diff --git a/adminpanel/urls.py b/adminpanel/urls.py index 9e1d200..6f7db62 100644 --- a/adminpanel/urls.py +++ b/adminpanel/urls.py @@ -8,4 +8,4 @@ urlpatterns = [ path('admin/', admin.site.urls), path('', include('adminpanelapp.urls')), path('', RedirectView.as_view(url='/admin/adminpanelapp/orders'), name='') -] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file +] \ No newline at end of file diff --git a/adminpanelapp/admin.py b/adminpanelapp/admin.py index 5b56fdf..e066c49 100644 --- a/adminpanelapp/admin.py +++ b/adminpanelapp/admin.py @@ -1,9 +1,6 @@ -import requests from django.http import HttpResponseRedirect -from django.utils.html import format_html from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME - -from adminpanel.settings import g_telegram_bot_api_token +from django.utils.safestring import mark_safe from .models import Orders @@ -13,25 +10,23 @@ from django.urls import reverse class OrdersAdmin(admin.ModelAdmin): - list_display = ('orderName', 'orderCreateDateTime', 'show_orderPhoto', 'show_orderPhotoPay',) - actions =['change_value_and_redirect', ] - readonly_fields = ['orderPhoto', 'orderPhotoPay'] + list_display = ('orderName', 'orderCreateDateTime', 'orderDesc', 'orderAddress', 'show_photo', 'show_photopay') + actions =['send_message'] + exclude = ['orderAccess', 'userID', 'orderPhoto', 'orderPhotoPay'] + def show_photo(self, obj): + html = obj.get_photo_html() + return mark_safe(html) - def show_orderPhoto(self, obj): - if obj.orderPhoto and obj.orderPhoto.url: - return format_html('', obj.orderPhoto.url) - return "-" - show_orderPhoto.short_description = 'Фото заказа' + show_photo.short_description = 'Фото' - def show_orderPhotoPay(self, obj): - if obj.orderPhotoPay and obj.orderPhotoPay.url: - return format_html('', obj.orderPhotoPay.url) - return "-" - show_orderPhotoPay.short_description = 'Фото чека' + def show_photopay(self, obj): + html = obj.get_photopay_html() + return mark_safe(html) + show_photopay.short_description = 'Чек' - def change_value_and_redirect(orders, request, queryset): + def send_message(orders, request, queryset): selected_objects = request.POST.getlist(ACTION_CHECKBOX_NAME) if len(selected_objects) != 1: messages.error(request, "Выберите только один объект") @@ -44,6 +39,6 @@ class OrdersAdmin(admin.ModelAdmin): return HttpResponseRedirect(url) - change_value_and_redirect.short_description = 'Отправка сообщения' + send_message.short_description = 'Отправка сообщения' admin.site.register(Orders, OrdersAdmin) diff --git a/adminpanelapp/models.py b/adminpanelapp/models.py index 067f4cc..38d817b 100644 --- a/adminpanelapp/models.py +++ b/adminpanelapp/models.py @@ -1,40 +1,139 @@ -from django.db import models - -import asyncio +from urllib.parse import quote +import requests +from django.db import models +from adminpanel.settings import g_telegram_bot_api_token class Orders(models.Model): - SEND_MESSAGE = ( - (True, 'Написать'), - (False, 'Не отправлять'), - (None, 'Неизвестно'), - ) - - IS_APPROVED = ( - (True, 'Заказ подтвержден'), - (False, 'Заказ не подтвержден'), - (None, 'Неизвестно'), - ) orderID = models.AutoField(primary_key=True, verbose_name='id заказа') userID = models.CharField(max_length=100, verbose_name='id пользователя в tg', null=True) orderName = models.CharField(max_length=100, verbose_name='наименование', null=True) orderDesc = models.TextField(verbose_name='описание', null=True) - orderPhoto = models.ImageField(upload_to='photo/', verbose_name='фото') + orderPhoto = models.ImageField(verbose_name='фото', null=True) orderPhotoPay = models.ImageField(upload_to='photo/', verbose_name='чек') orderAddress = 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='дата и время создания') orderStatus = 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): + token = g_telegram_bot_api_token + file_id = self.orderPhoto + url = f"https://api.telegram.org/bot{token}/getFile?file_id={file_id}" + + response = requests.get(url) + data = response.json() + + if data['ok']: + file_path = data["result"]["file_path"] + photo_url = f"https://api.telegram.org/file/bot{token}/{quote(file_path, safe='')}" + + html = f""" + +
+ + + Увеличить фото + + + + + """ + return html + + def get_photopay_html(self, width=100, height=100, large_width=400, large_height=400): + token = g_telegram_bot_api_token + file_id = self.orderPhotoPay + url = f"https://api.telegram.org/bot{token}/getFile?file_id={file_id}" + + response = requests.get(url) + data = response.json() + + if data['ok']: + file_path = data["result"]["file_path"] + photo_url = f"https://api.telegram.org/file/bot{token}/{quote(file_path, safe='')}" + + html = f""" + + + + + Увеличить фото + + + + + """ + return html class Meta: - # app_label = 'adminpanel' + verbose_name_plural = 'Заказы' managed = False db_table = 'orders' - - # def __str__(self): - # return self.name \ No newline at end of file diff --git a/adminpanelapp/urls.py b/adminpanelapp/urls.py index 37a940e..989f17d 100644 --- a/adminpanelapp/urls.py +++ b/adminpanelapp/urls.py @@ -4,4 +4,7 @@ from .views import send_telegram_message urlpatterns = [ path('send_telegram_message/