def get(self):
        """

        :return: Renders the html page with all substituted content needed.
        """
        # INFO: For now requirement to login is not needed. Open access is fine, if needed uncomment below code to
        # INFO: require authentication via JWT from login app.
        # if request.cookies.get('access_token_cookie') is None:
        #     self.redirect_to_uscc_login()
        #     return self.login_redirect_response

        form = NeTextForm()
        if len(self.neh_text_dir_found_list) != 0:
            neh_file_path = self.neh_text_dir_found_list[0]
            if request.args.get('search_first') is not None and request.args.get('search_first') != '' and \
                    request.args.get('search_last') is not None and request.args.get('search_last') != '':
                search_key = '{} {}'.format(request.args.get('search_first'), request.args.get('search_last'))
                neh_text_dict = self.data_file_to_dict(neh_file_path)

                if neh_text_dict.get(search_key) is not None:
                    # Get the phone number (which is a key in the sub dictionary for the search name)
                    user_phone = list(neh_text_dict.get(search_key).keys())[0]
                else:
                    Common.create_flash_message(message="No phone number for {} has been registered".format(search_key))
            else:
                user_phone = None

        return render_template('network_health/ne_text_tracking.html', form=form, display_phone=user_phone)
    def post(self):
        """
        """

        # INFO: No requirement for being authenticated to add phone number for text message services. If needed
        # INFO: uncomment both 'if' blocks below.
        # if 'logout_btn' in request.form:
        #     self.delete()
        #     return self.login_redirect_response
        #
        # if request.cookies.get('access_token_cookie') is None:
        #     self.redirect_to_uscc_login()
        #     return self.login_redirect_response

        # else:
        #     # INFO: needed if JWT_TOKEN_LOCATION is set to headers as opposed to in cookies
        #     # self.imsi_header['Authorization'] = 'JWT {}'.format(request.cookies.get('access_token_cookie'))
        #     # INFO: With JWT in cookies set the CSRF token is still expected to be in the header for POST to succeed
        #     self.imsi_header['X-CSRF-TOKEN'] = request.cookies.get('csrf_access_token')
        #     self.imsi_header['content_type'] = 'application/json'

        form = NeTextForm()

        if form.validate_on_submit():
            if len(self.neh_text_dir_found_list) != 0:
                neh_text_path = self.neh_text_dir_found_list[0]
                nehtext_file_dict = self.data_file_to_dict(neh_text_path)
                name_key = '{} {}'.format(form.first_name.data, form.last_name.data)

                if name_key not in nehtext_file_dict:
                    nehtext_file_dict[name_key] = {form.phone_num.data: form.carrier.data}

                    with open(neh_text_path, mode='w') as nehwfh:
                        json.dump(nehtext_file_dict, nehwfh)

                Common.create_flash_message(message="Phone Number added")
            else:
                Common.create_flash_message(message="'{}' file not found. Please contact Core Automation Team.".format(os.environ.get('neh_text_file')))

        else:
            if len(form.errors) != 0:
                for form_field, error_message_text in form.errors.items():
                    Common.create_flash_message(message=form_field + ':' + error_message_text[0])

        return render_template('network_health/ne_text_tracking.html', form=form)
Beispiel #3
0
    def post(self):
        """

        """

        form = LoginForm()
        if form.validate_on_submit():
            self.auth_data['username'] = request.form['username']
            self.auth_data['password'] = request.form['password']
            self.client_referrer = request.args.get('referrer')
            auth_response = requests.post(api.url_for(Authenticate,
                                                      _external=True),
                                          data=json.dumps(self.auth_data),
                                          headers=self.headers)
            auth_response_text_dict = json.loads(auth_response.text)
            if auth_response.status_code == requests.codes.ok:
                login_data_dict = json.loads(auth_response.text)
                if self.client_referrer is not None:
                    response = redirect(self.client_referrer)
                    response.set_cookie('username',
                                        value=self.auth_data.get('username'),
                                        httponly=True)
                    response.set_cookie(
                        'automations',
                        value=json.dumps(login_data_dict.get('automations')),
                        max_age=network_health_app.config.get(
                            'JWT_ACCESS_TOKEN_EXPIRES'))
                    set_access_cookies(
                        response,
                        login_data_dict.get('art').get('access_token'),
                        max_age=network_health_app.config.get(
                            'JWT_ACCESS_TOKEN_EXPIRES'))
                    set_refresh_cookies(
                        response,
                        login_data_dict.get('art').get('refresh_token'),
                        max_age=network_health_app.config.get(
                            'JWT_REFRESH_TOKEN_EXPIRES'))
                    return response
                else:
                    response = make_response(
                        render_template(
                            'login_welcome.html',
                            username=self.auth_data.get('username')))
                    response.set_cookie(
                        'automations',
                        value=json.dumps(login_data_dict.get('automations')),
                        max_age=network_health_app.config.get(
                            'JWT_ACCESS_TOKEN_EXPIRES'))
                    response.set_cookie('username',
                                        value=self.auth_data.get('username'),
                                        httponly=True)
                    set_access_cookies(
                        response,
                        login_data_dict.get('art').get('access_token'),
                        max_age=network_health_app.config.get(
                            'JWT_ACCESS_TOKEN_EXPIRES'))
                    set_refresh_cookies(
                        response,
                        login_data_dict.get('art').get('refresh_token'),
                        max_age=network_health_app.config.get(
                            'JWT_REFRESH_TOKEN_EXPIRES'))
                    return response
            elif auth_response.status_code == requests.codes.unauthorized:
                network_health_app.logger.info(
                    'Invalid login occurred. IP=%s:Username=%s' %
                    (request.remote_addr, self.auth_data.get('username')))
                bad_cred_mmsg = 'Username and/or password is invalid. Please reenter.'
                Common.create_flash_message(bad_cred_mmsg)
            else:
                if auth_response_text_dict.get(
                        'message') == 'api_cred_path invalid':

                    auth_response_cred_path_msg = '%s:%s. Configuration error please contact Core Automation Team.' \
                                                  % (auth_response.status_code, auth_response.reason)
                    # auth_response_cred_path_msg = auth_response_text_dict.get('message') + '. Path set as %s.' \
                    #                               % os.environ.get('api_cred_path')
                    # INFO: Temporary code. Once LDAP integration put in place this is not needed.
                    api_cred_dir_found_list = Common.find_file_in_project(
                        'api_cred')
                    if len(api_cred_dir_found_list) > 0:
                        network_health_app.logger.critical(
                            auth_response_text_dict.get('message') +
                            '. Path set as %s. Did you mean to set path as %s?'
                            % (os.environ.get('api_cred_path'),
                               api_cred_dir_found_list[0]))

                    Common.create_flash_message(auth_response_cred_path_msg)
                else:
                    Common.create_flash_message(
                        "%s:%s Please Contact Core Automation Team" %
                        (auth_response.status_code, auth_response.reason))
        else:
            if len(form.errors) != 0:
                for error_message_text in form.errors.values():
                    Common.create_flash_message(error_message_text[0])

        return render_template('login.html', form=form)