|
| 1 | +""" |
| 2 | +Account Manager |
| 3 | +Handles multiple student account management for course selection. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import json |
| 8 | +import uuid |
| 9 | +from dataclasses import dataclass, asdict |
| 10 | +from typing import List, Optional |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class Account: |
| 15 | + """Represents a student account.""" |
| 16 | + id: str |
| 17 | + name: str |
| 18 | + username: str |
| 19 | + password: str |
| 20 | + |
| 21 | + def to_dict(self) -> dict: |
| 22 | + return asdict(self) |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def from_dict(cls, data: dict) -> 'Account': |
| 26 | + return cls( |
| 27 | + id=data.get('id', str(uuid.uuid4())), |
| 28 | + name=data.get('name', ''), |
| 29 | + username=data.get('username', ''), |
| 30 | + password=data.get('password', '') |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class AccountManager: |
| 35 | + """Manages multiple student accounts.""" |
| 36 | + |
| 37 | + DEFAULT_BASE_URL = "" # Must be configured by user |
| 38 | + |
| 39 | + def __init__(self, config_path: str = None): |
| 40 | + if config_path is None: |
| 41 | + config_path = os.path.join(os.path.dirname(__file__), "config.json") |
| 42 | + self.config_path = config_path |
| 43 | + self.accounts: List[Account] = [] |
| 44 | + self.default_account_id: Optional[str] = None |
| 45 | + self.base_url: str = self.DEFAULT_BASE_URL |
| 46 | + self._load() |
| 47 | + |
| 48 | + def _load(self): |
| 49 | + """Load accounts from config file.""" |
| 50 | + if not os.path.exists(self.config_path): |
| 51 | + self.accounts = [] |
| 52 | + self.default_account_id = None |
| 53 | + return |
| 54 | + |
| 55 | + try: |
| 56 | + with open(self.config_path, 'r', encoding='utf-8') as f: |
| 57 | + config = json.load(f) |
| 58 | + |
| 59 | + # Check if it's the new format (has 'accounts' key) |
| 60 | + if 'accounts' in config: |
| 61 | + self.accounts = [Account.from_dict(acc) for acc in config.get('accounts', [])] |
| 62 | + self.default_account_id = config.get('default_account') |
| 63 | + self.base_url = config.get('base_url', self.DEFAULT_BASE_URL) |
| 64 | + else: |
| 65 | + # Legacy format: single account with username/password at root |
| 66 | + self._migrate_legacy_config(config) |
| 67 | + except Exception as e: |
| 68 | + print(f"Error loading config: {e}") |
| 69 | + self.accounts = [] |
| 70 | + self.default_account_id = None |
| 71 | + |
| 72 | + def _migrate_legacy_config(self, legacy_config: dict): |
| 73 | + """Convert old single-account config to new format.""" |
| 74 | + username = legacy_config.get('username', '') |
| 75 | + password = legacy_config.get('password', '') |
| 76 | + |
| 77 | + if username: |
| 78 | + account = Account( |
| 79 | + id=str(uuid.uuid4()), |
| 80 | + name=f"账号1", |
| 81 | + username=username, |
| 82 | + password=password |
| 83 | + ) |
| 84 | + self.accounts = [account] |
| 85 | + self.default_account_id = account.id |
| 86 | + # Save in new format |
| 87 | + self.save() |
| 88 | + else: |
| 89 | + self.accounts = [] |
| 90 | + self.default_account_id = None |
| 91 | + |
| 92 | + def save(self): |
| 93 | + """Save accounts to config file.""" |
| 94 | + config = { |
| 95 | + 'accounts': [acc.to_dict() for acc in self.accounts], |
| 96 | + 'default_account': self.default_account_id, |
| 97 | + 'base_url': self.base_url |
| 98 | + } |
| 99 | + |
| 100 | + with open(self.config_path, 'w', encoding='utf-8') as f: |
| 101 | + json.dump(config, f, ensure_ascii=False, indent=2) |
| 102 | + |
| 103 | + def add_account(self, name: str, username: str, password: str) -> Account: |
| 104 | + """Add a new account.""" |
| 105 | + account = Account( |
| 106 | + id=str(uuid.uuid4()), |
| 107 | + name=name, |
| 108 | + username=username, |
| 109 | + password=password |
| 110 | + ) |
| 111 | + self.accounts.append(account) |
| 112 | + |
| 113 | + # Set as default if it's the first account |
| 114 | + if len(self.accounts) == 1: |
| 115 | + self.default_account_id = account.id |
| 116 | + |
| 117 | + self.save() |
| 118 | + return account |
| 119 | + |
| 120 | + def update_account(self, account_id: str, name: str = None, |
| 121 | + username: str = None, password: str = None) -> Optional[Account]: |
| 122 | + """Update an existing account.""" |
| 123 | + account = self.get_account(account_id) |
| 124 | + if not account: |
| 125 | + return None |
| 126 | + |
| 127 | + if name is not None: |
| 128 | + account.name = name |
| 129 | + if username is not None: |
| 130 | + account.username = username |
| 131 | + if password is not None: |
| 132 | + account.password = password |
| 133 | + |
| 134 | + self.save() |
| 135 | + return account |
| 136 | + |
| 137 | + def remove_account(self, account_id: str) -> bool: |
| 138 | + """Remove an account by ID.""" |
| 139 | + for i, acc in enumerate(self.accounts): |
| 140 | + if acc.id == account_id: |
| 141 | + self.accounts.pop(i) |
| 142 | + |
| 143 | + # Update default if we removed the default account |
| 144 | + if self.default_account_id == account_id: |
| 145 | + self.default_account_id = self.accounts[0].id if self.accounts else None |
| 146 | + |
| 147 | + self.save() |
| 148 | + return True |
| 149 | + return False |
| 150 | + |
| 151 | + def get_account(self, account_id: str) -> Optional[Account]: |
| 152 | + """Get account by ID.""" |
| 153 | + for acc in self.accounts: |
| 154 | + if acc.id == account_id: |
| 155 | + return acc |
| 156 | + return None |
| 157 | + |
| 158 | + def get_account_by_username(self, username: str) -> Optional[Account]: |
| 159 | + """Get account by username.""" |
| 160 | + for acc in self.accounts: |
| 161 | + if acc.username == username: |
| 162 | + return acc |
| 163 | + return None |
| 164 | + |
| 165 | + def get_default_account(self) -> Optional[Account]: |
| 166 | + """Get the default account.""" |
| 167 | + if self.default_account_id: |
| 168 | + return self.get_account(self.default_account_id) |
| 169 | + return self.accounts[0] if self.accounts else None |
| 170 | + |
| 171 | + def set_default_account(self, account_id: str) -> bool: |
| 172 | + """Set the default account.""" |
| 173 | + if self.get_account(account_id): |
| 174 | + self.default_account_id = account_id |
| 175 | + self.save() |
| 176 | + return True |
| 177 | + return False |
| 178 | + |
| 179 | + def get_all_accounts(self) -> List[Account]: |
| 180 | + """Get all accounts.""" |
| 181 | + return self.accounts.copy() |
| 182 | + |
| 183 | + def get_base_url(self) -> str: |
| 184 | + """Get the configured base URL.""" |
| 185 | + return self.base_url |
| 186 | + |
| 187 | + def set_base_url(self, url: str) -> None: |
| 188 | + """Set the base URL.""" |
| 189 | + self.base_url = url.rstrip('/') if url else self.DEFAULT_BASE_URL |
| 190 | + self.save() |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + # Simple test |
| 195 | + manager = AccountManager() |
| 196 | + print(f"Loaded {len(manager.accounts)} accounts") |
| 197 | + for acc in manager.accounts: |
| 198 | + print(f" - {acc.name}: {acc.username}") |
0 commit comments