Skip to content

Commit

Permalink
Merge branch 'orcid_date_added' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
tompollard committed May 29, 2022
2 parents ce28cae + fd944e0 commit d2c1205
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
31 changes: 31 additions & 0 deletions physionet-django/user/migrations/0044_orcid_date_added.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.db import migrations, models
from django.utils.timezone import make_aware
from datetime import datetime, timedelta


def migrate_forward(apps, schema_editor):
orcid_model = apps.get_model('user', 'Orcid')
for row in orcid_model.objects.all():
row.date_added = make_aware(datetime.fromtimestamp(row.token_expiration)) - timedelta(days=20 * 365.2422)
row.save()


def migrate_backward(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('user', '0043_auto_20220406_1229'),
]

operations = [
migrations.AddField(
model_name='orcid',
name='date_added',
field=models.DateTimeField(default=make_aware(datetime(2200, 1, 1, 0, 0))),
preserve_default=False,
),
migrations.RunPython(migrate_forward, migrate_backward),
]
2 changes: 2 additions & 0 deletions physionet-django/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import uuid
from datetime import timedelta
from datetime import datetime

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -657,6 +658,7 @@ class Orcid(models.Model):
token_type = models.CharField(max_length=50, default='', blank=True)
token_scope = models.CharField(max_length=50, default='', blank=True)
token_expiration = models.DecimalField(max_digits=50, decimal_places=40, default=0)
date_added = models.DateTimeField(default=datetime(2200, 1, 1))

class Meta:
default_permissions = ()
Expand Down
3 changes: 2 additions & 1 deletion physionet-django/user/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import pdb
from datetime import datetime
from datetime import datetime, timedelta

import django.contrib.auth.views as auth_views
import pytz
Expand Down Expand Up @@ -448,6 +448,7 @@ def auth_orcid(request):
orcid_profile.token_type = token.get('token_type')
orcid_profile.token_scope = token.get('scope')
orcid_profile.token_expiration = token.get('expires_at')
orcid_profile.date_added = datetime.fromtimestamp(token.get('expires_at')) - timedelta(days=20*365.2422)
orcid_profile.full_clean()
orcid_profile.save()

Expand Down

0 comments on commit d2c1205

Please sign in to comment.