def add_time_entry(self, api_key: str, workspace: Workspace, time_entry: TimeEntry): """ Parameters ---------- api_key: str Clockify Api key workspace: Workspace Get projects in this workspace time_entry: TimeEntry The time entry to add Returns ------- TimeEntry The created time entry """ result = self.api_server.post( path=f"/workspaces/{workspace.obj_id}/time-entries", api_key=api_key, data=time_entry.to_dict(), ) return TimeEntry.init_from_dict(result)
def test_time_entry(a_date): """Test with different input parameters""" # minimal parameters entry = TimeEntry(obj_id=None, start=a_date, description='test description') entry.to_dict()
def add_time_entry_object(self, api_key: str, workspace: Workspace, time_entry: TimeEntry): """Save the given TimeEntry instance to server. If TimeEntry.obj_id is set will update existing. If TimeEntry.obj_id is None will add instance as new Parameters ---------- api_key: str Clockify Api key workspace: Workspace Get projects in this workspace time_entry: TimeEntry The time entry to add Returns ------- TimeEntry The created time entry """ if time_entry.obj_id: self.api_server.put( path=f"/workspaces/{workspace.obj_id}/time-entries/" f"{time_entry.obj_id}", api_key=api_key, data=time_entry.to_dict(), ) return time_entry else: result = self.api_server.post( path=f"/workspaces/{workspace.obj_id}/time-entries", api_key=api_key, data=time_entry.to_dict(), ) return TimeEntry.init_from_dict(result)
def test_time_entry_no_project_no_task(mock_models_timezone): time_entry_dict = json.loads(POST_TIME_ENTRY_NO_PROJECT_NO_TASK.text) time_entry = TimeEntry.init_from_dict(time_entry_dict) assert time_entry.project == None assert time_entry.task == None time_entry_dict_again = TimeEntry.to_dict(time_entry) assert time_entry_dict_again["end"] == "2019-10-23T18:18:58Z"
def test_truncate(a_date): entry = TimeEntry(obj_id="123", start=a_date) assert str(entry).endswith("''") entry.description = "A short description" assert str(entry).endswith("description'") entry.description = "A longer description thats 30c" assert str(entry).endswith("thats 30c'") entry.description = "A longer description thats a lot longer then 30 characters" assert str(entry).endswith("thats ...'")
def test_time_entry_from_dict(mock_models_timezone): time_entry_dict = json.loads(POST_TIME_ENTRY.text) time_entry = TimeEntry.init_from_dict(time_entry_dict) assert time_entry.description == "testing description" time_entry_dict_again = TimeEntry.to_dict(time_entry) assert time_entry_dict_again["start"] == "2019-10-23T17:18:58Z" assert time_entry_dict_again["description"] == "testing description" assert time_entry_dict_again["projectId"] == "123456"
def test_time_entry_from_dict(mock_models_timezone): time_entry_dict = json.loads(ClockifyMockResponses.POST_TIME_ENTRY.text) time_entry = TimeEntry.init_from_dict(time_entry_dict) assert time_entry.description == 'testing description' time_entry_dict_again = TimeEntry.to_dict(time_entry) assert time_entry_dict_again['start'] == '2019-10-23T17:18:58Z' assert time_entry_dict_again['description'] == 'testing description' assert time_entry_dict_again['projectId'] == '123456'
def add_time_entry(self, start_time, end_time=None, description=None, project=None,billable='false'): """Add a time entry to default workspace. If no end time is given stopwatch mode is activated. This will stop any previously running stopwatch Parameters ---------- start_time: datetime, UTC Set start of time entry to this end_time: datetime, UTC, optional Set end of time entry to this. If not given, activate stopwatch mode. Defaults to None description: str, optional Description of this time entry. Defaults to None project: Project, optional Set the project that this time entry belongs to. Defaults to None Returns ------- TimeEntry The created time entry """ time_entry = TimeEntry(obj_id=None, start=start_time, description=description, project=project, end=end_time, billable=billable) return self.add_time_entry_object(time_entry=time_entry)
def set_active_time_entry_end( self, api_key: str, workspace: Workspace, user: User, end_time: datetime ): """Set the end time for the currently active entry Parameters ---------- api_key: str Clockify Api key workspace: Workspace Get projects in this workspace user: User The use for which to end the active time entry end_time: datetime Set the end time to this Returns ------- TimeEntry The updated time entry, if an active one was found None If there was no active time entry (if a stopwatch was not running) """ try: result = self.api_server.patch( path=f"/workspaces/{workspace.obj_id}/user/{user.obj_id}/time-entries/", api_key=api_key, data={"end": str(ClockifyDatetime(end_time))}, ) except APIServer404: return None return TimeEntry.init_from_dict(result)
def a_time_entry(a_project): return TimeEntry( obj_id=None, start=datetime.datetime( year=2019, month=10, day=12, hour=14, minute=10, second=1 ), description="test description", project=a_project, )
def test_str(a_date): """Getting coverage up""" str(User(obj_id="123", name="test")) str(Project(obj_id="123", name="test")) str(ProjectStub(obj_id="123")) str(Workspace(obj_id="123", name="test")) str(APIObject(obj_id="123")) str(NamedAPIObject(obj_id="123", name="test")) str(TimeEntry(obj_id="123", start=a_date))
def a_time_entry(a_project): api_123 = APIObjectID(obj_id='123') h_rate_10USD = HourlyRate(amount=10, currency='USD') rates_123_10USD = {api_123: h_rate_10USD} a_user = User('123', 'name', 'email', rates_123_10USD) return TimeEntry(obj_id=None, start=datetime.datetime(year=2019, month=10, day=12, hour=14, minute=10, second=1), description='test description', project=a_project, user=a_user)
def get_time_entries(self, api_key: str, workspace: Workspace, user: User, start_datetime, end_datetime): params = { 'start': ClockifyDatetime(start_datetime).clockify_datetime, 'end': ClockifyDatetime(end_datetime).clockify_datetime } response = self.api_server.get( path= f"/workspaces/{workspace.obj_id}/user/{user.obj_id}/time-entries", api_key=api_key, params=params) return [TimeEntry.init_from_dict(te) for te in response]
def get_time_entries_iterator( self, api_key: str, workspace: Workspace, user: User, query: TimeEntryQuery) -> Generator[TimeEntry, None, None]: """Get all time entries corresponding to search criteria Notes ----- This method might make multiple calls to the API if the number of results is over 50 """ iterator = self.api_server.get_iterator( path= f"/workspaces/{workspace.obj_id}/user/{user.obj_id}/time-entries", api_key=api_key, params=query.to_dict(), ) return (TimeEntry.init_from_dict(x) for x in iterator)