This commit is contained in:
Administrator 2023-10-09 23:55:43 +03:00
parent 6bb4e9ac44
commit 4cb525ac11
2 changed files with 10 additions and 6 deletions

View File

@ -1,4 +1,8 @@
import logging
class BaseEntity: class BaseEntity:
logger = logging.getLogger(__name__)
def get_user(self): def get_user(self):
raise NotImplementedError raise NotImplementedError

View File

@ -19,26 +19,26 @@ class YandexAuthView(BaseView):
'code': code 'code': code
}) })
if response.status_code != 200: if response.status_code != 200:
print("Cant access, json: ", response.json()) self.logger.warning("Cant access, json: %s", response.json())
return '/welcome' return '/welcome'
access_token = response.json().get('access_token') access_token = response.json().get('access_token')
if access_token is None: if access_token is None:
print('no access token') self.logger.warning('no access token')
return '/welcome' return '/welcome'
info_response = get('https://login.yandex.ru/info', headers={ info_response = get('https://login.yandex.ru/info', headers={
'Authorization': f'OAuth {access_token}' 'Authorization': f'OAuth {access_token}'
}) })
if info_response.status_code != 200: if info_response.status_code != 200:
print("Cant access, json: ", response.json()) self.logger.warning("Cant access, json: %s", response.json())
return '/welcome' return '/welcome'
data = info_response.json() data = info_response.json()
yandex_id = data['id'] yandex_id = data['id']
if self.request.user.is_authenticated: if self.request.user.is_authenticated:
print('Got yandex_id', yandex_id, 'writing to db') self.logger.warning('Got yandex_id %s writing to db', yandex_id)
self.request.user.yandex_id = yandex_id self.request.user.yandex_id = yandex_id
self.request.user.save() self.request.user.save()
else: else:
print('Got yandex_id', yandex_id, 'logging in') self.logger.warning('Got yandex_id %s logging in', yandex_id)
user = CustomUser.objects.filter(yandex_id=yandex_id).first() user = CustomUser.objects.filter(yandex_id=yandex_id).first()
if user is not None: if user is not None:
login(self.request, user) login(self.request, user)