def create_absence_for_user(employee: Employee, time_off_type: AbsenceType = None, start_date: date = None, end_date: date = None, half_day_start: bool = False, half_day_end: bool = False, comment: str = None, create: bool = False) -> Absence: if not time_off_type: absence_types = personio.get_absence_types() time_off_type = [ absence_type for absence_type in absence_types if absence_type.name == "Unpaid vacation" ][0] if not start_date: start_date = date(2021, 1, 1) if not end_date: end_date = date(2021, 1, 10) absence_to_create = Absence(start_date=start_date, end_date=end_date, time_off_type=time_off_type, employee=employee, half_day_start=half_day_start, half_day_end=half_day_end, comment=comment) if create: absence_to_create.create(personio) return absence_to_create
def create_absence(self, absence: Absence) -> Absence: """ Creates an absence record on the Personio servers :param absence: The absence object to be created :raises PersonioError: If the absence could not be created on the Personio servers """ data = absence.to_body_params() response = self.request_json('company/time-offs', method='POST', data=data) if response['success']: absence.id_ = response['data']['attributes']['id'] return absence raise PersonioError("Could not create absence")
def create_absence(self, absence: Absence) -> bool: """ Creates an absence record on the Personio servers :param absence: The absence object to be created """ data = absence.to_body_params() response = self.request_json('company/time-offs', method='POST', data=data) if response['success']: absence.id_ = response['data']['attributes']['id'] return True return False
def test_create_absence(): mock_absence_types() mock_create_absence_no_halfdays() personio = mock_personio() absence_type = personio.get_absence_types()[0] employee = Employee(first_name="Alan", last_name='Turing', email='*****@*****.**') absence = Absence(client=personio, employee=employee, start_date=date(2020, 1, 1), end_date=date(2020, 1, 10), half_day_start=False, half_day_end=False, time_off_type=absence_type) absence.create() assert absence.id_
def test_get_absence_from_object_without_id(): personio = mock_personio() mock_get_absence() mock_single_absences() absence_id_only = Absence(id_=2628890) absence = personio.get_absence(absence_id_only) absence.id_ = None personio.get_absence(absence)
def test_get_absence_from_id(): personio = mock_personio() mock_get_absence() absence_id_only = Absence(id_=2628890) absence = personio.get_absence(absence_id_only) assert absence.employee.first_name == 'Alan' assert absence.employee.last_name == 'Turing' assert absence.id_ == 2628890 assert absence.start_date == date(2021, 1, 1) assert absence.end_date == date(2021, 1, 10)
def get_absence(self, absence: Union[Absence, int]) -> Absence: """ Get an absence record from a given id. :param absence: The absence id to fetch. """ if isinstance(absence, int): response = self.request_json(f'company/time-offs/{absence}') return Absence.from_dict(response['data'], self) else: if absence.id_: return self.get_absence(absence.id_) else: self.__add_remote_absence_id(absence) return self.get_absence(absence.id_)
def __add_remote_absence_id(self, absence: Absence) -> Absence: """ Queries the API for an absence record matching the given Absence object and adds the remote id. :param absence: The absence object to be updated :return: The absence object with the absence_id set """ if absence.employee is None: raise ValueError("For a remote query an employee_id is required") if absence.start_date is None: raise ValueError("For a remote query a start date is required") if absence.end_date is None: raise ValueError("For a remote query an end date is required") matching_remote_absences = self.get_absences(employees=[absence.employee.id_], start_date=absence.start_date, end_date=absence.end_date) if len(matching_remote_absences) == 0: raise PersonioError("The absence to patch was not found") elif len(matching_remote_absences) > 1: raise PersonioError("More than one absence found.") absence.id_ = matching_remote_absences[0].id_ return absence