Beispiel #1
0
def update_user(id, body):  # noqa: E501
    """Updated user

    This can only be done by with write:users role. # noqa: E501

    :param id: The id of user that needs to be updated
    :type id: int
    :param body: Updated user object
    :type body: dict | bytes

    :rtype: UserSafe
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.UserNotFoundError(id=id), 404
    if connexion.request.is_json:
        body = UserSafe.from_dict(connexion.request.get_json())  # noqa: E501
    # check username already exists
    duplicate = dao.get_by_name(body.username)
    if duplicate is not None and duplicate.id != found.id:
        return ErrorApiResponse.UsernameExistError(body.username), 409
    # check email already exists
    duplicate = dao.get_by_email(body.email)
    if duplicate is not None and duplicate.id != found.id:
        return ErrorApiResponse.UseremailExistError(body.email), 409

    found.username = body.username
    found.email = body.email
    found.roles = body.roles if body.roles else ''
    try:
        dao.persist(found)
        return get_user_by_id(id)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex), 500
Beispiel #2
0
def patch_user(id, body):  # noqa: E501
    """Updated user password

    This can only be done by with write:users role. # noqa: E501

    :param id: The id of user that needs to be updated
    :type id: int
    :param body: Updated user object
    :type body: dict | bytes

    :rtype: UserSafe
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.UserNotFoundError(id=id), 404
    if connexion.request.is_json:
        body = User.from_dict(connexion.request.get_json())  # noqa: E501

    # update only password
    found.password = auth.generate_password_hash(body.password)
    try:
        dao.persist(found)
        return get_user_by_id(id)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex), 500
Beispiel #3
0
def create_user(body):  # noqa: E501
    """Create user

    Role write:users role must be granted # noqa: E501

    :param body: Created user object
    :type body: dict | bytes

    :rtype: UserSafe
    """
    if connexion.request.is_json:
        body = User.from_dict(connexion.request.get_json())  # noqa: E501
    orm = User_orm(username=body.username,
                   password=auth.generate_password_hash(body.password),
                   email=body.email,
                   roles=body.roles if body.roles else '')
    # check username already exists
    found = dao.get_by_name(body.username)
    if found is not None:
        return ErrorApiResponse.UsernameExistError(body.username), 409
    # check email already exists
    found = dao.get_by_email(body.email)
    if found is not None:
        return ErrorApiResponse.UseremailExistError(body.email), 409
    try:
        dao.persist(orm)
        return get_user_by_name(body.username)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex), 500
def add_employee(body):  # noqa: E501
    """Add a new employee to the system. Role write:employees must be granted

     # noqa: E501

    :param body: Employee object that needs to be added to the system
    :type body: dict | bytes

    :rtype: Employee
    """
    if connexion.request.is_json:
        body = Employee.from_dict(connexion.request.get_json())  # noqa: E501
    # check email already exists
    found = dao.find_by_email(body.email)
    if found is not None:
        return ErrorApiResponse.EmployeeEmailExistError(body.email), 409
    # check for team number
    team = team_dao.get(body.team_id)
    if team is None:
        return ErrorApiResponse.TeamNotFoundError(id=body.team_id), 404
    # persist new employee
    orm = Employee_orm(
        full_name=body.full_name,
        position=body.position,
        specialization=body.specialization if body.specialization else '',
        team_id=body.team_id,
        expert=body.expert,
        email=body.email)
    try:
        dao.persist(orm)
        return find_employee_by_email(body.email)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
def update_team_by_id(teamId, body):  # noqa: E501
    """Updates a team in the system with form data. Role write:teams must be granted

     # noqa: E501

    :param teamId: ID of team to return
    :type teamId: int
    :param body: Team object that needs to be updated in the system
    :type body: dict | bytes

    :rtype: Team
    """
    found = dao.get(teamId)
    if found is None:
        return ErrorApiResponse.TeamNotFoundError(id=teamId), 404
    if connexion.request.is_json:
        body = Team.from_dict(connexion.request.get_json())  # noqa: E501
    # check team already exists by name
    duplicate = dao.find_by_name(name=body.name)
    if duplicate is not None and duplicate.team_id != teamId:
        return ErrorApiResponse.TeamExistError(body.name), 409
    found.name = body.name
    try:
        dao.persist(found)
        return get_team_by_id(teamId)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='team'), 500
