vk
This commit is contained in:
parent
5f80636349
commit
fa65d90a65
18
Main/migrations/0031_userinfo_vk_user_id.py
Normal file
18
Main/migrations/0031_userinfo_vk_user_id.py
Normal file
@ -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),
|
||||||
|
),
|
||||||
|
]
|
@ -21,6 +21,7 @@ class UserInfo(models.Model):
|
|||||||
rating = models.IntegerField(default=0)
|
rating = models.IntegerField(default=0)
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
|
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
|
||||||
telegram_chat_id = models.TextField(default="")
|
telegram_chat_id = models.TextField(default="")
|
||||||
|
vk_user_id = models.IntegerField(null=True)
|
||||||
notification_solution_result = models.BooleanField(default=False)
|
notification_solution_result = models.BooleanField(default=False)
|
||||||
notification_friends = models.BooleanField(default=False)
|
notification_friends = models.BooleanField(default=False)
|
||||||
notification_messages = models.BooleanField(default=False)
|
notification_messages = models.BooleanField(default=False)
|
||||||
|
@ -3,6 +3,7 @@ from random import randrange
|
|||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
from Sprint import settings
|
||||||
from SprintLib.BaseView import BaseView
|
from SprintLib.BaseView import BaseView
|
||||||
from SprintLib.queue import notify
|
from SprintLib.queue import notify
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ class SendCodeView(BaseView):
|
|||||||
"message": "Пользователя с таким именем не существует",
|
"message": "Пользователя с таким именем не существует",
|
||||||
}
|
}
|
||||||
code = int(self.request.POST["code"])
|
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.code = None
|
||||||
user.userinfo.save()
|
user.userinfo.save()
|
||||||
login(self.request, user)
|
login(self.request, user)
|
||||||
|
@ -19,3 +19,4 @@ from Main.views.GroupView import GroupView
|
|||||||
from Main.views.CheckersView import CheckersView
|
from Main.views.CheckersView import CheckersView
|
||||||
from Main.views.ChatsView import ChatsView
|
from Main.views.ChatsView import ChatsView
|
||||||
from Main.views.DownloadFileView import DownloadFileView
|
from Main.views.DownloadFileView import DownloadFileView
|
||||||
|
from Main.views.social import *
|
||||||
|
27
Main/views/social/VKAddView.py
Normal file
27
Main/views/social/VKAddView.py
Normal file
@ -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'
|
31
Main/views/social/VKAuthView.py
Normal file
31
Main/views/social/VKAuthView.py
Normal file
@ -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"
|
2
Main/views/social/__init__.py
Normal file
2
Main/views/social/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from .VKAuthView import VKAuthView
|
||||||
|
from .VKAddView import VKAddView
|
@ -16,6 +16,7 @@ class BaseView:
|
|||||||
required_login: Optional[bool] = None
|
required_login: Optional[bool] = None
|
||||||
view_file: Optional[str] = None
|
view_file: Optional[str] = None
|
||||||
endpoint: Optional[str] = None
|
endpoint: Optional[str] = None
|
||||||
|
fields_except: tuple[str] = ()
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
self.context = {}
|
self.context = {}
|
||||||
@ -38,7 +39,7 @@ class BaseView:
|
|||||||
exec("from Main.models import *")
|
exec("from Main.models import *")
|
||||||
context = {}
|
context = {}
|
||||||
for key in request.GET.keys():
|
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")
|
model_name = key.rstrip("_id")
|
||||||
setattr(
|
setattr(
|
||||||
c,
|
c,
|
||||||
|
@ -91,6 +91,7 @@ services:
|
|||||||
DB_PASSWORD: $DB_PASSWORD
|
DB_PASSWORD: $DB_PASSWORD
|
||||||
DEBUG: $DEBUG
|
DEBUG: $DEBUG
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN
|
||||||
|
VK_SERVICE_TOKEN: $VK_SERVICE_TOKEN
|
||||||
volumes:
|
volumes:
|
||||||
- /sprint-data/static:/usr/src/app/static
|
- /sprint-data/static:/usr/src/app/static
|
||||||
command: ./manage.py runserver 0.0.0.0:8000 --noreload --insecure
|
command: ./manage.py runserver 0.0.0.0:8000 --noreload --insecure
|
||||||
|
@ -85,6 +85,17 @@
|
|||||||
<p style="padding-top: 8px; font-size: 24px;">{{ account.date_joined.date }}</p>
|
<p style="padding-top: 8px; font-size: 24px;">{{ account.date_joined.date }}</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
{% if account == user %}
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<h2><i class="fa fa-users"></i></h2>
|
||||||
|
</td>
|
||||||
|
<td><div style="width: 20px;"></div></td>
|
||||||
|
<td>
|
||||||
|
<a href="https://oauth.vk.com/authorize?client_id=8123759&redirect_uri=http://demo.dev.sprinthub.ru/vk_add&display=page&response_type=token&v=5.59"><img style="width: 40px; height: 40px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/VK_Compact_Logo_%282021-present%29.svg/1200px-VK_Compact_Logo_%282021-present%29.svg.png"></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endif %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<h2><i class="fa fa-desktop"></i></h2>
|
<h2><i class="fa fa-desktop"></i></h2>
|
||||||
|
@ -55,7 +55,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<a href="https://t.me/sprint_notifications_bot" target="_blank" class="sub btn btn-dark form">Регистрация</a>
|
<a href="https://t.me/sprint_notifications_bot" target="_blank" class="sub btn btn-dark form">Регистрация</a>
|
||||||
</div>
|
</div><br>
|
||||||
|
или войти с помощью<br><br>
|
||||||
|
<a href="https://oauth.vk.com/authorize?client_id=8123759&redirect_uri=http://demo.dev.sprinthub.ru/vk_auth&display=page&response_type=token&v=5.59"><img style="width: 40px; height: 40px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/VK_Compact_Logo_%282021-present%29.svg/1200px-VK_Compact_Logo_%282021-present%29.svg.png"></a>
|
||||||
</center>
|
</center>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
9
templates/vk_auth.html
Normal file
9
templates/vk_auth.html
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{% extends 'base_main.html' %}
|
||||||
|
|
||||||
|
{% block scripts %}
|
||||||
|
function onLoad() {
|
||||||
|
window.location.href = window.location.href.replace('#', '?');
|
||||||
|
}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block onload %}onLoad(){% endblock %}
|
Loading…
Reference in New Issue
Block a user