From d165a945d76ff588f43dea17648459e81cf0bfa1 Mon Sep 17 00:00:00 2001 From: mahdavi Date: Sun, 25 Sep 2022 14:48:59 +0330 Subject: [PATCH] set phone number as username --- accounts/settings.py | 2 +- apps/users/serializers.py | 1 + client.py | 60 ++++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/accounts/settings.py b/accounts/settings.py index 09fe3e4..06b3189 100644 --- a/accounts/settings.py +++ b/accounts/settings.py @@ -203,4 +203,4 @@ CELERY_BROKER_URL = config('CELERY_BROKER_URL') SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') USE_X_FORWARDED_HOST = True -USE_X_FORWARDED_PORT = True \ No newline at end of file +USE_X_FORWARDED_PORT = True diff --git a/apps/users/serializers.py b/apps/users/serializers.py index 7284523..a8247bb 100644 --- a/apps/users/serializers.py +++ b/apps/users/serializers.py @@ -61,6 +61,7 @@ class RequestOTPSerializer(serializers.ModelSerializer): validated_data = self.validated_data user = User.objects.filter(phone_number=validated_data['phone_number']).first() if not user: + validated_data['username'] = validated_data['phone_number'] user = User.objects.create_user(**validated_data) if not user.otp_is_valid(): diff --git a/client.py b/client.py index 6160ab3..c729eff 100644 --- a/client.py +++ b/client.py @@ -1,10 +1,11 @@ import json import requests -OAUTH_CLIENT_ID = 'qHhNuALPINvJ8UHAcBGsX1uVRWu3AmRMwRL5kVbi' +OAUTH_CLIENT_ID = 'WNQzBFHxell95Px45veRncL2vIw6l90Q4Zr5yKuU' +'PFY1JXAb0c7H88GAsyzAYzSMWBR5R8QvSux7e0uCeeqGeUJj8yNvW4isZz6E7U0gUTTF5KRtuTZf59iztjfwZgto8rGe0czqMSarUhLHf8hYMotsJNRgQb98wvbFiHca' # OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$A8ru6iwhcjU1wEgPL6n6b8$GOhCcrfqw4Xkw6USpuEL6esjmNgqJe1A8q1Ec2dLxhw=' -OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$A8ru6iwhcjU1wEgPL6n6b8$GOhCcrfqw4Xkw6USpuEL6esjmNgqJe1A8q1Ec2dLxhw=' -API_URI = 'http://127.0.0.1:8000' +OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$cu9a2xBKZzR5sLfcBNsRB6$UZ6MurBq8mfccWDdCCvNTCv3EyiAKSGkh51l2rlAYVk=' +API_URI = 'https://accounts.mindplus-dev.ir' # API_URI = 'https://accounts.gooyal.com' # NOTIFICATION_URI = 'https://notifications.gooyal.com' # NOTIFICATION_URI = 'http://127.0.0.1:8001' @@ -26,14 +27,16 @@ class ApiClient(): "username": phone_number, "password": password, # "scope": 'introspection', - "scope": 'introspection superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', + "scope": 'introspection education.course:retrieve superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', "auth_fields": 'phone_number:otp' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, - auth=auth) + auth=auth, + verify=False + ) auth_data = response.json() if 'access_token' in auth_data: @@ -48,14 +51,16 @@ class ApiClient(): "grant_type": "password", "username": phone_number, "password": token, - "scope": 'introspection superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', + "scope": 'introspection education.course:retrieve education.course:submit superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', "auth_fields": 'phone_number:ott' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, - auth=auth) + auth=auth, + verify=False + ) auth_data = response.json() if 'access_token' in auth_data: @@ -63,7 +68,7 @@ class ApiClient(): return auth_data - def _request(self, path, data=None, files=None, method='get', with_auth=True, encode=True): + def _request(self, url, data=None, files=None, method='get', with_auth=True, encode=True): if files: header = {} else: @@ -78,7 +83,7 @@ class ApiClient(): if data and encode: data = json.dumps(data) - response = requests.request(method, f'{API_URI}/{path}', headers=header, data=data, files=files) + response = requests.request(method, url, headers=header, data=data, files=files, verify=False) try: return response.json() except: @@ -88,20 +93,23 @@ class ApiClient(): phone_number = self.phone_number path = 'users/api/request_otp/' + url = f'{API_URI}/{path}' method = 'post' data = { 'phone_number': phone_number } - result = self._request(path=path, data=data, method=method, with_auth=False) + result = self._request(url=url, data=data, method=method, with_auth=False) return result def get_account(self): path = 'users/api/account/' - result = self._request(path=path) + url = f'{API_URI}/{path}' + result = self._request(url=url) return result def update_account(self, **kwargs): path = 'users/api/account/' + url = f'{API_URI}/{path}' data = {} fields = ( 'username', @@ -124,22 +132,24 @@ class ApiClient(): files = None encode = True - result = self._request(path=path, data=data, method='put', files=files, encode=encode) + result = self._request(url=url, data=data, method='put', files=files, encode=encode) return result def change_password(self, old_password, new_password, old_password_gateway='otp'): path = 'users/api/change_password/' + url = f'{API_URI}/{path}' data = {'old_password': old_password, 'new_password': new_password, 'old_password_gateway': old_password_gateway } - result = self._request(path=path, data=data, method='put') + result = self._request(url=url, data=data, method='put') return result def get_user_profile(self, code): path = f'users/api/users/{code}/' - result = self._request(path=path) + url = f'{API_URI}/{path}' + result = self._request(url=url) return result def get_application_list(self, page=None): @@ -147,20 +157,27 @@ class ApiClient(): if page: page = int(page) path = f'{path}?page={page}' + url = f'{API_URI}/{path}' - return self._request(path=path) + return self._request(url=url) def get_transactions(self, page=None): path = 'wallet/api/transactions/' if page: page = int(page) path = f'{path}?page={page}' + url = f'{API_URI}/{path}' - return self._request(path=path) + return self._request(url=url) def get_transaction(self, code): path = f'transactions/{code}' - return self._request(path=path) + url = f'{API_URI}/{path}' + return self._request(url=url) + + def get_course_list(self): + url = 'http://127.0.0.1:8000/api/courses/' + return self._request(url=url) # create user invoice # def create_invoice(self, amount, delay, payer=None): @@ -175,15 +192,19 @@ class ApiClient(): # create application invoice def get_application_token(self): - response = self._request('users/api/request_ott/', method='get') + path = 'users/api/request_ott/' + url = f'{API_URI}/{path}' + response = self._request(url, method='get') return response and 'code' in response, response def create_invoice(self, amount, delay=0): + path = 'wallet/api/transactions/' + url = f'{API_URI}/{path}' data = { 'delay': delay, 'amount': amount } - response = self._request('wallet/api/transactions/', method='post', data=data) + response = self._request(url, method='post', data=data) return response and 'code' in response, response def pay_invoice(self, code): @@ -227,6 +248,7 @@ client = ApiClient('+989106853582') client.request_otp() client.otp_login('12345') print(client.get_application_list()) +print(client.get_course_list()) # print(client.get_application_token()) # print(client.ott_login('abcdef12345'))