Beispiel #1
0
    def get(self, request, id):
        identity_actions = IdentityGatewayActions()
        api_response = identity_actions.list('user',
                                             params={
                                                 'magic_link_email': id,
                                                 'service': "NANNY"
                                             })

        if api_response.status_code == 200:

            self.record = api_response.record[0]

            if not self.link_has_expired():

                # If user has come from the 'Change Email' journey
                if self.record['email'] != self.record[
                        'change_email'] and self.record[
                            'change_email'] is not None and self.record[
                                'change_email'] != "":
                    # Update the user's email
                    update_email = self.record['change_email']
                    update_email_record = self.record
                    update_email_record['email'] = update_email

                    response = identity_actions.put('user',
                                                    params=update_email_record)

                    if response.status_code != 200:
                        return HttpResponseRedirect(
                            reverse('Service-Unavailable'))

                    http_response = HttpResponseRedirect(
                        self.get_success_url())
                    CustomAuthenticationHandler.create_session(
                        http_response, {'email': update_email})
                    return http_response

                email = self.record['email']
                http_response = HttpResponseRedirect(self.get_success_url())
                CustomAuthenticationHandler.create_session(
                    http_response, {'email': email})

                return http_response

            else:
                return HttpResponseRedirect(reverse('Link-Used'))

        elif api_response.status_code == 404:
            return HttpResponseRedirect(reverse('Link-Used'))

        else:
            return HttpResponseRedirect(reverse('Service-Unavailable'))
    def form_valid(self, form):
        if not utilities.test_notify():
            return HttpResponseRedirect(reverse('Service-Unavailable'))

        application_id = self.request.GET.get('id')
        self.email_address = form.cleaned_data['email_address']

        # Create GatewayActions instances
        identity_actions = IdentityGatewayActions()
        nanny_actions = NannyGatewayActions()

        # Get relevant records
        user_identity_record = identity_actions.read('user',
                                                     params={
                                                         'application_id':
                                                         application_id
                                                     }).record

        # Get personal_details response, not record, and check if a record exists
        personal_details_response = nanny_actions.read(
            'applicant-personal-details',
            params={'application_id': application_id})
        try:
            personal_details_record_exists = personal_details_response.record is not None
        except AttributeError:
            personal_details_record_exists = False

        # Get user's current email
        account_email = user_identity_record['email']

        # Get first_name if it exists, otherwise use 'Applicant'
        if personal_details_record_exists:
            first_name = personal_details_response.record['first_name']
        else:
            first_name = "Applicant"

        existing_account_response = identity_actions.list(
            'user', params={
                'email': self.email_address,
                'service': 'NANNY'
            })
        existing_account_response_status_code = existing_account_response.status_code

        email_in_use = existing_account_response_status_code == 200

        if self.email_address == account_email:
            # If email is unchanged, return to the sign-in details check-answers page
            same_email_redirect = utilities.build_url(
                self.check_answers_url, get={'id': application_id})
            return HttpResponseRedirect(same_email_redirect)

        elif email_in_use:
            # If the email is already being used,
            if settings.DEBUG:
                print(
                    "You will not see an email validation link printed because an account already exists with that email."
                )
            not_sent_email_redirect = utilities.build_url(
                self.success_url, get={'id': application_id})
            email_address = self.email_address
            return HttpResponseRedirect(not_sent_email_redirect)

        else:
            change_email = self.email_address
            # Generate a new magic link and expiry date
            validation_link, email_expiry_date = utilities.generate_email_validation_link(
                change_email)
            magic_link = validation_link.split('/')[-1]

            # Create an update record with the magic_link information
            email_update_record = user_identity_record
            email_update_record['magic_link_email'] = magic_link
            email_update_record['email_expiry_date'] = email_expiry_date
            email_update_record['change_email'] = change_email

            # Update the user record
            record = IdentityGatewayActions().put('user',
                                                  params=email_update_record)

            # Send the 'Change Email' email
            if settings.DEBUG:
                print(validation_link)

            send_change_email_email(change_email, first_name, validation_link)

            sent_email_redirect = utilities.build_url(
                self.success_url, get={'id': application_id})

            return HttpResponseRedirect(sent_email_redirect)
    def get(self, request):
        email_address = request.GET['email_address']
        application_id = request.GET['id']

        # Create GatewayActions instances
        identity_actions = IdentityGatewayActions()
        nanny_actions = NannyGatewayActions()

        # Get relevant records
        user_identity_record = identity_actions.read('user',
                                                     params={
                                                         'application_id':
                                                         application_id
                                                     }).record

        # Get personal_details response, not record, and check if a record exists
        personal_details_response = nanny_actions.read(
            'applicant-personal-details',
            params={'application_id': application_id})

        try:
            personal_details_record_exists = personal_details_response.record is not None
        except AttributeError:
            personal_details_record_exists = False

        # Get first_name if it exists, otherwise use 'Applicant'
        if personal_details_record_exists:
            first_name = personal_details_response.record['first_name']
        else:
            first_name = "Applicant"

        # Check if email already exists/in use
        existing_account_response = identity_actions.list('user',
                                                          params={
                                                              'email':
                                                              email_address,
                                                              'service':
                                                              'NANNY'
                                                          })
        existing_account_response_status_code = existing_account_response.status_code

        email_in_use = existing_account_response_status_code == 200

        if email_in_use:
            if settings.DEBUG:
                print(
                    "You will not see an email validation link printed because an account already exists with that email."
                )
        else:
            # Generate a new magic link and expiry date
            validation_link, email_expiry_date = utilities.generate_email_validation_link(
                email_address)
            magic_link = validation_link.split('/')[-1]
            validation_link += '?email=' + email_address

            # Create an update record with the magic_link information
            email_update_record = user_identity_record
            email_update_record['magic_link_email'] = magic_link
            email_update_record['email_expiry_date'] = email_expiry_date

            # Update the user record
            IdentityGatewayActions().put('user', params=email_update_record)

            # Send the 'Change Email' email
            if settings.DEBUG:
                print(validation_link)

            send_change_email_email(email_address, first_name, validation_link)

        return render(request,
                      template_name='email-resent.html',
                      context={
                          'id': application_id,
                          'email_address': email_address
                      })