2019-08-02 12:57:53 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2021-11-14 13:26:41 +00:00
|
|
|
# Copyright (C) 2017-2021 by BDE ENS Paris-Saclay
|
2019-08-02 12:57:53 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2021-11-04 10:29:03 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2021-11-04 13:13:17 +00:00
|
|
|
import requests
|
2021-11-04 10:29:03 +00:00
|
|
|
from authlib.integrations.django_client import OAuth
|
|
|
|
from django.conf import settings
|
2019-08-08 10:16:40 +00:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
2019-08-02 12:57:53 +00:00
|
|
|
from django.db import models
|
2021-11-04 13:13:17 +00:00
|
|
|
from django.db.models import Q
|
2019-08-10 09:21:50 +00:00
|
|
|
from django.utils import timezone
|
2019-08-02 12:57:53 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2017-06-12 01:25:22 +00:00
|
|
|
|
2019-08-08 10:16:40 +00:00
|
|
|
class User(AbstractUser):
|
2021-11-14 13:26:41 +00:00
|
|
|
phone_number = models.CharField(
|
2019-08-08 10:16:40 +00:00
|
|
|
verbose_name=_('phone number'),
|
|
|
|
max_length=15,
|
|
|
|
blank=True,
|
2019-08-02 19:35:30 +00:00
|
|
|
)
|
2019-08-08 10:16:40 +00:00
|
|
|
address = models.CharField(
|
|
|
|
verbose_name=_('address'),
|
|
|
|
max_length=255,
|
|
|
|
blank=True,
|
2019-08-02 16:37:54 +00:00
|
|
|
)
|
2019-08-08 10:16:40 +00:00
|
|
|
comment = models.CharField(
|
|
|
|
verbose_name=_('comment'),
|
|
|
|
help_text=_('Promotion...'),
|
|
|
|
max_length=255,
|
|
|
|
blank=True,
|
2019-08-02 16:37:54 +00:00
|
|
|
)
|
2019-08-10 09:21:50 +00:00
|
|
|
date_joined = models.DateTimeField(
|
2021-11-14 13:26:41 +00:00
|
|
|
verbose_name=_('date joined'),
|
2019-08-10 09:21:50 +00:00
|
|
|
default=timezone.now,
|
|
|
|
null=True,
|
|
|
|
)
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2019-08-08 10:16:40 +00:00
|
|
|
REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
|
2017-06-11 23:34:13 +00:00
|
|
|
|
2017-07-04 22:47:05 +00:00
|
|
|
@property
|
2019-08-10 14:22:04 +00:00
|
|
|
def is_member(self):
|
2021-11-04 13:23:03 +00:00
|
|
|
"""
|
|
|
|
Return True if user is member of the club.
|
|
|
|
"""
|
|
|
|
return Membership.objects.filter(
|
|
|
|
user=self,
|
|
|
|
date_start__lte=timezone.now(),
|
|
|
|
date_end__gte=timezone.now()).exists()
|
2021-11-04 10:29:03 +00:00
|
|
|
|
2021-11-04 13:13:17 +00:00
|
|
|
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']
|
2021-11-14 13:26:41 +00:00
|
|
|
self.phone_number = data['profile']['phone_number']
|
2021-11-04 13:13:17 +00:00
|
|
|
self.address = data['profile']['address']
|
|
|
|
self.comment = data['profile']['section']
|
|
|
|
|
2021-11-04 13:23:03 +00:00
|
|
|
for membership_dict in data['memberships']:
|
|
|
|
if membership_dict['club'] != 22: # Med
|
|
|
|
continue
|
|
|
|
# Add membership if not exists
|
|
|
|
Membership.objects.get_or_create(
|
|
|
|
user=self,
|
|
|
|
date_start=membership_dict['date_start'],
|
|
|
|
date_end=membership_dict['date_end'],
|
|
|
|
)
|
|
|
|
|
2021-11-04 14:04:57 +00:00
|
|
|
# Only members or old members are allow to connect to the website
|
|
|
|
self.is_active = Membership.objects.filter(user=self).exists()
|
|
|
|
|
2021-11-04 13:23:03 +00:00
|
|
|
|
|
|
|
class Membership(models.Model):
|
|
|
|
user = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('user'),
|
|
|
|
)
|
|
|
|
|
|
|
|
date_start = models.DateField(
|
|
|
|
auto_now_add=True,
|
|
|
|
verbose_name=_('start date'),
|
|
|
|
)
|
|
|
|
|
|
|
|
date_end = models.DateField(
|
|
|
|
auto_now_add=True,
|
|
|
|
verbose_name=_('start date'),
|
|
|
|
)
|
|
|
|
|
2021-11-14 13:28:13 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f'{self.user}: {self.date_start} to {self.date_end}'
|
|
|
|
|
2021-11-04 13:23:03 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('membership')
|
|
|
|
verbose_name_plural = _('memberships')
|
|
|
|
|
2021-11-04 10:29:03 +00:00
|
|
|
|
|
|
|
class AccessToken(models.Model):
|
|
|
|
owner = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
verbose_name=_('owner'),
|
|
|
|
)
|
|
|
|
|
|
|
|
access_token = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
verbose_name=_('access token'),
|
|
|
|
)
|
|
|
|
|
|
|
|
expires_in = models.PositiveSmallIntegerField(
|
|
|
|
verbose_name=_('expires in'),
|
|
|
|
)
|
|
|
|
|
|
|
|
scopes = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
verbose_name=_('scopes'),
|
|
|
|
)
|
|
|
|
|
|
|
|
refresh_token = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
verbose_name=_('refresh token'),
|
|
|
|
)
|
|
|
|
|
|
|
|
expires_at = models.DateTimeField(
|
|
|
|
verbose_name=_('expires at'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
"""
|
|
|
|
Refresh the access token.
|
|
|
|
"""
|
|
|
|
oauth = OAuth()
|
|
|
|
oauth.register('notekfet')
|
|
|
|
# Get the OAuth client
|
|
|
|
oauth_client = oauth.notekfet._get_oauth_client()
|
|
|
|
# Actually refresh the token
|
|
|
|
token = oauth_client.refresh_token(oauth.notekfet.access_token_url,
|
|
|
|
refresh_token=self.refresh_token)
|
|
|
|
self.access_token = token['access_token']
|
|
|
|
self.expires_in = token['expires_in']
|
|
|
|
self.scopes = token['scope']
|
|
|
|
self.refresh_token = token['refresh_token']
|
|
|
|
self.expires_at = timezone.utc.fromutc(
|
|
|
|
datetime.fromtimestamp(token['expires_at'])
|
|
|
|
)
|
|
|
|
|
|
|
|
self.save()
|
|
|
|
|
2021-11-04 13:13:17 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
2021-11-04 13:23:03 +00:00
|
|
|
# Store token owner
|
|
|
|
self.owner = user
|
|
|
|
self.save()
|
|
|
|
|
2021-11-04 13:13:17 +00:00
|
|
|
return user
|
|
|
|
|
2021-11-04 13:25:35 +00:00
|
|
|
@classmethod
|
|
|
|
def get_token(cls, request):
|
|
|
|
return AccessToken.objects.get(pk=request.session['access_token_id'])
|
|
|
|
|
2021-11-14 13:28:13 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.access_token
|
|
|
|
|
2021-11-04 10:29:03 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('access token')
|
|
|
|
verbose_name_plural = _('access tokens')
|