import zeep import json class SmartschoolService: def __init__(self, platform, webservice_pwd): #----------------------------------- clean_platform = platform.replace("https://", "").replace("http://", "").split('/')[0] self.platform = clean_platform #------------------------------ #self.platform = platform self.webservice_pwd = webservice_pwd self.wsdl_url = f"https://{self.platform}/Webservices/V3?wsdl" self.client = zeep.Client(wsdl=self.wsdl_url) def get_student_details(self, search_term): """Zoekt een student op basis van internnummer of gebruikersnaam.""" try: # Poging 1: Via internnummer raw_result = self.client.service.getUserDetails(self.webservice_pwd, search_term) if raw_result and isinstance(raw_result, str) and raw_result.startswith('{'): return json.loads(raw_result) # Poging 2: Via gebruikersnaam raw_result_alt = self.client.service.getUserDetailsByUsername(self.webservice_pwd, search_term) if raw_result_alt and isinstance(raw_result_alt, str) and raw_result_alt.startswith('{'): return json.loads(raw_result_alt) return None except Exception as e: print(f"Service Error (GetDetails): {e}") return None def get_co_accounts_from_details(self, username): """Haalt co-accounts op en onthoudt hun index (1-6) voor wachtwoordresets.""" try: raw_result = self.client.service.getUserDetailsByUsername(self.webservice_pwd, username) if raw_result and isinstance(raw_result, str) and raw_result.startswith('{'): data = json.loads(raw_result) co_accounts = [] for i in range(1, 7): voornaam = data.get(f"voornaam_coaccount{i}") if voornaam: co_accounts.append({ "voornaam": voornaam, "naam": data.get(f"naam_coaccount{i}", ""), "type": data.get(f"type_coaccount{i}", "Co-account"), "index": i, # Belangrijk voor savePassword "parent_internnummer": data.get("internnummer") # Nodig voor de reset actie }) return co_accounts return [] except Exception as e: print(f"Service Error (Co-Accounts): {e}") return [] def reset_password(self, identifier, new_password, account_type=0): """ account_type: 0 voor leerling, 1-6 voor co-accounts. """ try: # Gebruik de parameters uit je werkende voorbeeld result_code = self.client.service.savePassword( self.webservice_pwd, identifier, new_password, account_type, 0 ) return str(result_code) except Exception as e: print(f"Service Error (ResetPassword): {e}") return "ERROR" def get_co_accounts_with_login(self, username): """Haalt co-accounts op met de exacte velden uit de Smartschool JSON.""" try: # Gebruik de bestaande methode om de JSON op te halen raw_result = self.client.service.getUserDetailsByUsername(self.webservice_pwd, username) if raw_result and isinstance(raw_result, str) and raw_result.startswith('{'): data = json.loads(raw_result) co_accounts = [] # Smartschool ondersteunt tot 6 co-accounts for i in range(1, 7): voornaam = data.get(f"voornaam_coaccount{i}") # Alleen toevoegen als er daadwerkelijk een voornaam is ingevuld (account bestaat) if voornaam and voornaam.strip() != "": # Gebruik het specifieke veld uit jouw JSON voorbeeld last_login = data.get(f"last_successful_login_coaccount{i}") co_accounts.append({ "voornaam": voornaam, "naam": data.get(f"naam_coaccount{i}", ""), "type": data.get(f"type_coaccount{i}", "Onbekend"), "last_login": last_login if last_login else "Nooit ingelogd", "email": data.get(f"email_coaccount{i}", ""), "index": i }) return co_accounts return [] except Exception as e: print(f"Fout bij ophalen co-account status: {e}") return [] def get_coaccount_report(self, search_query): """ Bundelt alle logica: zoekt student, formatteert naam en haalt co-accounts op. Geeft een tuple terug: (student_naam, resultaten_lijst) """ student_data = self.get_student_details(search_query) if not student_data: return None, [] student_name = f"{student_data.get('voornaam')} {student_data.get('naam')}" username = student_data.get('gebruikersnaam') results = self.get_co_accounts_with_login(username) return student_name, results