|
|
|
from urllib.parse import quote
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from adminpanel.configadmin import GetTelegramBotApiToken
|
|
|
|
|
|
|
|
|
|
|
|
class Orders(models.Model):
|
|
|
|
|
|
|
|
|
|
|
|
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(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):
|
|
|
|
file_id = self.orderPhoto
|
|
|
|
token = GetTelegramBotApiToken()
|
|
|
|
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 = GetTelegramBotApiToken()
|
|
|
|
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:
|
|
|
|
|
|
|
|
verbose_name_plural = 'Заказы'
|
|
|
|
managed = False
|
|
|
|
db_table = 'orders'
|