1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-27 10:13:02 +00:00

Get memberships from Note Kfet

This commit is contained in:
Yohann D'ANELLO 2021-11-04 14:23:03 +01:00
parent d036ea6f27
commit d3c2441111
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 71 additions and 5 deletions

View File

@ -0,0 +1,28 @@
# Generated by Django 2.2.24 on 2021-11-04 13:20
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0043_accesstoken'),
]
operations = [
migrations.CreateModel(
name='Membership',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_start', models.DateField(auto_now_add=True, verbose_name='start date')),
('date_end', models.DateField(auto_now_add=True, verbose_name='start date')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='user')),
],
options={
'verbose_name': 'membership',
'verbose_name_plural': 'memberships',
},
),
]

View File

@ -48,8 +48,13 @@ class User(AbstractUser):
@property
def is_member(self):
# FIXME Use NK20
return True
"""
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()
def update_data(self, data: dict):
"""
@ -68,6 +73,38 @@ class User(AbstractUser):
self.address = data['profile']['address']
self.comment = data['profile']['section']
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'],
)
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'),
)
class Meta:
verbose_name = _('membership')
verbose_name_plural = _('memberships')
class AccessToken(models.Model):
owner = models.ForeignKey(
@ -142,9 +179,6 @@ class AccessToken(models.Model):
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']
@ -162,6 +196,10 @@ class AccessToken(models.Model):
user.update_data(data)
user.save()
# Store token owner
self.owner = user
self.save()
return user
class Meta: