flpolprojects commited on
Commit
dd12782
·
verified ·
1 Parent(s): dbbc0da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -54
app.py CHANGED
@@ -1,13 +1,13 @@
1
  import telebot
2
  from telebot import types
3
- from flask import Flask, request, jsonify, render_template
4
  import threading
5
  import json
6
  from datetime import datetime
7
  import os
8
 
9
  # Инициализация бота и Flask
10
- BOT_TOKEN = '7734802681:AAGKHGG8O9uNk64JWTHH5yqXzvSxCcoLUdA' # Замени на токен твоего бота от @BotFather
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
- .product { border: 1px solid #ccc; padding: 10px; margin: 10px 0; }
156
- button { margin: 5px; }
 
 
 
 
157
  </style>
158
  </head>
159
  <body>
160
- <h1>Управление товарами</h1>
161
- <form id="addProductForm">
162
- <input type="text" name="name" placeholder="Название" required><br>
163
- <input type="number" name="price" placeholder="Цена" step="0.01" required><br>
164
- <textarea name="description" placeholder="Описание" required></textarea><br>
165
- <button type="submit">Добавить товар</button>
166
- </form>
167
- <h2>Существующие товары</h2>
168
- {% for product in products %}
169
- <div class="product">
170
- {{ product.name }} - {{ product.price }} руб.<br>
171
- {{ product.description }}<br>
172
- <button onclick="deleteProduct({{ product.id }})">Удалить</button>
173
- </div>
174
- {% endfor %}
175
- <h2>Заказы</h2>
176
- {% for order in orders %}
177
- <div class="product">
178
- Пользователь: {{ order.user_id }}<br>
179
- Дата: {{ order.date }}<br>
180
- Товары: {% for item in order.items %}
181
- {{ item.quantity }} x {{ [p.name for p in products if p.id == item.product_id][0] }}<br>
 
 
 
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
- # Сохранение HTML в файл (если нужно, можно убрать и использовать как строку)
202
- with open('templates/admin.html', 'w', encoding='utf-8') as f:
203
- f.write(admin_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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():