Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import telebot
|
| 2 |
from telebot import types
|
| 3 |
-
from flask import Flask, request, jsonify,
|
| 4 |
import threading
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Инициализация бота и Flask
|
| 10 |
-
BOT_TOKEN = '7734802681:AAGKHGG8O9uNk64JWTHH5yqXzvSxCcoLUdA'
|
| 11 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
|
@@ -122,66 +122,50 @@ def show_orders(message):
|
|
| 122 |
response += f"\nИтого: {total} руб.\nДата: {order['date']}"
|
| 123 |
bot.reply_to(message, response)
|
| 124 |
|
| 125 |
-
# Админ-панель (Flask)
|
| 126 |
-
@app.route('/')
|
| 127 |
-
def admin_panel():
|
| 128 |
-
return render_template('admin.html', products=data['products'], orders=data['orders'])
|
| 129 |
-
|
| 130 |
-
@app.route('/add_product', methods=['POST'])
|
| 131 |
-
def add_product():
|
| 132 |
-
if request.method == 'POST':
|
| 133 |
-
name = request.form['name']
|
| 134 |
-
price = float(request.form['price'])
|
| 135 |
-
description = request.form['description']
|
| 136 |
-
product_id = max((p['id'] for p in data['products']), default=0) + 1
|
| 137 |
-
data['products'].append({'id': product_id, 'name': name, 'price': price, 'description': description})
|
| 138 |
-
save_data(data)
|
| 139 |
-
return jsonify({'status': 'success'})
|
| 140 |
-
|
| 141 |
-
@app.route('/delete_product/<int:product_id>', methods=['POST'])
|
| 142 |
-
def delete_product(product_id):
|
| 143 |
-
data['products'] = [p for p in data['products'] if p['id'] != product_id]
|
| 144 |
-
save_data(data)
|
| 145 |
-
return jsonify({'status': 'success'})
|
| 146 |
-
|
| 147 |
-
# HTML для админ-панели (встроенный в код для простоты)
|
| 148 |
admin_html = """
|
| 149 |
<!DOCTYPE html>
|
| 150 |
<html>
|
| 151 |
<head>
|
| 152 |
<title>Админ-панель</title>
|
| 153 |
<style>
|
| 154 |
-
body { font-family: Arial, sans-serif; margin: 20px; }
|
| 155 |
-
.
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
</style>
|
| 158 |
</head>
|
| 159 |
<body>
|
| 160 |
-
<
|
| 161 |
-
|
| 162 |
-
<
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
| 182 |
{% endfor %}
|
| 183 |
</div>
|
| 184 |
-
{% endfor %}
|
| 185 |
<script>
|
| 186 |
document.getElementById('addProductForm').onsubmit = async (e) => {
|
| 187 |
e.preventDefault();
|
|
@@ -198,9 +182,26 @@ admin_html = """
|
|
| 198 |
</html>
|
| 199 |
"""
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
# Запуск бота и Flask в разных потоках
|
| 206 |
def run_bot():
|
|
|
|
| 1 |
import telebot
|
| 2 |
from telebot import types
|
| 3 |
+
from flask import Flask, request, jsonify, render_template_string
|
| 4 |
import threading
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Инициализация бота и Flask
|
| 10 |
+
BOT_TOKEN = '7734802681:AAGKHGG8O9uNk64JWTHH5yqXzvSxCcoLUdA'
|
| 11 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
|
|
|
| 122 |
response += f"\nИтого: {total} руб.\nДата: {order['date']}"
|
| 123 |
bot.reply_to(message, response)
|
| 124 |
|
| 125 |
+
# Админ-панель (Flask) с HTML, CSS и JavaScript в строке
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
admin_html = """
|
| 127 |
<!DOCTYPE html>
|
| 128 |
<html>
|
| 129 |
<head>
|
| 130 |
<title>Админ-панель</title>
|
| 131 |
<style>
|
| 132 |
+
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f0f0f0; }
|
| 133 |
+
.container { max-width: 800px; margin: 0 auto; }
|
| 134 |
+
.product { border: 1px solid #ccc; padding: 15px; margin: 10px 0; background-color: #fff; border-radius: 5px; }
|
| 135 |
+
form { background-color: #fff; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
|
| 136 |
+
input, textarea { width: 100%; margin: 5px 0; padding: 8px; }
|
| 137 |
+
button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; }
|
| 138 |
+
button:hover { background-color: #45a049; }
|
| 139 |
</style>
|
| 140 |
</head>
|
| 141 |
<body>
|
| 142 |
+
<div class="container">
|
| 143 |
+
<h1>Управление товарами</h1>
|
| 144 |
+
<form id="addProductForm">
|
| 145 |
+
<input type="text" name="name" placeholder="Название" required><br>
|
| 146 |
+
<input type="number" name="price" placeholder="Цена" step="0.01" required><br>
|
| 147 |
+
<textarea name="description" placeholder="Описание" required></textarea><br>
|
| 148 |
+
<button type="submit">Добавить товар</button>
|
| 149 |
+
</form>
|
| 150 |
+
<h2>Существующие товары</h2>
|
| 151 |
+
{% for product in products %}
|
| 152 |
+
<div class="product">
|
| 153 |
+
{{ product.name }} - {{ product.price }} руб.<br>
|
| 154 |
+
{{ product.description }}<br>
|
| 155 |
+
<button onclick="deleteProduct({{ product.id }})">Удалить</button>
|
| 156 |
+
</div>
|
| 157 |
+
{% endfor %}
|
| 158 |
+
<h2>Заказы</h2>
|
| 159 |
+
{% for order in orders %}
|
| 160 |
+
<div class="product">
|
| 161 |
+
Пользователь: {{ order.user_id }}<br>
|
| 162 |
+
Дата: {{ order.date }}<br>
|
| 163 |
+
Товары: {% for item in order.items %}
|
| 164 |
+
{{ item.quantity }} x {{ [p.name for p in products if p.id == item.product_id][0] }}<br>
|
| 165 |
+
{% endfor %}
|
| 166 |
+
</div>
|
| 167 |
{% endfor %}
|
| 168 |
</div>
|
|
|
|
| 169 |
<script>
|
| 170 |
document.getElementById('addProductForm').onsubmit = async (e) => {
|
| 171 |
e.preventDefault();
|
|
|
|
| 182 |
</html>
|
| 183 |
"""
|
| 184 |
|
| 185 |
+
@app.route('/')
|
| 186 |
+
def admin_panel():
|
| 187 |
+
return render_template_string(admin_html, products=data['products'], orders=data['orders'])
|
| 188 |
+
|
| 189 |
+
@app.route('/add_product', methods=['POST'])
|
| 190 |
+
def add_product():
|
| 191 |
+
if request.method == 'POST':
|
| 192 |
+
name = request.form['name']
|
| 193 |
+
price = float(request.form['price'])
|
| 194 |
+
description = request.form['description']
|
| 195 |
+
product_id = max((p['id'] for p in data['products']), default=0) + 1
|
| 196 |
+
data['products'].append({'id': product_id, 'name': name, 'price': price, 'description': description})
|
| 197 |
+
save_data(data)
|
| 198 |
+
return jsonify({'status': 'success'})
|
| 199 |
+
|
| 200 |
+
@app.route('/delete_product/<int:product_id>', methods=['POST'])
|
| 201 |
+
def delete_product(product_id):
|
| 202 |
+
data['products'] = [p for p in data['products'] if p['id'] != product_id]
|
| 203 |
+
save_data(data)
|
| 204 |
+
return jsonify({'status': 'success'})
|
| 205 |
|
| 206 |
# Запуск бота и Flask в разных потоках
|
| 207 |
def run_bot():
|