Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import flask
|
| 4 |
from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file, jsonify, Response
|
| 5 |
from flask_caching import Cache
|
|
@@ -15,8 +13,9 @@ import requests
|
|
| 15 |
from io import BytesIO
|
| 16 |
import uuid
|
| 17 |
from functools import wraps
|
|
|
|
| 18 |
|
| 19 |
-
app = Flask(
|
| 20 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey_folders_unique_tma")
|
| 21 |
DATA_FILE = 'cloudeng_data_tma.json'
|
| 22 |
REPO_ID = "Eluza133/Z1e1u"
|
|
@@ -231,8 +230,8 @@ def load_data():
|
|
| 231 |
if not isinstance(data, dict):
|
| 232 |
data = {'users': {}, 'promo_codes': {}}
|
| 233 |
data.setdefault('users', {})
|
| 234 |
-
data.setdefault('promo_codes', {})
|
| 235 |
-
|
| 236 |
for tma_user_id_str, user_data_item in data['users'].items():
|
| 237 |
user_data_item.setdefault('storage_limit_gb', DEFAULT_STORAGE_LIMIT_GB)
|
| 238 |
user_data_item.setdefault('unlimited_storage', False)
|
|
@@ -242,7 +241,7 @@ def load_data():
|
|
| 242 |
return data
|
| 243 |
except Exception as e:
|
| 244 |
logging.error(f"Error loading data: {e}")
|
| 245 |
-
return {'users': {}, 'promo_codes': {}}
|
| 246 |
|
| 247 |
def save_data(data):
|
| 248 |
try:
|
|
@@ -409,7 +408,15 @@ TMA_DASHBOARD_HTML_TEMPLATE = '''
|
|
| 409 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 410 |
<title>Zeus Cloud</title><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
|
| 411 |
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
| 412 |
-
<style>''' + BASE_STYLE + '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
<h1>Zeus Cloud</h1><p>Пользователь: {{ display_name }}</p>
|
| 414 |
<p>
|
| 415 |
{% if user_data.unlimited_storage %}
|
|
@@ -419,13 +426,11 @@ TMA_DASHBOARD_HTML_TEMPLATE = '''
|
|
| 419 |
{% endif %}
|
| 420 |
</p>
|
| 421 |
{% if not user_data.unlimited_storage %}
|
| 422 |
-
<
|
| 423 |
-
|
| 424 |
-
<div style="margin-top: 15px; display: flex; align-items: center; flex-wrap: wrap; gap: 10px;">
|
| 425 |
-
<h4>Активировать промокод:</h4>
|
| 426 |
<form method="POST" action="{{ url_for('tma_activate_promo') }}" style="display: contents;">
|
| 427 |
-
<input type="text" name="promo_code" placeholder="Введите промокод" required
|
| 428 |
-
<button type="submit" class="btn" style="background: var(--accent);
|
| 429 |
</form>
|
| 430 |
</div>
|
| 431 |
{% endif %}
|
|
@@ -565,17 +570,6 @@ TMA_DASHBOARD_HTML_TEMPLATE = '''
|
|
| 565 |
});
|
| 566 |
}
|
| 567 |
document.getElementById('logout-btn').addEventListener('click', function(e) { e.preventDefault(); window.location.href = "{{ url_for('tma_logout') }}"; });
|
| 568 |
-
|
| 569 |
-
function openTelegramPaymentLink() {
|
| 570 |
-
const adminUsername = "morshenkhan";
|
| 571 |
-
const message = "Здравствуйте, хочу приобрести полный доступ на Zeus Cloud";
|
| 572 |
-
const telegramLink = `https://t.me/${adminUsername}?startgroup=${encodeURIComponent(message)}`;
|
| 573 |
-
if (window.Telegram && window.Telegram.WebApp && Telegram.WebApp.openTelegramLink) {
|
| 574 |
-
Telegram.WebApp.openTelegramLink(telegramLink);
|
| 575 |
-
} else {
|
| 576 |
-
window.open(telegramLink, '_blank');
|
| 577 |
-
}
|
| 578 |
-
}
|
| 579 |
</script></body></html>
|
| 580 |
'''
|
| 581 |
|
|
@@ -705,6 +699,8 @@ def tma_dashboard():
|
|
| 705 |
breadcrumbs.reverse()
|
| 706 |
|
| 707 |
storage_used_formatted = format_bytes(user_data['storage_used_bytes'])
|
|
|
|
|
|
|
| 708 |
|
| 709 |
return render_template_string(TMA_DASHBOARD_HTML_TEMPLATE,
|
| 710 |
display_name=display_name, items=items_in_folder,
|
|
@@ -712,59 +708,59 @@ def tma_dashboard():
|
|
| 712 |
breadcrumbs=breadcrumbs, repo_id_js=REPO_ID, HF_TOKEN_READ_js=HF_TOKEN_READ,
|
| 713 |
hf_file_url_jinja=lambda path, download=False: f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{path}{'?download=true' if download else ''}",
|
| 714 |
is_tma_user_admin_flag=is_admin_tma(),
|
| 715 |
-
user_data=user_data, storage_used_formatted=storage_used_formatted
|
| 716 |
-
|
| 717 |
|
| 718 |
-
@app.route('/
|
| 719 |
def tma_activate_promo():
|
| 720 |
if 'telegram_user_id' not in session:
|
| 721 |
flash('Пожалуйста, авторизуйтесь.', 'error')
|
| 722 |
return redirect(url_for('tma_entry_page'))
|
| 723 |
-
|
| 724 |
tma_user_id = session['telegram_user_id']
|
| 725 |
-
|
| 726 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
data = load_data()
|
| 728 |
-
user_data = data['users']
|
| 729 |
-
promo_codes = data.get('promo_codes', {})
|
| 730 |
-
|
| 731 |
-
if not user_data:
|
| 732 |
-
session.clear()
|
| 733 |
-
flash('Пользователь не найден.', 'error')
|
| 734 |
-
return redirect(url_for('tma_entry_page'))
|
| 735 |
|
| 736 |
-
if user_data.get('unlimited_storage'):
|
| 737 |
-
flash('У вас уже
|
| 738 |
return redirect(url_for('tma_dashboard'))
|
|
|
|
|
|
|
|
|
|
| 739 |
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
if not promo:
|
| 743 |
flash('Неверный промокод.', 'error')
|
| 744 |
return redirect(url_for('tma_dashboard'))
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
|
|
|
| 748 |
return redirect(url_for('tma_dashboard'))
|
| 749 |
-
|
| 750 |
-
if
|
| 751 |
-
flash('Вы уже использовали
|
| 752 |
return redirect(url_for('tma_dashboard'))
|
| 753 |
|
| 754 |
user_data['unlimited_storage'] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
|
| 756 |
-
promo['current_activations'] += 1
|
| 757 |
-
promo['activated_by'].append(tma_user_id)
|
| 758 |
-
|
| 759 |
try:
|
| 760 |
save_data(data)
|
| 761 |
-
flash('
|
| 762 |
except Exception as e:
|
| 763 |
-
flash(f'
|
| 764 |
-
|
| 765 |
return redirect(url_for('tma_dashboard'))
|
| 766 |
|
| 767 |
-
|
| 768 |
@app.route('/create_folder_tma', methods=['POST'])
|
| 769 |
def create_folder_tma():
|
| 770 |
if 'telegram_user_id' not in session:
|
|
@@ -1068,9 +1064,9 @@ ADMIN_PANEL_HTML_TEMPLATE = '''
|
|
| 1068 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1069 |
<title>Админ-панель</title><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
|
| 1070 |
<style>''' + BASE_STYLE + '''</style></head><body class="dark"><div class="container"><h1>Админ-панель</h1>
|
| 1071 |
-
<a href="{{ url_for('admin_logout') }}" class="btn" style="margin-bottom:20px; background-color: var(--
|
|
|
|
| 1072 |
<a href="{{ url_for('tma_dashboard') }}" class="btn" style="margin-bottom:20px;">В приложение (если TMA сессия есть)</a>
|
| 1073 |
-
<a href="{{ url_for('admin_manage_promos') }}" class="btn" style="margin-bottom:20px; background-color: var(--folder-color);">Управление промокодами</a>
|
| 1074 |
{% with messages = get_flashed_messages(with_categories=true) %}{% if messages %}{% for category, message in messages %}<div class="flash {{ category }}">{{ message }}</div>{% endfor %}{% endif %}{% endwith %}
|
| 1075 |
<h2>Пользователи</h2><div class="user-list">
|
| 1076 |
{% for user in user_details %}
|
|
@@ -1301,129 +1297,110 @@ def admin_delete_file(tma_user_id_str_form, file_id):
|
|
| 1301 |
flash(f'Ошибка при удалении файла {original_filename}: {e}', 'error')
|
| 1302 |
return redirect(url_for('admin_user_files', tma_user_id_str=tma_user_id_str_form))
|
| 1303 |
|
| 1304 |
-
|
| 1305 |
-
ADMIN_PROMO_CODES_HTML_TEMPLATE = '''
|
| 1306 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1307 |
-
<title>Управление
|
| 1308 |
-
<style>''' + BASE_STYLE + '''
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1312 |
|
| 1313 |
<h2>Создать новый промокод</h2>
|
| 1314 |
-
<form method="POST" action="{{ url_for('
|
| 1315 |
-
<
|
| 1316 |
-
|
| 1317 |
-
|
|
|
|
|
|
|
| 1318 |
</form>
|
| 1319 |
|
| 1320 |
<h2>Существующие промокоды</h2>
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
<p>
|
| 1326 |
-
<
|
| 1327 |
-
|
| 1328 |
-
<p><b>Активирован пользователями:</b> {{ promo.activated_by | join(', ') if promo.activated_by else 'Нет' }}</p>
|
| 1329 |
-
<form method="POST" action="{{ url_for('admin_delete_promo', code=promo.code) }}" style="display: inline; margin-right: 10px;" onsubmit="return confirm('Удалить промокод {{ promo.code }}?');">
|
| 1330 |
-
<button type="submit" class="btn delete-btn" style="padding: 5px 10px; font-size: 0.9em;">Удалить</button>
|
| 1331 |
-
</form>
|
| 1332 |
-
<form method="POST" action="{{ url_for('admin_reset_promo', code=promo.code) }}" style="display: inline;" onsubmit="return confirm('Сбросить активации для промокода {{ promo.code }}?');">
|
| 1333 |
-
<button type="submit" class="btn folder-btn" style="padding: 5px 10px; font-size: 0.9em;">Сбросить активации</button>
|
| 1334 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1335 |
</div>
|
|
|
|
|
|
|
| 1336 |
{% endfor %}
|
| 1337 |
</div>
|
| 1338 |
-
{% else %}
|
| 1339 |
-
<p>Промокоды отсутствуют.</p>
|
| 1340 |
-
{% endif %}
|
| 1341 |
</div></body></html>
|
| 1342 |
'''
|
| 1343 |
|
| 1344 |
-
@app.route('/admhosto/
|
| 1345 |
@admin_browser_login_required
|
| 1346 |
-
def
|
| 1347 |
data = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1348 |
promo_codes = data.get('promo_codes', {})
|
| 1349 |
-
return render_template_string(
|
| 1350 |
|
| 1351 |
-
@app.route('/admhosto/
|
| 1352 |
-
@admin_browser_login_required
|
| 1353 |
-
def admin_create_promo():
|
| 1354 |
-
code = request.form.get('code', '').strip().upper()
|
| 1355 |
-
try:
|
| 1356 |
-
max_activations = int(request.form.get('max_activations'))
|
| 1357 |
-
if max_activations < 0: raise ValueError("Max activations must be non-negative.")
|
| 1358 |
-
except (ValueError, TypeError):
|
| 1359 |
-
flash('Неверное значение для максимального количества активаций.', 'error')
|
| 1360 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1361 |
-
|
| 1362 |
-
if not code:
|
| 1363 |
-
flash('Код промокода не может быть пустым.', 'error')
|
| 1364 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1365 |
-
|
| 1366 |
-
data = load_data()
|
| 1367 |
-
promo_codes = data.get('promo_codes', {})
|
| 1368 |
-
|
| 1369 |
-
if code in promo_codes:
|
| 1370 |
-
flash(f'Промокод "{code}" уже существует.', 'error')
|
| 1371 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1372 |
-
|
| 1373 |
-
promo_codes[code] = {
|
| 1374 |
-
'code': code,
|
| 1375 |
-
'max_activations': max_activations,
|
| 1376 |
-
'current_activations': 0,
|
| 1377 |
-
'activated_by': []
|
| 1378 |
-
}
|
| 1379 |
-
data['promo_codes'] = promo_codes
|
| 1380 |
-
|
| 1381 |
-
try:
|
| 1382 |
-
save_data(data)
|
| 1383 |
-
flash(f'Промокод "{code}" успешно создан.', 'success')
|
| 1384 |
-
except Exception as e:
|
| 1385 |
-
flash(f'Ошибка сохранения данных при создании промокода: {e}', 'error')
|
| 1386 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1387 |
-
|
| 1388 |
-
@app.route('/admhosto/promos/delete/<string:code>', methods=['POST'])
|
| 1389 |
@admin_browser_login_required
|
| 1390 |
def admin_delete_promo(code):
|
| 1391 |
data = load_data()
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
| 1395 |
-
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
-
|
| 1401 |
-
save_data(data)
|
| 1402 |
-
flash(f'Промокод "{code}" успешно удален.', 'success')
|
| 1403 |
-
except Exception as e:
|
| 1404 |
-
flash(f'Ошибка сохранения данных при удалении промокода: {e}', 'error')
|
| 1405 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1406 |
-
|
| 1407 |
-
@app.route('/admhosto/promos/reset/<string:code>', methods=['POST'])
|
| 1408 |
-
@admin_browser_login_required
|
| 1409 |
-
def admin_reset_promo(code):
|
| 1410 |
-
data = load_data()
|
| 1411 |
-
promo_codes = data.get('promo_codes', {})
|
| 1412 |
-
|
| 1413 |
-
promo = promo_codes.get(code)
|
| 1414 |
-
if not promo:
|
| 1415 |
flash(f'Промокод "{code}" не найден.', 'error')
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
promo['current_activations'] = 0
|
| 1419 |
-
promo['activated_by'] = []
|
| 1420 |
|
| 1421 |
-
try:
|
| 1422 |
-
save_data(data)
|
| 1423 |
-
flash(f'Активации для промокода "{code}" сброшены.', 'success')
|
| 1424 |
-
except Exception as e:
|
| 1425 |
-
flash(f'Ошибка сохранения данных при сбросе активаций: {e}', 'error')
|
| 1426 |
-
return redirect(url_for('admin_manage_promos'))
|
| 1427 |
|
| 1428 |
if __name__ == '__main__':
|
| 1429 |
if not HF_TOKEN_WRITE: logging.warning("HF_TOKEN (write access) is not set. Uploads/deletions/backups will fail.")
|
|
@@ -1432,7 +1409,6 @@ if __name__ == '__main__':
|
|
| 1432 |
if ADMIN_USERNAME == "admin" and ADMIN_PASSWORD == "zeusadminpass":
|
| 1433 |
logging.warning("Using default admin username/password for browser admin panel. Please change ADMIN_USERNAME and ADMIN_PASSWORD environment variables.")
|
| 1434 |
|
| 1435 |
-
|
| 1436 |
if HF_TOKEN_WRITE:
|
| 1437 |
download_db_from_hf()
|
| 1438 |
threading.Thread(target=periodic_backup, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
| 1 |
import flask
|
| 2 |
from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file, jsonify, Response
|
| 3 |
from flask_caching import Cache
|
|
|
|
| 13 |
from io import BytesIO
|
| 14 |
import uuid
|
| 15 |
from functools import wraps
|
| 16 |
+
from urllib.parse import quote
|
| 17 |
|
| 18 |
+
app = Flask(__name__)
|
| 19 |
app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey_folders_unique_tma")
|
| 20 |
DATA_FILE = 'cloudeng_data_tma.json'
|
| 21 |
REPO_ID = "Eluza133/Z1e1u"
|
|
|
|
| 230 |
if not isinstance(data, dict):
|
| 231 |
data = {'users': {}, 'promo_codes': {}}
|
| 232 |
data.setdefault('users', {})
|
| 233 |
+
data.setdefault('promo_codes', {})
|
| 234 |
+
|
| 235 |
for tma_user_id_str, user_data_item in data['users'].items():
|
| 236 |
user_data_item.setdefault('storage_limit_gb', DEFAULT_STORAGE_LIMIT_GB)
|
| 237 |
user_data_item.setdefault('unlimited_storage', False)
|
|
|
|
| 241 |
return data
|
| 242 |
except Exception as e:
|
| 243 |
logging.error(f"Error loading data: {e}")
|
| 244 |
+
return {'users': {}, 'promo_codes': {}}
|
| 245 |
|
| 246 |
def save_data(data):
|
| 247 |
try:
|
|
|
|
| 408 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 409 |
<title>Zeus Cloud</title><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
|
| 410 |
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
| 411 |
+
<style>''' + BASE_STYLE + '''
|
| 412 |
+
.promo-actions { margin-top: 10px; margin-bottom: 20px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
|
| 413 |
+
.promo-actions input[type=text] { width: auto; flex-grow: 1; margin: 0; min-width: 150px; }
|
| 414 |
+
.promo-actions .btn { margin: 0; flex-shrink: 0;}
|
| 415 |
+
@media (max-width: 768px) {
|
| 416 |
+
.promo-actions { flex-direction: column; align-items: stretch; }
|
| 417 |
+
.promo-actions input[type=text] { width: 100%; }
|
| 418 |
+
}
|
| 419 |
+
</style></head><body class="dark"><div class="container">
|
| 420 |
<h1>Zeus Cloud</h1><p>Пользователь: {{ display_name }}</p>
|
| 421 |
<p>
|
| 422 |
{% if user_data.unlimited_storage %}
|
|
|
|
| 426 |
{% endif %}
|
| 427 |
</p>
|
| 428 |
{% if not user_data.unlimited_storage %}
|
| 429 |
+
<a href="https://t.me/morshenkhan?text={{ contact_message_url_encoded }}" target="_blank" class="btn" style="background: var(--secondary); margin-left: 0; margin-bottom: 10px;">Приобрести доступ (20$)</a>
|
| 430 |
+
<div class="promo-actions">
|
|
|
|
|
|
|
| 431 |
<form method="POST" action="{{ url_for('tma_activate_promo') }}" style="display: contents;">
|
| 432 |
+
<input type="text" name="promo_code" placeholder="Введите промокод" required>
|
| 433 |
+
<button type="submit" class="btn" style="background-color: var(--accent);">Активировать</button>
|
| 434 |
</form>
|
| 435 |
</div>
|
| 436 |
{% endif %}
|
|
|
|
| 570 |
});
|
| 571 |
}
|
| 572 |
document.getElementById('logout-btn').addEventListener('click', function(e) { e.preventDefault(); window.location.href = "{{ url_for('tma_logout') }}"; });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
</script></body></html>
|
| 574 |
'''
|
| 575 |
|
|
|
|
| 699 |
breadcrumbs.reverse()
|
| 700 |
|
| 701 |
storage_used_formatted = format_bytes(user_data['storage_used_bytes'])
|
| 702 |
+
contact_message = "здравствуйте , хочу приобрести полный доступ на zeuscloud"
|
| 703 |
+
contact_message_url_encoded = quote(contact_message)
|
| 704 |
|
| 705 |
return render_template_string(TMA_DASHBOARD_HTML_TEMPLATE,
|
| 706 |
display_name=display_name, items=items_in_folder,
|
|
|
|
| 708 |
breadcrumbs=breadcrumbs, repo_id_js=REPO_ID, HF_TOKEN_READ_js=HF_TOKEN_READ,
|
| 709 |
hf_file_url_jinja=lambda path, download=False: f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{path}{'?download=true' if download else ''}",
|
| 710 |
is_tma_user_admin_flag=is_admin_tma(),
|
| 711 |
+
user_data=user_data, storage_used_formatted=storage_used_formatted,
|
| 712 |
+
contact_message_url_encoded=contact_message_url_encoded)
|
| 713 |
|
| 714 |
+
@app.route('/tma/activate_promo', methods=['POST'])
|
| 715 |
def tma_activate_promo():
|
| 716 |
if 'telegram_user_id' not in session:
|
| 717 |
flash('Пожалуйста, авторизуйтесь.', 'error')
|
| 718 |
return redirect(url_for('tma_entry_page'))
|
| 719 |
+
|
| 720 |
tma_user_id = session['telegram_user_id']
|
| 721 |
+
promo_code = request.form.get('promo_code', '').strip().upper()
|
| 722 |
|
| 723 |
+
if not promo_code:
|
| 724 |
+
flash('Пожалуйста, введите промокод.', 'error')
|
| 725 |
+
return redirect(url_for('tma_dashboard'))
|
| 726 |
+
|
| 727 |
data = load_data()
|
| 728 |
+
user_data = data['users'][tma_user_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
|
| 730 |
+
if user_data.get('unlimited_storage', False):
|
| 731 |
+
flash('У вас уже есть безлимитное хранилище.', 'info')
|
| 732 |
return redirect(url_for('tma_dashboard'))
|
| 733 |
+
|
| 734 |
+
promo_codes = data.get('promo_codes', {})
|
| 735 |
+
code_data = promo_codes.get(promo_code)
|
| 736 |
|
| 737 |
+
if not code_data:
|
|
|
|
|
|
|
| 738 |
flash('Неверный промокод.', 'error')
|
| 739 |
return redirect(url_for('tma_dashboard'))
|
| 740 |
+
|
| 741 |
+
activations = code_data.get('activations', [])
|
| 742 |
+
if len(activations) >= code_data.get('max_activations', 0):
|
| 743 |
+
flash('Этот промокод уже закончился.', 'error')
|
| 744 |
return redirect(url_for('tma_dashboard'))
|
| 745 |
+
|
| 746 |
+
if any(act['user_id'] == tma_user_id for act in activations):
|
| 747 |
+
flash('Вы уже использовали промокод.', 'error')
|
| 748 |
return redirect(url_for('tma_dashboard'))
|
| 749 |
|
| 750 |
user_data['unlimited_storage'] = True
|
| 751 |
+
activations.append({
|
| 752 |
+
'user_id': tma_user_id,
|
| 753 |
+
'activated_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 754 |
+
})
|
| 755 |
|
|
|
|
|
|
|
|
|
|
| 756 |
try:
|
| 757 |
save_data(data)
|
| 758 |
+
flash('Промокод успешно активирован! Вы получили безлимитное хранилище.', 'success')
|
| 759 |
except Exception as e:
|
| 760 |
+
flash(f'Произошла ошибка при активации промокода: {e}', 'error')
|
| 761 |
+
|
| 762 |
return redirect(url_for('tma_dashboard'))
|
| 763 |
|
|
|
|
| 764 |
@app.route('/create_folder_tma', methods=['POST'])
|
| 765 |
def create_folder_tma():
|
| 766 |
if 'telegram_user_id' not in session:
|
|
|
|
| 1064 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1065 |
<title>Админ-панель</title><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
|
| 1066 |
<style>''' + BASE_STYLE + '''</style></head><body class="dark"><div class="container"><h1>Админ-панель</h1>
|
| 1067 |
+
<a href="{{ url_for('admin_logout') }}" class="btn" style="margin-bottom:20px; background-color: var(--delete-color);">Выйти из админ-панели</a>
|
| 1068 |
+
<a href="{{ url_for('admin_promo_codes') }}" class="btn" style="margin-bottom:20px; background-color: var(--folder-color);">Управление промокодами</a>
|
| 1069 |
<a href="{{ url_for('tma_dashboard') }}" class="btn" style="margin-bottom:20px;">В приложение (если TMA сессия есть)</a>
|
|
|
|
| 1070 |
{% with messages = get_flashed_messages(with_categories=true) %}{% if messages %}{% for category, message in messages %}<div class="flash {{ category }}">{{ message }}</div>{% endfor %}{% endif %}{% endwith %}
|
| 1071 |
<h2>Пользователи</h2><div class="user-list">
|
| 1072 |
{% for user in user_details %}
|
|
|
|
| 1297 |
flash(f'Ошибка при удалении файла {original_filename}: {e}', 'error')
|
| 1298 |
return redirect(url_for('admin_user_files', tma_user_id_str=tma_user_id_str_form))
|
| 1299 |
|
| 1300 |
+
ADMIN_PROMO_CODES_TEMPLATE = '''
|
|
|
|
| 1301 |
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1302 |
+
<title>Управление промокодами</title><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
|
| 1303 |
+
<style>''' + BASE_STYLE + '''
|
| 1304 |
+
.promo-item { padding: 20px; background: var(--card-bg-dark); border-radius: 16px; margin-bottom: 15px; box-shadow: var(--shadow); }
|
| 1305 |
+
.promo-item h3 { color: var(--primary); margin-top: 0; }
|
| 1306 |
+
.activations-list { list-style: none; padding-left: 0; max-height: 150px; overflow-y: auto; background: rgba(0,0,0,0.2); border-radius: 8px; padding: 10px; margin-top: 10px; }
|
| 1307 |
+
.activations-list li { font-size: 0.9em; color: #ccc; }
|
| 1308 |
+
.form-grid { display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center; }
|
| 1309 |
+
.form-grid input { margin: 0; }
|
| 1310 |
+
</style></head><body class="dark"><div class="container">
|
| 1311 |
+
<h1>Управление промокодами</h1>
|
| 1312 |
+
<a href="{{ url_for('admin_panel') }}" class="btn" style="margin-bottom:20px;">Назад в админ-панель</a>
|
| 1313 |
+
{% with messages = get_flashed_messages(with_categories=true) %}{% if messages %}
|
| 1314 |
+
{% for category, message in messages %}<div class="flash {{ category }}">{{ message }}</div>{% endfor %}
|
| 1315 |
+
{% endif %}{% endwith %}
|
| 1316 |
|
| 1317 |
<h2>Создать новый промокод</h2>
|
| 1318 |
+
<form method="POST" action="{{ url_for('admin_promo_codes') }}">
|
| 1319 |
+
<div class="form-grid">
|
| 1320 |
+
<input type="text" name="code" placeholder="Промокод (напр. UNLIMITED2024)" required>
|
| 1321 |
+
<input type="number" name="max_activations" placeholder="Кол-во активаций" required min="1">
|
| 1322 |
+
<button type="submit" class="btn" style="background: var(--accent);">Создать</button>
|
| 1323 |
+
</div>
|
| 1324 |
</form>
|
| 1325 |
|
| 1326 |
<h2>Существующие промокоды</h2>
|
| 1327 |
+
<div class="user-list">
|
| 1328 |
+
{% for code, details in promo_codes.items() %}
|
| 1329 |
+
<div class="promo-item">
|
| 1330 |
+
<h3>{{ code }}</h3>
|
| 1331 |
+
<p>Использовано: <b>{{ details.activations|length }} / {{ details.max_activations }}</b></p>
|
| 1332 |
+
<form method="POST" action="{{ url_for('admin_delete_promo', code=code) }}" onsubmit="return confirm('Удалить промокод {{ code }}?');" style="display: inline-block;">
|
| 1333 |
+
<button type="submit" class="btn delete-btn" style="padding: 5px 10px; font-size: 0.9em;">Удалить промокод</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1334 |
</form>
|
| 1335 |
+
{% if details.activations %}
|
| 1336 |
+
<h4>Активации:</h4>
|
| 1337 |
+
<ul class="activations-list">
|
| 1338 |
+
{% for activation in details.activations %}
|
| 1339 |
+
<li>Пользователь ID <b>{{ activation.user_id }}</b> - {{ activation.activated_at }}</li>
|
| 1340 |
+
{% endfor %}
|
| 1341 |
+
</ul>
|
| 1342 |
+
{% else %}
|
| 1343 |
+
<p style="color: #888; margin-top: 10px;">Еще не было активаций.</p>
|
| 1344 |
+
{% endif %}
|
| 1345 |
</div>
|
| 1346 |
+
{% else %}
|
| 1347 |
+
<p>Промокоды еще не созданы.</p>
|
| 1348 |
{% endfor %}
|
| 1349 |
</div>
|
|
|
|
|
|
|
|
|
|
| 1350 |
</div></body></html>
|
| 1351 |
'''
|
| 1352 |
|
| 1353 |
+
@app.route('/admhosto/promocodes', methods=['GET', 'POST'])
|
| 1354 |
@admin_browser_login_required
|
| 1355 |
+
def admin_promo_codes():
|
| 1356 |
data = load_data()
|
| 1357 |
+
if request.method == 'POST':
|
| 1358 |
+
code = request.form.get('code', '').strip().upper()
|
| 1359 |
+
max_activations_str = request.form.get('max_activations')
|
| 1360 |
+
if not code or not max_activations_str:
|
| 1361 |
+
flash('Необходимо указать и код, и количество активаций.', 'error')
|
| 1362 |
+
return redirect(url_for('admin_promo_codes'))
|
| 1363 |
+
try:
|
| 1364 |
+
max_activations = int(max_activations_str)
|
| 1365 |
+
if max_activations <= 0:
|
| 1366 |
+
raise ValueError
|
| 1367 |
+
except ValueError:
|
| 1368 |
+
flash('Количество активаций должно быть положительным числом.', 'error')
|
| 1369 |
+
return redirect(url_for('admin_promo_codes'))
|
| 1370 |
+
|
| 1371 |
+
if code in data.get('promo_codes', {}):
|
| 1372 |
+
flash(f'Промокод "{code}" уже существует.', 'error')
|
| 1373 |
+
else:
|
| 1374 |
+
data.setdefault('promo_codes', {})[code] = {
|
| 1375 |
+
'max_activations': max_activations,
|
| 1376 |
+
'activations': []
|
| 1377 |
+
}
|
| 1378 |
+
try:
|
| 1379 |
+
save_data(data)
|
| 1380 |
+
flash(f'Промокод "{code}" успешно создан.', 'success')
|
| 1381 |
+
except Exception as e:
|
| 1382 |
+
flash(f'Ошибка сохранения промокода: {e}', 'error')
|
| 1383 |
+
return redirect(url_for('admin_promo_codes'))
|
| 1384 |
+
|
| 1385 |
promo_codes = data.get('promo_codes', {})
|
| 1386 |
+
return render_template_string(ADMIN_PROMO_CODES_TEMPLATE, promo_codes=promo_codes)
|
| 1387 |
|
| 1388 |
+
@app.route('/admhosto/promocodes/delete/<code>', methods=['POST'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1389 |
@admin_browser_login_required
|
| 1390 |
def admin_delete_promo(code):
|
| 1391 |
data = load_data()
|
| 1392 |
+
code = code.upper()
|
| 1393 |
+
if code in data.get('promo_codes', {}):
|
| 1394 |
+
del data['promo_codes'][code]
|
| 1395 |
+
try:
|
| 1396 |
+
save_data(data)
|
| 1397 |
+
flash(f'Промокод "{code}" удален.', 'success')
|
| 1398 |
+
except Exception as e:
|
| 1399 |
+
flash(f'Ошибка при удалении промокода: {e}', 'error')
|
| 1400 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1401 |
flash(f'Промокод "{code}" не найден.', 'error')
|
| 1402 |
+
return redirect(url_for('admin_promo_codes'))
|
|
|
|
|
|
|
|
|
|
| 1403 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1404 |
|
| 1405 |
if __name__ == '__main__':
|
| 1406 |
if not HF_TOKEN_WRITE: logging.warning("HF_TOKEN (write access) is not set. Uploads/deletions/backups will fail.")
|
|
|
|
| 1409 |
if ADMIN_USERNAME == "admin" and ADMIN_PASSWORD == "zeusadminpass":
|
| 1410 |
logging.warning("Using default admin username/password for browser admin panel. Please change ADMIN_USERNAME and ADMIN_PASSWORD environment variables.")
|
| 1411 |
|
|
|
|
| 1412 |
if HF_TOKEN_WRITE:
|
| 1413 |
download_db_from_hf()
|
| 1414 |
threading.Thread(target=periodic_backup, daemon=True).start()
|