class BaseAPI(object): def __init__(self, server): self._browser = CurrentTimeBrowser(server) self._parser = CurrentTimeParser() def login(self, username, password): return self._browser.login(username, password) @property def current_date(self): return self._parser.parse_current_month(self._browser.current_page) def report_activity(self, activity): session_id = self._parser.parse_session_id(self._browser.current_page) if not self._is_in_correct_state(activity.day): raise ValueError("Date argument is not withing the currently displayed date.") response = self._browser.post( session_id, activity.project_id, activity.day.day, activity.duration, activity.comment ) return response def valid_session(self): return self._parser.valid_session(self._browser.current_page) def get_activities(self): return self._parser.parse_activities(self._browser.current_page) def goto_next_month(self): response = self._browser.goto_next_month() return self._parser.parse_navigation(response) def goto_prev_month(self): response = self._browser.goto_prev_month() return self._parser.parse_navigation(response) def goto_next_year(self): response = self._browser.goto_next_year() return self._parser.parse_navigation(response) def goto_prev_year(self): response = self._browser.goto_prev_year() return self._parser.parse_navigation(response) def get_projects(self): response = self._browser.get_projects() return self._parser.parse_projects(response) def _is_in_correct_state(self, date): current = self._parser.parse_current_month(self._browser.current_page) return current.year == date.year and current.month == date.month
class CurrentTimeParserTestCase(unittest.TestCase): def setUp(self): self.browser = CurrentTimeBrowser() self.parser = CurrentTimeParser() def test_that_we_get_current_month_on_login(self): current_date = self.parser.current_month(self.browser.current_page) now = datetime.datetime.now() expected_date = datetime.date(now.year, now.month, 1) self.assertEquals(expected_date, current_date) def test_that_current_date_is_previous_month_after_navigation(self): self.browser.goto_prev_month() current_date = self.parser.current_month(self.browser.current_page) now = datetime.datetime.now() expected_year = now.year expected_month = now.month - 1 if expected_month < 1: expected_month = 12 expected_year = expected_year - 1 expected_date = datetime.date(expected_year, expected_month, 1) self.assertEquals(expected_date, current_date) def test_that_current_date_is_previous_year_after_navigation(self): self.browser.goto_prev_year() current_date = self.parser.current_month(self.browser.current_page) now = datetime.datetime.now() expected_date = datetime.date(now.year - 1, now.month, 1) self.assertEquals(expected_date, current_date)
def __init__(self, server): self._browser = CurrentTimeBrowser(server) self._parser = CurrentTimeParser()
def setUp(self): self.browser = CurrentTimeBrowser() self.parser = CurrentTimeParser()
class BaseAPI(object): def __init__(self, server): self._browser = CurrentTimeBrowser(server) self._parser = CurrentTimeParser() def login(self, username, password): return self._browser.login(username, password) def valid_session(self): return self._parser.valid_session(self._browser.current_page) def get_day(self, year, month, day): start, end = self._parser._get_current_range( self._browser.current_page) current = datetime.date(year, month, day) if start <= current and current <= end: activities = self._parser.parse_activities( self._browser.current_page) return [a for a in activities if a.date == current] else: self._goto(year, month) page = self._browser._current_page command = self._parser.get_day_command(page, day) page = self._browser.get(command) return self._parser.parse_activities(page) def get_week(self, year, week): date = datetime.date(year, 1, 1) + datetime.timedelta(weeks=week) self._goto(year, date.month) page = self._browser._current_page command = self._parser.get_week_command(page, week) page = self._browser.get(command) return self._parser.parse_activities(page) def get_month(self, year, month): start, end = self._parser._get_current_range( self._browser.current_page) if start.year != year or start.month != month: self._goto(year, month) self._browser.get_current_month() start, end = self._parser._get_current_range( self._browser.current_page) if not (start.day == 1 and end.day >= 28): self._browser.get_current_month() return self._parser.parse_activities(self._browser.current_page) def report_activity(self, activity): session_id = self._parser.parse_session_id(self._browser.current_page) if not self._is_in_correct_state(activity.date): raise ValueError( "Date argument is not withing the currently displayed date.") return self._browser.update(session_id, activity) def get_projects(self): response = self._browser.get_projects() return self._parser.parse_projects(response) def _goto(self, year, month): assert month > 0 and month <= 12 current = self._parser.parse_navigation(self._browser.current_page) offset = abs(current.year - year) if current.year < year: for _ in range(offset): self._goto_next_year() else: for _ in range(offset): self._goto_prev_year() offset = abs(current.month - month) if current.month < month: for _ in range(offset): self._goto_next_month() else: for _ in range(offset): self._goto_prev_month() def _goto_next_month(self): response = self._browser.goto_next_month() return self._parser.parse_navigation(response) def _goto_prev_month(self): response = self._browser.goto_prev_month() return self._parser.parse_navigation(response) def _goto_next_year(self): response = self._browser.goto_next_year() return self._parser.parse_navigation(response) def _goto_prev_year(self): response = self._browser.goto_prev_year() return self._parser.parse_navigation(response) def _is_in_correct_state(self, date): start, end = self._parser._get_current_range( self._browser.current_page) return start <= date and date <= end
class BaseAPI(object): def __init__(self, server): self._browser = CurrentTimeBrowser(server) self._parser = CurrentTimeParser() def login(self, username, password): return self._browser.login(username, password) def valid_session(self): return self._parser.valid_session(self._browser.current_page) def get_day(self, year, month, day): start, end = self._parser._get_current_range(self._browser.current_page) current = datetime.date(year, month, day) if start <= current and current <= end: activities = self._parser.parse_activities(self._browser.current_page) return [a for a in activities if a.date == current] else: self._goto(year, month) page = self._browser._current_page command = self._parser.get_day_command(page, day) page = self._browser.get(command) return self._parser.parse_activities(page) def get_week(self, year, week): date = datetime.date(year, 1, 1) + datetime.timedelta(weeks=week) self._goto(year, date.month) page = self._browser._current_page command = self._parser.get_week_command(page, week) page = self._browser.get(command) return self._parser.parse_activities(page) def get_month(self, year, month): start, end = self._parser._get_current_range(self._browser.current_page) if start.year != year or start.month != month: self._goto(year, month) self._browser.get_current_month() start, end = self._parser._get_current_range(self._browser.current_page) if not (start.day == 1 and end.day >= 28): self._browser.get_current_month() return self._parser.parse_activities(self._browser.current_page) def report_activity(self, activity): session_id = self._parser.parse_session_id(self._browser.current_page) if not self._is_in_correct_state(activity.date): raise ValueError( "Date argument is not withing the currently displayed date.") return self._browser.update(session_id, activity) def get_projects(self): response = self._browser.get_projects() return self._parser.parse_projects(response) def _goto(self, year, month): assert month > 0 and month <= 12 current = self._parser.parse_navigation(self._browser.current_page) offset = abs(current.year - year) if current.year < year: for _ in range(offset): self._goto_next_year() else: for _ in range(offset): self._goto_prev_year() offset = abs(current.month - month) if current.month < month: for _ in range(offset): self._goto_next_month() else: for _ in range(offset): self._goto_prev_month() def _goto_next_month(self): response = self._browser.goto_next_month() return self._parser.parse_navigation(response) def _goto_prev_month(self): response = self._browser.goto_prev_month() return self._parser.parse_navigation(response) def _goto_next_year(self): response = self._browser.goto_next_year() return self._parser.parse_navigation(response) def _goto_prev_year(self): response = self._browser.goto_prev_year() return self._parser.parse_navigation(response) def _is_in_correct_state(self, date): start, end = self._parser._get_current_range(self._browser.current_page) return start <= date and date <= end