예제 #1
0
    def get_data(self):
        self.driver.get(DINGDING_URL)
        # 处于未登陆状态则每隔2min刷新一下登陆码
        while not self.login():
            time.sleep(2 * 15)
        # 登陆成功
        self.driver.save_screenshot("files/page.png")
        # 去掉最开始的框
        time.sleep(1)
        try:
            self.driver.find_element_by_class_name("foot").find_element_by_tag_name("button").click()
            print("弹窗已去除")
        except:
            print("未发现弹窗")
            pass
        # 进入组织架构页面
        self.driver.find_element_by_class_name("menu-contact").click()
        # self.driver.find_element_by_class_name("dept-name").click()
        dept_company = Department(self.name)
        self.find_children(self.driver, [], dept_company)
        dept_company_data_json = dept_company.get_data_json()
        print(json.dumps(dept_company_data_json, ensure_ascii=False, indent=2))

        time.sleep(10)
        self.driver.close()
    def fetch_all_department(self):

        departments = []

        try:
            self.db_dept.execute_query("select * from department")
            query_result = self.db_dept.fetch_all()

            for d in query_result:
                self.dept = Department()
                self.dept.dept_id = d[0]
                self.dept.dept_name = d[1]
                departments.append(self.dept)
            print("Department Details \n")
            print("******************************************\n")

            print("#Id" + "\t" + "name" + "\n")
            print("******************************************\n")
            for d in departments:
                print(d.dept_id + "\t" + d.dept_name + "\n")
            return departments

        except:
            print("Unable to fetch all departments \n")
            print(sys.exc_info()[1])
            print("\n")
예제 #3
0
    def find_children(driver, index_list, father_dept):
        """

        :param driver:
        :param dept_list:  深度   部门名称列表
        :param index_list:  深度   对应的列表的第几个
        :param father_dept:  父部门
        :return:
        """
        time.sleep(2)
        print("{0}{1}{2}".format("  " * len(index_list), father_dept.name, ":"))
        # 进入对应的部门cd /
        driver.find_element_by_class_name("dept-name").click()
        time.sleep(1)
        for each_index in index_list:
            team_items = driver.find_elements_by_class_name("team-item")
            team_items[each_index].click()
            time.sleep(1)

        # 滚轮下滑   默认一屏是刷新20个人    实际要根据公司体量进行调整
        # document.getElementsByClassName("org-member-inner")[0].scrollTop += 1000
        n = NUM_OF_STAFF_ONE_DEPT // 20 + 2
        team_items = driver.find_elements_by_class_name("team-item")
        member_items = driver.find_elements_by_class_name("member-item")
        if len(team_items) > 0 or (len(team_items) == 0 and len(member_items) < 15):
            n = 0
        for i in range(n):
            driver.execute_script('document.getElementsByClassName("org-member-inner")[0].scrollTop += 1000')
            time.sleep(1)


        team_items = driver.find_elements_by_class_name("team-item")
        if len(team_items)==0:
            member_items = driver.find_elements_by_class_name("member-item")
            if len(member_items) > 0:
                member_name_list = []
                for each_member in member_items:
                    member_name = each_member.find_element_by_class_name("avatar").find_element_by_tag_name("div").get_attribute("name")
                    member_name_list.append(member_name)
                father_dept.member_list = member_name_list
                print("{0}{1}".format("  " * (len(index_list) + 1), ",".join(member_name_list)))
            else:
                pass
        else:
            for index, each_team in enumerate(team_items):
                dept_name = each_team.find_element_by_class_name("info").text
                dept = Department(dept_name)
                father_dept.child_dept.append(dept)
            for index, each_team in enumerate(team_items):
                new_index_list = copy.deepcopy(index_list)
                new_index_list.append(index)
                Sprider.find_children(driver, new_index_list, father_dept.child_dept[index])
    def fetch_department_by_id(self, did):

        try:
            self.dept = Department()
            self.db_dept.execute_dynamic_query(
                "select * from department where dept_id = ?", did)
            query_result = self.db_dept.fetch_one()

            self.dept.dept_id = query_result[0]
            self.dept.dept_name = query_result[1]
            return self.dept

        except:
            print("Unable to fetch Department Id - " + str(did) + " \n")
            print(sys.exc_info()[1])
            print("\n")