Beispiel #6
0
def update_total_days_by_id(id, body):  # noqa: E501
    """Updates a TotalDays in the system with form data. Role write:total_days must be granted

     # noqa: E501

    :param id: ID of TotalDays to return
    :type id: int
    :param body: TotalDays object that needs to be updated in the system
    :type body: dict | bytes

    :rtype: TotalDays
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.TotalDaysNotFoundError(id=id), 404
    if connexion.request.is_json:
        body = TotalDays.from_dict(connexion.request.get_json())  # noqa: E501
    employee = employee_dao.get(body.employee_id)
    if employee is None:
        return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404
    # check already exists
    duplicate = dao.find_by_year(body.employee_id, body.year)
    if duplicate is not None and duplicate.id != id:
        return ErrorApiResponse.TotalDaysExistError(body.employee_id,
                                                    body.year), 409
    found.employee_id = body.employee_id
    found.year = body.year
    found.total_days = body.total_days
    try:
        dao.persist(found)
        return get_total_days_by_id(id)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
Beispiel #7
0
def add_total_days(body):  # noqa: E501
    """Add a employee total days to the system. Role write:total_days must be granted

     # noqa: E501

    :param body: TotalDays object that needs to be added to the system
    :type body: dict | bytes

    :rtype: TotalDays
    """
    if connexion.request.is_json:
        body = TotalDays.from_dict(connexion.request.get_json())  # noqa: E501
    # check already exists
    found = dao.find_by_year(body.employee_id, body.year)
    if found is not None:
        return ErrorApiResponse.TotalDaysExistError(body.employee_id,
                                                    body.year), 409
    employee = employee_dao.get(body.employee_id)
    if employee is None:
        return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404
    orm = TotalDays_orm(employee_id=body.employee_id,
                        total_days=body.total_days,
                        year=body.year)
    try:
        dao.persist(orm)
        return find_total_days_by_year(body.employee_id, body.year)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
def update_leave_days_by_id(id, body):  # noqa: E501
    """Updates a LeaveDays in the system with form data. Role write:leave_days must be granted

     # noqa: E501

    :param id: ID of LeaveDays to return
    :type id: int
    :param body: LeaveDays object that needs to be updated in the system
    :type body: dict | bytes

    :rtype: LeaveDays
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.LeaveDaysNotFoundError(id=id), 404
    if connexion.request.is_json:
        body = LeaveDays.from_dict(connexion.request.get_json())  # noqa: E501
    delta = body.end_date - body.start_date
    if delta.days < 0:
        return ErrorApiResponse.LeaveDaysPeriodError(body.start_date,
                                                     body.end_date), 409
    # check already exists
    duplicate = find_leave_days_by(body.employee_id, body.start_date,
                                   body.end_date)
    if len(duplicate) > 1 or (len(duplicate) == 1 and duplicate[0].id != id):
        return ErrorApiResponse.LeaveDaysExistError(body.employee_id,
                                                    body.start_date,
                                                    body.end_date), 409
    # check employee exist
    employee = employee_dao.get(body.employee_id)
    if employee is None:
        return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404
    # check rules
    conflict = rules.check_rules(body.employee_id, body.start_date,
                                 body.end_date, id)
    if len(conflict) > 0:
        return ApiResponseConflict(code=5000,
                                   type='leave days',
                                   message='Restrictions are violated',
                                   details=conflict), 409
    found.employee_id = body.employee_id
    found.start_date = body.start_date
    found.end_date = body.end_date
    found.leave_days = body.leave_days
    try:
        dao.persist(found)
        return get_leave_days_by_id(id)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def find_leave_days_by(employee_id=None,
                       start_date=None,
                       end_date=None,
                       year=None):  # noqa: E501
    """Finds LeaveDays by given parameters

     # noqa: E501

    :param employee_id: Employee id to filter by
    :type employee_id: int
    :param start_date: Start date to filter by
    :type start_date: str
    :param end_date: End date to filter by
    :type end_date: str
    :param year: Year to filter by
    :type year: int

    :rtype: List[LeaveDays]
    """
    try:
        if start_date and isinstance(start_date, str):
            start_date = util.deserialize_date(start_date)
        if end_date and isinstance(end_date, str):
            end_date = util.deserialize_date(end_date)
        return [
            to_dto(elem)
            for elem in dao.find_by(employee_id, start_date, end_date, year)
        ]
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def find_employees_by(full_name=None,
                      position=None,
                      specialization=None,
                      expert=None,
                      team_id=None,
                      email=None):  # noqa: E501
    """Finds Employees by given parameters

     # noqa: E501

    :param full_name: Full name template to filter by
    :type full_name: str
    :param position: Position template to filter by
    :type position: str
    :param specialization: Specialization template to filter by
    :type specialization: str
    :param expert: Expert mark to filter by
    :type expert: bool
    :param team_id: Team number to filter by
    :type team_id: int
    :param email: Email template to filter by
    :type email: str

    :rtype: List[Employee]
    """
    try:
        return [
            to_employee_dto(elem) for elem in dao.find_by(
                full_name, position, specialization, expert, team_id, email)
        ]
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
Beispiel #11
0
def find_employee_total_days(employeeId):  # noqa: E501
    """Finds TotalDays by employee

     # noqa: E501

    :param employeeId: Employee id to filter by
    :type employeeId: int

    :rtype: List[TotalDays]
    """
    employee = employee_dao.get(employeeId)
    if employee is None:
        return ErrorApiResponse.EmployeeNotFoundError(employeeId), 404
    try:
        return [to_dto(elem) for elem in dao.find_by(employeeId)]
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
def delete_team(teamId):  # noqa: E501
    """Deletes an team. Role write:teams must be granteds

     # noqa: E501

    :param teamId: Team id to delete
    :type teamId: int

    :rtype: ApiResponse
    """
    found = dao.get(teamId)
    if found is None:
        return ErrorApiResponse.TeamNotFoundError(id=teamId), 404
    try:
        dao.delete(found)
        return 'Successful operation', 204
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='team'), 500
def delete_employee(employeeId):  # noqa: E501
    """Deletes an employee. Role write:employees must be granteds

     # noqa: E501

    :param employeeId: Employee id to delete
    :type employeeId: int

    :rtype: ApiResponse
    """
    found = dao.get(employeeId)
    if found is None:
        return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404
    try:
        dao.delete(found)
        return 'Successful operation', 204
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
Beispiel #14
0
def delete_user(id):  # noqa: E501
    """Delete user

    This can only be done with write:users role. # noqa: E501

    :param id: The id of user that needs to be deleted
    :type id: int

    :rtype: ApiResponse
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.UserNotFoundError(id=id), 404
    try:
        dao.delete(found)
        return 'Successful operation', 204
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex), 500
def delete_leave_days(id):  # noqa: E501
    """Deletes a LeaveDays. Role write:leave_days must be granteds

     # noqa: E501

    :param id: LeaveDays id to delete
    :type id: int

    :rtype: ApiResponse
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.LeaveDaysNotFoundError(id=id), 404
    try:
        dao.delete(found)
        return 'Successful operation', 204
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
Beispiel #16
0
def get_user_by_name(username):  # noqa: E501
    """Get user by user name. Role read:users role must be granted

     # noqa: E501

    :param username: The name of user that needs to be fetched. 
    :type username: str

    :type username: str

    :rtype: UserSafe
    """
    if not username:
        return ErrorApiResponse.NoUsernameError(), 400
    found = dao.get_by_name(username)
    if found is None:
        return ErrorApiResponse.UserNotFoundError(username=username), 404
    return to_user_dto(found)
