from django.shortcuts import get_object_or_404 from api.models import User import requests from apibase.env import env, BASE_DIR import os env.read_env(os.path.join(BASE_DIR, ".env")) PEOPLE_API_URL = env.str("PEOPLE_API_URL", "") def verify_user_with_person_api_task(user_id: int): """ Verify the user with the Person API. :param user_id: The ID of the user to verify. """ user = get_object_or_404(User, id=user_id) # Call the Person API to verify the user if not PEOPLE_API_URL: raise ValueError("PEOPLE_API_URL is not set in the environment variables.") response = requests.get(f"{PEOPLE_API_URL}/api/person/{user.id_card}") if response.status_code == 200: data = response.json() api_nic = data.get("nic") api_name = data.get("name_en") api_house_name = data.get("house_name_en") api_dob = data.get("dob") print(f"API nic: {api_nic}") print(f"API name: {api_name}") print(f"API house name: {api_house_name}") print(f"API dob: {api_dob}") user_nic = user.id_card user_name = f"{user.first_name} {user.last_name}" user_house_name = user.address user_dob = user.dob.isoformat() print(f"User nic: {user_nic}") print(f"User name: {user_name}") print(f"User house name: {user_house_name}") print(f"User dob: {user_dob}") if ( data.get("nic") == user.id_card and data.get("name_en") == f"{user.first_name} {user.last_name}" and data.get("house_name_en") == user.address and data.get("dob").split("T")[0] == user.dob.isoformat() ): user.verified = True user.save() return True else: user.verified = False user.save() return False else: # Handle the error case print(f"Error verifying user: {response.status_code} - {response.text}") return False