def department_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # TODO: Add to query: e.department, db_cursor.execute(""" SELECT d.id, d.name, d.budget FROM hrapp_department AS d ORDER BY d.name """) all_departments = [] dataset = db_cursor.fetchall() for row in dataset: department = Department() department.id = row['id'] department.name = row['name'] department.budget = row['budget'] all_departments.append(department) template = 'departments/department_list.html' context = { 'departments': all_departments } return render(request, template, context)
def create_list_employees(cursor, row): _row = sqlite3.Row(cursor, row) department = Department() department.id = _row["id"] department.name = _row["name"] department.budget = _row["budget"] department.employees = [] employee = Employee() employee.department_id = _row["department_id"] employee.first_name = _row["first_name"] employee.last_name = _row["last_name"] department_employees = {} for (department, employee) in department: if department.id not in department_employees: department_employees[department.id] = department department_employees[department.id].employees.append(employee) else: department_employees[department.id].employees.append(employee) return ( department, employee, )
def get_department(department_id): with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = create_employee db_cursor = conn.cursor() db_cursor.execute( """ SELECT d.id, d.department_name, d.budget, e.first_name, e.last_name, e.id AS e_id FROM hrapp_department d LEFT JOIN hrapp_employee e ON e.department_id = d.id WHERE d.id = ? """, (department_id, )) department_employees = db_cursor.fetchall() department = Department() department.employees = [] department.name = department_employees[0]["department_name"] for employee in department_employees: department.employees.append(employee["employee"]) return department
def department_list(request): if request.method == "GET": with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select count(department_id) as employee_count, dep.id, dep.name, dep.budget from hrapp_employee emp inner join hrapp_department dep on emp.department_id = dep.id group by department_id """) all_departments = [] dataset = db_cursor.fetchall() for row in dataset: dep = Department() dep.id = row["id"] dep.employee_count = row["employee_count"] dep.name = row["name"] dep.budget = row["budget"] all_departments.append(dep) template_name = 'departments/list.html' context = {'all_departments': all_departments} return render(request, template_name, context)
def employee_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # TODO: Add to query: e.department, db_cursor.execute(""" select e.id, e.first_name, e.last_name, e.start_date, e.is_supervisor, d.dept_name from hrapp_employee e join hrapp_department d on e.department_id = d.id """) all_employees = [] dataset = db_cursor.fetchall() for row in dataset: employee = Employee() employee.id = row['id'] employee.first_name = row['first_name'] employee.last_name = row['last_name'] employee.start_date = row['start_date'] employee.is_supervisor = row['is_supervisor'] department = Department() department.name = row['dept_name'] employee.department = department all_employees.append(employee) template = 'employees/employees_list.html' context = {'employees': all_employees} return render(request, template, context) elif request.method == 'POST': form_data = request.POST with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute( """ INSERT INTO hrapp_employee (first_name, last_name, start_date, is_supervisor, department_id) VALUES(?,?,?,?,?) """, (form_data['first'], form_data['last'], form_data['start_date'], form_data['is_supervisor'], form_data['department'])) return redirect(reverse('hrapp:employee_list'))
def create_employee(cursor, row): _row = sqlite3.Row(cursor, row) e = Employee() e.id = _row["id"] e.first_name = _row["first_name"] e.last_name = _row["last_name"] d = Department() d.name = _row["department"] e.department = d return e
def create_department(cursor, row): _row = sqlite3.Row(cursor, row) department = Department() department.id = _row["id"] department.name = _row["name"] department.budget = _row["budget"] department.employees = [] employee = Employee() employee.id = _row["id"] employee.first_name = _row["first_name"] employee.last_name = _row["last_name"] employee.department_id = _row["department_id"] return (department, employee)
def employee_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select e.id, e.first_name, e.last_name, e.is_supervisor, e.start_date, e.department_id, d.name FROM hrapp_employee e LEFT JOIN hrapp_department d ON e.department_id = d.id; """) all_employees = [] dataset = db_cursor.fetchall() for row in dataset: employee = Employee() employee.id = row['id'] employee.first_name = row['first_name'] employee.last_name = row['last_name'] employee.is_supervisor = row['is_supervisor'] employee.start_date = row['start_date'] employee.department_id = row['department_id'] department = Department() department.id = row['id'] department.name = row['name'] # department.budget = row['budget'] employee.department = department all_employees.append(employee) template = 'employees/employees_list.html' context = { 'employees': all_employees } return render(request, template, context)
def all_departments(department_id): with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute( """ SELECT d.id, d.dept_name, d.budget, e.id, e.first_name, e.last_name, e.department_id FROM hrapp_department d LEFT JOIN hrapp_employee e ON d.id = e.department_id where d.id = ? """, (department_id, )) data = db_cursor.fetchall() department_list = [] employee_list = [] for row in data: if (len(department_list) == 0): department = Department() department.id = row['department_id'] department.name = row['dept_name'] department.budget = row['budget'] department_list.append(department) employee = Employee() employee.id = row['id'] employee.first_name = row['first_name'] employee.last_name = row['last_name'] employee.department_id = row['department_id'] employee_list.append(employee) department_obj = {} department_obj["department"] = department_list[0] department_obj["employees"] = employee_list return department_obj
def create_employee(cursor, row): _row = sqlite3.Row(cursor, row) e = Employee() e.id = _row["id"] e.first_name = _row["first_name"] e.last_name = _row["last_name"] d = Department() d.name = _row["department"] c = Computer() c.id = _row["computer_id"] c.make = _row["computer"] e.department = d e.computer = c return e
def get_department(department_name): with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute( """ SELECT d.name, d.budget, e.first_name, e.last_name, e.is_supervisor FROM hrapp_department AS d LEFT JOIN hrapp_employee AS e ON e.department_id = d.id WHERE d.name = ? """, (department_name, )) response = db_cursor.fetchall() for row in response: if row == response[0]: department = Department() department.employees = [] department.name = row['name'] department.budget = row['budget'] if not row['first_name'] == None: employee = Employee() employee.first_name = row['first_name'] employee.last_name = row['last_name'] employee.is_supervisor = row['is_supervisor'] department.employees.append(employee) else: employee = Employee() employee.first_name = row['first_name'] employee.last_name = row['last_name'] employee.is_supervisor = row['is_supervisor'] department.employees.append(employee) print(department.employees) return department
def department_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT d.id, d.dept_name, d.budget, e.id AS 'employee_id' FROM hrapp_department d LEFT JOIN hrapp_employee e ON d.id = e.department_id """) all_departments = [] dataset = db_cursor.fetchall() for row in dataset: department = Department() department.id = row['id'] department.name = row['dept_name'] department.budget = row['budget'] if(department not in all_departments): all_departments.append(department) department_sizes = dict() for row in dataset: if(row['dept_name'] not in department_sizes): name = row['dept_name'] department_sizes[name] = 0 for row in dataset: if(row['employee_id'] is not None): name = row['dept_name'] department_sizes[name] += 1 for department in all_departments: department.size = department_sizes[department.name] template = 'departments/department_list.html' context = { 'departments': all_departments } return render(request, template, context) elif request.method == 'POST': form_data = request.POST with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute(""" INSERT into hrapp_department ( dept_name, budget ) VALUES (?, ?) """, (form_data['dept_name'], form_data['budget'])) return redirect(reverse('hrapp:department_list'))
def department_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT d.id, d.name, d.budget, e.id AS 'employee_id' FROM hrapp_department d LEFT JOIN hrapp_employee e ON d.id = e.department_id """) all_departments = [] dataset = db_cursor.fetchall() # creates a list of all unique department objects for row in dataset: department = Department() department.id = row['id'] department.name = row['name'] department.budget = row['budget'] if (department not in all_departments): all_departments.append(department) # creates a dictionary of all unique department names with values representing employee count department_sizes = dict() for row in dataset: if (row['name'] not in department_sizes): name = row['name'] department_sizes[name] = 0 # and here we set the employee count for each department for row in dataset: if (row['employee_id'] is not None): name = row['name'] department_sizes[name] += 1 # transfer the department sizes to the department objects in all_departments for department in all_departments: department.size = department_sizes[department.name] if request.user.is_authenticated: template = 'departments/department_list.html' else: template = 'departments/department_list_view_only.html' context = {'departments': all_departments} return render(request, template, context) elif request.method == 'POST': form_data = request.POST with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute( """ INSERT into hrapp_department ( name, budget ) VALUES (?, ?) """, (form_data['name'], form_data['budget'])) return redirect(reverse('hrapp:department_list'))