Google Drive Service
סקירה
שירות גוגל דרייב אחראי לאימות (Device Flow), ניהול טוקנים, יצירת ZIP, והעלאות לתיקיות ממוסלות לפי קטגוריה/תאריך/ריפו.
הערות OAuth
שימור refresh_token: בשמירה מתמזג עם טוקנים קיימים כדי לא למחוק רענון שלא הוחזר.
רענון טוקן: ניסיון רענון עם טיפול כשלים שקט.
מבני קבצים
שמות קבצים: BKP_{label}_{entity}_v{n}_{date}.zip
נתיב משנה: {קטגוריה}/{YYYY}/{MM-DD} ולפי ריפו אם רלוונטי.
API (autodoc)
- services.google_drive_service.start_device_authorization(user_id)[מקור]
Starts OAuth Device Authorization flow for Google Drive.
Returns a dict with: verification_url, user_code, device_code, interval, expires_in.
- services.google_drive_service.poll_device_token(device_code)[מקור]
Attempts a single token exchange for a device_code.
- services.google_drive_service.save_tokens(user_id, tokens)[מקור]
Save OAuth tokens, preserving existing refresh_token if missing.
Some OAuth exchanges (and refreshes) do not return refresh_token again. We merge with existing tokens to avoid wiping the refresh_token.
- services.google_drive_service.ensure_folder(user_id, name, parent_id=None)[מקור]
- services.google_drive_service.get_or_create_default_folder(user_id)[מקור]
- services.google_drive_service.ensure_path(user_id, path)[מקור]
Ensure nested folders path like ”Parent/Sub1/Sub2“ exists; returns last folder id.
- services.google_drive_service.ensure_subpath(user_id, sub_path)[מקור]
Ensure nested subfolders under the user’s root. Does not change prefs.
- services.google_drive_service.compute_subpath(category, repo_name=None)[מקור]
Return destination subpath under the user’s root folder.
Simplified structure without date-based nesting to avoid deep paths: - zip -> ”zip“ - all -> ”הכל“ - by_repo -> ”לפי_ריפו/<repo_name>“ - large -> ”קבצים_גדולים“ - other -> ”שאר_קבצים“
- services.google_drive_service.compute_friendly_name(user_id, category, entity_name, rating=None, content_sample=None)[מקור]
Return a friendly filename per spec using underscores.
Pattern examples: - BKP_zip_CodeBot_v7_26-08-2025.zip - BKP_zip_CodeBot_v7_🏆_26-08-2025.zip
- services.google_drive_service.upload_bytes(user_id, filename, data, folder_id=None, sub_path=None)[מקור]
- services.google_drive_service.upload_all_saved_zip_backups(user_id)[מקור]
Upload only ZIP backups that were not uploaded before for this user.
Uses db.drive_prefs.uploaded_backup_ids (set) to deduplicate uploads.
- services.google_drive_service.create_repo_grouped_zip_bytes(user_id)[מקור]
Return zips grouped by repo: (repo_name, suggested_name, zip_bytes).
- services.google_drive_service.create_full_backup_zip_bytes(user_id, category='all')[מקור]
Creates a ZIP of user data by category and returns (filename, bytes).
- services.google_drive_service.perform_scheduled_backup(user_id)[מקור]
Runs a scheduled backup to Drive according to user’s selected category.
Category resolution priority: 1) drive_prefs.schedule_category (explicit) 2) drive_prefs.last_selected_category (UI selection) 3) fallback to ”all“
Updates last_backup_at on any successful scheduled upload. Updates last_full_backup_at only if category == ”all“.