Update app.py
Browse files
app.py
CHANGED
|
@@ -534,9 +534,9 @@ def catalog():
|
|
| 534 |
</div>
|
| 535 |
<div class="products-grid" id="products-grid">
|
| 536 |
{% for product in products %}
|
| 537 |
-
<div class="product"
|
| 538 |
onclick="openModal({{ loop.index0 }})"
|
| 539 |
-
data-name="{{ product['name']|lower }}"
|
| 540 |
data-description="{{ product['description']|lower }}"
|
| 541 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 542 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
@@ -544,8 +544,8 @@ def catalog():
|
|
| 544 |
</button>
|
| 545 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 546 |
<div class="product-image">
|
| 547 |
-
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 548 |
-
alt="{{ product['name'] }}"
|
| 549 |
loading="lazy">
|
| 550 |
</div>
|
| 551 |
{% endif %}
|
|
@@ -1450,9 +1450,9 @@ def category_products(category):
|
|
| 1450 |
</div>
|
| 1451 |
<div class="products-grid" id="products-grid">
|
| 1452 |
{% for product in products %}
|
| 1453 |
-
<div class="product"
|
| 1454 |
onclick="openModal({{ loop.index0 }})"
|
| 1455 |
-
data-name="{{ product['name']|lower }}"
|
| 1456 |
data-description="{{ product['description']|lower }}"
|
| 1457 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 1458 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
@@ -1460,8 +1460,8 @@ def category_products(category):
|
|
| 1460 |
</button>
|
| 1461 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 1462 |
<div class="product-image">
|
| 1463 |
-
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 1464 |
-
alt="{{ product['name'] }}"
|
| 1465 |
loading="lazy">
|
| 1466 |
</div>
|
| 1467 |
{% endif %}
|
|
@@ -2377,7 +2377,7 @@ def favorites_page():
|
|
| 2377 |
const cartContent = document.getElementById('cartContent');
|
| 2378 |
let total = 0;
|
| 2379 |
|
| 2380 |
-
|
| 2381 |
const itemTotal = item.price * item.quantity;
|
| 2382 |
total += itemTotal;
|
| 2383 |
return `
|
|
@@ -2836,9 +2836,9 @@ def discounts_page():
|
|
| 2836 |
</div>
|
| 2837 |
<div class="products-grid" id="products-grid">
|
| 2838 |
{% for product in products %}
|
| 2839 |
-
<div class="product"
|
| 2840 |
onclick="openModal({{ loop.index0 }})"
|
| 2841 |
-
data-name="{{ product['name']|lower }}"
|
| 2842 |
data-description="{{ product['description']|lower }}"
|
| 2843 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 2844 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
@@ -2846,8 +2846,8 @@ def discounts_page():
|
|
| 2846 |
</button>
|
| 2847 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 2848 |
<div class="product-image">
|
| 2849 |
-
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 2850 |
-
alt="{{ product['name'] }}"
|
| 2851 |
loading="lazy">
|
| 2852 |
</div>
|
| 2853 |
{% endif %}
|
|
@@ -3147,7 +3147,7 @@ def discounts_page():
|
|
| 3147 |
@app.route('/product/<int:index>')
|
| 3148 |
def product_details(index):
|
| 3149 |
data = load_data()
|
| 3150 |
-
if index < 0 or index >= len(data['products']):
|
| 3151 |
return "Product not found", 404 # Return a 404 error if index is out of bounds
|
| 3152 |
product = data['products'][index]
|
| 3153 |
|
|
@@ -3225,7 +3225,7 @@ def product_details(index):
|
|
| 3225 |
font-size: 0.9rem;
|
| 3226 |
}
|
| 3227 |
body.dark-mode .product-details .colors span {
|
| 3228 |
-
background: #
|
| 3229 |
color: #e0e0e0;
|
| 3230 |
}
|
| 3231 |
</style>
|
|
@@ -3277,7 +3277,7 @@ def admin():
|
|
| 3277 |
|
| 3278 |
if request.method == 'POST':
|
| 3279 |
action = request.form.get('action')
|
| 3280 |
-
|
| 3281 |
if action == 'add':
|
| 3282 |
photos = request.files.getlist('photos')
|
| 3283 |
photo_filenames = []
|
|
@@ -3286,17 +3286,17 @@ def admin():
|
|
| 3286 |
filename = secure_filename(photo.filename)
|
| 3287 |
api = HfApi()
|
| 3288 |
api.upload_file(
|
| 3289 |
-
path_or_fileobj=photo,
|
| 3290 |
path_in_repo=f"photos/{filename}",
|
| 3291 |
repo_id=REPO_ID,
|
| 3292 |
repo_type="dataset",
|
| 3293 |
token=HF_TOKEN_WRITE
|
| 3294 |
)
|
| 3295 |
photo_filenames.append(filename)
|
| 3296 |
-
|
| 3297 |
colors = request.form.getlist('colors')
|
| 3298 |
colors = [color.strip() for color in colors if color.strip()]
|
| 3299 |
-
|
| 3300 |
new_product = {
|
| 3301 |
'name': request.form['name'],
|
| 3302 |
'price': float(request.form['price']),
|
|
@@ -3309,7 +3309,7 @@ def admin():
|
|
| 3309 |
'discount': float(request.form['discount']) if request.form['discount'] else None
|
| 3310 |
}
|
| 3311 |
products.append(new_product)
|
| 3312 |
-
|
| 3313 |
elif action == 'edit':
|
| 3314 |
index = int(request.form['index'])
|
| 3315 |
photos = request.files.getlist('photos')
|
|
@@ -3319,17 +3319,17 @@ def admin():
|
|
| 3319 |
filename = secure_filename(photo.filename)
|
| 3320 |
api = HfApi()
|
| 3321 |
api.upload_file(
|
| 3322 |
-
path_or_fileobj=photo,
|
| 3323 |
path_in_repo=f"photos/{filename}",
|
| 3324 |
repo_id=REPO_ID,
|
| 3325 |
repo_type="dataset",
|
| 3326 |
token=HF_TOKEN_WRITE
|
| 3327 |
)
|
| 3328 |
photo_filenames.append(filename) # Add new photos
|
| 3329 |
-
|
| 3330 |
colors = request.form.getlist('colors')
|
| 3331 |
colors = [color.strip() for color in colors if color.strip()]
|
| 3332 |
-
|
| 3333 |
products[index] = {
|
| 3334 |
'name': request.form['name'],
|
| 3335 |
'price': float(request.form['price']),
|
|
@@ -3341,7 +3341,7 @@ def admin():
|
|
| 3341 |
'photos': photo_filenames, # Updated photo list
|
| 3342 |
'discount': float(request.form['discount']) if request.form['discount'] else None
|
| 3343 |
}
|
| 3344 |
-
|
| 3345 |
elif action == 'delete':
|
| 3346 |
index = int(request.form['index'])
|
| 3347 |
# Delete photos from Hugging Face Hub before deleting the product
|
|
@@ -3360,12 +3360,12 @@ def admin():
|
|
| 3360 |
logging.error(f"Error deleting photo {photo}: {e}")
|
| 3361 |
products.pop(index)
|
| 3362 |
|
| 3363 |
-
|
| 3364 |
elif action == 'add_category':
|
| 3365 |
category = request.form['category_name'].strip()
|
| 3366 |
if category and category not in categories:
|
| 3367 |
categories.append(category)
|
| 3368 |
-
|
| 3369 |
elif action == 'delete_category':
|
| 3370 |
index = int(request.form['category_index'])
|
| 3371 |
category_to_delete = categories[index]
|
|
@@ -3374,7 +3374,7 @@ def admin():
|
|
| 3374 |
for product in products:
|
| 3375 |
if product.get('category') == category_to_delete:
|
| 3376 |
product['category'] = 'Без категории'
|
| 3377 |
-
|
| 3378 |
save_data({'products': products, 'categories': categories})
|
| 3379 |
return redirect(url_for('admin'))
|
| 3380 |
|
|
@@ -3596,8 +3596,8 @@ def admin():
|
|
| 3596 |
</div>
|
| 3597 |
<div class="product-list" id="product-list">
|
| 3598 |
{% for product in products %}
|
| 3599 |
-
<div class="product-item"
|
| 3600 |
-
data-name="{{ product['name']|lower }}"
|
| 3601 |
data-description="{{ product['description']|lower }}"
|
| 3602 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 3603 |
<h2>{{ product['name'] }}</h2>
|
|
|
|
| 534 |
</div>
|
| 535 |
<div class="products-grid" id="products-grid">
|
| 536 |
{% for product in products %}
|
| 537 |
+
<div class="product"
|
| 538 |
onclick="openModal({{ loop.index0 }})"
|
| 539 |
+
data-name="{{ product['name']|lower }}"
|
| 540 |
data-description="{{ product['description']|lower }}"
|
| 541 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 542 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
|
|
| 544 |
</button>
|
| 545 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 546 |
<div class="product-image">
|
| 547 |
+
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 548 |
+
alt="{{ product['name'] }}"
|
| 549 |
loading="lazy">
|
| 550 |
</div>
|
| 551 |
{% endif %}
|
|
|
|
| 1450 |
</div>
|
| 1451 |
<div class="products-grid" id="products-grid">
|
| 1452 |
{% for product in products %}
|
| 1453 |
+
<div class="product"
|
| 1454 |
onclick="openModal({{ loop.index0 }})"
|
| 1455 |
+
data-name="{{ product['name']|lower }}"
|
| 1456 |
data-description="{{ product['description']|lower }}"
|
| 1457 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 1458 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
|
|
| 1460 |
</button>
|
| 1461 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 1462 |
<div class="product-image">
|
| 1463 |
+
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 1464 |
+
alt="{{ product['name'] }}"
|
| 1465 |
loading="lazy">
|
| 1466 |
</div>
|
| 1467 |
{% endif %}
|
|
|
|
| 2377 |
const cartContent = document.getElementById('cartContent');
|
| 2378 |
let total = 0;
|
| 2379 |
|
| 2380 |
+
cartContent.innerHTML = cart.length === 0 ? '<p>Корзина пуста</p>' : cart.map(item => {
|
| 2381 |
const itemTotal = item.price * item.quantity;
|
| 2382 |
total += itemTotal;
|
| 2383 |
return `
|
|
|
|
| 2836 |
</div>
|
| 2837 |
<div class="products-grid" id="products-grid">
|
| 2838 |
{% for product in products %}
|
| 2839 |
+
<div class="product"
|
| 2840 |
onclick="openModal({{ loop.index0 }})"
|
| 2841 |
+
data-name="{{ product['name']|lower }}"
|
| 2842 |
data-description="{{ product['description']|lower }}"
|
| 2843 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 2844 |
<button class="favorite-button" onclick="event.stopPropagation(); toggleFavorite({{ loop.index0 }})">
|
|
|
|
| 2846 |
</button>
|
| 2847 |
{% if product.get('photos') and product['photos']|length > 0 %}
|
| 2848 |
<div class="product-image">
|
| 2849 |
+
<img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/photos/{{ product['photos'][0] }}"
|
| 2850 |
+
alt="{{ product['name'] }}"
|
| 2851 |
loading="lazy">
|
| 2852 |
</div>
|
| 2853 |
{% endif %}
|
|
|
|
| 3147 |
@app.route('/product/<int:index>')
|
| 3148 |
def product_details(index):
|
| 3149 |
data = load_data()
|
| 3150 |
+
if (index < 0 or index >= len(data['products'])):
|
| 3151 |
return "Product not found", 404 # Return a 404 error if index is out of bounds
|
| 3152 |
product = data['products'][index]
|
| 3153 |
|
|
|
|
| 3225 |
font-size: 0.9rem;
|
| 3226 |
}
|
| 3227 |
body.dark-mode .product-details .colors span {
|
| 3228 |
+
background: #333;
|
| 3229 |
color: #e0e0e0;
|
| 3230 |
}
|
| 3231 |
</style>
|
|
|
|
| 3277 |
|
| 3278 |
if request.method == 'POST':
|
| 3279 |
action = request.form.get('action')
|
| 3280 |
+
|
| 3281 |
if action == 'add':
|
| 3282 |
photos = request.files.getlist('photos')
|
| 3283 |
photo_filenames = []
|
|
|
|
| 3286 |
filename = secure_filename(photo.filename)
|
| 3287 |
api = HfApi()
|
| 3288 |
api.upload_file(
|
| 3289 |
+
path_or_fileobj=photo, # Исправлено: передаем сам объект photo
|
| 3290 |
path_in_repo=f"photos/{filename}",
|
| 3291 |
repo_id=REPO_ID,
|
| 3292 |
repo_type="dataset",
|
| 3293 |
token=HF_TOKEN_WRITE
|
| 3294 |
)
|
| 3295 |
photo_filenames.append(filename)
|
| 3296 |
+
|
| 3297 |
colors = request.form.getlist('colors')
|
| 3298 |
colors = [color.strip() for color in colors if color.strip()]
|
| 3299 |
+
|
| 3300 |
new_product = {
|
| 3301 |
'name': request.form['name'],
|
| 3302 |
'price': float(request.form['price']),
|
|
|
|
| 3309 |
'discount': float(request.form['discount']) if request.form['discount'] else None
|
| 3310 |
}
|
| 3311 |
products.append(new_product)
|
| 3312 |
+
|
| 3313 |
elif action == 'edit':
|
| 3314 |
index = int(request.form['index'])
|
| 3315 |
photos = request.files.getlist('photos')
|
|
|
|
| 3319 |
filename = secure_filename(photo.filename)
|
| 3320 |
api = HfApi()
|
| 3321 |
api.upload_file(
|
| 3322 |
+
path_or_fileobj=photo, # Исправлено: передаём сам объект photo
|
| 3323 |
path_in_repo=f"photos/{filename}",
|
| 3324 |
repo_id=REPO_ID,
|
| 3325 |
repo_type="dataset",
|
| 3326 |
token=HF_TOKEN_WRITE
|
| 3327 |
)
|
| 3328 |
photo_filenames.append(filename) # Add new photos
|
| 3329 |
+
|
| 3330 |
colors = request.form.getlist('colors')
|
| 3331 |
colors = [color.strip() for color in colors if color.strip()]
|
| 3332 |
+
|
| 3333 |
products[index] = {
|
| 3334 |
'name': request.form['name'],
|
| 3335 |
'price': float(request.form['price']),
|
|
|
|
| 3341 |
'photos': photo_filenames, # Updated photo list
|
| 3342 |
'discount': float(request.form['discount']) if request.form['discount'] else None
|
| 3343 |
}
|
| 3344 |
+
|
| 3345 |
elif action == 'delete':
|
| 3346 |
index = int(request.form['index'])
|
| 3347 |
# Delete photos from Hugging Face Hub before deleting the product
|
|
|
|
| 3360 |
logging.error(f"Error deleting photo {photo}: {e}")
|
| 3361 |
products.pop(index)
|
| 3362 |
|
| 3363 |
+
|
| 3364 |
elif action == 'add_category':
|
| 3365 |
category = request.form['category_name'].strip()
|
| 3366 |
if category and category not in categories:
|
| 3367 |
categories.append(category)
|
| 3368 |
+
|
| 3369 |
elif action == 'delete_category':
|
| 3370 |
index = int(request.form['category_index'])
|
| 3371 |
category_to_delete = categories[index]
|
|
|
|
| 3374 |
for product in products:
|
| 3375 |
if product.get('category') == category_to_delete:
|
| 3376 |
product['category'] = 'Без категории'
|
| 3377 |
+
|
| 3378 |
save_data({'products': products, 'categories': categories})
|
| 3379 |
return redirect(url_for('admin'))
|
| 3380 |
|
|
|
|
| 3596 |
</div>
|
| 3597 |
<div class="product-list" id="product-list">
|
| 3598 |
{% for product in products %}
|
| 3599 |
+
<div class="product-item"
|
| 3600 |
+
data-name="{{ product['name']|lower }}"
|
| 3601 |
data-description="{{ product['description']|lower }}"
|
| 3602 |
data-category="{{ product.get('category', 'Без категории') }}">
|
| 3603 |
<h2>{{ product['name'] }}</h2>
|