Anton
1 year ago
9 changed files with 165 additions and 124 deletions
@ -1,40 +1,139 @@ |
|||||||
from django.db import models |
from urllib.parse import quote |
||||||
|
|
||||||
import asyncio |
|
||||||
|
|
||||||
|
import requests |
||||||
|
from django.db import models |
||||||
|
from adminpanel.settings import g_telegram_bot_api_token |
||||||
|
|
||||||
class Orders(models.Model): |
class Orders(models.Model): |
||||||
SEND_MESSAGE = ( |
|
||||||
(True, 'Написать'), |
|
||||||
(False, 'Не отправлять'), |
|
||||||
(None, 'Неизвестно'), |
|
||||||
) |
|
||||||
|
|
||||||
IS_APPROVED = ( |
|
||||||
(True, 'Заказ подтвержден'), |
|
||||||
(False, 'Заказ не подтвержден'), |
|
||||||
(None, 'Неизвестно'), |
|
||||||
) |
|
||||||
|
|
||||||
|
|
||||||
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) |
||||||
orderName = models.CharField(max_length=100, verbose_name='наименование', null=True) |
orderName = models.CharField(max_length=100, verbose_name='наименование', null=True) |
||||||
orderDesc = models.TextField(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='чек') |
orderPhotoPay = models.ImageField(upload_to='photo/', verbose_name='чек') |
||||||
orderAddress = models.CharField(max_length=100, verbose_name='адрес доставки', blank=True, null=True) |
orderAddress = models.CharField(max_length=100, verbose_name='адрес доставки', blank=True, null=True) |
||||||
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) |
||||||
|
|
||||||
|
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""" |
||||||
|
<html> |
||||||
|
<body> |
||||||
|
<img src="{photo_url}" width="{width}" height="{height}" class="small-photo"> |
||||||
|
<img src="{photo_url}" width="{large_width}" height="{large_height}" class="large-photo"> |
||||||
|
<a href="#" onclick="togglePhoto()" id="toggle-button" class="button">Увеличить фото</a> |
||||||
|
<script> |
||||||
|
function togglePhoto() {{ |
||||||
|
var smallPhoto = document.getElementsByClassName("small-photo")[0]; |
||||||
|
var largePhoto = document.getElementsByClassName("large-photo")[0]; |
||||||
|
var toggleButton = document.getElementById("toggle-button"); |
||||||
|
|
||||||
|
if (smallPhoto.style.display === "none") {{ |
||||||
|
smallPhoto.style.display = "block"; |
||||||
|
largePhoto.style.display = "none"; |
||||||
|
toggleButton.innerHTML = "Увеличить фото"; |
||||||
|
}} else {{ |
||||||
|
smallPhoto.style.display = "none"; |
||||||
|
largePhoto.style.display = "block"; |
||||||
|
toggleButton.innerHTML = "Уменьшить фото"; |
||||||
|
}} |
||||||
|
}} |
||||||
|
</script> |
||||||
|
<style> |
||||||
|
.large-photo {{ |
||||||
|
display: none; |
||||||
|
}} |
||||||
|
.button {{ |
||||||
|
display: inline-block; |
||||||
|
padding: 10px 20px; |
||||||
|
background-color: #4CAF50; |
||||||
|
color: white; |
||||||
|
text-align: center; |
||||||
|
text-decoration: none; |
||||||
|
font-size: 16px; |
||||||
|
border: none; |
||||||
|
border-radius: 5px; |
||||||
|
cursor: pointer; |
||||||
|
}} |
||||||
|
</style> |
||||||
|
</body> |
||||||
|
</html> |
||||||
|
""" |
||||||
|
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""" |
||||||
|
<html> |
||||||
|
<body> |
||||||
|
<img src="{photo_url}" width="{width}" height="{height}" class="small-photo"> |
||||||
|
<img src="{photo_url}" width="{large_width}" height="{large_height}" class="large-photo"> |
||||||
|
<a href="#" onclick="togglePhotoPay()" id="toggle-button" class="button">Увеличить фото</a> |
||||||
|
<script> |
||||||
|
function togglePhotoPay() {{ |
||||||
|
var smallPhotoPay = document.getElementsByClassName("small-photo")[1]; |
||||||
|
var largePhotoPay = document.getElementsByClassName("large-photo")[1]; |
||||||
|
var toggleButton = document.getElementById("toggle-button"); |
||||||
|
|
||||||
|
if (smallPhotoPay.style.display === "none") {{ |
||||||
|
smallPhotoPay.style.display = "block"; |
||||||
|
largePhotoPay.style.display = "none"; |
||||||
|
toggleButton.innerHTML = "Увеличить фото"; |
||||||
|
}} else {{ |
||||||
|
smallPhotoPay.style.display = "none"; |
||||||
|
largePhotoPay.style.display = "block"; |
||||||
|
toggleButton.innerHTML = "Уменьшить фото"; |
||||||
|
}} |
||||||
|
}} |
||||||
|
</script> |
||||||
|
<style> |
||||||
|
.large-photo {{ |
||||||
|
display: none; |
||||||
|
}} |
||||||
|
.button {{ |
||||||
|
display: inline-block; |
||||||
|
padding: 10px 20px; |
||||||
|
background-color: #4CAF50; |
||||||
|
color: white; |
||||||
|
text-align: center; |
||||||
|
text-decoration: none; |
||||||
|
font-size: 16px; |
||||||
|
border: none; |
||||||
|
border-radius: 5px; |
||||||
|
cursor: pointer; |
||||||
|
}} |
||||||
|
</style> |
||||||
|
</body> |
||||||
|
</html> |
||||||
|
""" |
||||||
|
return html |
||||||
|
|
||||||
class Meta: |
class Meta: |
||||||
# app_label = 'adminpanel' |
|
||||||
verbose_name_plural = 'Заказы' |
verbose_name_plural = 'Заказы' |
||||||
managed = False |
managed = False |
||||||
db_table = 'orders' |
db_table = 'orders' |
||||||
|
|
||||||
# def __str__(self): |
|
||||||
# return self.name |
|
Loading…
Reference in new issue