Beispiel #1
0
    def __delete_home_childcare_addresses(app_id):
        """
        Deletes childcare addresses that are the same as the home childcare address in all address fields.
        :param app_id: Applicant's id
        :return: None
        """
        nanny_actions = NannyGatewayActions()

        home_address_record = nanny_actions.read('applicant-home-address', params={'application_id': app_id}).record

        home_address_filter = {
            'application_id': app_id,
            'street_line1': home_address_record['street_line1'] if home_address_record['street_line1'] else "",
            'street_line2': home_address_record['street_line2'] if home_address_record['street_line2'] else "",
            'town': home_address_record['town'] if home_address_record['town'] else "",
            'county': home_address_record['county'] if home_address_record['county'] else "",
            'country': home_address_record['country'] if home_address_record['country'] else "",
            'postcode': home_address_record['postcode'] if home_address_record['postcode'] else ""
        }

        home_childcare_address_response = nanny_actions.list('childcare-address', params=home_address_filter)

        if home_childcare_address_response.status_code == 200:
            home_childcare_address_list = home_childcare_address_response.record

            for address in home_childcare_address_list:
                nanny_actions.delete('childcare-address', params={'childcare_address_id': address['childcare_address_id']})
    def form_valid(self, form):
        application_id = self.request.GET['id']
        record = IdentityGatewayActions().read('user',
                                               params={
                                                   'application_id':
                                                   application_id
                                               }).record
        record['sms_resend_attempts'] = 0
        record['email_expiry_date'] = 0
        IdentityGatewayActions().put('user', params=record)
        response = login_redirect_helper.redirect_by_status(
            record['application_id'])
        CustomAuthenticationHandler.create_session(response,
                                                   {'email': record['email']})

        # Update last accessed time when successfully signed in
        nanny_actions = NannyGatewayActions()
        app_response = nanny_actions.read(
            'application', params={'application_id': application_id})
        if app_response.status_code == 200 and hasattr(app_response, 'record'):
            # application might not exist yet, if user signed out before completing contact details task
            application = app_response.record
            application['date_last_accessed'] = datetime.datetime.now()
            application['application_expiry_email_sent'] = False
            nanny_actions.put('application', application)

        return response
Beispiel #3
0
    def __check_childcare_address_changed_to_false(app_id, cleaned_childcare_address):
        """
        Returns True if the childcare_address field for the applicant's home address was False and has been changed to
        True.
        :param app_id: Application id
        :param cleaned_childcare_address: Boolean of cleaned form data.
        :return: Boolean
        """
        if not cleaned_childcare_address:
            nanny_actions = NannyGatewayActions()
            applicant_home_address_record = nanny_actions.read('applicant-home-address',
                                                               params={'application_id': app_id}).record

            if applicant_home_address_record['childcare_address']:
                return True

        return False
    def form_valid(self, form):
        app_id = self.request.GET.get('id')
        nanny_actions = NannyGatewayActions()
        address_to_be_provided = form.cleaned_data['address_to_be_provided']

        # Convert address_to_be_provided to a boolean.
        address_to_be_provided = address_to_be_provided == 'True' \
            if type(address_to_be_provided) == str \
            else address_to_be_provided

        application_record = nanny_actions.read('application',
                                                params={
                                                    'application_id': app_id
                                                }).record

        # Patch new address_to_be_provided value
        patch_record = {
            'application_id': application_record['application_id'],
            'address_to_be_provided': address_to_be_provided,
            'childcare_address_status': 'IN_PROGRESS'
        }
        nanny_actions.patch('application', params=patch_record)

        # Set success_url
        if address_to_be_provided:
            self.success_url = 'Childcare-Address-Location'
        else:
            self.success_url = 'Childcare-Address-Details-Later'

            # Delete all existing childcare addresses, if any exist.
            self.__delete_childcare_addresses(app_id)

            # Nullify answer to 'Will you work and live at the same address?'
            self.__nullify_answers(app_id)

        return super(WhereYouWorkView, self).form_valid(form)
    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
                      })