diff --git "a/app.py" "b/app.py" --- "a/app.py" +++ "b/app.py" @@ -1,5 +1,3 @@ - - from flask import Flask, render_template_string, request, redirect, url_for, session, send_file, flash, jsonify, g import json import os @@ -17,7 +15,7 @@ import copy load_dotenv() -app = Flask(__name__) +app = Flask(name) app.secret_key = os.getenv("FLASK_SECRET_KEY", 'your_unique_secret_key_soola_cosmetics_67890') DATA_FILE = 'data_soola.json' USERS_FILE = 'users_soola.json' @@ -47,2296 +45,1296 @@ app_data = {'products': [], 'categories': [], 'orders': {}} app_users = {} def download_db_from_hf(specific_file=None, retries=DOWNLOAD_RETRIES, delay=DOWNLOAD_DELAY): - if not HF_TOKEN_READ and not HF_TOKEN_WRITE: - logging.warning("HF_TOKEN_READ/HF_TOKEN_WRITE not set. Download might fail for private repos.") - - token_to_use = HF_TOKEN_READ if HF_TOKEN_READ else HF_TOKEN_WRITE - - files_to_download = [specific_file] if specific_file else SYNC_FILES - logging.info(f"Attempting download for {files_to_download} from {REPO_ID}...") - all_successful = True +token_to_use = HF_TOKEN_READ if HF_TOKEN_READ else HF_TOKEN_WRITE +files_to_download = [specific_file] if specific_file else SYNC_FILES +all_successful = True + +code +Code +download +content_copy +expand_less +for file_name in files_to_download: + success = False + local_file_path = os.path.join(".", file_name) + for attempt in range(retries + 1): + try: + hf_hub_download( + repo_id=REPO_ID, + filename=file_name, + repo_type="dataset", + token=token_to_use, + local_dir=".", + local_dir_use_symlinks=False, + force_download=True, + resume_download=False + ) + success = True + break + except RepositoryNotFoundError: + return False + except HfHubHTTPError as e: + if e.response.status_code == 404: + if not os.path.exists(local_file_path): + try: + default_content = {} + if file_name == DATA_FILE: + default_content = {'products': [], 'categories': [], 'orders': {}} + elif file_name == USERS_FILE: + default_content = {} - for file_name in files_to_download: - success = False - local_file_path = os.path.join(".", file_name) - for attempt in range(retries + 1): - try: - logging.info(f"Downloading {file_name} (Attempt {attempt + 1}/{retries + 1})...") - hf_hub_download( - repo_id=REPO_ID, - filename=file_name, - repo_type="dataset", - token=token_to_use, - local_dir=".", - local_dir_use_symlinks=False, - force_download=True, - resume_download=False, - cache_dir=None - ) - logging.info(f"Successfully downloaded and overwrote {file_name}.") + if default_content is not None: + with open(local_file_path, 'w', encoding='utf-8') as f: + json.dump(default_content, f, ensure_ascii=False, indent=4) + except Exception: + pass success = True break - except RepositoryNotFoundError: - logging.error(f"Repository {REPO_ID} not found. Download cancelled for all files.") - return False - except HfHubHTTPError as e: - if e.response.status_code == 404: - logging.warning(f"File {file_name} not found in repo {REPO_ID} (404). Checking local file.") - if not os.path.exists(local_file_path): - logging.warning(f"Local file {file_name} also not found. Creating an empty default.") - try: - default_content = {} - if file_name == DATA_FILE: - default_content = {'products': [], 'categories': [], 'orders': {}} - elif file_name == USERS_FILE: - default_content = {} - - if default_content is not None: - with open(local_file_path, 'w', encoding='utf-8') as f: - json.dump(default_content, f, ensure_ascii=False, indent=4) - logging.info(f"Created empty local file {file_name}.") - except Exception as create_e: - logging.error(f"Failed to create empty local file {file_name}: {create_e}") - else: - logging.info(f"File {file_name} not found on HF, but exists locally. Using local version.") - success = True - break - else: - logging.error(f"HTTP error downloading {file_name} (Attempt {attempt + 1}): {e}. Retrying in {delay}s...") - except requests.exceptions.RequestException as e: - logging.error(f"Network error downloading {file_name} (Attempt {attempt + 1}): {e}. Retrying in {delay}s...") - except Exception as e: - logging.error(f"Unexpected error downloading {file_name} (Attempt {attempt + 1}): {e}. Retrying in {delay}s...", exc_info=True) - - if attempt < retries: - time.sleep(delay) - - if not success: - logging.error(f"Failed to download {file_name} after {retries + 1} attempts.") - all_successful = False + else: + pass + except Exception: + pass - logging.info(f"Download process finished. Overall success: {all_successful}") - return all_successful + if attempt < retries: + time.sleep(delay) + + if not success: + all_successful = False + +return all_successful def _load_from_file(file_path, default_value, lock): - try: - with lock: - with open(file_path, 'r', encoding='utf-8') as file: - content = json.load(file) - logging.info(f"Data loaded successfully from {file_path}") - if file_path == DATA_FILE: - if not isinstance(content, dict): raise ValueError("Data file is not a dictionary") - if 'products' not in content: content['products'] = [] - if 'categories' not in content: content['categories'] = [] - if 'orders' not in content: content['orders'] = {} - elif file_path == USERS_FILE: - if not isinstance(content, dict): raise ValueError("Users file is not a dictionary") - return content - except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: - logging.error(f"Error loading local file {file_path}: {e}. Returning default.") - if not os.path.exists(file_path): - try: - with lock: - with open(file_path, 'w', encoding='utf-8') as f: - json.dump(default_value, f, ensure_ascii=False, indent=4) - logging.info(f"Created default local file {file_path}.") - except Exception as create_e: - logging.error(f"Failed to create default local file {file_path}: {create_e}") - return copy.deepcopy(default_value) +try: +with lock: +with open(file_path, 'r', encoding='utf-8') as file: +content = json.load(file) +if file_path == DATA_FILE: +if not isinstance(content, dict): raise ValueError +if 'products' not in content: content['products'] = [] +if 'categories' not in content: content['categories'] = [] +if 'orders' not in content: content['orders'] = {} +elif file_path == USERS_FILE: +if not isinstance(content, dict): raise ValueError +return content +except Exception: +if not os.path.exists(file_path): +try: +with lock: +with open(file_path, 'w', encoding='utf-8') as f: +json.dump(default_value, f, ensure_ascii=False, indent=4) +except Exception: +pass +return copy.deepcopy(default_value) def load_initial_data(): - global app_data, app_users - logging.info("Attempting initial data load...") - download_db_from_hf() - app_data = _load_from_file(DATA_FILE, {'products': [], 'categories': [], 'orders': {}}, data_lock) - app_users = _load_from_file(USERS_FILE, {}, users_lock) - logging.info(f"Initial load complete. Products: {len(app_data.get('products',[]))}, Categories: {len(app_data.get('categories',[]))}, Orders: {len(app_data.get('orders',{}))}, Users: {len(app_users)}") +global app_data, app_users +download_db_from_hf() +app_data = _load_from_file(DATA_FILE, {'products': [], 'categories': [], 'orders': {}}, data_lock) +app_users = _load_from_file(USERS_FILE, {}, users_lock) def get_data(): - with data_lock: - return copy.deepcopy(app_data) +with data_lock: +return copy.deepcopy(app_data) def save_data(new_data): - global app_data - try: - if not isinstance(new_data, dict): - logging.error("Attempted to save invalid data structure (not a dict). Aborting save.") - return False - if 'products' not in new_data: new_data['products'] = [] - if 'categories' not in new_data: new_data['categories'] = [] - if 'orders' not in new_data: new_data['orders'] = {} - - with data_lock: - app_data = copy.deepcopy(new_data) - with open(DATA_FILE, 'w', encoding='utf-8') as file: - json.dump(app_data, file, ensure_ascii=False, indent=4) - logging.info(f"Data successfully saved to {DATA_FILE} and memory cache updated.") - return True - except Exception as e: - logging.error(f"Error saving data to {DATA_FILE}: {e}", exc_info=True) - return False +global app_data +try: +if not isinstance(new_data, dict): +return False +if 'products' not in new_data: new_data['products'] = [] +if 'categories' not in new_data: new_data['categories'] = [] +if 'orders' not in new_data: new_data['orders'] = {} + +code +Code +download +content_copy +expand_less +with data_lock: + app_data = copy.deepcopy(new_data) + with open(DATA_FILE, 'w', encoding='utf-8') as file: + json.dump(app_data, file, ensure_ascii=False, indent=4) + return True +except Exception: + return False def get_users(): - with users_lock: - return copy.deepcopy(app_users) +with users_lock: +return copy.deepcopy(app_users) def save_users(new_users): - global app_users - try: - if not isinstance(new_users, dict): - logging.error("Attempted to save invalid users structure (not a dict). Aborting save.") - return False - with users_lock: - app_users = copy.deepcopy(new_users) - with open(USERS_FILE, 'w', encoding='utf-8') as file: - json.dump(app_users, file, ensure_ascii=False, indent=4) - logging.info(f"User data successfully saved to {USERS_FILE} and memory cache updated.") - return True - except Exception as e: - logging.error(f"Error saving user data to {USERS_FILE}: {e}", exc_info=True) - return False +global app_users +try: +if not isinstance(new_users, dict): +return False +with users_lock: +app_users = copy.deepcopy(new_users) +with open(USERS_FILE, 'w', encoding='utf-8') as file: +json.dump(app_users, file, ensure_ascii=False, indent=4) +return True +except Exception: +return False def upload_db_to_hf(specific_file=None): - if not HF_TOKEN_WRITE: - logging.warning("HF_TOKEN (for writing) not set. Skipping upload to Hugging Face.") - return False - try: - api = HfApi() - files_to_upload = [specific_file] if specific_file else SYNC_FILES - logging.info(f"Starting upload of {files_to_upload} to HF repo {REPO_ID}...") - all_successful = True - - for file_name in files_to_upload: - if os.path.exists(file_name): - try: - lock = data_lock if file_name == DATA_FILE else users_lock - with lock: - api.upload_file( - path_or_fileobj=file_name, - path_in_repo=file_name, - repo_id=REPO_ID, - repo_type="dataset", - token=HF_TOKEN_WRITE, - commit_message=f"Sync {file_name} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" - ) - logging.info(f"File {file_name} successfully uploaded to Hugging Face.") - time.sleep(UPLOAD_DELAY) - except Exception as e: - logging.error(f"Error uploading file {file_name} to Hugging Face: {e}") - all_successful = False - else: - logging.warning(f"File {file_name} not found locally, skipping upload.") - all_successful = False +if not HF_TOKEN_WRITE: +return False +try: +api = HfApi() +files_to_upload = [specific_file] if specific_file else SYNC_FILES +all_successful = True + +code +Code +download +content_copy +expand_less +for file_name in files_to_upload: + if os.path.exists(file_name): + try: + lock = data_lock if file_name == DATA_FILE else users_lock + with lock: + api.upload_file( + path_or_fileobj=file_name, + path_in_repo=file_name, + repo_id=REPO_ID, + repo_type="dataset", + token=HF_TOKEN_WRITE, + commit_message=f"Sync {file_name} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" + ) + time.sleep(UPLOAD_DELAY) + except Exception: + all_successful = False + else: + all_successful = False - logging.info(f"Finished uploading files to HF. Overall success: {all_successful}") - return all_successful - except Exception as e: - logging.error(f"General error during Hugging Face upload initialization or process: {e}", exc_info=True) - return False + return all_successful +except Exception: + return False def periodic_backup(): - logging.info(f"Setting up periodic backup every {BACKUP_INTERVAL} seconds.") - while True: - time.sleep(BACKUP_INTERVAL) - logging.info("Starting periodic backup...") - try: - upload_success = upload_db_to_hf() - if upload_success: - logging.info("Periodic backup finished successfully.") - else: - logging.warning("Periodic backup finished with errors (some files might not have been uploaded).") - except Exception as e: - logging.error(f"Error during periodic backup execution: {e}", exc_info=True) - +while True: +time.sleep(BACKUP_INTERVAL) +try: +upload_db_to_hf() +except Exception: +pass CATALOG_TEMPLATE = ''' - - -
- - -
+ {% endif %}
+ {{ product.get('description', '')[:50] }}{% if product.get('description', '')|length > 50 %}...{% endif %}
- {% endif %}
- {{ product.get('description', '')[:50] }}{% if product.get('description', '')|length > 50 %}...{% endif %}
-