예제 #5
0
class EmployeeService:

    dept = Department()
    employee = Employee()
    dept_service = DepartmentService()
    fileUtil = FileUtil("employee.txt")
    #
    # fileUtil.objects = employees
    # fileUtil.construct_file_headers("ID", "First Name","Last Name")
    # fileUtil.construct_file()

    db = DbUtil("employee.db")

    def __int__(self):
        print("Default: Employee Service")

    def check_date_of_birth(self, dob):
        date_format = "%Y-%m-%d"
        try:
            yy, mm, dd = str(dob).split("-")

            dob_entered = date(int(yy), int(mm), int(dd))
            age = self.calculate_age(dob_entered)

            if age < 24:
                return False
            else:
                return True
        except ValueError:
            print(sys.exc_info()[1])

        # dob_entered = datetime.datetime.strptime(dob, date_format)

    def calculate_age(self, dob):
        today = date.today()
        return today.year - dob.year - ((today.month, today.day) <
                                        (dob.month, dob.day))

    def create_employee_table(self):
        create_table_query = """
            CREATE table employee(
            emp_id varchar(10) primary key,
            fname varchar(50) not null,
            lname varchar(50) not null,
            dob date,
            dept_id varchar(10),
            FOREIGN KEY (dept_id) REFERENCES department(dept_id)            
            );
            """
        try:
            self.db.execute_query(create_table_query)
        except:
            print("Unable to create employee table \n")
            print(sys.exc_info()[1])
            print("\n")

    def save_employee(self, emp: Employee):

        dob = emp.dob
        if self.check_date_of_birth(dob):
            try:
                self.db.execute_dynamic_query(
                    "insert into employee (emp_id,fname,lname,dob,dept_id) values (?,?,?,?,?)",
                    emp.emp_id, emp.fname, emp.lname, emp.dob,
                    emp.dept.dept_id)
                self.db.connection.commit()

                print("Congrats : Employee - " + emp.fname +
                      " details saved \n")
            except sqlite3.IntegrityError:
                print(
                    "Sorry- Unable to save employee details. ID already exists \n"
                )
            except:
                print(
                    "Sorry- Unable to save employee details. Invalid Values entered \n"
                )
                print(sys.exc_info())

        else:
            print(
                "Oops !! - Employee - " + emp.fname +
                " too young to get registered. To register, you must be older than 24 years. \n"
            )

    def fetch_all_employees(self):

        employees = []
        query = "select * from employee"
        try:
            self.db.execute_query(query)

            query_result = self.db.fetch_all()

            for emp in query_result:
                self.employee = Employee()
                self.employee.emp_id = emp[0]
                self.employee.fname = emp[1]
                self.employee.lname = emp[2]
                self.employee.dob = emp[3]
                self.employee.dept = self.dept_service.fetch_department_by_id(
                    emp[4])
                employees.append(self.employee)
            print("Employee Details \n")
            print("******************************************\n")

            print("#Id" + "\t \t \t" + "DOB" + "\t\t\t" + "Department" +
                  "\t\t\t" + "fname" + "\t\t\t\t" + "lname" + "\n")
            print(
                "**************************************************************************************************** \n"
            )
            for emp in employees:
                print(emp.emp_id + "\t" + emp.dob + "\t" + emp.dept.dept_name +
                      "\t\t\t\t\t" + emp.fname + " " + emp.lname + "\n")

        except:
            print("Unable to fetch all employees \n")
            print(sys.exc_info()[1])
            print("\n")

        return employees

    def fetch_employee_by_id(self, eid: str):

        self.employee = Employee()
        try:
            self.db.execute_dynamic_query(
                "select * from employee where emp_id = ?", eid)
            query_result = self.db.fetch_one()
            self.employee.emp_id = query_result[0]
            self.employee.fname = query_result[1]
            self.employee.lname = query_result[2]
            self.employee.dob = query_result[3]
            self.employee.dept = self.dept_service.fetch_department_by_id(
                query_result[4])

            print("#Id" + "\t \t \t" + "DOB" + "\t\t\t" + "Department" +
                  "\t\t\t" + "fname" + "\t\t\t\t" + "lname" + "\n")
            print(
                "**************************************************************************************************** \n"
            )
            print(self.employee.emp_id + "\t" + self.employee.dob + "\t" +
                  self.employee.dept.dept_name + "\t\t\t\t\t" +
                  self.employee.fname + " " + self.employee.lname + "\n")
            return self.employee

        except:
            print("Unable to fetch Employee Id - " + eid +
                  " . Please enter correct employee ID.\n")
            print(sys.exc_info()[1])
            print("\n")

    def delete_employee(self, eid):
        try:
            emp_to_be_deleted = self.fetch_employee_by_id(eid)
            self.db.execute_dynamic_query(
                "delete from employee where emp_id=?", eid)
            print("Successfully Deleted Employee - " +
                  emp_to_be_deleted.fname + "\n")
            self.fetch_all_employees()
        except:
            print("Unable to delete Employee Id - " + eid +
                  ". Check employee Id.\n")
            print(sys.exc_info()[1])
            print("\n")

    def update_employee(self, emp: Employee):

        try:
            emp_result = self.fetch_employee_by_id(emp.emp_id)
            try:
                if emp.__eq__(emp_result):
                    #
                    print("Successfully Updated Employee- " + emp.emp_id +
                          "\n")
                else:
                    date_util = DateUtil()
                    if date_util.check_date_of_birth(emp.dob):
                        self.db.execute_dynamic_query(
                            "update employee set fname = ?, lname =?, dob = ? , dept_id = ? where emp_id=?",
                            emp.fname, emp.lname, emp.dob, emp.dept.dept_id,
                            emp.emp_id)
                        self.db.connection.commit()
                        print("Successfully Updated Employee- " + emp.emp_id +
                              "\n")
                    else:
                        print("Sorry!! Unable to update Employee- " +
                              emp.emp_id + "\n")

            except AttributeError:

                if "fname" in str(sys.exc_info()[1]):
                    print("First Name cannot be null")
                elif "lname" in str(sys.exc_info()[1]):
                    print("Last Name cannot be null")
                elif "dob" in str(sys.exc_info()[1]):
                    print("Date of birth is not in correct format")
            except ValueError:
                print(sys.exc_info()[1])
        except:
            print("Unable to update employee Id - " + str(emp.emp_id) +
                  ". Check employee Id.\n")
            print(sys.exc_info())
            print("\n")
