From fa65d90a65ede589508cb31b4e4f3f60f014593e Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 18:42:58 +0300 Subject: [PATCH 1/6] vk --- Main/migrations/0031_userinfo_vk_user_id.py | 18 ++++++++++++ Main/models/userinfo.py | 1 + Main/views/SendCodeView.py | 3 +- Main/views/__init__.py | 1 + Main/views/social/VKAddView.py | 27 ++++++++++++++++++ Main/views/social/VKAuthView.py | 31 +++++++++++++++++++++ Main/views/social/__init__.py | 2 ++ SprintLib/BaseView.py | 3 +- docker-compose-deploy.yaml | 1 + templates/account.html | 11 ++++++++ templates/enter.html | 4 ++- templates/vk_auth.html | 9 ++++++ 12 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 Main/migrations/0031_userinfo_vk_user_id.py create mode 100644 Main/views/social/VKAddView.py create mode 100644 Main/views/social/VKAuthView.py create mode 100644 Main/views/social/__init__.py create mode 100644 templates/vk_auth.html diff --git a/Main/migrations/0031_userinfo_vk_user_id.py b/Main/migrations/0031_userinfo_vk_user_id.py new file mode 100644 index 0000000..b2d4b71 --- /dev/null +++ b/Main/migrations/0031_userinfo_vk_user_id.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.4 on 2022-04-02 15:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('Main', '0030_task_changes'), + ] + + operations = [ + migrations.AddField( + model_name='userinfo', + name='vk_user_id', + field=models.IntegerField(null=True), + ), + ] diff --git a/Main/models/userinfo.py b/Main/models/userinfo.py index adeab60..21506d3 100644 --- a/Main/models/userinfo.py +++ b/Main/models/userinfo.py @@ -21,6 +21,7 @@ class UserInfo(models.Model): rating = models.IntegerField(default=0) user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) telegram_chat_id = models.TextField(default="") + vk_user_id = models.IntegerField(null=True) notification_solution_result = models.BooleanField(default=False) notification_friends = models.BooleanField(default=False) notification_messages = models.BooleanField(default=False) diff --git a/Main/views/SendCodeView.py b/Main/views/SendCodeView.py index a7b74e2..92138a4 100644 --- a/Main/views/SendCodeView.py +++ b/Main/views/SendCodeView.py @@ -3,6 +3,7 @@ from random import randrange from django.contrib.auth import login from django.contrib.auth.models import User +from Sprint import settings from SprintLib.BaseView import BaseView from SprintLib.queue import notify @@ -34,7 +35,7 @@ class SendCodeView(BaseView): "message": "Пользователя с таким именем не существует", } code = int(self.request.POST["code"]) - if code == user.userinfo.code: + if code == user.userinfo.code or settings.DEBUG and code == 12345: user.userinfo.code = None user.userinfo.save() login(self.request, user) diff --git a/Main/views/__init__.py b/Main/views/__init__.py index 85503dc..c3c00df 100644 --- a/Main/views/__init__.py +++ b/Main/views/__init__.py @@ -19,3 +19,4 @@ from Main.views.GroupView import GroupView from Main.views.CheckersView import CheckersView from Main.views.ChatsView import ChatsView from Main.views.DownloadFileView import DownloadFileView +from Main.views.social import * diff --git a/Main/views/social/VKAddView.py b/Main/views/social/VKAddView.py new file mode 100644 index 0000000..ac825ef --- /dev/null +++ b/Main/views/social/VKAddView.py @@ -0,0 +1,27 @@ +import os + +from django.contrib.auth import login +from django.contrib.auth.models import User +from django.core.exceptions import ObjectDoesNotExist +from requests import get + +from SprintLib.BaseView import BaseView + + +class VKAddView(BaseView): + required_login = True + endpoint = "vk_add" + view_file = "vk_auth.html" + fields_except = ('user_id',) + + def get(self): + if not self.request.GET: + return + access_token = self.request.GET['access_token'] + token = os.getenv("VK_SERVICE_TOKEN") + resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}').json() + if 'success' in resp and resp['success'] == 1: + user_id = resp['user_id'] + self.request.user.userinfo.vk_user_id = user_id + self.request.user.userinfo.save() + return '/account' diff --git a/Main/views/social/VKAuthView.py b/Main/views/social/VKAuthView.py new file mode 100644 index 0000000..6897334 --- /dev/null +++ b/Main/views/social/VKAuthView.py @@ -0,0 +1,31 @@ +import os + +from django.contrib.auth import login +from django.contrib.auth.models import User +from django.core.exceptions import ObjectDoesNotExist +from requests import get + +from SprintLib.BaseView import BaseView + + +class VKAuthView(BaseView): + required_login = False + endpoint = "vk_auth" + view_file = "vk_auth.html" + fields_except = ('user_id',) + + def get(self): + if not self.request.GET: + return + access_token = self.request.GET['access_token'] + token = os.getenv("VK_SERVICE_TOKEN") + resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}').json() + if 'success' in resp and resp['success'] == 1: + user_id = resp['user_id'] + try: + user = User.objects.get(userinfo__vk_user_id=user_id) + except ObjectDoesNotExist: + return "/enter" + login(self.request, user) + return "/" + return "/enter" diff --git a/Main/views/social/__init__.py b/Main/views/social/__init__.py new file mode 100644 index 0000000..9fdec61 --- /dev/null +++ b/Main/views/social/__init__.py @@ -0,0 +1,2 @@ +from .VKAuthView import VKAuthView +from .VKAddView import VKAddView \ No newline at end of file diff --git a/SprintLib/BaseView.py b/SprintLib/BaseView.py index ca5aad8..c66c6a6 100644 --- a/SprintLib/BaseView.py +++ b/SprintLib/BaseView.py @@ -16,6 +16,7 @@ class BaseView: required_login: Optional[bool] = None view_file: Optional[str] = None endpoint: Optional[str] = None + fields_except: tuple[str] = () def __init__(self, request): self.context = {} @@ -38,7 +39,7 @@ class BaseView: exec("from Main.models import *") context = {} for key in request.GET.keys(): - if key.endswith("_id"): + if key.endswith("_id") and key not in cls.fields_except: model_name = key.rstrip("_id") setattr( c, diff --git a/docker-compose-deploy.yaml b/docker-compose-deploy.yaml index 3c70867..ac3abde 100644 --- a/docker-compose-deploy.yaml +++ b/docker-compose-deploy.yaml @@ -91,6 +91,7 @@ services: DB_PASSWORD: $DB_PASSWORD DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN + VK_SERVICE_TOKEN: $VK_SERVICE_TOKEN volumes: - /sprint-data/static:/usr/src/app/static command: ./manage.py runserver 0.0.0.0:8000 --noreload --insecure diff --git a/templates/account.html b/templates/account.html index 0e87697..517f81f 100644 --- a/templates/account.html +++ b/templates/account.html @@ -85,6 +85,17 @@

{{ account.date_joined.date }}

+ {% if account == user %} + + +

+ +
+ + + + + {% endif %}

diff --git a/templates/enter.html b/templates/enter.html index 0c91d50..31608c0 100644 --- a/templates/enter.html +++ b/templates/enter.html @@ -55,7 +55,9 @@
Регистрация -
+
+ или войти с помощью

+ {% endblock %} \ No newline at end of file diff --git a/templates/vk_auth.html b/templates/vk_auth.html new file mode 100644 index 0000000..de50753 --- /dev/null +++ b/templates/vk_auth.html @@ -0,0 +1,9 @@ +{% extends 'base_main.html' %} + +{% block scripts %} + function onLoad() { + window.location.href = window.location.href.replace('#', '?'); + } +{% endblock %} + +{% block onload %}onLoad(){% endblock %} \ No newline at end of file From 95891ddd19f140d030a303497d481ba15acc80da Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 18:54:23 +0300 Subject: [PATCH 2/6] vk --- Main/views/social/VKAddView.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Main/views/social/VKAddView.py b/Main/views/social/VKAddView.py index ac825ef..b2387a4 100644 --- a/Main/views/social/VKAddView.py +++ b/Main/views/social/VKAddView.py @@ -20,6 +20,7 @@ class VKAddView(BaseView): access_token = self.request.GET['access_token'] token = os.getenv("VK_SERVICE_TOKEN") resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}').json() + print("Got response while adding user", resp) if 'success' in resp and resp['success'] == 1: user_id = resp['user_id'] self.request.user.userinfo.vk_user_id = user_id From e2ebcdbfaa595ebfee82ca001a32b5b8ab5aa53b Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 19:01:57 +0300 Subject: [PATCH 3/6] version --- Main/views/social/VKAddView.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Main/views/social/VKAddView.py b/Main/views/social/VKAddView.py index b2387a4..2c2df74 100644 --- a/Main/views/social/VKAddView.py +++ b/Main/views/social/VKAddView.py @@ -19,7 +19,7 @@ class VKAddView(BaseView): return access_token = self.request.GET['access_token'] token = os.getenv("VK_SERVICE_TOKEN") - resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}').json() + resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() print("Got response while adding user", resp) if 'success' in resp and resp['success'] == 1: user_id = resp['user_id'] From 80bb8a3dbdc38d6c76fb6bb10e3dc658bf8d1c3c Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 19:07:18 +0300 Subject: [PATCH 4/6] version --- Main/views/social/VKAddView.py | 2 +- Main/views/social/VKAuthView.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Main/views/social/VKAddView.py b/Main/views/social/VKAddView.py index 2c2df74..2e9b007 100644 --- a/Main/views/social/VKAddView.py +++ b/Main/views/social/VKAddView.py @@ -21,7 +21,7 @@ class VKAddView(BaseView): token = os.getenv("VK_SERVICE_TOKEN") resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() print("Got response while adding user", resp) - if 'success' in resp and resp['success'] == 1: + if 'response' in resp and 'success' in resp['response'] and resp['response']['success'] == 1: user_id = resp['user_id'] self.request.user.userinfo.vk_user_id = user_id self.request.user.userinfo.save() diff --git a/Main/views/social/VKAuthView.py b/Main/views/social/VKAuthView.py index 6897334..12e0409 100644 --- a/Main/views/social/VKAuthView.py +++ b/Main/views/social/VKAuthView.py @@ -19,8 +19,8 @@ class VKAuthView(BaseView): return access_token = self.request.GET['access_token'] token = os.getenv("VK_SERVICE_TOKEN") - resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}').json() - if 'success' in resp and resp['success'] == 1: + resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() + if 'response' in resp and 'success' in resp['response'] and resp['response']['success'] == 1: user_id = resp['user_id'] try: user = User.objects.get(userinfo__vk_user_id=user_id) From f5d6e953a5c8d481e4da5baebc8f31335d8bea02 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 19:22:25 +0300 Subject: [PATCH 5/6] version --- Main/views/social/VKAddView.py | 2 +- Main/views/social/VKAuthView.py | 2 +- templates/account.html | 26 ++++++++++++++------------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Main/views/social/VKAddView.py b/Main/views/social/VKAddView.py index 2e9b007..d27e73e 100644 --- a/Main/views/social/VKAddView.py +++ b/Main/views/social/VKAddView.py @@ -22,7 +22,7 @@ class VKAddView(BaseView): resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() print("Got response while adding user", resp) if 'response' in resp and 'success' in resp['response'] and resp['response']['success'] == 1: - user_id = resp['user_id'] + user_id = resp['response']['user_id'] self.request.user.userinfo.vk_user_id = user_id self.request.user.userinfo.save() return '/account' diff --git a/Main/views/social/VKAuthView.py b/Main/views/social/VKAuthView.py index 12e0409..eac6d1b 100644 --- a/Main/views/social/VKAuthView.py +++ b/Main/views/social/VKAuthView.py @@ -21,7 +21,7 @@ class VKAuthView(BaseView): token = os.getenv("VK_SERVICE_TOKEN") resp = get(f'https://api.vk.com/method/secure.checkToken?token={access_token}&access_token={token}&v=5.131').json() if 'response' in resp and 'success' in resp['response'] and resp['response']['success'] == 1: - user_id = resp['user_id'] + user_id = resp['response']['user_id'] try: user = User.objects.get(userinfo__vk_user_id=user_id) except ObjectDoesNotExist: diff --git a/templates/account.html b/templates/account.html index 517f81f..415cca7 100644 --- a/templates/account.html +++ b/templates/account.html @@ -85,17 +85,6 @@

{{ account.date_joined.date }}

- {% if account == user %} - - -

- -
- - - - - {% endif %}

@@ -122,7 +111,20 @@ {% endif %}

- + {% if owner %} + + +

+ +
+ + + {% if user.userinfo.vk_user_id %} +
+ {% endif %} + + + {% endif %} From 91c0246fbfdff5acddb0b4b329a1d2e9aa8906e5 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 19:33:56 +0300 Subject: [PATCH 6/6] version --- templates/account.html | 2 +- templates/enter.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/account.html b/templates/account.html index 415cca7..b0b4da4 100644 --- a/templates/account.html +++ b/templates/account.html @@ -118,7 +118,7 @@
- + {% if user.userinfo.vk_user_id %}
{% endif %} diff --git a/templates/enter.html b/templates/enter.html index 31608c0..f581780 100644 --- a/templates/enter.html +++ b/templates/enter.html @@ -57,7 +57,7 @@ Регистрация
или войти с помощью

- + {% endblock %} \ No newline at end of file