mirror of
https://gitlab.crans.org/mediatek/med.git
synced 2024-11-27 13:33:07 +00:00
Get memberships from Note Kfet
This commit is contained in:
parent
d036ea6f27
commit
d3c2441111
28
users/migrations/0044_membership.py
Normal file
28
users/migrations/0044_membership.py
Normal 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',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
@ -48,8 +48,13 @@ class User(AbstractUser):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def is_member(self):
|
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):
|
def update_data(self, data: dict):
|
||||||
"""
|
"""
|
||||||
@ -68,6 +73,38 @@ class User(AbstractUser):
|
|||||||
self.address = data['profile']['address']
|
self.address = data['profile']['address']
|
||||||
self.comment = data['profile']['section']
|
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):
|
class AccessToken(models.Model):
|
||||||
owner = models.ForeignKey(
|
owner = models.ForeignKey(
|
||||||
@ -142,9 +179,6 @@ class AccessToken(models.Model):
|
|||||||
Extract information about the Note Kfet API by using the current
|
Extract information about the Note Kfet API by using the current
|
||||||
access token.
|
access token.
|
||||||
"""
|
"""
|
||||||
if self.owner:
|
|
||||||
return self.owner
|
|
||||||
|
|
||||||
data = requests.get('https://note-dev.crans.org/api/me/',
|
data = requests.get('https://note-dev.crans.org/api/me/',
|
||||||
headers=self.auth_header()).json()
|
headers=self.auth_header()).json()
|
||||||
username = data['username']
|
username = data['username']
|
||||||
@ -162,6 +196,10 @@ class AccessToken(models.Model):
|
|||||||
user.update_data(data)
|
user.update_data(data)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
|
# Store token owner
|
||||||
|
self.owner = user
|
||||||
|
self.save()
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
Loading…
Reference in New Issue
Block a user