Spaces:
Runtime error
Runtime error
Update app.py
Browse files10 unmodified lines111213141516148 unmodified lines1651661671681691704 unmodified lines17517617717817918018118218318418518618718818919019119219319419519619719819920020120220320412 unmodified lines2172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712729 unmodified lines28228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032135 unmodified lines35735835936036136236336436536636736836917 unmodified lines38738838939039139239339439539639739839940040140240340440540640740840941056 unmodified lines4674684694704714724733 unmodified lines47747847948048148239 unmodified lines52252352452552652752852953053153253354 unmodified lines58858959059159259359459559659759859960 unmodified lines66066166266366466566666764 unmodified lines73273373473573673773873974074174274384 unmodified lines82882983083183283383483583630 unmodified lines86786886987087187287387487587687787887988088188288388488588688788810 unmodified linesfrom datetime import datetimeimport loggingimport threadingfrom huggingface_hub import HfApi, hf_hub_downloadfrom huggingface_hub.utils import RepositoryNotFoundError148 unmodified lineslogging.error(f"Error verifying Telegram data: {e}")return None, FalseTEMPLATE = """<!DOCTYPE html><html lang="ru">4 unmodified lines<script src="https://telegram.org/js/telegram-web-app.js"></script><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet"><style>:root {--tg-theme-bg-color: #111111;--tg-theme-text-color: #ffffff;--tg-theme-hint-color: #aaaaaa;--tg-theme-link-color: #FFC107;--tg-theme-button-color: #FFC107;--tg-theme-button-text-color: #000000;--tg-theme-secondary-bg-color: #1e1e1e;--brand-yellow: #FFC107;--brand-black: #111111;--card-bg: #222222;--text-color: #ffffff;--text-secondary-color: #aaaaaa;--border-radius-m: 14px;--border-radius-l: 18px;--padding-m: 18px;--padding-l: 28px;--font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;--shadow-color: rgba(0, 0, 0, 0.4);--shadow-light: 0 4px 15px var(--shadow-color);--shadow-medium: 0 6px 25px var(--shadow-color);}* { box-sizing: border-box; margin: 0; padding: 0; }html, body {12 unmodified linesmargin: 0 auto;display: flex;flex-direction: column;gap: var(--padding-l);}.header {text-align: center;padding: var(--padding-m) 0;}.logo {font-size: 2em;font-weight: 700;color: var(--brand-yellow);}.welcome-text {font-size: 1.1em;color: var(--text-secondary-color);margin-top: 8px;}.bonus-card {background: linear-gradient(145deg, #2a2a2a, #1c1c1c);border-radius: var(--border-radius-l);padding: var(--padding-l);text-align: center;box-shadow: var(--shadow-medium);border: 1px solid rgba(255, 193, 7, 0.2);}.bonus-label {font-size: 1.2em;font-weight: 500;color: var(--text-secondary-color);margin-bottom: 12px;}.bonus-amount {font-size: 3.5em;font-weight: 700;color: var(--brand-yellow);letter-spacing: -1px;}.bonus-amount-minor {font-size: 0.5em;opacity: 0.8;}.history-section {background-color: var(--card-bg);border-radius: var(--border-radius-l);padding: var(--padding-l);box-shadow: var(--shadow-light);border: 1px solid rgba(255, 255, 255, 0.08);}.history-title {font-size: 1.5em;font-weight: 600;margin-bottom: var(--padding-m);padding-bottom: var(--padding-m);border-bottom: 1px solid rgba(255, 255, 255, 0.1);9 unmodified linesdisplay: flex;justify-content: space-between;align-items: center;padding: 16px 4px;border-bottom: 1px solid rgba(255, 255, 255, 0.05);}.history-item:last-child {border-bottom: none;}.history-details {display: flex;flex-direction: column;}.history-description {font-size: 1em;font-weight: 500;}.history-date {font-size: 0.85em;color: var(--text-secondary-color);margin-top: 4px;}.history-amount {font-size: 1.2em;font-weight: 600;}.history-amount.accrual {color: #4CAF50;}.history-amount.deduction {color: #F44336;}.no-history {text-align: center;color: var(--text-secondary-color);padding: 2rem 0;}</style></head><body>35 unmodified linesfunction applyTheme(themeParams) {const root = document.documentElement;if (themeParams.bg_color) root.style.setProperty('--tg-theme-bg-color', themeParams.bg_color);if (themeParams.text_color) root.style.setProperty('--tg-theme-text-color', themeParams.text_color);if (themeParams.hint_color) root.style.setProperty('--tg-theme-hint-color', themeParams.hint_color);if (themeParams.link_color) root.style.setProperty('--tg-theme-link-color', themeParams.link_color);if (themeParams.button_color) root.style.setProperty('--tg-theme-button-color', themeParams.button_color);if (themeParams.button_text_color) root.style.setProperty('--tg-theme-button-text-color', themeParams.button_text_color);if (themeParams.secondary_bg_color) root.style.setProperty('--tg-theme-secondary-bg-color', themeParams.secondary_bg_color);}function setupTelegram() {17 unmodified linesif (!userIdForTest) {fetch('/verify', {method: 'POST',headers: {'Content-Type': 'application/json','Accept': 'application/json'},body: JSON.stringify({ initData: tg.initData }),}).then(response => response.json()).then(data => {if (data.status === 'ok' && data.verified && data.user_id) {console.log('Backend verification successful. Reloading with user data.');window.location.replace('/?user_id_for_test=' + data.user_id);} else {console.warn('Backend verification failed:', data.message);document.body.style.visibility = 'visible';}}).catch(error => {console.error('Error sending initData for verification:', error);document.body.style.visibility = 'visible';});} else {56 unmodified linesh1 { text-align: center; color: var(--admin-secondary); margin-bottom: var(--padding); font-weight: 600; }.controls-bar { display: flex; gap: 1rem; align-items: center; background: var(--admin-card-bg); padding: var(--padding); border-radius: var(--border-radius); box-shadow: 0 4px 15px var(--admin-shadow); border: 1px solid var(--admin-border); margin-bottom: var(--padding); }.controls-bar input[type="text"] { flex-grow: 1; padding: 12px 15px; font-size: 1.1em; border-radius: 8px; border: 1px solid var(--admin-border); box-sizing: border-box; }.btn { padding: 12px 20px; font-size: 1em; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; transition: background-color 0.2s ease; }.btn-primary { background-color: var(--admin-primary); color: #000; }.btn-primary:hover { background-color: var(--admin-primary-dark); }.user-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: var(--padding); margin-top: var(--padding); }3 unmodified lines.user-info img { width: 60px; height: 60px; border-radius: 50%; object-fit: cover; border: 3px solid var(--admin-border); background-color: #eee; }.user-details .name { font-weight: 600; font-size: 1.2em; }.user-details .username { color: var(--admin-secondary); font-size: 0.95em; }.user-bonuses { text-align: center; margin-bottom: 1rem; }.user-bonuses .label { font-size: 0.9em; color: var(--admin-secondary); }.user-bonuses .amount { font-size: 1.8em; font-weight: 700; color: var(--admin-primary-dark); }39 unmodified lines{% if users %}<div class="user-grid" id="userGrid">{% for user in users|sort(attribute='visited_at', reverse=true) %}<div class="user-card" data-user-id="{{ user.id }}" data-search-term="{{ user.first_name|lower }} {{ user.last_name|lower }} {{ user.username|lower }} {{ user.id }}"><div class="user-info"><img src="{{ user.photo_url if user.photo_url else 'data:image/svg+xml;charset=UTF-8,%3csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 100 100%27%3e%3crect width=%27100%27 height=%27100%27 fill=%27%23e9ecef%27/%3e%3ctext x=%2750%25%27 y=%2755%25%27 dominant-baseline=%27middle%27 text-anchor=%27middle%27 font-size=%2745%27 font-family=%27sans-serif%27 fill=%27%23adb5bd%27%3e?%3c/text%3e%3c/svg%3e' }}" alt="User Avatar"><div class="user-details"><div class="name">{{ user.first_name or '' }} {{ user.last_name or '' }}</div><div class="username">@{{ user.username or 'N/A' }}</div></div></div><div class="user-bonuses">54 unmodified lines<h2>Добавить нового клиента</h2></div><div class="form-group" style="margin-bottom: 1rem;"><label for="newClientFirstName">Имя</label><input type="text" id="newClientFirstName" placeholder="Иван"></div><div class="form-group" style="margin-bottom: 1rem;"><label for="newClientLastName">Фамилия</label><input type="text" id="newClientLastName" placeholder="Иванов"></div><div class="form-group" style="margin-bottom: 1.5rem;"><label for="newClientPhone">Номер телефона (уникальный)</label>60 unmodified lines}function openAddClientModal() {document.getElementById('newClientFirstName').value = '';document.getElementById('newClientLastName').value = '';document.getElementById('newClientPhone').value = '';document.getElementById('addClientStatus').textContent = '';addClientModal.style.display = 'block';64 unmodified linesstatusEl.textContent = 'Сохранение...';const payload = {first_name: document.getElementById('newClientFirstName').value.trim(),last_name: document.getElementById('newClientLastName').value.trim(),phone_number: document.getElementById('newClientPhone').value.trim(),};if (!payload.first_name || !payload.phone_number) {statusEl.style.color = 'var(--admin-danger)';statusEl.textContent = 'Имя и номер телефона обязательны.';return;84 unmodified lines'visited_at': now.timestamp(),'visited_at_str': now.strftime('%Y-%m-%d %H:%M:%S')})else:user_entry = {'id': user_id,'first_name': user_info_dict.get('first_name'),'last_name': user_info_dict.get('last_name'),'username': user_info_dict.get('username'),30 unmodified linestry:data = request.get_json()phone_number = data.get('phone_number')first_name = data.get('first_name')last_name = data.get('last_name')if not phone_number or not first_name:return jsonify({"status": "error", "message": "Имя и номер телефона обязательны."}), 400all_data = load_visitor_data()if phone_number in all_data:return jsonify({"status": "error", "message": "Клиент с таким номером телефона уже существует."}), 409now = datetime.now()new_client = {'id': phone_number,'first_name': first_name,'last_name': last_name,'username': phone_number,'photo_url': None,'language_code': 'ru',10 unmodified lines11121314151617148 unmodified lines1661671681691701711721731741751761771784 unmodified lines18318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221312 unmodified lines2262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892909 unmodified lines30030130230330430530630730830931031131231331431531631731831932035 unmodified lines35635735835936036136236336436536617 unmodified lines38438538638738838939039139239339439539639739839940040156 unmodified lines4584594604614624634644653 unmodified lines46947047147247347447539 unmodified lines51551651751851952052152252352452552652754 unmodified lines58258358458558658758858960 unmodified lines65065165265365465565664 unmodified lines72172272372472572672772872973073184 unmodified lines81681781881982082182282382482582682730 unmodified lines85885986086186286386486586686786886987087187287387487587687787887988010 unmodified linesfrom datetime import datetimeimport loggingimport threadingimport randomfrom huggingface_hub import HfApi, hf_hub_downloadfrom huggingface_hub.utils import RepositoryNotFoundError148 unmodified lineslogging.error(f"Error verifying Telegram data: {e}")return None, Falsedef generate_unique_bonus_id(all_data):existing_ids = {user.get('bonus_id') for user in all_data.values() if 'bonus_id' in user}while True:new_id = str(random.randint(10000, 99999))if new_id not in existing_ids:return new_idTEMPLATE = """<!DOCTYPE html><html lang="ru">4 unmodified lines<script src="https://telegram.org/js/telegram-web-app.js"></script><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet"><style>:root {--brand-yellow: #FFC107;--brand-yellow-glow: rgba(255, 193, 7, 0.5);--brand-black: #0d0d0d;--card-bg: #1a1a1a;--card-bg-gradient: radial-gradient(circle, #2a2a2a 0%, #1a1a1a 100%);--text-color: #f5f5f7;--text-secondary-color: #888888;--border-radius-l: 24px;--padding-m: 20px;--padding-l: 30px;--font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;--shadow-color: rgba(0, 0, 0, 0.6);--shadow-glow: 0 0 40px var(--brand-yellow-glow);}@keyframes fadeIn {from { opacity: 0; transform: translateY(20px); }to { opacity: 1; transform: translateY(0); }}@keyframes pulseGlow {0% { text-shadow: 0 0 4px var(--brand-yellow-glow), 0 0 10px var(--brand-yellow-glow); }50% { text-shadow: 0 0 10px var(--brand-yellow-glow), 0 0 30px var(--brand-yellow-glow); }100% { text-shadow: 0 0 4px var(--brand-yellow-glow), 0 0 10px var(--brand-yellow-glow); }}* { box-sizing: border-box; margin: 0; padding: 0; }html, body {12 unmodified linesmargin: 0 auto;display: flex;flex-direction: column;gap: 2rem;animation: fadeIn 0.8s ease-out forwards;}.header {text-align: center;padding: var(--padding-m) 0;animation-delay: 0.1s;}.logo {font-size: 2.5em;font-weight: 800;color: var(--brand-yellow);letter-spacing: 2px;text-transform: uppercase;}.welcome-text {font-size: 1.1em;color: var(--text-secondary-color);margin-top: 10px;}.bonus-card {background: var(--card-bg-gradient);border-radius: var(--border-radius-l);padding: var(--padding-l);text-align: center;border: 1px solid rgba(255, 193, 7, 0.2);box-shadow: 0 10px 30px var(--shadow-color);transition: transform 0.3s ease, box-shadow 0.3s ease;animation-delay: 0.2s;}.bonus-card:hover {transform: translateY(-8px) scale(1.02);box-shadow: 0 15px 40px var(--shadow-color), var(--shadow-glow);}.bonus-label {font-size: 1.3em;font-weight: 500;color: var(--text-secondary-color);margin-bottom: 15px;text-transform: uppercase;letter-spacing: 1px;}.bonus-amount {font-size: 4.5em;font-weight: 800;color: var(--brand-yellow);line-height: 1;animation: pulseGlow 3s infinite ease-in-out;}.history-section {background-color: var(--card-bg);border-radius: var(--border-radius-l);padding: var(--padding-l);border: 1px solid rgba(255, 255, 255, 0.08);animation-delay: 0.3s;}.history-title {font-size: 1.6em;font-weight: 700;margin-bottom: var(--padding-m);padding-bottom: var(--padding-m);border-bottom: 1px solid rgba(255, 255, 255, 0.1);9 unmodified linesdisplay: flex;justify-content: space-between;align-items: center;padding: 18px 8px;border-bottom: 1px solid rgba(255, 255, 255, 0.05);transition: background-color 0.2s ease;}.history-item:hover {background-color: rgba(255, 255, 255, 0.03);}.history-item:last-child { border-bottom: none; }.history-details { display: flex; flex-direction: column; }.history-description { font-size: 1.05em; font-weight: 500; }.history-date { font-size: 0.85em; color: var(--text-secondary-color); margin-top: 5px; }.history-amount { font-size: 1.3em; font-weight: 700; }.history-amount.accrual { color: #28a745; }.history-amount.deduction { color: #dc3545; }.no-history { text-align: center; color: var(--text-secondary-color); padding: 3rem 0; font-size: 1.1em; }</style></head><body>35 unmodified linesfunction applyTheme(themeParams) {const root = document.documentElement;if (themeParams.bg_color) root.style.setProperty('--brand-black', themeParams.bg_color);if (themeParams.text_color) root.style.setProperty('--text-color', themeParams.text_color);if (themeParams.hint_color) root.style.setProperty('--text-secondary-color', themeParams.hint_color);if (themeParams.button_color) root.style.setProperty('--brand-yellow', themeParams.button_color);if (themeParams.secondary_bg_color) root.style.setProperty('--card-bg', themeParams.secondary_bg_color);}function setupTelegram() {17 unmodified linesif (!userIdForTest) {fetch('/verify', {method: 'POST',headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },body: JSON.stringify({ initData: tg.initData }),}).then(response => response.json()).then(data => {if (data.status === 'ok' && data.verified && data.user_id) {window.location.replace('/?user_id_for_test=' + data.user_id);} else {document.body.style.visibility = 'visible';}}).catch(error => {document.body.style.visibility = 'visible';});} else {56 unmodified linesh1 { text-align: center; color: var(--admin-secondary); margin-bottom: var(--padding); font-weight: 600; }.controls-bar { display: flex; gap: 1rem; align-items: center; background: var(--admin-card-bg); padding: var(--padding); border-radius: var(--border-radius); box-shadow: 0 4px 15px var(--admin-shadow); border: 1px solid var(--admin-border); margin-bottom: var(--padding); }.controls-bar input[type="text"] { flex-grow: 1; padding: 12px 15px; font-size: 1.1em; border-radius: 8px; border: 1px solid var(--admin-border); box-sizing: border-box; }.btn { padding: 12px 20px; font-size: 1em; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; transition: background-color 0.2s ease, transform 0.2s ease; }.btn:hover { transform: translateY(-2px); }.btn-primary { background-color: var(--admin-primary); color: #000; }.btn-primary:hover { background-color: var(--admin-primary-dark); }.user-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: var(--padding); margin-top: var(--padding); }3 unmodified lines.user-info img { width: 60px; height: 60px; border-radius: 50%; object-fit: cover; border: 3px solid var(--admin-border); background-color: #eee; }.user-details .name { font-weight: 600; font-size: 1.2em; }.user-details .username { color: var(--admin-secondary); font-size: 0.95em; }.user-details .bonus-id { color: var(--admin-primary-dark); font-size: 0.9em; font-weight: 500; margin-top: 4px; background: #fff8e1; padding: 2px 6px; border-radius: 4px; display: inline-block;}.user-bonuses { text-align: center; margin-bottom: 1rem; }.user-bonuses .label { font-size: 0.9em; color: var(--admin-secondary); }.user-bonuses .amount { font-size: 1.8em; font-weight: 700; color: var(--admin-primary-dark); }39 unmodified lines{% if users %}<div class="user-grid" id="userGrid">{% for user in users|sort(attribute='visited_at', reverse=true) %}<div class="user-card" data-user-id="{{ user.id }}" data-search-term="{{ user.first_name|lower }} {{ user.last_name|lower }} {{ user.username|lower }} {{ user.id }} {{ user.bonus_id }}"><div class="user-info"><img src="{{ user.photo_url if user.photo_url else 'data:image/svg+xml;charset=UTF-8,%3csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 100 100%27%3e%3crect width=%27100%27 height=%27100%27 fill=%27%23e9ecef%27/%3e%3ctext x=%2750%25%27 y=%2755%25%27 dominant-baseline=%27middle%27 text-anchor=%27middle%27 font-size=%2745%27 font-family=%27sans-serif%27 fill=%27%23adb5bd%27%3e?%3c/text%3e%3c/svg%3e' }}" alt="User Avatar"><div class="user-details"><div class="name">{{ user.first_name or '' }} {{ user.last_name or '' }}</div><div class="username">@{{ user.username or 'N/A' }}</div><div class="bonus-id">ID: {{ user.bonus_id or 'N/A' }}</div></div></div><div class="user-bonuses">54 unmodified lines<h2>Добавить нового клиента</h2></div><div class="form-group" style="margin-bottom: 1rem;"><label for="newClientName">Имя</label><input type="text" id="newClientName" placeholder="Иван Иванов"></div><div class="form-group" style="margin-bottom: 1.5rem;"><label for="newClientPhone">Номер телефона (уникальный)</label>60 unmodified lines}function openAddClientModal() {document.getElementById('newClientName').value = '';document.getElementById('newClientPhone').value = '';document.getElementById('addClientStatus').textContent = '';addClientModal.style.display = 'block';64 unmodified linesstatusEl.textContent = 'Сохранение...';const payload = {name: document.getElementById('newClientName').value.trim(),phone_number: document.getElementById('newClientPhone').value.trim(),};if (!payload.name || !payload.phone_number) {statusEl.style.color = 'var(--admin-danger)';statusEl.textContent = 'Имя и номер телефона обязательны.';return;84 unmodified lines'visited_at': now.timestamp(),'visited_at_str': now.strftime('%Y-%m-%d %H:%M:%S')})if 'bonus_id' not in user_entry or not user_entry['bonus_id']:user_entry['bonus_id'] = generate_unique_bonus_id(all_data)else:user_entry = {'id': user_id,'bonus_id': generate_unique_bonus_id(all_data),'first_name': user_info_dict.get('first_name'),'last_name': user_info_dict.get('last_name'),'username': user_info_dict.get('username'),30 unmodified linestry:data = request.get_json()phone_number = data.get('phone_number')name = data.get('name')if not phone_number or not name:return jsonify({"status": "error", "message": "Имя и номер телефона обязательны."}), 400all_data = load_visitor_data()for user in all_data.values():if user.get('id') == phone_number or user.get('username') == phone_number:return jsonify({"status": "error", "message": "Клиент с таким номером телефона уже существует."}), 409now = datetime.now()new_client = {'id': phone_number,'bonus_id': generate_unique_bonus_id(all_data),'first_name': name,'last_name': "",'username': phone_number,'photo_url': None,'language_code': 'ru',
app.py+95-103