-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #398 from kids-first/refferal-token
✨ Add ReferralToken Model and create referral tokens mutation
- Loading branch information
Showing
20 changed files
with
1,008 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
creator/events/migrations/0011_add_referral_tokens_status.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.1.11 on 2020-05-26 20:15 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('events', '0010_add_phenotype_status'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='event', | ||
name='event_type', | ||
field=models.CharField(choices=[('SF_CRE', 'Study File Created'), ('SF_UPD', 'Study File Updated'), ('SF_DEL', 'Study File Deleted'), ('FV_CRE', 'File Version Created'), ('FV_UPD', 'File Version Updated'), ('SD_CRE', 'Study Created'), ('SD_UPD', 'Study Updated'), ('PR_CRE', 'Project Created'), ('PR_UPD', 'Project Updated'), ('PR_DEL', 'Project Deleted'), ('PR_LIN', 'Project Linked'), ('PR_UNL', 'Project Unlinked'), ('PR_STR', 'Project Creation Start'), ('PR_ERR', 'Project Creation Error'), ('PR_SUC', 'Project Creation Success'), ('BK_STR', 'Bucket Creation Start'), ('BK_ERR', 'Bucket Creation Error'), ('BK_SUC', 'Bucket Creation Success'), ('BK_LIN', 'Bucket Linked'), ('BK_UNL', 'Bucket Unlinked'), ('IM_STR', 'File Import Start'), ('IM_ERR', 'File Import Error'), ('IM_SUC', 'File Import Success'), ('CB_ADD', 'Collaborator Added'), ('CB_REM', 'Collaborator Removed'), ('IN_UPD', 'Ingestion Status Updated'), ('PH_UPD', 'Phenotype Status Updated'), ('ST_UPD', 'Sequencing Status Updated'), ('RT_CRE', 'Referral Token Created'), ('RT_CLA', 'Referral Token Claimed'), ('OTH', 'Other')], default='OTH', max_length=6), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ReferralTokensConfig(AppConfig): | ||
name = "referral_tokens" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytz | ||
import factory | ||
from .models import ReferralToken | ||
|
||
|
||
class ReferralTokenFactory(factory.DjangoModelFactory): | ||
class Meta: | ||
model = ReferralToken | ||
django_get_or_create = ("uuid",) | ||
|
||
uuid = factory.Faker("uuid4") | ||
email = factory.Faker("email") | ||
created_at = factory.Faker( | ||
"date_time_between", start_date="-2d", end_date="now", tzinfo=pytz.UTC | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 2.1.11 on 2020-05-22 20:07 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('auth', '0009_alter_user_last_name_max_length'), | ||
('studies', '0016_add_phenotype_status'), | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ReferralToken', | ||
fields=[ | ||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)), | ||
('email', models.EmailField(help_text='The email that the token link will be sent to', max_length=254)), | ||
('claimed', models.BooleanField(default=False, help_text='If the token has been used')), | ||
('created_at', models.DateTimeField(default=django.utils.timezone.now, help_text='Time the referral token was created')), | ||
('claimed_by', models.ForeignKey(blank=True, help_text='The user who claims the token', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_tokens_claimed_by', to=settings.AUTH_USER_MODEL)), | ||
('created_by', models.ForeignKey(blank=True, help_text='The user who created the token', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_tokens_created_by', to=settings.AUTH_USER_MODEL)), | ||
('groups', models.ManyToManyField(blank=True, help_text='The role the user will assume in these studies', related_name='referral_tokens', related_query_name='referral_tokens', to='auth.Group')), | ||
('studies', models.ManyToManyField(help_text='List of studies that the user will be added to', related_name='referral_tokens', to='studies.Study')), | ||
], | ||
options={ | ||
'permissions': [('list_all_referraltoken', 'Can view all referral tokens')], | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import uuid | ||
import pytz | ||
from datetime import datetime | ||
from django.db import models | ||
from django.utils import timezone | ||
from django.contrib.auth import get_user_model | ||
from django.conf import settings | ||
from creator.studies.models import Study | ||
from django.contrib.auth.models import Group | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
class ReferralToken(models.Model): | ||
""" | ||
An ReferralToken will be exchanged by a new or existing user in order to | ||
be added to a studies with a given role. | ||
""" | ||
|
||
class Meta: | ||
permissions = [ | ||
("list_all_referraltoken", "Can view all referral tokens"), | ||
] | ||
|
||
uuid = models.UUIDField( | ||
default=uuid.uuid4, unique=True, editable=False, primary_key=True | ||
) | ||
|
||
email = models.EmailField( | ||
max_length=254, | ||
help_text="The email that the token link will be sent to", | ||
) | ||
|
||
claimed = models.BooleanField( | ||
default=False, help_text="If the token has been used" | ||
) | ||
|
||
studies = models.ManyToManyField( | ||
Study, | ||
related_name="referral_tokens", | ||
help_text="List of studies that the user will be added to", | ||
) | ||
|
||
groups = models.ManyToManyField( | ||
Group, | ||
blank=True, | ||
help_text="The role the user will assume in these studies", | ||
related_name="referral_tokens", | ||
related_query_name="referral_tokens", | ||
) | ||
|
||
created_at = models.DateTimeField( | ||
default=timezone.now, | ||
null=False, | ||
help_text="Time the referral token was created", | ||
) | ||
|
||
claimed_by = models.ForeignKey( | ||
User, | ||
related_name="referral_tokens_claimed_by", | ||
help_text="The user who claims the token", | ||
null=True, | ||
blank=True, | ||
on_delete=models.SET_NULL, | ||
) | ||
|
||
created_by = models.ForeignKey( | ||
User, | ||
related_name="referral_tokens_created_by", | ||
help_text="The user who created the token", | ||
null=True, | ||
blank=True, | ||
on_delete=models.SET_NULL, | ||
) | ||
|
||
@property | ||
def is_valid(self): | ||
""" | ||
If the token has already been claimed or if it has expired | ||
""" | ||
|
||
exp_length = settings.REFERRAL_TOKEN_EXPIRATION_DAYS | ||
expired = ( | ||
datetime.now().replace(tzinfo=pytz.UTC) - self.created_at | ||
).days > exp_length | ||
return not self.claimed and not expired | ||
|
||
@property | ||
def invite_url(self): | ||
""" | ||
User invitation url | ||
""" | ||
return f"{settings.DATA_TRACKER_URL}/join?token={self.uuid}" |
Oops, something went wrong.