Kgshop commited on
Commit
5ecfc02
·
verified ·
1 Parent(s): cc35212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -89
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # --- START OF FILE app.py ---
2
-
3
  from flask import Flask, render_template_string, request, redirect, url_for
4
  import json
5
  import os
@@ -87,6 +85,7 @@ def save_data(data):
87
  except Exception as e:
88
  logging.error(f"Ошибка при сохранении данных: {e}")
89
 
 
90
  def upload_db_to_hf():
91
  if not HF_TOKEN_WRITE:
92
  logging.warning("HF_TOKEN_WRITE не установлен. Загрузка на Hugging Face пропущена.")
@@ -955,14 +954,6 @@ def admin():
955
  products = data.get('products', [])
956
  categories = data.get('categories', [])
957
 
958
- products_for_admin_display = []
959
- for i, p in enumerate(products):
960
- products_for_admin_display.append((p, i))
961
-
962
- sorted_products_for_display = sorted(products_for_admin_display,
963
- key=lambda x: x[0].get('added_at', ''),
964
- reverse=True)
965
-
966
  if request.method == 'POST':
967
  action = request.form.get('action')
968
 
@@ -1238,7 +1229,7 @@ def admin():
1238
  textarea {
1239
  resize: vertical;
1240
  min-height: 80px;
1241
- white-space: normal;
1242
  }
1243
  input[type="file"] {
1244
  padding: 10px;
@@ -1284,7 +1275,7 @@ def admin():
1284
  }
1285
  .delete-button:hover {
1286
  background-color: var(--delete-hover-color);
1287
- box-shadow: 0 44px 15px rgba(220, 38, 38, 0.4);
1288
  }
1289
  .add-color-btn {
1290
  background-color: var(--secondary-color);
@@ -1391,34 +1382,10 @@ def admin():
1391
  background: var(--edit-bg-color);
1392
  border-radius: 10px;
1393
  border: 1px solid #eee;
1394
- width: 100%;
1395
- box-sizing: border-box;
1396
- }
1397
- .edit-form label {
1398
- margin-top: 15px;
1399
- margin-bottom: 5px;
1400
- display: block;
1401
- color: #555;
1402
- font-size: 0.9rem;
1403
- }
1404
- .edit-form input[type="text"],
1405
- .edit-form input[type="number"],
1406
- .edit-form textarea,
1407
- .edit-form select,
1408
- .edit-form input[type="file"] {
1409
- width: 100%;
1410
- padding: 12px;
1411
- margin-top: 0;
1412
- margin-bottom: 15px;
1413
- border: 1px solid var(--secondary-color);
1414
- border-radius: 8px;
1415
- font-size: 1rem;
1416
- transition: all 0.3s ease;
1417
- box-sizing: border-box;
1418
  }
1419
-
1420
  .color-input-container {
1421
- margin-bottom: 15px;
 
1422
  }
1423
  .color-input-group {
1424
  display: flex;
@@ -1465,33 +1432,11 @@ def admin():
1465
  padding: 0;
1466
  margin: 0;
1467
  box-shadow: none;
1468
- flex-basis: 100%;
1469
- max-width: 100%;
1470
  }
1471
- .button-group details summary {
1472
- display: block;
1473
- width: fit-content;
1474
- padding: 12px 25px;
1475
- border: none;
1476
- border-radius: 8px;
1477
- background-color: var(--primary-color);
1478
- color: var(--light-text);
1479
- font-weight: 600;
1480
- font-size: 1rem;
1481
- cursor: pointer;
1482
- transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
1483
  margin-top: 0;
1484
- margin-right: 10px;
1485
  }
1486
- .button-group details summary:hover {
1487
- background-color: var(--hover-primary-color);
1488
- box-shadow: 0 4px 15px rgba(251, 140, 0, 0.4);
1489
- transform: translateY(-2px);
1490
- }
1491
- .button-group details[open] > summary {
1492
- margin-bottom: 10px;
1493
- }
1494
-
1495
  button > i.fas {
1496
  margin-right: 8px;
1497
  }
@@ -1503,8 +1448,6 @@ def admin():
1503
  details > summary i.fas {
1504
  margin-right: 8px;
1505
  }