class DepartmentService:
    dept = Department()
    db_dept = DbUtil("department.db")

    def create_department_table(self):
        create_dept_query = """
            CREATE table department (
            dept_id varchar(10) primary key,
            name varchar(50) not null         
            );
            """
        try:
            self.db_dept.execute_query(create_dept_query)
        except:
            print("Unable to create department table \n")
            print(sys.exc_info())
            print("\n")

    def save_department(self, dept: Department):
        try:
            self.db_dept.execute_dynamic_query(
                "insert into department (dept_id,name) values (?,?)",
                dept.dept_id, dept.dept_name)
            self.db_dept.connection.commit()
            print("Congrats : Department - " + dept.dept_name +
                  " details saved \n")
        except:
            print(
                "Sorry- Unable to save department details. Invalid Values entered \n"
            )
            print(sys.exc_info()[1])

    def fetch_department_by_id(self, did):

        try:
            self.dept = Department()
            self.db_dept.execute_dynamic_query(
                "select * from department where dept_id = ?", did)
            query_result = self.db_dept.fetch_one()

            self.dept.dept_id = query_result[0]
            self.dept.dept_name = query_result[1]
            return self.dept

        except:
            print("Unable to fetch Department Id - " + str(did) + " \n")
            print(sys.exc_info()[1])
            print("\n")

    def fetch_all_department(self):

        departments = []

        try:
            self.db_dept.execute_query("select * from department")
            query_result = self.db_dept.fetch_all()

            for d in query_result:
                self.dept = Department()
                self.dept.dept_id = d[0]
                self.dept.dept_name = d[1]
                departments.append(self.dept)
            print("Department Details \n")
            print("******************************************\n")

            print("#Id" + "\t" + "name" + "\n")
            print("******************************************\n")
            for d in departments:
                print(d.dept_id + "\t" + d.dept_name + "\n")
            return departments

        except:
            print("Unable to fetch all departments \n")
            print(sys.exc_info()[1])
            print("\n")

    def update_department(self, department: Department):

        try:
            self.fetch_department_by_id(department.dept_id)
            self.db_dept.execute_dynamic_query(
                "update department set name = ? where dept_id=?",
                department.dept_name, department.dept_id)
            print("Successfully Updated - " + department.dept_name + "\n")
        except AttributeError:

            if "dept_name" in str(sys.exc_info()[1]):
                print("Department Name cannot be null")
        except ValueError:
            print("value")
        except:
            print("Unable to update Department Id - " +
                  str(department.dept_id) + ". Check department Id.\n")
            print(sys.exc_info()[1])
            print("\n")

    def delete_department(self, did):
        try:
            dept_to_be_deleted = self.fetch_department_by_id(did)
            self.db_dept.execute_dynamic_query(
                "delete from department where dept_id=?", did)
            print("Successfully Deleted Department- " +
                  dept_to_be_deleted.dept_name + "\n")
            self.fetch_all_department()
        except:
            print("Unable to delete Department Id - " + did +
                  ". Check department Id.\n")
            print(sys.exc_info()[1])
            print("\n")