def add_leave_days(body):  # noqa: E501
    """Add a employee LeaveDays to the system. Role write:leave_days must be granted

     # noqa: E501

    :param body: LeaveDays object that needs to be added to the system
    :type body: dict | bytes

    :rtype: LeaveDays
    """
    if connexion.request.is_json:
        body = LeaveDays.from_dict(connexion.request.get_json())  # noqa: E501
    delta = body.end_date - body.start_date
    if delta.days < 0:
        return ErrorApiResponse.LeaveDaysPeriodError(body.start_date,
                                                     body.end_date), 409
    # check already exists
    found = dao.find_by(body.employee_id, body.start_date, body.end_date, None)
    if len(found) > 0:
        return ErrorApiResponse.LeaveDaysExistError(body.employee_id,
                                                    body.start_date,
                                                    body.end_date), 409
    # check employee exist
    found = employee_dao.get(body.employee_id)
    if found is None:
        return ErrorApiResponse.EmployeeNotFoundError(body.employee_id), 404

    # check rules FIXME
    conflict = rules.check_rules(body.employee_id, body.start_date,
                                 body.end_date)
    if len(conflict) > 0:
        return ApiResponseConflict(code=5000,
                                   type='leave days',
                                   message='Restrictions are violated',
                                   details=conflict), 409
    orm = LeaveDays_orm(employee_id=body.employee_id,
                        leave_days=body.leave_days,
                        start_date=body.start_date,
                        end_date=body.end_date)
    try:
        dao.persist(orm)
        return find_leave_days_by_date(body.employee_id, body.start_date)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def add_team(body):  # noqa: E501
    """Add a new team to the system. Role write:teams must be granted

     # noqa: E501

    :param body: Team object that needs to be added to the system
    :type body: dict | bytes

    :rtype: Team
    """
    if connexion.request.is_json:
        body = Team.from_dict(connexion.request.get_json())  # noqa: E501
    # check team already exists by name
    found = dao.find_by_name(body.name)
    if found is not None:
        return ErrorApiResponse.TeamExistError(body.name), 409
    try:
        dao.persist(Team_orm(name=body.name))
        return find_team_by_name(body.name)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='team'), 500
