Verified Commit d3c24411 authored by ynerant's avatar ynerant
Browse files

Get memberships from Note Kfet

parent d036ea6f
# 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',
},
),
]
......@@ -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:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment