def add_a_manager(company_id, email, name, designation, department_id,
                   date_of_joining):
     # to add manager by the admin of registered company
     password = managerConstants.password_generator()
     Manager(company_id, email, Utils.hash_password(password), name,
             designation, department_id, date_of_joining).save_to_db()
     print(password)
     managerConstants.send_email(email, password)
    def register(company_name, ceo, email, password, contact, gst_no):
        # """
        # registers the company for bill reimbursement
        # :raises: company already registered, admin already registered
        # :return: true if successfully registered
        # """
        company_data = Database.find_one(adminConstant.COLLECTION, {"company_name":company_name})
        admin_data = Database.find_one(adminConstant.COLLECTION, {"email": email})

        if company_data is not None:
            raise adminErrors.CompanyAlreadyRegisteredError("Your company is already registered")
        if admin_data is not None:
            raise adminErrors.AdminAlreadyRegisteredError("You are already registered")
        if not Utils.email_is_valid(email):
            raise adminErrors.InvalidEmailError("You entered wrong email")
        Admin(company_name, ceo, email, Utils.hash_password(password), contact, gst_no).save_to_db()

        return True
    def is_reset_password_valid(cls,email, old_password):
        # """
        # It checks whether the credentials are valid and exist
        # :param email: user email
        # :param old_password: user entered
        # :raise: Incorrect password exception
        # :return: employee's data
        # """
        employee_data = Database.find_one(employeeConstants.COLLECTION, {'email':email})
        print(employee_data['password'])
        if not Utils.check_hashed_password(old_password, employee_data['password']):
            raise EmployeeError.IncorrectPasswordError("Password does not match")

        return cls(**employee_data)
    def is_reset_password_valid(cls, email, old_password):
        # """
        # It checks whether the credentials are valid and exist
        # :param email: user email
        # :param old_password: user entered
        # :raise: Incorrect password exception
        # :return: admins's data
        # """
        admin_data = Database.find_one(adminConstant.COLLECTION, {'email': email})
        #print(manager_data['password'])
        if not Utils.check_hashed_password(old_password, admin_data['password']):
            raise adminErrors.IncorrectPasswordError("Password does not match")

        return cls(**admin_data)
def reset_password():
    # reset old password
    if request.method == 'POST':
        email = session['email']
        old_password = request.form['old_password']
        new_password = request.form['new_password']
        try:
            director = Director.is_reset_password_valid(email, old_password)
            director.password = Utils.hash_password(new_password)
            director.update_to_db()
            return redirect(url_for('bills.view_bills_to_director', sort_type="default", filter_type="pending"))
        except director_errors.IncorrectPasswordError as error:
            return error.message

    return render_template('directors/reset_password.html')
def reset_password():
    # reset old password
    if request.method == 'POST':
        email = session['email']
        old_password = request.form['old_password']
        new_password = request.form['new_password']
        try:
            admin = Admin.is_reset_password_valid(email, old_password)
            admin.password = Utils.hash_password(new_password)
            admin.update_to_db()
            return redirect(url_for('.to_menue', sort_type="default", filter_type="pending"))
        except admin_errors.IncorrectPasswordError as error:
            return error.message

    return render_template('admins/reset_password.html')
    def is_login_valid(email, password):
        # """
        # checks if the entered credentials exist in database
        # :param email: user entered
        # :param password: user entered
        # :raises: Employee not exist, incorrect password exception
        # :return: True
        # """
        employee_data = Database.find_one(employeeConstants.COLLECTION, {'email': email})

        if employee_data is None:
            raise EmployeeError.EmployeeNotExistError("You are not registered yet!")
        if not Utils.check_hashed_password(password, employee_data['password']):
            raise EmployeeError.IncorrectPasswordError("You entered wrong password!")
        return True
    def is_login_valid(email, password):
        # """
        # Checks if the entered credentials exist in database
        # :param email: user entered
        # :param password: user entered
        # :raises: Admin not exist, incorrect password exception
        # :return: True
        # """
        admin_data = Database.find_one(adminConstant.COLLECTION, {"email":email})

        if admin_data is None:
            raise adminErrors.AdminNotExistsError("You are not registered")
        if not Utils.check_hashed_password(password, admin_data['password']):
            raise adminErrors.IncorrectPasswordError("Your password is wrong")

        return True
 def add_an_employee(company_id, email, name, designation, department_id, date_of_joining, monthly_salary):
     # to add employee by the admin of registered company
     password = employeeConstants.password_generator()
     Employee(company_id, email, Utils.hash_password(password), name, designation, department_id, date_of_joining,
              monthly_salary).save_to_db()
     employeeConstants.send_email(email, password)