Exemplo n.º 1
0
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 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_department(cursor, row):
    _row = sqlite3.Row(cursor, row)

    department = Department()
    department.id = _row["department_id"]
    department.dept_name = _row["dept_name"]
    department.budget = _row["budget"]

    # Note: You are adding a blank employees list to the department object
    # This list will be populated later (see below)
    department.employees = []

    employee = Employee()
    employee.id = _row["employee_id"]
    employee.first_name = _row["first_name"]
    employee.Last_name = _row["last_name"]
    employee.department_id = _row["department_id"]

    # Return a tuple containing the department and the
    # employee built from the data in the current row of
    # the data set
    return (
        department,
        employee,
    )
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)
Exemplo n.º 5
0
def department_list(request):
    """Show all departments include budget and department size"""
    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.department,
                d.budget,
                e.department_id
            from hrapp_department d
            join hrapp_employee e on  e.department_id = d.id;
            """)

            all_departments = []
            dataset = db_cursor.fetchall()

            for row in dataset:
                department = Department()
                department.id = row['id']
                department.department = row['department']
                department.budget = row['budget']
                # e.department_id = row['department_id']

                all_departments.append(department)

    template = 'departments/departments_list.html'
    context = {'departments': all_departments}

    return render(request, template, context)
def create_department(cursor, row):
    _row = sqlite3.Row(cursor, row)

    department = Department()
    department.id = _row["id"]
    department.dept_name = _row["dept_name"]
    department.budget = _row["budget"]

    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()

            # TODO: Add dept total
            db_cursor.execute("""
            select
            d.id,
            d.department_name,
            d.budget,
            count(e.id) as dept_total
            from hrapp_department d
            left join hrapp_employee e
            on d.id = e.department_id
            group by d.id
            """)

            all_departments = []
            dataset = db_cursor.fetchall()

            for row in dataset:
                department = Department()
                department.id = row['id']
                department.department_name = row['department_name']
                department.budget = row['budget']
                department.dept_total = row['dept_total']

                all_departments.append(department)

        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
            (
                department_name, budget
            )
            VALUES (?, ?)
            """, (
                    form_data['department_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 
                Count(e.id) employees,
                d.department_name,
                d.id,
                d.budget,
                e.id
            FROM hrapp_department d
            Left JOIN hrapp_employee e ON e.department_id = d.id
            GROUP BY d.department_name
                        """)

            all_departments = []
            dataset = db_cursor.fetchall()

            for row in dataset:
                department = Department()
                department.id = row['id']
                department.department_name = row['department_name']
                department.budget = row['budget']
                department.employees = row['employees']

                all_departments.append(department)

        template = 'departments/departments_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
            (
                department_name, budget
            )
            VALUES (?, ?)
            """, (form_data['department_name'], form_data['budget']))

        return redirect(reverse('hrapp:departments'))
def create_department(cursor, row):
    _row = sqlite3.Row(cursor, row)

    department = Department()
    department.id = _row["id"]
    department.dept_name = _row["dept_name"]
    department.budget = _row["budget"]
    department.employees = []

    employee = Employee()
    employee.first_name = _row["first_name"]
    employee.last_name = _row["last_name"]
    employee.is_supervisor = _row["is_supervisor"]
    employee.start_date = _row["start_date"]

    return (department, employee,)
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 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 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
Exemplo n.º 13
0
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'))