1506
-
1507
-
1508
  @media (max-width: 768px) {
1509
  .product-list, .category-list {
1510
  grid-template-columns: 1fr;
@@ -1611,9 +1554,8 @@ def admin():
1611
 
1612
  <h2>Список товаров</h2>
1613
  <div class="product-list">
1614
- {% for product_tuple in sorted_products_for_display %}
1615
- {% set product = product_tuple[0] %}
1616
- {% set original_index = product_tuple[1] %}
1617
  <div class="product-item">
1618
  <h3>{{ product['name'] }}</h3>
1619
  <p><strong>Категория:</strong> {{ product.get('category', 'Без категории') }}</p>
@@ -1637,24 +1579,24 @@ def admin():
1637
  <summary><i class="fas fa-edit"></i> Редактировать</summary>
1638
  <form method="POST" enctype="multipart/form-data" class="edit-form">
1639
  <input type="hidden" name="action" value="edit">
1640
- <input type="hidden" name="index" value="{{ original_index }}">
1641
- <label for="edit-name-{{ original_index }}">Название:</label>
1642
- <input type="text" id="edit-name-{{ original_index }}" name="name" value="{{ product['name'] }}" required>
1643
- <label for="edit-price-{{ original_index }}">Цена (с):</label>
1644
- <input type="text" inputmode="decimal" id="edit-price-{{ original_index }}" name="price" value="{{ product['price'] }}" required>
1645
- <label for="edit-description-{{ original_index }}">Описание:</label>
1646
- <textarea id="edit-description-{{ original_index }}" name="description" rows="4" required>{{ product['description'] }}</textarea>
1647
- <label for="edit-category-{{ original_index }}">Категория:</label>
1648
- <select id="edit-category-{{ original_index }}" name="category">
1649
  <option value="Без категории" {% if product.get('category', 'Без категории') == 'Без категории' %}selected{% endif %}>Без категории</option>
1650
  {% for category in categories %}
1651
  <option value="{{ category }}" {% if product.get('category') == category %}selected{% endif %}>{{ category }}</option>
1652
  {% endfor %}
1653
  </select>
1654
- <label for="edit-photos-{{ original_index }}">Заменить фотографии (до 10):</label>
1655
- <input type="file" id="edit-photos-{{ original_index }}" name="photos" accept="image/jpeg, image/png, image/webp" multiple>
1656
  <label>Доступные цвета:</label>
1657
- <div id="edit-color-inputs-{{ original_index }}" class="color-input-container">
1658
  {% set colors = product.get('colors', []) %}
1659
  {% if colors %}
1660
  {% for color in colors %}
@@ -1670,13 +1612,13 @@ def admin():
1670
  </div>
1671
  {% endif %}
1672
  </div>
1673
- <button type="button" class="add-color-btn" onclick="addColorInput('edit-color-inputs-{{ original_index }}')"><i class="fas fa-plus"></i> Добавить цвет</button>
1674
  <button type="submit"><i class="fas fa-save"></i> Сохранить изменения</button>
1675
  </form>
1676
  </details>
1677
  <form method="POST" onsubmit="return confirm('Вы уверены, что хотите удалить товар \'{{ product['name'] }}\'?');">
1678
  <input type="hidden" name="action" value="delete">
1679
- <input type="hidden" name="index" value="{{ original_index }}">
1680
  <button type="submit" class="delete-button"><i class="fas fa-trash-alt"></i> Удалить товар</button>
1681
  </form>
1682
  </div>
@@ -1746,10 +1688,7 @@ def admin():
1746
  </body>
1747
  </html>
1748
  '''
1749
- return render_template_string(admin_html,
1750
- products_for_admin_display=sorted_products_for_display,
1751
- categories=categories,
1752
- repo_id=REPO_ID)
1753
 
1754
  @app.route('/backup', methods=['POST'])
1755
  def backup():
@@ -1791,7 +1730,4 @@ if __name__ == '__main__':
1791
 
1792
  port = int(os.environ.get("PORT", 7860))
1793
  logging.info(f"Запуск Flask приложения на хосте 0.0.0.0 и порту {port}")
1794
- app.run(debug=False, host='0.0.0.0', port=port)
1795
-
1796
-
1797
- # --- END OF FILE app.py ---
 
 
 
1
  from flask import Flask, render_template_string, request, redirect, url_for
2
  import json
3
  import os
 
85
  except Exception as e:
86
  logging.error(f"Ошибка при сохранении данных: {e}")
87
 
88
+
89
  def upload_db_to_hf():
90
  if not HF_TOKEN_WRITE:
91
  logging.warning("HF_TOKEN_WRITE не установлен. Загрузка на Hugging Face пропущена.")
 
954
  products = data.get('products', [])
955
  categories = data.get('categories', [])
956
 
 
 
 
 
 
 
 
 
957
  if request.method == 'POST':
958
  action = request.form.get('action')
959
 
 
1229
  textarea {
1230
  resize: vertical;
1231
  min-height: 80px;
1232
+ white-space: pre-wrap;
1233
  }
1234
  input[type="file"] {
1235
  padding: 10px;
 
1275
  }
1276
  .delete-button:hover {
1277
  background-color: var(--delete-hover-color);
1278
+ box-shadow: 0 4px 15px rgba(220, 38, 38, 0.4);
1279
  }
1280
  .add-color-btn {
1281
  background-color: var(--secondary-color);
 
1382
  background: var(--edit-bg-color);
1383
  border-radius: 10px;
1384
  border: 1px solid #eee;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1385
  }
 
1386
  .color-input-container {
1387
+ margin-top: 10px;
1388
+ margin-bottom: 10px;
1389
  }
1390
  .color-input-group {
1391
  display: flex;
 
1432
  padding: 0;
1433
  margin: 0;
1434
  box-shadow: none;
 
 
1435
  }
1436
+ .button-group button,
1437
+ .button-group summary {
 
 
 
 
 
 
 
 
 
 
1438
  margin-top: 0;
 
1439
  }
 
 
 
 
 
 
 
 
 
1440
  button > i.fas {
1441
  margin-right: 8px;
1442
  }
 
1448
  details > summary i.fas {
1449
  margin-right: 8px;
1450
  }
 
 
1451
  @media (max-width: 768px) {
1452
  .product-list, .category-list {
1453
  grid-template-columns: 1fr;
 
1554
 
1555
  <h2>Список товаров</h2>
1556
  <div class="product-list">
1557
+ {% set sorted_admin_products = products|sort(attribute='added_at', reverse=True) %}
1558
+ {% for product in sorted_admin_products %}
 
1559
  <div class="product-item">
1560
  <h3>{{ product['name'] }}</h3>
1561
  <p><strong>Категория:</strong> {{ product.get('category', 'Без категории') }}</p>
 
1579
  <summary><i class="fas fa-edit"></i> Редактировать</summary>
1580
  <form method="POST" enctype="multipart/form-data" class="edit-form">
1581
  <input type="hidden" name="action" value="edit">
1582
+ <input type="hidden" name="index" value="{{ loop.index0 }}">
1583
+ <label for="edit-name-{{ loop.index0 }}">Название:</label>
1584
+ <input type="text" id="edit-name-{{ loop.index0 }}" name="name" value="{{ product['name'] }}" required>
1585
+ <label for="edit-price-{{ loop.index0 }}">Цена (с):</label>
1586
+ <input type="text" inputmode="decimal" id="edit-price-{{ loop.index0 }}" name="price" value="{{ product['price'] }}" required>
1587
+ <label for="edit-description-{{ loop.index0 }}">Описание:</label>
1588
+ <textarea id="edit-description-{{ loop.index0 }}" name="description" rows="4" required>{{ product['description'] }}</textarea>
1589
+ <label for="edit-category-{{ loop.index0 }}">Категория:</label>
1590
+ <select id="edit-category-{{ loop.index0 }}" name="category">
1591
  <option value="Без категории" {% if product.get('category', 'Без категории') == 'Без категории' %}selected{% endif %}>Без категории</option>
1592
  {% for category in categories %}
1593
  <option value="{{ category }}" {% if product.get('category') == category %}selected{% endif %}>{{ category }}</option>
1594
  {% endfor %}
1595
  </select>
1596
+ <label for="edit-photos-{{ loop.index0 }}">Заменить фотографии (до 10):</label>
1597
+ <input type="file" id="edit-photos-{{ loop.index0 }}" name="photos" accept="image/jpeg, image/png, image/webp" multiple>
1598
  <label>Доступные цвета:</label>
1599
+ <div id="edit-color-inputs-{{ loop.index0 }}" class="color-input-container">
1600
  {% set colors = product.get('colors', []) %}
1601
  {% if colors %}
1602
  {% for color in colors %}
 
1612
  </div>
1613
  {% endif %}
1614
  </div>
1615
+ <button type="button" class="add-color-btn" onclick="addColorInput('edit-color-inputs-{{ loop.index0 }}')"><i class="fas fa-plus"></i> Добавить цвет</button>
1616
  <button type="submit"><i class="fas fa-save"></i> Сохранить изменения</button>
1617
  </form>
1618
  </details>
1619
  <form method="POST" onsubmit="return confirm('Вы уверены, что хотите удалить товар \'{{ product['name'] }}\'?');">
1620
  <input type="hidden" name="action" value="delete">
1621
+ <input type="hidden" name="index" value="{{ loop.index0 }}">
1622
  <button type="submit" class="delete-button"><i class="fas fa-trash-alt"></i> Удалить товар</button>
1623
  </form>
1624
  </div>
 
1688
  </body>
1689
  </html>
1690
  '''
1691
+ return render_template_string(admin_html, products=products, categories=categories, repo_id=REPO_ID)
 
 
 
1692
 
1693
  @app.route('/backup', methods=['POST'])
1694
  def backup():
 
1730
 
1731
  port = int(os.environ.get("PORT", 7860))
1732
  logging.info(f"Запуск Flask приложения на хосте 0.0.0.0 и порту {port}")
1733
+ app.run(debug=False, host='0.0.0.0', port=port)