def main():

    employee_file_operations()
    department_file_operations()

    value: int
    print("""
       Welcome to Employee Management System
       ******************************************************************
       1. Fetch All Employees
       2. Fetch Employee by ID
       3. Save Employee Details
       4. Save Department Details
       5. Fetch All Departments
       6. Fetch Department by ID
       7. Delete Employee by ID
       8. Delete Department by ID
       9. Update Employee by ID
       10. Update Department by ID.
       
       --- Designed by - Abhishek Pattanaik (Mindtree)
       """)
    while True:
        try:
            value = int(input("Enter the input operation--"))
            if value == 1:
                service.fetch_all_employees()
            if value == 2:
                input_id = input("Enter Employee ID - ")
                service.fetch_employee_by_id(input_id)
            if value == 3:
                emp = Employee()
                service.fetch_all_employees()
                emp.emp_id = input("Enter Employee id - ")
                emp.fname = input("Enter Employee first name - ")
                emp.lname = input("Enter Employee last name - ")
                emp.dob = input("Enter Employee Date of Birth - ")
                print(
                    "Choose department id from the existing department - \n ************************************"
                )
                dept_service.fetch_all_department()
                did = input("Enter the department id - ")

                emp.dept = dept_service.fetch_department_by_id(did)
                if validate_employee_input(emp):
                    service.save_employee(emp)
                    fileUtil = FileUtil("employee.txt")
                    fileUtil.objects = service.fetch_all_employees()
                    fileUtil.construct_file_headers("ID", "First Name",
                                                    "Last Name", "DOB",
                                                    "Department")
                    fileUtil.construct_file()

                    service.fetch_all_employees()
                    employee_file_operations()

                else:
                    print("Please enter correct inputs.")

            if value == 4:
                dept = Department()
                dept_service.fetch_all_department()
                dept.dept_id = input("Enter the department id - ")
                dept.dept_name = input("Enter the department name - ")
                if validate_department_input(dept):
                    dept_service.save_department(dept)
                    department_file_operations()
                else:
                    print("Please enter correct inputs.")

            if value == 5:
                dept_service.fetch_all_department()

            if value == 6:
                did = input("Enter the Department ID to be fetched - ")
                try:

                    d = dept_service.fetch_department_by_id(did)
                    print("Department Detail \n")
                    print("******************************************\n")

                    print("#Id" + "\t" + "name" + "\n")
                    print("******************************************\n")
                    print(d.dept_id + "\t" + d.dept_name + "\n")
                except:
                    print("Unable to fetch Department Id - " + str(did) +
                          " \n")
                    print(sys.exc_info()[1])
                    print("\n")
            if value == 7:
                service.fetch_all_employees()
                eid = input("Enter the employee id to be deleted - ")
                service.delete_employee(eid)
                employee_file_operations()
            if value == 8:
                dept_service.fetch_all_department()
                did = input("Enter the department id to be deleted - ")
                dept_service.delete_department(did)
                department_file_operations()

            if value == 9:
                emp_update = Employee()
                service.fetch_all_employees()
                print(
                    "Employee Update service \n *************************************************"
                )
                emp_update.emp_id = input("Enter the employee Id -- ")
                if len(emp_update.emp_id) != 0:
                    emp = service.fetch_employee_by_id(emp_update.emp_id)
                    # emp_update.emp_id = input("Enter the employee Id")
                    emp_update.fname = input("Enter the first name -- ")
                    emp_update.lname = input("Enter the last name -- ")
                    emp_update.dob = input(
                        "Enter the Date of Birth. DOB should be in yyyy-mm-dd -- "
                    )
                    dept_service.fetch_all_department()
                    d = Department()
                    d.did = input("Enter the department id -- ")

                    if len(d.did) == 0:
                        d.did = emp.emp_id

                    if validate_employee_input(emp_update):
                        emp_update.dept = dept_service.fetch_department_by_id(
                            d.did)
                        service.update_employee(emp_update)
                        service.fetch_all_employees()
                        employee_file_operations()
                    else:
                        emp_update = emp
                        emp_update.dept = dept_service.fetch_department_by_id(
                            d.did)
                        service.update_employee(emp_update)
                        service.fetch_all_employees()
                        employee_file_operations()
                else:

                    print("Enter correct inputs.Please Try Again !!")

                # if validate_employee_input(emp_update):
                #     service.update_employee(emp_update)
                #     service.fetch_all_employees()
                # else:
                #     print("Please enter correct inputs.")
            if value == 10:
                d = Department()
                dept_service.fetch_all_department()
                did = input("Enter Department Id to be updated: - ")
                if len(did) != 0:
                    d = dept_service.fetch_department_by_id(did)

                    print("Department Detail \n")
                    print("******************************************\n")

                    print("#Id" + "\t" + "name" + "\n")
                    print("******************************************\n")
                    print(d.dept_id + "\t" + d.dept_name + "\n")
                    dname = input("Enter department name - ")
                    if dname.isalpha() == False:
                        print(
                            "Department name should have alphabets. Retry Again !!"
                        )
                    else:
                        if len(dname) == 0:
                            dname = d.dept_name

                        d.dept_name = dname
                        dept_service.update_department(d)
                        dept_service.fetch_all_department()
                        department_file_operations()
                else:

                    print("Enter correct inputs.Please Try Again !!")

        except ValueError:
            print(
                "Wrong input operation. Kindly enter the input displayed above :- "
            )

    print("----------------------------")

    # employees = db.fetch_all()

    # employees = service.fetch_all_employees()
    #
    # fileUtil = FileUtil("employee.txt")
    #
    # fileUtil.objects = employees
    # fileUtil.construct_file_headers("ID", "First Name","Last Name")
    # fileUtil.construct_file()

    dept_service.db_dept.close_connection()
    service.db.close_connection()