diff --git "a/app.py" "b/app.py" --- "a/app.py" +++ "b/app.py" @@ -11,8 +11,8 @@ from datetime import datetime import logging import threading import random -import pytz # Import pytz for timezone handling -import uuid # For generating unique invoice IDs +import pytz +import uuid from huggingface_hub import HfApi, hf_hub_download from huggingface_hub.utils import RepositoryNotFoundError @@ -27,7 +27,6 @@ HF_DATA_FILE_PATH = "data.json" HF_TOKEN_WRITE = os.getenv("HF_TOKEN_WRITE") HF_TOKEN_READ = os.getenv("HF_TOKEN_READ") -# Define Bishkek timezone BISHKEK_TZ = pytz.timezone('Asia/Bishkek') app = Flask(__name__) @@ -35,11 +34,10 @@ logging.basicConfig(level=logging.INFO) app.secret_key = os.urandom(24) _data_lock = threading.Lock() -visitor_data_cache = {} # This will store all data, including organization details +visitor_data_cache = {} def generate_unique_id(all_data): while True: - # Check against both client IDs and invoice IDs new_id = str(random.randint(10000, 99999)) if new_id not in all_data: return new_id @@ -80,14 +78,14 @@ def download_data_from_hf(): def load_visitor_data(): global visitor_data_cache with _data_lock: - if not visitor_data_cache: # Only load from file if cache is empty + if not visitor_data_cache: try: with open(DATA_FILE, 'r', encoding='utf-8') as f: visitor_data_cache = json.load(f) logging.info("Visitor data loaded from local JSON.") except FileNotFoundError: logging.warning(f"{DATA_FILE} not found locally. Starting with empty data.") - visitor_data_cache = {"organization_details": {}} # Initialize with empty org details + visitor_data_cache = {"organization_details": {}} except json.JSONDecodeError: logging.error(f"Error decoding {DATA_FILE}. Starting with empty data.") visitor_data_cache = {"organization_details": {}} @@ -95,7 +93,6 @@ def load_visitor_data(): logging.error(f"Unexpected error loading visitor data: {e}") visitor_data_cache = {"organization_details": {}} - # Ensure organization_details key exists if "organization_details" not in visitor_data_cache: visitor_data_cache["organization_details"] = {} @@ -104,29 +101,7 @@ def load_visitor_data(): def save_visitor_data(data): with _data_lock: try: - # When `data` is a dictionary, update it directly. - # If `data` is a partial update for `visitor_data_cache`, merge it. - # For simplicity, this function now assumes `data` is the complete `visitor_data_cache` - # or a mergeable dictionary that should be applied to the cache before saving. - # Given current usage, it's typically `save_visitor_data({user_id: user_entry})` - # or `save_visitor_data({"organization_details": new_org_details})` etc. - # It should ideally update the global `visitor_data_cache` and then dump it. - - # This line needs to be careful: if `data` is a single user, it overwrites. - # It's better to update specific parts of the cache or always pass the full cache. - # Let's adjust existing call sites to pass the full `all_data` after modification. - # For now, let's assume `data` is what needs to be *merged* into `visitor_data_cache` - # or `data` IS the new `visitor_data_cache`. - - # A more robust approach for `save_visitor_data` would be: - # 1. Take a user_id and user_data to update a specific user - # 2. Take an org_details dict to update org details - # 3. Then, always dump the *entire* `visitor_data_cache`. - - # Simpler change for existing code: - # Ensure `visitor_data_cache` is directly modified by operations, - # and `save_visitor_data` just dumps the current `visitor_data_cache`. - + visitor_data_cache.update(data) with open(DATA_FILE, 'w', encoding='utf-8') as f: json.dump(visitor_data_cache, f, ensure_ascii=False, indent=4) logging.info(f"Visitor data successfully saved to {DATA_FILE}.") @@ -221,6 +196,7 @@ TEMPLATE = """ --brand-yellow: #FFC107; --brand-black: #101010; --brand-red: #F44336; + --brand-green: #4CAF50; --card-bg: #1c1c1e; --text-color: #ffffff; --text-secondary-color: #a0a0a0; @@ -252,23 +228,10 @@ TEMPLATE = """ flex-direction: column; gap: var(--padding-m); } - .header { - text-align: left; - padding: var(--padding-m) 0; - margin-bottom: 0; /* Adjusted for nav buttons */ - } - .logo { - font-size: 2.5em; - font-weight: 800; - color: var(--text-color); - letter-spacing: -1px; - } + .header { text-align: left; padding: var(--padding-m) 0; margin-bottom: 0; } + .logo { font-size: 2.5em; font-weight: 800; color: var(--text-color); letter-spacing: -1px; } .logo span { color: var(--brand-yellow); } - .welcome-text { - font-size: 1em; - color: var(--text-secondary-color); - margin-top: 4px; - } + .welcome-text { font-size: 1em; color: var(--text-secondary-color); margin-top: 4px; } .nav-buttons { display: flex; justify-content: space-around; @@ -278,274 +241,132 @@ TEMPLATE = """ margin-bottom: var(--padding-m); } .nav-btn { - flex-grow: 1; - padding: 10px 15px; - border: none; - border-radius: 12px; - background-color: transparent; - color: var(--text-secondary-color); - font-family: var(--font-family); - font-weight: 600; - font-size: 1em; - cursor: pointer; - transition: background-color 0.2s, color 0.2s; - } - .nav-btn.active { - background-color: var(--brand-yellow); - color: var(--brand-black); - box-shadow: 0 2px 10px rgba(255,193,7,0.3); - } - .content-section { - display: none; /* Hidden by default */ - flex-direction: column; - gap: var(--padding-m); - } - .content-section.active { - display: flex; /* Shown when active */ - } - .card-grid { - display: grid; - grid-template-columns: 1fr 1fr; - gap: var(--padding-m); - } - .bonus-card, .debt-card { + flex-grow: 1; padding: 10px 15px; border: none; border-radius: 12px; + background-color: transparent; color: var(--text-secondary-color); + font-family: var(--font-family); font-weight: 600; font-size: 1em; + cursor: pointer; transition: background-color 0.2s, color 0.2s; + } + .nav-btn.active { background-color: var(--brand-yellow); color: var(--brand-black); box-shadow: 0 2px 10px rgba(255,193,7,0.3); } + .content-section { display: none; flex-direction: column; gap: var(--padding-m); } + .content-section.active { display: flex; } + .card-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--padding-m); } + .bonus-card, .debt-card, .promo-card { background: linear-gradient(145deg, #2a2a2a, #1c1c1c); - border-radius: calc(var(--border-radius) + 8px); - padding: var(--padding-l); - text-align: center; - position: relative; - overflow: hidden; - } - .bonus-card { - box-shadow: var(--shadow-glow); - border: 1px solid rgba(255, 193, 7, 0.2); - } - .debt-card { - box-shadow: var(--shadow-glow-red); - border: 1px solid rgba(244, 67, 54, 0.2); - } - .card-label { - font-size: 1.1em; - font-weight: 500; - color: var(--text-secondary-color); - margin-bottom: 12px; - } - .bonus-amount { - font-size: 3em; - font-weight: 800; - color: var(--brand-yellow); - letter-spacing: -2px; - line-height: 1; + border-radius: calc(var(--border-radius) + 8px); padding: var(--padding-l); + text-align: center; position: relative; overflow: hidden; + } + .bonus-card { box-shadow: var(--shadow-glow); border: 1px solid rgba(255, 193, 7, 0.2); } + .debt-card { box-shadow: var(--shadow-glow-red); border: 1px solid rgba(244, 67, 54, 0.2); } + .promo-card { grid-column: 1 / -1; border: 1px solid rgba(255,255,255,0.1); } + .card-label { font-size: 1.1em; font-weight: 500; color: var(--text-secondary-color); margin-bottom: 12px; } + .bonus-amount, .debt-amount { font-size: 3em; font-weight: 800; letter-spacing: -2px; line-height: 1; } + .bonus-amount { color: var(--brand-yellow); } + .debt-amount { color: var(--brand-red); } + .client-id-card { + background-color: var(--card-bg); border-radius: var(--border-radius); + padding: var(--padding-m); display: flex; justify-content: space-between; align-items: center; } - .debt-amount { - font-size: 3em; - font-weight: 800; - color: var(--brand-red); - letter-spacing: -2px; - line-height: 1; + .client-id-label { font-weight: 500; color: var(--text-secondary-color); } + .client-id-value { + font-size: 1.3em; font-weight: 700; color: var(--brand-yellow); + letter-spacing: 2px; background-color: rgba(255,193,7,0.1); padding: 4px 10px; border-radius: 8px; } - .client-id-card { - background-color: var(--card-bg); - border-radius: var(--border-radius); - padding: var(--padding-m); - display: flex; - justify-content: space-between; - align-items: center; + .promo-code-display { + display: flex; align-items: center; justify-content: center; gap: 12px; + background-color: rgba(0,0,0,0.2); border-radius: 12px; padding: 12px; } - .client-id-label { - font-weight: 500; - color: var(--text-secondary-color); + .promo-code-value { + font-size: 1.5em; font-weight: 700; color: var(--brand-yellow); letter-spacing: 2px; } - .client-id-value { - font-size: 1.3em; - font-weight: 700; - color: var(--brand-yellow); - letter-spacing: 2px; - background-color: rgba(255,193,7,0.1); - padding: 4px 10px; - border-radius: 8px; + .copy-btn { + padding: 8px 16px; font-size: 0.9em; font-weight: 600; color: var(--brand-black); + background-color: var(--brand-yellow); border: none; border-radius: 8px; cursor: pointer; + transition: background-color 0.2s; } + .copy-btn:hover { background-color: #ffd447; } .history-section, .invoices-section, .business-card-section { - background-color: var(--card-bg); - border-radius: var(--border-radius); - padding: var(--padding-l); - display: flex; - flex-direction: column; - gap: var(--padding-m); + background-color: var(--card-bg); border-radius: var(--border-radius); + padding: var(--padding-l); display: flex; flex-direction: column; gap: var(--padding-m); } .history-title, .invoices-title, .business-card-title { - font-size: 1.4em; - font-weight: 700; - padding-bottom: var(--padding-m); + font-size: 1.4em; font-weight: 700; padding-bottom: var(--padding-m); border-bottom: 1px solid rgba(255, 255, 255, 0.1); } - .history-list, .invoices-list { - list-style: none; - padding: 0; - margin: 0; - max-height: 35vh; - overflow-y: auto; - } + .history-list, .invoices-list { list-style: none; padding: 0; margin: 0; max-height: 35vh; overflow-y: auto; } .history-item, .invoice-item { - display: flex; - justify-content: space-between; - align-items: center; - padding: 14px 4px; - border-bottom: 1px solid rgba(255, 255, 255, 0.05); + display: flex; justify-content: space-between; align-items: center; + padding: 14px 4px; border-bottom: 1px solid rgba(255, 255, 255, 0.05); } .history-item:last-child, .invoice-item:last-child { border-bottom: none; } + .invoice-item { cursor: pointer; transition: background-color 0.2s; border-radius: 8px; } + .invoice-item:hover { background-color: rgba(255,255,255,0.05); } .history-details, .invoice-details { display: flex; flex-direction: column; } .history-description, .invoice-description { font-size: 1em; font-weight: 500; } .history-date, .invoice-date { font-size: 0.8em; color: var(--text-secondary-color); margin-top: 4px; } .history-amount, .invoice-amount { font-size: 1.1em; font-weight: 700; } - .history-amount.accrual { color: #4CAF50; } - .history-amount.deduction { color: #F44336; } + .history-amount.accrual { color: var(--brand-green); } + .history-amount.deduction { color: var(--brand-red); } .invoice-amount { color: var(--brand-yellow); } - .no-history, .no-invoices { - text-align: center; - color: var(--text-secondary-color); - padding: 2rem 0; - } - - /* Business Card Styles */ - .business-card-item { - margin-bottom: 10px; - } - .business-card-label { - font-weight: 500; - color: var(--text-secondary-color); - margin-bottom: 4px; - } - .business-card-value { - font-size: 1.1em; - font-weight: 600; - color: var(--text-color); - } - .business-card-value a { - color: var(--brand-yellow); - text-decoration: none; - word-break: break-all; /* For long URLs */ - } - .business-card-value a:hover { - text-decoration: underline; - } - .business-card-phone-list { - list-style: none; - padding: 0; - margin: 0; - } - .business-card-phone-item { - margin-bottom: 5px; - } + .no-history, .no-invoices { text-align: center; color: var(--text-secondary-color); padding: 2rem 0; } + .business-card-item { margin-bottom: 10px; } + .business-card-label { font-weight: 500; color: var(--text-secondary-color); margin-bottom: 4px; } + .business-card-value { font-size: 1.1em; font-weight: 600; color: var(--text-color); } + .business-card-value a { color: var(--brand-yellow); text-decoration: none; word-break: break-all; } + .business-card-value a:hover { text-decoration: underline; } + .business-card-phone-list { list-style: none; padding: 0; margin: 0; } + .business-card-phone-item { margin-bottom: 5px; } .business-card-phone-item a { - display: inline-flex; - align-items: center; - gap: 8px; - color: var(--text-color); - text-decoration: none; - background-color: #2a2a2a; - padding: 8px 12px; - border-radius: 8px; - transition: background-color 0.2s; - } - .business-card-phone-item a:hover { - background-color: #3a3a3a; - } - .business-card-phone-item img { - height: 20px; - width: 20px; + display: inline-flex; align-items: center; gap: 8px; color: var(--text-color); + text-decoration: none; background-color: #2a2a2a; padding: 8px 12px; + border-radius: 8px; transition: background-color 0.2s; } - - /* Invoice Detail Modal */ + .business-card-phone-item a:hover { background-color: #3a3a3a; } + .business-card-phone-item img { height: 20px; width: 20px; } .modal { - display: none; - position: fixed; - z-index: 1000; - left: 0; - top: 0; - width: 100%; - height: 100%; - overflow: auto; - background-color: rgba(0,0,0,0.7); - backdrop-filter: blur(5px); - -webkit-backdrop-filter: blur(5px); + display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; + height: 100%; overflow: auto; background-color: rgba(0,0,0,0.7); + backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); + align-items: center; justify-content: center; } .modal-content { - background-color: var(--card-bg); - margin: 15% auto; - padding: var(--padding-l); - border-radius: var(--border-radius); - max-width: 90%; - width: 500px; - box-shadow: 0 5px 15px rgba(0,0,0,0.5); - position: relative; + background-color: var(--card-bg); margin: auto; padding: var(--padding-l); + border-radius: var(--border-radius); max-width: 90%; width: 500px; + box-shadow: 0 5px 15px rgba(0,0,0,0.5); position: relative; } .modal-close { - color: var(--text-secondary-color); - position: absolute; - top: 10px; - right: 20px; - font-size: 28px; - font-weight: bold; - cursor: pointer; - } - .modal-close:hover, - .modal-close:focus { - color: var(--text-color); - text-decoration: none; - cursor: pointer; - } - .modal-title { - font-size: 1.5em; - font-weight: 700; - margin-bottom: var(--padding-m); - color: var(--brand-yellow); - } - .invoice-detail-list { - list-style: none; - padding: 0; - margin: 0; + color: var(--text-secondary-color); position: absolute; top: 10px; right: 20px; + font-size: 28px; font-weight: bold; cursor: pointer; } + .modal-close:hover, .modal-close:focus { color: var(--text-color); text-decoration: none; cursor: pointer; } + .modal-title { font-size: 1.5em; font-weight: 700; margin-bottom: var(--padding-m); color: var(--brand-yellow); } + .invoice-detail-list, .promo-modal-body { list-style: none; padding: 0; margin: 0; } .invoice-detail-item { - display: flex; - justify-content: space-between; - padding: 10px 0; + display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px dashed rgba(255,255,255,0.1); } - .invoice-detail-item:last-child { - border-bottom: none; - } - .item-name { - font-weight: 500; - flex-basis: 60%; - } - .item-qty-price { - font-size: 0.9em; - color: var(--text-secondary-color); - flex-basis: 20%; - text-align: right; - } - .item-total { - font-weight: 700; - flex-basis: 20%; - text-align: right; - color: var(--brand-yellow); - } + .invoice-detail-item:last-child { border-bottom: none; } + .item-name { font-weight: 500; flex-basis: 60%; } + .item-qty-price { font-size: 0.9em; color: var(--text-secondary-color); flex-basis: 20%; text-align: right; } + .item-total { font-weight: 700; flex-basis: 20%; text-align: right; color: var(--brand-yellow); } .invoice-total-display { - padding-top: var(--padding-m); - border-top: 1px solid rgba(255,255,255,0.2); - margin-top: var(--padding-m); - display: flex; - justify-content: space-between; - font-size: 1.2em; - font-weight: 700; - } - .invoice-total-display span:first-child { - color: var(--text-color); - } - .invoice-total-display span:last-child { - color: var(--brand-yellow); - } + padding-top: var(--padding-m); border-top: 1px solid rgba(255,255,255,0.2); margin-top: var(--padding-m); + display: flex; justify-content: space-between; font-size: 1.2em; font-weight: 700; + } + .promo-modal-body { text-align: center; } + .promo-modal-body p { color: var(--text-secondary-color); margin-bottom: 1rem; } + .promo-modal-body input { + width: 100%; padding: 14px; margin-bottom: 1rem; font-size: 1.1em; + background-color: #2a2a2a; border: 1px solid #3a3a3a; border-radius: 12px; + color: var(--text-color); text-align: center; letter-spacing: 2px; + } + .promo-modal-actions { display: flex; gap: 1rem; } + .promo-modal-actions button { + flex-grow: 1; padding: 14px; font-size: 1em; font-weight: 700; + border: none; border-radius: 12px; cursor: pointer; transition: all 0.2s; + } + .btn-apply-promo { background-color: var(--brand-yellow); color: var(--brand-black); } + .btn-skip-promo { background-color: transparent; border: 2px solid #3a3a3a; color: var(--text-secondary-color); } + #promoStatus { margin-top: 1rem; font-weight: 500; min-height: 20px; }
@@ -554,13 +375,11 @@ TEMPLATE = """Добро пожаловать!
- -Ваш долг
{{ "%.2f"|format(user.debts|float) }}
Ваш промокод для друзей
+Ваш ID клиента
{{ user.id }}