def test_get_employee_error(self):
        """ Test getting IntegrityError passing invalid id for employee."""
        with self.app.app_context():
            with self.assertRaises(Exception) as context:
                es.get(1111)

            self.assertEqual("Can't get employee with id 1111",
                             str(context.exception))
    def get(id_=None):
        """ GET method, returns certain employee by id. """

        logger.debug('Catch GET request by URL /api/employees/%i.', id_)
        try:
            employee = es.get(id_)
        except Exception:
            return {'message': f'Can\'t get employee with id {id_}.'}, 404
        return marshal_employee(employee), 200
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 test_method_post_update_employee_success(self):
     """ Tests if employee was updated and getting right status code. """
     new_name = 'Taras'
     with self.app.app_context():
         with self.app.test_request_context \
                     (method='POST', data=dict(name=new_name,
                                               birthday=date(2000, 9, 22),
                                               department_name=1,
                                               working_since=date(2020, 1,
                                                                  13),
                                               salary=30000.0)):
             response = ev.update_employee(1)
             self.assertEqual(response.status_code, 302)
         self.assertEqual(es.get(1).name, new_name)
    def put(id_=None):
        """ PUT method, updates existing employee by id. """

        logger.debug('Catch PUT request by URL /api/employees/%i.', id_)
        args = employee_args.parse_args()
        try:
            es.update(id_=id_,
                      name=args['name'],
                      birthday=args['birthday'],
                      salary=args['salary'],
                      department=args['department'],
                      working_since=args['working_since'])
        except Exception:
            return {'message': 'Can\'t update employee.'}, 404
        return marshal_employee(es.get(id_)), 200
    def post():
        """ POST method, adds new employee. """

        logger.debug('Catch POST request by URL /api/employees.')
        args = employee_args.parse_args()
        try:
            id_ = es.add(name=args['name'],
                         birthday=args['birthday'],
                         salary=args['salary'],
                         department=args['department'],
                         working_since=args['working_since'])
        except Exception:
            return {'message': "Can't post employee."}, 404

        return marshal_employee(es.get(id_)), 201
Exemple #7
0
def delete_employee(id_: int):
    """ Delete an employee. """

    logger.debug('Routed to /employees/%i/delete', id_)
    employee = None

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

    es.delete(id_)
    logger.info('Successfully deleted employee %s', employee.name)

    return redirect(url_for("employee.show_all_employees"))
Exemple #8
0
def show_employee(id_: int):
    """ Render template with the information of certain employee. """

    logger.debug('Routed to /employees/%i', id_)
    titles = ['Name', 'Birthday', 'In Department', 'Working Since', 'Salary']
    employee = None

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

    logger.info('Got employee %s', employee.name)
    return render_template('employee.html',
                           title=f'Employee {employee.name}',
                           table_title=f'Employee: {employee.name}',
                           headers=titles,
                           employee=employee)