added user search

This commit is contained in:
Sergey
2024-08-09 15:14:10 +03:00
parent e2c0ff8a1d
commit 273b97bbc7
2 changed files with 24 additions and 6 deletions

22
app.py
View File

@@ -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)

View File

@@ -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,
}