Get or create a user and query Note Kfet to get data
This commit is contained in:
parent
e2aa645bbf
commit
d036ea6f27
|
@ -56,9 +56,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
<h3>
|
<h3>
|
||||||
{% trans 'My profile' %}
|
{% trans 'My profile' %}
|
||||||
<small><a class="changelink" href="{% url 'users:edit-info' %}">
|
|
||||||
{% trans 'Edit' %}
|
|
||||||
</a></small>
|
|
||||||
</h3>
|
</h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><strong>{% trans 'username' %}</strong> : {{ user.username }}</li>
|
<li><strong>{% trans 'username' %}</strong> : {{ user.username }}</li>
|
||||||
|
|
|
@ -4,10 +4,12 @@
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
import requests
|
||||||
from authlib.integrations.django_client import OAuth
|
from authlib.integrations.django_client import OAuth
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from med.settings import MAX_EMPRUNT
|
from med.settings import MAX_EMPRUNT
|
||||||
|
@ -49,6 +51,23 @@ class User(AbstractUser):
|
||||||
# FIXME Use NK20
|
# FIXME Use NK20
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def update_data(self, data: dict):
|
||||||
|
"""
|
||||||
|
Update user data from given dictionary.
|
||||||
|
Useful when we want to update user data from Note Kfet.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : dict
|
||||||
|
Dictionary with user data to update.
|
||||||
|
"""
|
||||||
|
self.email = data['email']
|
||||||
|
self.first_name = data['first_name']
|
||||||
|
self.last_name = data['last_name']
|
||||||
|
self.telephone = data['profile']['phone_number']
|
||||||
|
self.address = data['profile']['address']
|
||||||
|
self.comment = data['profile']['section']
|
||||||
|
|
||||||
|
|
||||||
class AccessToken(models.Model):
|
class AccessToken(models.Model):
|
||||||
owner = models.ForeignKey(
|
owner = models.ForeignKey(
|
||||||
|
@ -103,6 +122,48 @@ class AccessToken(models.Model):
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
def refresh_if_expired(self):
|
||||||
|
"""
|
||||||
|
Refresh the current token if it is invalid.
|
||||||
|
"""
|
||||||
|
if self.expires_at < timezone.now():
|
||||||
|
self.refresh()
|
||||||
|
|
||||||
|
def auth_header(self):
|
||||||
|
"""
|
||||||
|
Return HTTP header that contains the bearer access token.
|
||||||
|
Refresh the token if needed.
|
||||||
|
"""
|
||||||
|
self.refresh_if_expired()
|
||||||
|
return {'Authorization': f'Bearer {self.access_token}'}
|
||||||
|
|
||||||
|
def fetch_user(self, create_if_not_exist: bool = False):
|
||||||
|
"""
|
||||||
|
Extract information about the Note Kfet API by using the current
|
||||||
|
access token.
|
||||||
|
"""
|
||||||
|
if self.owner:
|
||||||
|
return self.owner
|
||||||
|
|
||||||
|
data = requests.get('https://note-dev.crans.org/api/me/',
|
||||||
|
headers=self.auth_header()).json()
|
||||||
|
username = data['username']
|
||||||
|
email = data['email']
|
||||||
|
qs = User.objects.filter(Q(username=username) | Q(email=email))
|
||||||
|
if not qs.exists():
|
||||||
|
if create_if_not_exist:
|
||||||
|
user = User.objects.create(username=username, email=email)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
user = qs.get()
|
||||||
|
|
||||||
|
# Update user data from Note Kfet
|
||||||
|
user.update_data(data)
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _('access token')
|
verbose_name = _('access token')
|
||||||
verbose_name_plural = _('access tokens')
|
verbose_name_plural = _('access tokens')
|
||||||
|
|
|
@ -20,18 +20,10 @@ class TemplateTests(TestCase):
|
||||||
)
|
)
|
||||||
self.client.force_login(self.user)
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
def test_users_edit_info(self):
|
|
||||||
response = self.client.get(reverse('users:edit-info'))
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_users_user_changelist(self):
|
def test_users_user_changelist(self):
|
||||||
response = self.client.get(reverse('admin:users_user_changelist'))
|
response = self.client.get(reverse('admin:users_user_changelist'))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_users_user_creation_form(self):
|
|
||||||
response = self.client.get(reverse('admin:users_user_add'))
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_users_user_add_init_mail(self):
|
def test_users_user_add_init_mail(self):
|
||||||
"""
|
"""
|
||||||
Test that an initialization mail is send when a new user is added
|
Test that an initialization mail is send when a new user is added
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from authlib.integrations.django_client import OAuth
|
from authlib.integrations.django_client import OAuth
|
||||||
|
from django.contrib.auth import login
|
||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -35,8 +36,9 @@ class AuthorizeView(RedirectView):
|
||||||
expires_at=timezone.utc.fromutc(
|
expires_at=timezone.utc.fromutc(
|
||||||
datetime.fromtimestamp(token['expires_at'])),
|
datetime.fromtimestamp(token['expires_at'])),
|
||||||
)
|
)
|
||||||
# TODO Log in or create user
|
user = token_obj.fetch_user(True)
|
||||||
return '/'
|
login(self.request, user)
|
||||||
|
return reverse('index')
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
class UserViewSet(viewsets.ModelViewSet):
|
||||||
|
|
Loading…
Reference in New Issue