def generate_user(self, type): if type == 'customer': extra_data = { 'firstname': "Mario", 'lastname': "Rossi", 'birthdate': TestUserManager.faker.date(), 'social_number': TestUserManager.faker.ssn(), 'health_status': choice([True, False]), 'phone': TestUserManager.faker.phone_number() } elif type == 'authority': extra_data = { 'name': self.faker.company(), 'city': self.faker.city(), 'address': self.faker.address(), 'phone': self.faker.phone_number() } else: extra_data = {} data = { 'id': randint(0, 999), 'email': TestUserManager.faker.email(), 'is_active': choice([True, False]), 'authenticated': choice([True, False]), 'is_anonymous': False, 'type': type, 'extra': extra_data, } user = User(**data) return user
def authenticate_user(cls, email: str, password: str) -> User: """ This method authenticates the user trough users AP :param email: user email :param password: user password :return: None if credentials are not correct, User instance if credentials are correct. """ payload = dict(email=email, password=password) try: response = requests.post('%s/authenticate' % cls.USERS_ENDPOINT, json=payload, timeout=cls.REQUESTS_TIMEOUT_SECONDS) json_response = response.json() except (requests.exceptions.ConnectionError, requests.exceptions.Timeout): return abort(500) if response.status_code == 401: # user is not authenticated return None elif response.status_code == 200: user = User.build_from_json(json_response['user']) return user else: raise RuntimeError( 'Microservice users returned an invalid status code %s, and message %s' % (response.status_code, json_response['error_message']))
def get_user_by_id(cls, user_id: int) -> User: """ This method contacts the users microservice and retrieves the user object by user id. :param user_id: the user id :return: User obj with id=user_id """ try: response = requests.get("%s/user/%s" % (cls.USERS_ENDPOINT, str(user_id)), timeout=cls.REQUESTS_TIMEOUT_SECONDS) json_payload = response.json() if response.status_code == 200: # user is authenticated user = User.build_from_json(json_payload) else: raise RuntimeError( 'Server has sent an unrecognized status code %s' % response.status_code) except (requests.exceptions.ConnectionError, requests.exceptions.Timeout): return abort(500) return user
def create_user_type(type_): """This method allows the creation of a new user into the database Args: type_ (string): as a parameter takes a string that defines the type of the new user Returns: Redirects the user into his profile page, once he's logged in """ form = LoginForm() if type_ == "customer": form = UserForm() if form.is_submitted(): email = form.data['email'] password = form.data['password'] if type_ == "operator": response = UserManager.create_operator(email, password) else: social_number = form.data['social_number'] firstname = form.data['firstname'] lastname = form.data['lastname'] birthdate = form.data['birthdate'] date = birthdate.strftime('%Y-%m-%d') phone = form.data['phone'] response = UserManager.create_customer('customer', email, password, social_number, firstname, lastname, date, phone) user = response.json() if user["status"] == "success": to_login = User.build_from_json(user["user"]) login_user(to_login) if to_login.type == "operator": return redirect(url_for('auth.operator', op_id=to_login.id)) else: return redirect(url_for('auth.profile', id=to_login.id)) else: flash("Invalid credentials") return render_template('create_user.html', form=form, user_type=type_) else: for fieldName, errorMessages in form.errors.items(): for errorMessage in errorMessages: flash('The field %s is incorrect: %s' % (fieldName, errorMessage)) return render_template('create_user.html', form=form, user_type=type_)
def get_user_by_social_number(cls, user_social_number: str) -> User: """ This method contacts the users microservice and retrieves the user object by user social_number. :param user_social_number: the user social_number :return: User obj with social_number=user_social_number """ try: response = requests.get("%s/user_social_number/%s" % (cls.USERS_ENDPOINT, user_social_number), timeout=cls.REQUESTS_TIMEOUT_SECONDS) json_payload = response.json() user = None if response.status_code == 200: user = User.build_from_json(json_payload) except (requests.exceptions.ConnectionError, requests.exceptions.Timeout): return abort(500) return user
def get_all_positive_customer(cls) -> [User]: """ This method contacts the users microservice and retrieves all positive customers. :return: A list of User obj with health_status = True """ try: response = requests.get("%s/positive_customers" % cls.USERS_ENDPOINT, timeout=cls.REQUESTS_TIMEOUT_SECONDS) json_payload = response.json() pos_customers = [] if response.status_code == 200: for json in json_payload: pos_customers.append(User.build_from_json(json)) return pos_customers except (requests.exceptions.ConnectionError, requests.exceptions.Timeout): return abort(500) return None