|
from flask import Flask, request, redirect |
|
import json |
|
import os |
|
|
|
app = Flask(__name__) |
|
DB_FILE = 'data.json' |
|
|
|
def initialize_db(): |
|
try: |
|
with open(DB_FILE, 'w') as f: |
|
json.dump([], f) |
|
print(f"Файл {DB_FILE} успешно создан.") |
|
except Exception as e: |
|
print(f"Ошибка при создании файла {DB_FILE}: {e}") |
|
|
|
def load_products(): |
|
if not os.path.exists(DB_FILE): |
|
initialize_db() |
|
try: |
|
with open(DB_FILE, 'r') as f: |
|
products = json.load(f) |
|
print(f"Данные успешно загружены из {DB_FILE}: {products}") |
|
return products |
|
except (json.JSONDecodeError, Exception) as e: |
|
print(f"Ошибка при чтении файла {DB_FILE}: {e}") |
|
return [] |
|
|
|
def save_products(products): |
|
try: |
|
with open(DB_FILE, 'w') as f: |
|
json.dump(products, f, indent=4) |
|
print(f"Данные успешно сохранены в {DB_FILE}: {products}") |
|
except Exception as e: |
|
print(f"Ошибка при записи в файл {DB_FILE}: {e}") |
|
|
|
def html_wrapper(content): |
|
return f''' |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Каталог</title> |
|
<style> |
|
body {{ font-family: Arial, sans-serif; margin: 20px; }} |
|
.header {{ font-size: 24px; margin-bottom: 20px; }} |
|
.products {{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }} |
|
.product {{ border: 1px solid #ddd; padding: 15px; border-radius: 5px; }} |
|
form {{ max-width: 500px; margin: 20px auto; }} |
|
input, textarea {{ width: 100%; margin: 5px 0; padding: 8px; }} |
|
button {{ background: #4CAF50; color: white; padding: 10px; border: none; cursor: pointer; }} |
|
nav {{ margin-bottom: 20px; }} |
|
a {{ margin-right: 15px; text-decoration: none; color: #333; }} |
|
</style> |
|
</head> |
|
<body> |
|
<nav> |
|
<a href="/">Каталог</a> |
|
<a href="/admin">Админка</a> |
|
</nav> |
|
{content} |
|
</body> |
|
</html> |
|
''' |
|
|
|
@app.route('/') |
|
def catalog(): |
|
products = load_products() |
|
if not products: |
|
return html_wrapper('<div class="header">Каталог товаров пуст</div>') |
|
products_html = ''.join([ |
|
f'''<div class="product"> |
|
<h3>{p['name']}</h3> |
|
<p>{p['description']}</p> |
|
<p>Цена: {p['price']} руб.</p> |
|
</div>''' |
|
for p in products |
|
]) |
|
return html_wrapper(f''' |
|
<div class="header">Каталог товаров</div> |
|
<div class="products">{products_html}</div> |
|
''') |
|
|
|
@app.route('/admin', methods=['GET', 'POST']) |
|
def admin(): |
|
if request.method == 'POST': |
|
products = load_products() |
|
|
|
try: |
|
new_product = { |
|
'name': request.form['name'], |
|
'description': request.form['description'], |
|
'price': float(request.form['price']) |
|
} |
|
products.append(new_product) |
|
save_products(products) |
|
print(f"Добавлен новый товар: {new_product}") |
|
return redirect('/admin') |
|
except ValueError as e: |
|
print(f"Ошибка при добавлении товара: {e}") |
|
return html_wrapper(f'<div class="header">Ошибка: {e}</div>') |
|
|
|
return html_wrapper(''' |
|
<div class="header">Админ-панель</div> |
|
<form method="POST"> |
|
<input type="text" name="name" placeholder="Название" required> |
|
<textarea name="description" placeholder="Описание" required></textarea> |
|
<input type="number" name="price" placeholder="Цена" required> |
|
<button type="submit">Добавить товар</button> |
|
</form> |
|
''') |
|
|
|
if __name__ == '__main__': |
|
if not os.path.exists(DB_FILE): |
|
initialize_db() |
|
|
|
app.run(host='0.0.0.0', port=7860, debug=True) |