Skip to content

Commit

Permalink
update institution in db when profile update form is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Rosenberg authored and Jake Rosenberg committed Nov 18, 2023
1 parent e3e3960 commit 471c8ab
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion designsafe/apps/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def clean_website(self):

class Meta:
model = DesignSafeProfile
exclude = ['user', 'ethnicity', 'gender', 'update_required']
exclude = ['user', 'ethnicity', 'gender', 'update_required', 'institution']


class UserRegistrationForm(UserProfileForm, ProfessionalProfileForm):
Expand Down
1 change: 1 addition & 0 deletions designsafe/apps/accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def mock_user_side_effect(username = 'ds_admin', email = '[email protected]'):

# pass test for duplicate email check
mock_tas().get_user.side_effect = mock_user_side_effect
mock_tas().save_user.side_effect = mock_user_side_effect

edit_url = reverse('designsafe_accounts:profile_edit')
self.client.login(username='ds_admin', password='admin/password')
Expand Down
6 changes: 4 additions & 2 deletions designsafe/apps/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def profile_edit(request):

# retain original account source
data['source'] = tas_user['source']
tas.save_user(tas_user['id'], data)
saved_user = tas.save_user(tas_user['id'], data)
messages.success(request, 'Your profile has been updated!')

try:
Expand All @@ -298,6 +298,7 @@ def profile_edit(request):
ds_profile.orcid_id = pro_data['orcid_id']
ds_profile.professional_level = pro_data['professional_level']
ds_profile.nh_interests_primary = pro_data['nh_interests_primary']
ds_profile.institution = saved_user.get('institution', None)

except ObjectDoesNotExist as e:
logger.info('exception e: {} {}'.format(type(e), e ))
Expand All @@ -309,7 +310,8 @@ def profile_edit(request):
website=pro_data['website'],
orcid_id=pro_data['orcid_id'],
professional_level=pro_data['professional_level'],
nh_interests_primary=pro_data['nh_interests_primary']
nh_interests_primary=pro_data['nh_interests_primary'],
institution=saved_user.get('institution', None)
)

ds_profile.update_required = False
Expand Down
13 changes: 13 additions & 0 deletions designsafe/apps/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from designsafe.apps.api.tasks import agave_indexer
from designsafe.apps.api.notifications.models import Notification
from celery import shared_task
from django.contrib.auth import get_user_model
from pytas.http import TASClient

from requests import HTTPError
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -101,3 +103,14 @@ def clear_old_notifications(self):
"""Delete notifications older than 30 days to prevent them cluttering the db."""
time_cutoff = datetime.now() - timedelta(days=30)
Notification.objects.filter(datetime__lte=time_cutoff).delete()


@shared_task(bind=True, max_retries=3)
def update_institution_from_tas(self, username):
user_model = get_user_model().objects.get(username=username)
try:
tas_model = TASClient().get_user(username=username)
except Exception as exc:
raise self.retry(exc=exc)
user_model.profile.institution = tas_model.get('institution', None)
user_model.profile.save()

0 comments on commit 471c8ab

Please sign in to comment.