From 273b97bbc7336b67b1b2a4095b1e9360226f1c75 Mon Sep 17 00:00:00 2001 From: Sergey Date: Fri, 9 Aug 2024 15:14:10 +0300 Subject: [PATCH] added user search --- app.py | 22 +++++++++++++++++++--- dinect_api.py | 8 +++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 4ee726b..15a3742 100644 --- a/app.py +++ b/app.py @@ -63,6 +63,7 @@ for f in files: email_info = validate_email(email, check_deliverability=False) email = email_info.normalized except EmailNotValidError as e: + email = None print(f'error in line: [{line_count}]- Invalid email: {email}') log_file.write(f'error in line: [{line_count}]- Invalid email: {email}\n') @@ -75,16 +76,31 @@ for f in files: except ValueError as e: ret = f'Unexpected error in line: [{line_count}] {repr(e)}' log_file.write(f'{ret}\n') - # Updating the database via the API - user_found, user_id, user_card, purchases_url, data = get_user(card) + # Find user via the API + # by card + user_found_by_card, user_id, user_card, purchases_url, data = get_user(card) + print(f'user_found_by_card {card}', user_found_by_card) + # by phone + phone = phone.replace('+', '') + if not user_found_by_card: + user_found_by_phone, user_id, user_card, purchases_url, data = get_user(phone, get_type='phone') + print(f'user_found_by_phone {phone}', user_found_by_phone) + + user_found = user_found_by_card or user_found_by_phone + + # create new user if not found if not user_found: - user_created, data = new_user('Иван тестов', '79039426495') + user_created, data = new_user( + full_name=nickname, phone=phone, gender=None, foreign_card=None, email=email, + ) if user_created: # log_file.write(f'error in line: [{line_count}]- Invalid user data: {data}\n') print('User created with', data['ID'], data['DIN']) else: log_file.write(f'error in line: [{line_count}]- Invalid user data: {data}\n') + else: + print('User found with', user_id, user_card, purchases_url) diff --git a/dinect_api.py b/dinect_api.py index 5d394e8..11c8f6e 100644 --- a/dinect_api.py +++ b/dinect_api.py @@ -80,7 +80,7 @@ def get_user(search_id, get_type='auto', headers=None) -> tuple: # return False, r.text -def new_user(nickname, phone, gender=None, foreign_card=None, headers=None): +def new_user(full_name, phone, gender=None, foreign_card=None, email=None, headers=None): """ A function that creates a new user with optional headers. @@ -91,11 +91,12 @@ def new_user(nickname, phone, gender=None, foreign_card=None, headers=None): tuple: A tuple containing a boolean indicating the success of the request and the JSON response. If the request is successful, the boolean is True and the JSON response is returned. If the request is unsuccessful, the boolean is False and the JSON response is returned. + :param email: :param gender: :param foreign_card: :param headers: :param phone: - :param nickname : + :param full_name : """ if headers is None: headers = HEADERS @@ -104,8 +105,9 @@ def new_user(nickname, phone, gender=None, foreign_card=None, headers=None): params = { # "short_name": nickname, - "full_name": nickname, + "full_name": full_name, "phone": phone, + "email": email, # "gender": gender, }