def find_leave_days_by_date(employeeId, leave_date):  # noqa: E501
    """Finds unique LeaveDays by employee and leave date

     # noqa: E501

    :param employeeId: Employee id to filter by
    :type employeeId: int
    :param leave_date: Leave date to filter by
    :type leave_date: str

    :rtype: LeaveDays
    """
    try:
        if leave_date and isinstance(leave_date, str):
            leave_date = util.deserialize_date(leave_date)
        found = dao.find_by_date(employeeId, leave_date)
        if found is None:
            return ErrorApiResponse.LeaveDaysNotFoundError(
                employee_id=employeeId, leave_date=leave_date), 404
        return to_dto(found)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='leave days'), 500
def find_team_by(name=None):  # noqa: E501
    """Finds Teams by given parameters

     # noqa: E501

    :param name: Team name template to filter by
    :type name: str

    :rtype: List[Team]
    """
    try:
        return [to_team_dto(elem) for elem in dao.find_by(name)]
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='team'), 500
def find_team_by_name(name):  # noqa: E501
    """Finds Team by name

     # noqa: E501

    :param name: Team name to find
    :type name: str

    :rtype: Team
    """
    found = dao.find_by_name(name)
    if found is None:
        return ErrorApiResponse.TeamNotFoundError(name=name), 404
    return to_team_dto(found)
