import datetime import random import faker import phonenumbers from phonenumbers import PhoneNumberFormat from preference_center.profile.types import BirthdayStr class FakerTyped: def __init__(self, restricted_date_of_birth_countries: list[str]) -> None: self._faker = faker.Faker() self._restricted_date_of_birth_countries = restricted_date_of_birth_countries def pystr(self) -> str: return self._faker.pystr() def first_name(self) -> str: return self._faker.first_name() def phone_number(self) -> str: digits = self._faker.pyint(50000000, 58999999) return phonenumbers.format_number( phonenumbers.parse(f"+372{digits}", _check_region=False), PhoneNumberFormat.E164, ) def date_of_birth(self, minimum_age: int = 18) -> datetime.date: return self._faker.date_of_birth(minimum_age=minimum_age) def birthday(self, minimum_age: int = 18) -> BirthdayStr: return BirthdayStr( # ty: ignore[call-non-callable] self.date_of_birth(minimum_age=minimum_age).strftime("%m/%d") ) def country_code(self, date_of_birth_restricted: bool = False) -> str: while True: if ( not date_of_birth_restricted and (country_code := self._faker.country()) not in self._restricted_date_of_birth_countries ): return country_code elif date_of_birth_restricted and ( country_code := random.choice(self._restricted_date_of_birth_countries) ): return country_code continue