From fa65d90a65ede589508cb31b4e4f3f60f014593e Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 2 Apr 2022 18:42:58 +0300 Subject: [PATCH] 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