You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
3.8 KiB

from django.http import HttpResponse, HttpResponseNotFound, JsonResponse, HttpRequest
from django.shortcuts import render, redirect
from django.core.paginator import Paginator
from django.db.models import Q
from django.urls import reverse
from requests_oauthlib import OAuth2Session
from apps.tuxbot_gnous_eu.models import Users
from django.conf import settings
TEMPLATE_PATH = 'users/'
class OAuth2:
OAUTH2_CLIENT_ID = settings.CLIENT_ID
OAUTH2_CLIENT_SECRET = settings.CLIENT_SECRET
OAUTH2_REDIRECT_URI = settings.REDIRECT_URI
API_BASE_URL = 'https://discordapp.com/api'
AUTHORIZATION_BASE_URL = API_BASE_URL + '/oauth2/authorize'
TOKEN_URL = API_BASE_URL + '/oauth2/token'
@staticmethod
def token_updater(request: HttpRequest, token):
request.session['oauth2_token'] = token
def make_session(self, token=None, state=None, scope=None):
return OAuth2Session(
client_id=self.OAUTH2_CLIENT_ID,
token=token,
state=state,
scope=scope,
redirect_uri=self.OAUTH2_REDIRECT_URI,
auto_refresh_kwargs={
'client_id': self.OAUTH2_CLIENT_ID,
'client_secret': self.OAUTH2_CLIENT_SECRET,
},
auto_refresh_url=self.TOKEN_URL,
token_updater=self.token_updater
)
def login(self, request: HttpRequest, *args, **kwargs):
discord = self.make_session(scope=['identify', 'guilds'])
authorization_url, state = discord.authorization_url(self.AUTHORIZATION_BASE_URL)
request.session['oauth2_state'] = state
return redirect(authorization_url)
def callback(self, request: HttpRequest, *args, **kwargs):
if request.GET.get('error'):
return request.GET.get('error')
discord = self.make_session()
token = discord.fetch_token(
self.TOKEN_URL,
client_secret=self.OAUTH2_CLIENT_SECRET,
authorization_response=reverse('tuxbot_gnous_eu:login_callback'),
code=request.GET.get('code')
)
request.session['oauth2_token'] = token
user = discord.get(self.API_BASE_URL + '/users/@me').json()
guilds = discord.get(self.API_BASE_URL + '/users/@me/guilds').json()
request.session['user'] = user
request.session['guilds'] = guilds
return redirect(reverse('tuxbot_gnous_eu:users_edit', kwargs={'user_id': user.get('id')}))
class UsersView:
@staticmethod
def index(request: HttpRequest, *args, **kwargs) -> HttpResponse:
search: str = request.GET.get('search')
if search:
data_users = Users.objects.all() \
.filter(
Q(user_name__icontains=search)
|
Q(os__icontains=search)
|
Q(checked_in__icontains=search)
|
Q(user_id__exact=int(search) if search.isdigit() else 0)
)
else:
data_users = Users.objects.all()
paginator = Paginator(data_users, 30)
page = request.GET.get('page')
return render(
request,
TEMPLATE_PATH + 'index.html',
{
'users': paginator.get_page(page),
'search': search
}
)
@staticmethod
def show(request: HttpRequest, *args, **kwargs) -> HttpResponse:
try:
data_user = Users.objects.get(user_id=kwargs.get('user_id'))
except Users.DoesNotExist:
return HttpResponseNotFound("Question does not exist")
return render(
request,
TEMPLATE_PATH + 'show.html',
{
'user': data_user
}
)
@staticmethod
def edit(request: HttpRequest, *args, **kwargs):
oauth2 = OAuth2(request)
return oauth2.login()