Browse Source

refactor(views): change urls and views pattern

master
Romain J 2 years ago
parent
commit
0a1b6da401
  1. 6
      .idea/gnousEU.iml
  2. 4
      apps/tuxbot_gnous_eu/models.py
  3. 6
      apps/tuxbot_gnous_eu/urls.py
  4. 85
      apps/tuxbot_gnous_eu/views.py
  5. 2
      tuxWeb/settings.py
  6. 10
      venv/bin/flask

6
.idea/gnousEU.iml

@ -4,11 +4,11 @@ @@ -4,11 +4,11 @@
<facet type="django" name="Django">
<configuration>
<option name="rootFolder" value="$MODULE_DIR$" />
<option name="settingsModule" value="gnousEU/settings.py" />
<option name="settingsModule" value="tuxWeb/settings.py" />
<option name="manageScript" value="$MODULE_DIR$/manage.py" />
<option name="environment" value="&lt;map/&gt;" />
<option name="environment" value="&lt;map&gt;&#10; &lt;entry&gt;&#10; &lt;string&gt;OAUTHLIB_INSECURE_TRANSPORT&lt;/string&gt;&#10; &lt;string&gt;1&lt;/string&gt;&#10; &lt;/entry&gt;&#10;&lt;/map&gt;" />
<option name="doNotUseTestRunner" value="false" />
<option name="trackFilePattern" value="migrations" />
<option name="trackFilePattern" value="apps/tuxbot_gnous_eu/migrations" />
</configuration>
</facet>
</component>

4
apps/tuxbot_gnous_eu/models.py

@ -19,10 +19,10 @@ class Users(models.Model): @@ -19,10 +19,10 @@ class Users(models.Model):
checked_in_id = models.BigIntegerField(null=True)
def image_tag(self):
return mark_safe('<img src="%s" width="50" height="50">' % self.user_avatar)
return mark_safe('<img src="%s?size=128" width="50" height="50">' % self.user_avatar)
def show_profile(self):
return mark_safe(
'<a href="%s" target="_blank">Voir le profil</a>'
% reverse('tuxbot_gnous_eu:user', args=[self.user_id])
% reverse('tuxbot_gnous_eu:users_show', args=[self.user_id])
)

6
apps/tuxbot_gnous_eu/urls.py

@ -1,9 +1,10 @@ @@ -1,9 +1,10 @@
from django.urls import path, include
from django.views.generic import TemplateView
from .views import UsersView
from .views import UsersView, OAuth2
app_name = 'tuxbot_gnous_eu'
oauth = OAuth2()
urlpatterns = [
path('', TemplateView.as_view(template_name="index.html"), name='index'),
@ -13,4 +14,7 @@ urlpatterns = [ @@ -13,4 +14,7 @@ urlpatterns = [
path('show', UsersView.show, name='users_show'),
path('edit', UsersView.edit, name='users_edit'),
])),
path('login', oauth.login, name='login'),
path('login/callback', oauth.callback, name='login_callback'),
]

85
apps/tuxbot_gnous_eu/views.py

@ -1,18 +1,75 @@ @@ -1,18 +1,75 @@
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse, HttpResponseNotFound
from django.shortcuts import render
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: WSGIRequest, *args, **kwargs) -> HttpResponse:
def index(request: HttpRequest, *args, **kwargs) -> HttpResponse:
search: str = request.GET.get('search')
if search:
@ -42,7 +99,7 @@ class UsersView: @@ -42,7 +99,7 @@ class UsersView:
)
@staticmethod
def show(request: WSGIRequest, *args, **kwargs) -> HttpResponse:
def show(request: HttpRequest, *args, **kwargs) -> HttpResponse:
try:
data_user = Users.objects.get(user_id=kwargs.get('user_id'))
except Users.DoesNotExist:
@ -57,16 +114,6 @@ class UsersView: @@ -57,16 +114,6 @@ class UsersView:
)
@staticmethod
def edit(request: WSGIRequest, *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 + 'edit.html',
{
'user': data_user
}
)
def edit(request: HttpRequest, *args, **kwargs):
oauth2 = OAuth2(request)
return oauth2.login()

2
tuxWeb/settings.py

@ -25,7 +25,7 @@ SECRET_KEY = '*0q64%2cb!f@w(o@b74yyi2#x1d2r#%uul+hc1=gne^8u+*3)k' @@ -25,7 +25,7 @@ SECRET_KEY = '*0q64%2cb!f@w(o@b74yyi2#x1d2r#%uul+hc1=gne^8u+*3)k'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['192.168.1.39', '127.0.0.1', 'localhost', '9d80b184.ngrok.io']
ALLOWED_HOSTS = ['192.168.1.39', '127.0.0.1', 'localhost', '7156cd1b.ngrok.io']
# Application definition

10
venv/bin/flask vendored

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
#!/home/romain/jetbrains/IDEs/PycharmProjects/gnousEU/web/gnousEU/venv/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from flask.cli import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
Loading…
Cancel
Save