Beispiel #22
0
def get_user_by_id(id):  # noqa: E501
    """Gets user by id. Role read:users role must be granted

     # noqa: E501

    :param id: The id of user that needs to be fetched
    :type id: int

    :rtype: UserSafe
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.UserNotFoundError(id=id), 404
    return to_user_dto(found)
def get_employee_by_id(employeeId):  # noqa: E501
    """Find employee by ID

    Returns a single employee. # noqa: E501

    :param employeeId: ID of empoyee to return
    :type employeeId: int

    :rtype: Employee
    """
    found = dao.get(employeeId)
    if found is None:
        return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404
    return to_employee_dto(found)
def get_team_by_id(teamId):  # noqa: E501
    """Find team by ID

    Returns a single team. # noqa: E501

    :param teamId: ID of team to return
    :type teamId: int

    :rtype: Team
    """
    found = dao.get(teamId)
    if found is None:
        return ErrorApiResponse.TeamNotFoundError(id=teamId), 404
    return to_team_dto(found)
def find_employee_by_email(email):  # noqa: E501
    """Finds Employee by given email

     # noqa: E501

    :param email: Unique employee email
    :type email: str

    :rtype: Employee
    """
    found = dao.find_by_email(email)
    if found is None:
        return ErrorApiResponse.EmployeeNotFoundError(email=email), 404
    return to_employee_dto(found)
def get_leave_days_by_id(id):  # noqa: E501
    """Find LeaveDays by ID

    Returns a single LeaveDays. # noqa: E501

    :param id: ID of LeaveDays to return
    :type id: int

    :rtype: LeaveDays
    """
    found = dao.get(id)
    if found is None:
        return ErrorApiResponse.LeaveDaysNotFoundError(id=id), 404
    return to_dto(found)
def update_employee_by_id(employeeId, body):  # noqa: E501
    """Updates an employee in the system with form data. Role write:employees must be granted

     # noqa: E501

    :param employeeId: ID of empoyee to return
    :type employeeId: int
    :param body: Employee object that needs to be added to the system
    :type body: dict | bytes

    :rtype: Employee
    """
    found = dao.get(employeeId)
    if found is None:
        return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404
    if connexion.request.is_json:
        body = Employee.from_dict(connexion.request.get_json())  # noqa: E501
    # check email already exists
    duplicate = dao.find_by_email(body.email)
    if duplicate is not None and duplicate.employee_id != body._employee_id:
        return ErrorApiResponse.EmployeeEmailExistError(body.email), 409
    # check for team number
    team = team_dao.get(body.team_id)
    if team is None:
        return ErrorApiResponse.TeamNotFoundError(id=body.team_id), 404
    found.full_name = body.full_name
    found.position = body.position
    found.specialization = body.specialization if body.specialization else ''
    found.team_id = body.team_id
    found.expert = body.expert
    found.email = body.email
    try:
        dao.persist(found)
        return get_employee_by_id(employeeId)
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
Beispiel #28
0
def find_total_days_by(employee_id=None, year=None):  # noqa: E501
    """Finds TotalDays by given parameters

     # noqa: E501

    :param employee_id: Employee id to filter by
    :type employee_id: int
    :param year: Year to filter by
    :type year: int

    :rtype: List[TotalDays]
    """
    try:
        return [to_dto(elem) for elem in dao.find_by(employee_id, year)]
    except Exception as ex:
        return ErrorApiResponse.InternalServerError(ex, type='total days'), 500
Beispiel #29
0
def find_total_days_by_year(employeeId, year):  # noqa: E501
    """Finds unique TotalDay by employee and year

     # noqa: E501

    :param employeeId: Employee id to filter by
    :type employeeId: int
    :param year: Year to filter by
    :type year: int

    :rtype: TotalDays
    """
    found = dao.find_by_year(employeeId, year)
    if found is None:
        return ErrorApiResponse.TotalDaysNotFoundError(employee_id=employeeId,
                                                       year=year), 404
    return to_dto(found)
Beispiel #30
0
def authenticate_user(username, password):  # noqa: E501
    """Generates token for user

     # noqa: E501

    :param username: The user name for login
    :type username: str
    :param password: The password for login in clear text
    :type password: str

    :rtype: str
    """
    token = auth.generate_token(username, password)
    if token is not None:
        return token
    else:
        return ErrorApiResponse.AuthError(), 400