mirror of
https://git.auk.su/Dinect/bonus-import-tools.git
synced 2025-12-28 18:10:01 +00:00
added user creation function
This commit is contained in:
@@ -2,4 +2,5 @@ APP_TOKEN='12345679'
|
||||
POS_TOKEN='123456'
|
||||
PRODUCTION=1
|
||||
CURRENCY='RUS'
|
||||
COUNTRY='RU'
|
||||
DRY_RUN=1
|
||||
@@ -5,7 +5,7 @@
|
||||
- Кодировка файла : UTF-8
|
||||
- Разделитель полей : , [Запятая]
|
||||
- Разделитель разрядов: . [Точка]
|
||||
- Номер телефона = 12 символов в формате: +7XXXXXXXXXX [+76543210987]
|
||||
- Номер телефона (Только Российские номера) = 12 символов в формате E164: +7XXXXXXXXXX [+76543210987]
|
||||
- Формат даты : YYYY-MM-DD [2002-03-11]
|
||||
- Формат времени : HH:MM:SS [21:05:36]
|
||||
- Гендер: M/F [M]
|
||||
|
||||
65
app.py
65
app.py
@@ -2,23 +2,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# __author__ = 'szhdanoff@gmail.com'
|
||||
import os
|
||||
import time
|
||||
import csv
|
||||
import phonenumbers
|
||||
|
||||
from email_validator import validate_email, EmailNotValidError
|
||||
from dotenv import load_dotenv
|
||||
# local imports
|
||||
from dinect_api import get_user
|
||||
from dinect_api import new_user
|
||||
|
||||
load_dotenv()
|
||||
|
||||
APP_TOKEN = os.getenv('APP_TOKEN')
|
||||
POS_TOKEN = os.getenv('POS_TOKEN')
|
||||
MERCHANT_ID = os.getenv('MERCHANT_ID')
|
||||
is_prod = bool(os.getenv('PRODUCTION', False))
|
||||
CURRENCY = os.getenv('CURRENCY', 'RUB')
|
||||
|
||||
if is_prod:
|
||||
API_URI = 'https://pos-api.dinect.com/20130701/'
|
||||
else:
|
||||
API_URI = 'https://pos-api-ote.dinect.com/20130701/'
|
||||
|
||||
print(is_prod, API_URI)
|
||||
COUNTRY = os.getenv('COUNTRY', 'RU')
|
||||
|
||||
|
||||
csv_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'csv')
|
||||
@@ -32,9 +29,10 @@ for r, d, f in os.walk(csv_path):
|
||||
files.append(os.path.join(r, file))
|
||||
|
||||
for f in files:
|
||||
with open(f + '.log', "w", encoding="utf-8") as log_file:
|
||||
with open(f + f'-{time.strftime("%Y%m%d-%H%M%S", time.localtime())}.log', "w", encoding="utf-8") as log_file:
|
||||
with open(f, "r", encoding="utf-8") as csv_file:
|
||||
file_name = os.path.basename(f)
|
||||
# USERS file
|
||||
if 'users' in file_name:
|
||||
print(f'Processing "users" file: {f}')
|
||||
csv_reader = csv.reader(csv_file, delimiter=',')
|
||||
@@ -46,11 +44,52 @@ for f in files:
|
||||
try:
|
||||
print(f'Processing line {line_count}: {row}')
|
||||
nickname, full_name, card, phone, email, gender = row
|
||||
# strip whitespaces
|
||||
nickname = nickname.strip()
|
||||
full_name = full_name.strip()
|
||||
card = card.strip()
|
||||
# validate phone
|
||||
phone = phone.strip()
|
||||
try:
|
||||
parsed_phone = phonenumbers.parse(phone, region=COUNTRY)
|
||||
if phonenumbers.is_valid_number(parsed_phone):
|
||||
phone = phonenumbers.format_number(parsed_phone, phonenumbers.PhoneNumberFormat.E164)
|
||||
except:
|
||||
print(f'error in line: [{line_count}]- Invalid phone number: {phone}')
|
||||
log_file.write(f'error in line: [{line_count}]- Invalid phone number: {phone}\n')
|
||||
# validate email
|
||||
email = email.strip()
|
||||
try:
|
||||
email_info = validate_email(email, check_deliverability=False)
|
||||
email = email_info.normalized
|
||||
except EmailNotValidError as e:
|
||||
print(f'error in line: [{line_count}]- Invalid email: {email}')
|
||||
log_file.write(f'error in line: [{line_count}]- Invalid email: {email}\n')
|
||||
|
||||
# validate / set gender
|
||||
gender = gender.strip()
|
||||
if gender not in ['M', 'F']:
|
||||
gender = 'M'
|
||||
|
||||
line_count += 1
|
||||
except ValueError as e:
|
||||
ret = f'error in line: [{line_count}] {repr(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)
|
||||
|
||||
if not user_found:
|
||||
user_created, data = new_user('Иван тестов', '79039426495')
|
||||
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')
|
||||
|
||||
|
||||
|
||||
|
||||
# TRANSACTIONS file
|
||||
if 'transaction' in file_name:
|
||||
print(f'Processing "transaction" file: {f}')
|
||||
csv_reader = csv.reader(csv_file, delimiter=',')
|
||||
|
||||
@@ -1,19 +1,33 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# __author__ = 'szhdanoff@gmail.com'
|
||||
__version__ = '1.0.1'
|
||||
import os
|
||||
import json
|
||||
# local imports
|
||||
import app
|
||||
import requests
|
||||
import json
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
app_token = app.APP_TOKEN
|
||||
pos_token = app.POS_TOKEN
|
||||
merchant_id = app.MERCHANT_ID
|
||||
url = app.API_URI
|
||||
load_dotenv()
|
||||
|
||||
# local imports
|
||||
# import app
|
||||
|
||||
is_prod = bool(os.getenv('PRODUCTION', False))
|
||||
if is_prod:
|
||||
url = 'https://pos-api.dinect.com/20130701/'
|
||||
else:
|
||||
url = 'https://pos-api-ote.dinect.com/20130701/'
|
||||
|
||||
print(is_prod, url)
|
||||
|
||||
APP_TOKEN = os.getenv('APP_TOKEN')
|
||||
POS_TOKEN = os.getenv('POS_TOKEN')
|
||||
|
||||
app_token = os.getenv('APP_TOKEN')
|
||||
pos_token = os.getenv('POS_TOKEN')
|
||||
currency = os.getenv('CURRENCY', 'RUB')
|
||||
|
||||
__version__ = '1.0.0'
|
||||
|
||||
HEADERS = {
|
||||
'Authorization': f'dmtoken {pos_token}',
|
||||
@@ -30,32 +44,24 @@ HEADERS = {
|
||||
|
||||
# GET /20130701/tokens/?next=/20130701/logon
|
||||
# https://pos-api.dinect.com/20130701/tokens/3b01228843d115ae8c03a4d3b20dcb545dbb228c
|
||||
|
||||
def get_user(search_id, get_type='auto', headers=None):
|
||||
def get_user(search_id, get_type='auto', headers=None) -> tuple:
|
||||
"""
|
||||
Retrieves user information based on the provided search ID.
|
||||
A function to get user information based on the search_id and get_type.
|
||||
|
||||
Args:
|
||||
search_id (str): The ID used to search for the user.
|
||||
get_type (str, optional): The type of search to perform. Defaults to 'auto'.
|
||||
headers (dict, optional): The headers to include in the request. Defaults to None.
|
||||
Parameters:
|
||||
search_id (str): The search id for the user.
|
||||
get_type (str, optional): The type of search (default is 'auto').
|
||||
headers (dict, optional): The headers for the request (default is None).
|
||||
|
||||
Returns:
|
||||
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.
|
||||
|
||||
Raises:
|
||||
None
|
||||
|
||||
Example:
|
||||
# >>> get_user('1234567890', 'card')
|
||||
#(True, {'id': 1002, 'card': '4620011139016364102713436', ...})
|
||||
tuple: A tuple containing a boolean value and the response data.
|
||||
- The boolean value indicating success or failure.
|
||||
- The response data based on the request made.
|
||||
"""
|
||||
if headers is None:
|
||||
headers = HEADERS
|
||||
|
||||
base_url = url + '/users/'
|
||||
base_url = url + 'users/'
|
||||
# get_type = auto, card, phone, email, foreigncard
|
||||
r = requests.get(
|
||||
base_url,
|
||||
@@ -64,14 +70,17 @@ def get_user(search_id, get_type='auto', headers=None):
|
||||
get_type: search_id
|
||||
}
|
||||
)
|
||||
|
||||
if r.status_code == 200:
|
||||
return True, r.json()
|
||||
if len(r.json()) == 0:
|
||||
return False, None, None, None, r.text
|
||||
else:
|
||||
return True, r.json()[0]['id'], r.json()[0]['card'], r.json()[0]['purchases_url'], r.json()
|
||||
else:
|
||||
return False, r.json()
|
||||
return False, None, None, None, r.text
|
||||
# return False, r.text
|
||||
|
||||
|
||||
def new_user(nickname, phone, foreign_card=None, headers=None):
|
||||
def new_user(nickname, phone, gender=None, foreign_card=None, headers=None):
|
||||
"""
|
||||
A function that creates a new user with optional headers.
|
||||
|
||||
@@ -82,6 +91,7 @@ def new_user(nickname, phone, 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 gender:
|
||||
:param foreign_card:
|
||||
:param headers:
|
||||
:param phone:
|
||||
@@ -93,8 +103,10 @@ def new_user(nickname, phone, foreign_card=None, headers=None):
|
||||
base_url = url + '/users/'
|
||||
|
||||
params = {
|
||||
"name": nickname ,
|
||||
# "short_name": nickname,
|
||||
"full_name": nickname,
|
||||
"phone": phone,
|
||||
# "gender": gender,
|
||||
}
|
||||
|
||||
r = requests.post(base_url, headers=headers, json=params)
|
||||
@@ -151,13 +163,30 @@ def bonuses_update(
|
||||
return False, r.json()
|
||||
|
||||
|
||||
result, data = get_user('1234567890123')
|
||||
if result:
|
||||
user_id = data[0].get('id')
|
||||
# user_date = new_user('Test2', '79039426493')
|
||||
# print(user_date)
|
||||
|
||||
# 79039426498
|
||||
# (True, {'DIN': 232113, 'ID': '4620011139016260791309380'})
|
||||
|
||||
# result, user_id, user_card, purchases_url, data = get_user('1234567890123')
|
||||
# result, user_id, user_card, purchases_url, data = get_user('79039426493', get_type='phone')
|
||||
# print(result, data)
|
||||
# if result:
|
||||
# # user_id = data[0].get('id')
|
||||
# # user_card = data[0].get('card')
|
||||
# # purchases_url = data[0].get('purchases_url')
|
||||
# print('user_id', user_id)
|
||||
# print('user_card', user_card)
|
||||
# print('purchases_url', purchases_url)
|
||||
|
||||
|
||||
# добавление внешней карты лояльности
|
||||
|
||||
|
||||
# print(get_user('1002'))
|
||||
print(get_user('4620011139016689273132009'))
|
||||
# print(get_user('1234567890123'))
|
||||
# print(get_user('+79039406889'))
|
||||
# print(new_user())
|
||||
# (True, '{"DIN":3152300,"ID":"4620011139016570939672611"}')
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# httpx~=0.27.0
|
||||
python-dotenv~=1.0.0
|
||||
requests
|
||||
requests~=2.32.3
|
||||
phonenumbers~=8.13.42
|
||||
email-validator
|
||||
Reference in New Issue
Block a user