def test_delete_data_from_tables(self):
     """ Test deleting all data from employee and department tables."""
     with self.app.app_context():
         es.delete_all()
         ds.delete_all()
         self.assertEqual(ds.get_all(), [])
         self.assertEqual(es.get_all(), [])
Exemple #2
0
def show_all_departments():
    """ Render template with the list of all departments. """

    logger.debug('Function show_all_departments(). Routed to /departments')
    titles = ['Name', 'Average Salary', 'Employees']
    departments = ds.get_all()
    logger.info('Get list of departments, length is %i', len(departments))
    return render_template('departments.html',
                           title='Departments',
                           table_title='List of Departments',
                           headers=titles,
                           departments=departments)
Exemple #3
0
def update_employee(id_: int):
    """ Render page for editing an existing employee. """
    logger.debug('Routed to /employees/%i/update', id_)

    if request.method == 'POST':
        name = request.form.get("name")
        birthday = request.form.get("birthday")
        department = int(request.form.get("department_name"))
        working_since = request.form.get("working_since")
        if not working_since:
            working_since = None
        salary = float(request.form.get("salary"))
        try:
            es.update(id_=id_,
                      name=name,
                      birthday=birthday,
                      department=department,
                      working_since=working_since,
                      salary=salary)
        except IntegrityError as exception:
            logger.error(
                'Can\'t update employee with name %s, birthday %s, '
                'department %i, salary %f and working since %s. '
                'Exception: %s', name, birthday, department, salary,
                working_since, str(exception))
            abort(404)
        except Exception:
            abort(404)

        logger.info(
            'Successfully updated employee with id {}. It\'s name %s, '
            'birthday %s, department %i, salary %f and '
            'working since %s. ', name, birthday, department, salary,
            working_since)
        return redirect(url_for("employee.show_employee", id_=id_))

    employee = None
    try:
        employee = es.get(id_)
    except IntegrityError:
        logger.error("Can't update employee with id %i", id_)
        abort(404)

    titles = ['Name', 'Birthday', 'In Department', 'Working Since', 'Salary']
    logger.info('Get employee %s', employee.name)
    return render_template('edit_employee.html',
                           title='Update employee',
                           table_title=f'Updating employee: {employee.name}',
                           headers=titles,
                           employee=employee,
                           departments=ds.get_all())
Exemple #4
0
def add_employee():
    """ Adding a new employee. """
    logger.debug('Routed to /employees/add')

    if request.method == 'POST':
        name = request.form.get("name")
        birthday = request.form.get("birthday")
        department = int(request.form.get("department"))
        working_since = request.form.get("working_since")
        if not working_since:
            working_since = None
        salary = float(request.form.get("salary"))
        try:
            es.add(name=name,
                   birthday=birthday,
                   department=department,
                   working_since=working_since,
                   salary=salary)
        except IntegrityError as exception:
            logger.error(
                'Can\'t add employee with name %s, birthday %s, '
                'department %i, salary %f and working since %s. '
                'Exception: %s', name, birthday, department, salary,
                working_since, str(exception))
            abort(404)

        logger.debug(
            'Successfully added new employee with name %s, birthday %s, '
            'department %i, salary %f and working since %s.', name, birthday,
            department, salary, working_since)
        return redirect(url_for("employee.show_all_employees"))

    titles = ['Name', 'Birthday', 'In Department', 'Working Since', 'Salary']
    return render_template('add_employee.html',
                           title='Add employee',
                           table_title='Adding new employee',
                           headers=titles,
                           departments=ds.get_all())
 def test_add_department(self):
     """ Test adding new department to database. """
     with self.app.app_context():
         ds.add(name='IT')
         self.assertEqual(len(ds.get_all()), 2)
 def test_get_all_departments(self):
     """ Test getting the list of all departments in database. """
     with self.app.app_context():
         self.assertNotEqual(ds.get_all(), [])