From 53b9f9d8404918d719cf1930156e489102b417bb Mon Sep 17 00:00:00 2001 From: Administrator Date: Sat, 23 Sep 2023 19:10:06 +0300 Subject: [PATCH] vk --- Platform/common_models.py | 4 +++- Platform/settings.py | 2 ++ templates/profile.html | 4 ++-- templates/vk_auth.html | 11 +++++++++++ web/urls.py | 3 ++- web/views/__init__.py | 3 ++- web/views/vk_auth.py | 31 +++++++++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 templates/vk_auth.html create mode 100644 web/views/vk_auth.py diff --git a/Platform/common_models.py b/Platform/common_models.py index c91f04b..4fa7772 100644 --- a/Platform/common_models.py +++ b/Platform/common_models.py @@ -1 +1,3 @@ -from web.models import * \ No newline at end of file +from web.models import * +from configs.models import * +from experiments.models import * diff --git a/Platform/settings.py b/Platform/settings.py index 836a67b..10730fa 100644 --- a/Platform/settings.py +++ b/Platform/settings.py @@ -145,3 +145,5 @@ MINIO_HOST = os.getenv("MINIO_HOST", "localhost") + ":9000" MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin") MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin") MINIO_BUCKET_NAME = 'platform' + +VK_SERVICE_TOKEN = os.getenv("VK_SERVICE_TOKEN", None) diff --git a/templates/profile.html b/templates/profile.html index 34fc67a..64fc380 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -51,8 +51,8 @@
- - {% if account.vk_user_id %} + + {% if account.vk_id %}
{% endif %} diff --git a/templates/vk_auth.html b/templates/vk_auth.html new file mode 100644 index 0000000..f6439b0 --- /dev/null +++ b/templates/vk_auth.html @@ -0,0 +1,11 @@ +{% extends 'layouts/base.html' %} + +{% block onload %}onLoad();{% endblock %} + +{% block javascripts %} + +{% endblock %} diff --git a/web/urls.py b/web/urls.py index 95cd312..4c75ae1 100644 --- a/web/urls.py +++ b/web/urls.py @@ -10,5 +10,6 @@ urlpatterns = [ path(*ProfileView.as_path()), path(*ProfilePhoto.as_path()), path(*LogoutView.as_path()), - path(*PingView.as_path()) + path(*PingView.as_path()), + path(*VKAuthView.as_path()) ] diff --git a/web/views/__init__.py b/web/views/__init__.py index 46f9f63..588a3df 100644 --- a/web/views/__init__.py +++ b/web/views/__init__.py @@ -4,4 +4,5 @@ from .select_project import SelectProject from .profile import ProfileView from .profile_photo import ProfilePhoto from .logout import LogoutView -from .ping import PingView \ No newline at end of file +from .ping import PingView +from .vk_auth import VKAuthView \ No newline at end of file diff --git a/web/views/vk_auth.py b/web/views/vk_auth.py new file mode 100644 index 0000000..4827518 --- /dev/null +++ b/web/views/vk_auth.py @@ -0,0 +1,31 @@ +from requests import get + +from BaseLib.BaseView import BaseView +from Platform import settings + + +class VKAuthView(BaseView): + required_login = True + endpoint = "vk_auth" + view_file = "vk_auth.html" + fields_except = ('user_id',) + + def get(self): + access_token = self.request.GET.get('access_token') + if access_token is None: + if self.request.GET.get("error") == "access_denied": + return "/profile" + return + token = settings.VK_SERVICE_TOKEN + resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() + print(resp) + if 'response' in resp and 'success' in resp['response'] and resp['response']['success'] == 1: + user_id = resp['response']['user_id'] + resp = get(f'https://api.vk.com/method/users.get?access_token={token}&user_ids={user_id}&v=5.131', + headers={"accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7"}) + if resp.status_code != 200: + return "/profile" + data = resp.json()['response'][0] + self.request.user.vk_id = data['id'] + self.request.user.save() + return "/profile"