def get_country_choices(): tas = TASClient() countries_list = tas.countries() return (('', 'Choose one'), ) + tuple( (c['id'], c['name']) for c in countries_list)
def get_country_choices(): tas = TASClient() countries_list = tas.countries() return (('', 'Choose one'),) + tuple((c['id'], c['name']) for c in countries_list)
def nees_migration(request, step=None): if step == 0 or step is None: return render(request, 'designsafe/apps/accounts/nees_migration.html', {'form': forms.NEESAccountMigrationForm()}) elif step == '1': if request.method == 'POST': email_address = request.POST.get('email_address') else: email_address = request.GET.get('email_address') if email_address: nees_user_match = NEESUser.lookup_user(email_address) if len(nees_user_match) == 0: messages.error(request, 'We were unable to locate a NEEShub account for the email ' 'address <b>%s</b>. Please confirm that you entered the ' 'email address correctly and try again. If you feel this ' 'is in error, please submit a support ticket.' % email_address) return HttpResponseRedirect(reverse('designsafe_accounts:nees_migration')) else: tas_by_username = None tas_by_email = None # check for existing TAS user tas_api = TASClient() try: tas_by_username = tas_api.get_user(username=nees_user_match[0].username) except: logger.exception('Error checking for existing TAS users') try: tas_by_email = tas_api.get_user(email=nees_user_match[0].email) except: logger.exception('Error checking for existing TAS users') context = { 'email_address': email_address, 'nees_user_match': nees_user_match, 'tas_by_username': tas_by_username, 'tas_by_email': tas_by_email, } return render(request, 'designsafe/apps/accounts/nees_migration_step_1.html', context) elif step == '2': tas_api = TASClient() if request.method == 'POST': email_address = request.POST.get('email_address') nees_user_match = NEESUser.lookup_user(email_address) countries = tas_api.countries() country_residence = next((c for c in countries if c['abbrev'] == nees_user_match[0].countryresident), {'id':None}) country_origin = next((c for c in countries if c['abbrev'] == nees_user_match[0].countryorigin), {'id':None}) initial_data = { 'firstName': nees_user_match[0].givenName, 'lastName': nees_user_match[0].surname, 'email': nees_user_match[0].email, 'phone': nees_user_match[0].phone, 'countryId': country_residence['id'], 'citizenshipId': country_origin['id'], 'username': nees_user_match[0].username, } if nees_user_match[0].organization is not None: initial_data['institutionId'] = -1 initial_data['institution'] = nees_user_match[0].organization form = forms.UserRegistrationForm(initial=initial_data) return render(request, 'designsafe/apps/accounts/nees_migration_step_2.html', {'form': form}) elif step == '3': # final step! tas_api = TASClient() if request.method == 'POST': form = forms.UserRegistrationForm(request.POST) if form.is_valid(): # attempt TAS User creation try: form.save() messages.success( request, 'Congratulations! Your account migration was successful. You ' 'still need you to activate your TACC account. You should an ' 'activation code at the email address provided. Please follow ' 'the instructions in the email to activate your account.' ) return HttpResponseRedirect('/') except Exception as e: logger.exception('Error saving user!') logger.info('error: {}'.format(e)) error_type = e.args[1] if len(e.args) > 1 else '' if 'DuplicateLoginException' in error_type: err_msg = ( 'The username you chose has already been taken. Please ' 'choose another. If you already have an account with TACC, ' 'please log in using those credentials.') form._errors.setdefault('username', [err_msg]) elif 'DuplicateEmailException' in error_type: err_msg = ( 'This email is already registered. If you already have an ' 'account with TACC, please log in using those credentials.') form._errors.setdefault('email', [err_msg]) err_msg = '%s <a href="%s">Did you forget your password?</a>' % ( err_msg, reverse('designsafe_accounts:password_reset')) elif 'PasswordInvalidException' in error_type: err_msg = ( 'The password you provided did not meet the complexity ' 'requirements.') form._errors.setdefault('password', [err_msg]) else: err_msg = ( 'An unexpected error occurred. If this problem persists ' 'please create a support ticket.') messages.error(request, err_msg) else: messages.error(request, 'There was an error processing your account ' 'migration. Please see below for details.') return render(request, 'designsafe/apps/accounts/nees_migration_step_2.html', {'form': form}) return HttpResponseRedirect(reverse('designsafe_accounts:nees_migration'))
def nees_migration(request, step=None): if step == 0 or step is None: return render(request, 'designsafe/apps/accounts/nees_migration.html', {'form': forms.NEESAccountMigrationForm()}) elif step == '1': if request.method == 'POST': email_address = request.POST.get('email_address') else: email_address = request.GET.get('email_address') if email_address: nees_user_match = NEESUser.lookup_user(email_address) if len(nees_user_match) == 0: messages.error(request, 'We were unable to locate a NEEShub account for the email ' 'address <b>%s</b>. Please confirm that you entered the ' 'email address correctly and try again. If you feel this ' 'is in error, please submit a support ticket.' % email_address) return HttpResponseRedirect(reverse('designsafe_accounts:nees_migration')) else: tas_by_username = None tas_by_email = None # check for existing TAS user tas_api = TASClient() try: tas_by_username = tas_api.get_user(username=nees_user_match[0].username) except: logger.exception('Error checking for existing TAS users') try: tas_by_email = tas_api.get_user(email=nees_user_match[0].email) except: logger.exception('Error checking for existing TAS users') context = { 'email_address': email_address, 'nees_user_match': nees_user_match, 'tas_by_username': tas_by_username, 'tas_by_email': tas_by_email, } return render(request, 'designsafe/apps/accounts/nees_migration_step_1.html', context) elif step == '2': tas_api = TASClient() if request.method == 'POST': email_address = request.POST.get('email_address') nees_user_match = NEESUser.lookup_user(email_address) countries = tas_api.countries() country_residence = next((c for c in countries if c['abbrev'] == nees_user_match[0].countryresident), {'id':None}) country_origin = next((c for c in countries if c['abbrev'] == nees_user_match[0].countryorigin), {'id':None}) initial_data = { 'firstName': nees_user_match[0].givenName, 'lastName': nees_user_match[0].surname, 'email': nees_user_match[0].email, 'phone': nees_user_match[0].phone, 'countryId': country_residence['id'], 'citizenshipId': country_origin['id'], 'username': nees_user_match[0].username, } if nees_user_match[0].organization is not None: initial_data['institutionId'] = -1 initial_data['institution'] = nees_user_match[0].organization form = forms.UserRegistrationForm(initial=initial_data) return render(request, 'designsafe/apps/accounts/nees_migration_step_2.html', {'form': form}) elif step == '3': # final step! tas_api = TASClient() if request.method == 'POST': form = forms.UserRegistrationForm(request.POST) if form.is_valid(): # attempt TAS User creation try: form.save() messages.success( request, 'Congratulations! Your account migration was successful. You ' 'still need you to activate your TACC account. You should an ' 'activation code at the email address provided. Please follow ' 'the instructions in the email to activate your account.' ) return HttpResponseRedirect('/') except Exception as e: logger.exception('Error saving user!') logger.info('error: {}'.format(e)) error_type = e.args[1] if len(e.args) > 1 else '' if 'DuplicateLoginException' in error_type: err_msg = ( 'The username you chose has already been taken. Please ' 'choose another. If you already have an account with TACC, ' 'please log in using those credentials.') form._errors.setdefault('username', [err_msg]) elif 'DuplicateEmailException' in error_type: err_msg = ( 'This email is already registered. If you already have an ' 'account with TACC, please log in using those credentials.') form._errors.setdefault('email', [err_msg]) err_msg = '%s <a href="%s">Did you forget your password?</a>' % ( err_msg, reverse('designsafe_accounts:password_reset')) elif 'PasswordInvalidException' in error_type: err_msg = ( 'The password you provided did not meet the complexity ' 'requirements.') form._errors.setdefault('password', [err_msg]) else: err_msg = ( 'An unexpected error occurred. If this problem persists ' 'please create a support ticket.') messages.error(request, err_msg) else: messages.error(request, 'There was an error processing your account ' 'migration. Please see below for details.') return render(request, 'designsafe/apps/accounts/nees_migration_step_2.html', {'form': form}) return HttpResponseRedirect(reverse('designsafe_accounts:nees_migration'))