def tasks_from_calendar(self, year: int, month: int, data: Dict) -> Dict: if not data or KEY_TASKS not in data: raise ValueError("Incomplete data for calendar") if not all([ KEY_NORMAL_TASK in data[KEY_TASKS], KEY_REPETITIVE_TASK in data[KEY_TASKS], KEY_REPETITIVE_HIDDEN_TASK in data[KEY_TASKS] ]): raise ValueError("Incomplete data for calendar") tasks = {} # type: Dict current_day, current_month, current_year = GregorianCalendar.current_date( ) for day in GregorianCalendar.month_days(year, month): month_str = str(day.month) year_str = str(day.year) if (year_str in data[KEY_TASKS][KEY_NORMAL_TASK] and month_str in data[KEY_TASKS][KEY_NORMAL_TASK][year_str] and month_str not in tasks): tasks[month_str] = data[KEY_TASKS][KEY_NORMAL_TASK][year_str][ month_str] return tasks
def main_calendar_action(calendar_id: str) -> Response: GregorianCalendar.setfirstweekday(current_app.config["WEEK_STARTING_DAY"]) current_day, current_month, current_year = GregorianCalendar.current_date() year = int(request.args.get("y", current_year)) year = max(min(year, current_app.config["MAX_YEAR"]), current_app.config["MIN_YEAR"]) month = int(request.args.get("m", current_month)) month = max(min(month, 12), 1) month_name = GregorianCalendar.MONTH_NAMES[month - 1] if current_app.config["HIDE_PAST_TASKS"]: view_past_tasks = False else: view_past_tasks = request.cookies.get("ViewPastTasks", "1") == "1" calendar_data = CalendarData(current_app.config["DATA_FOLDER"], current_app.config["WEEK_STARTING_DAY"]) try: data = calendar_data.load_calendar(calendar_id) except FileNotFoundError: abort(404) tasks = calendar_data.tasks_from_calendar(year, month, data) tasks = calendar_data.add_repetitive_tasks_from_calendar( year, month, data, tasks) if not view_past_tasks: calendar_data.hide_past_tasks(year, month, tasks) if current_app.config[ "WEEK_STARTING_DAY"] == constants.WEEK_START_DAY_MONDAY: weekdays_headers = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"] else: weekdays_headers = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] return cast( Response, render_template( "calendar.html", calendar_id=calendar_id, year=year, month=month, month_name=month_name, current_year=current_year, current_month=current_month, current_day=current_day, month_days=GregorianCalendar.month_days(year, month), previous_month_link=previous_month_link(year, month), next_month_link=next_month_link(year, month), base_url=current_app.config["BASE_URL"], tasks=tasks, display_view_past_button=current_app. config["SHOW_VIEW_PAST_BUTTON"], weekdays_headers=weekdays_headers, ), )
def _repetitive_tasks_from_calendar(self, year: int, month: int, data: Dict) -> Dict: if KEY_TASKS not in data: ValueError("Incomplete data for calendar") if KEY_REPETITIVE_TASK not in data[KEY_TASKS]: ValueError("Incomplete data for calendar") repetitive_tasks = {} # type: Dict year_and_months = set([ (source_day.year, source_day.month) for source_day in GregorianCalendar.month_days(year, month) ]) for source_year, source_month in year_and_months: month_str = str(source_month) year_str = str(source_year) repetitive_tasks[month_str] = {} for task in data[KEY_TASKS][KEY_REPETITIVE_TASK]: id_str = str(task["id"]) monthly_task_assigned = False for week in GregorianCalendar.month_days_with_weekday( source_year, source_month): for weekday, day in enumerate(week): if day == 0: continue day_str = str(day) if (task["repetition_type"] == self.REPETITION_TYPE_WEEKLY and not self._is_repetition_hidden_for_day( data, id_str, year_str, month_str, str(day)) and task["repetition_value"] == weekday): self.add_task_to_list(repetitive_tasks, day_str, month_str, task) elif (task["repetition_type"] == self.REPETITION_TYPE_MONTHLY and not self._is_repetition_hidden( data, id_str, year_str, month_str)): if task["repetition_subtype"] == self.REPETITION_SUBTYPE_WEEK_DAY: if task["repetition_value"] == weekday and not monthly_task_assigned: monthly_task_assigned = True self.add_task_to_list( repetitive_tasks, day_str, month_str, task) else: if task["repetition_value"] == day: self.add_task_to_list( repetitive_tasks, day_str, month_str, task) return repetitive_tasks
def hide_past_tasks(self, year: int, month: int, tasks: Dict) -> None: current_day, current_month, current_year = GregorianCalendar.current_date( ) for day in GregorianCalendar.month_days(year, month): month_str = str(day.month) if self.is_past(day.year, day.month, current_year, current_month): tasks[month_str] = {} for task_day_number in tasks[month_str]: if day.month == current_month and int( task_day_number) < current_day: tasks[month_str][task_day_number] = []
def main_calendar_action(calendar_id: str) -> Response: current_day, current_month, current_year = GregorianCalendar.current_date() year = int(request.args.get("y", current_year)) year = max(min(year, config.MAX_YEAR), config.MIN_YEAR) month = int(request.args.get("m", current_month)) month = max(min(month, 12), 1) month_name = GregorianCalendar.MONTH_NAMES[month - 1] if config.HIDE_PAST_TASKS: view_past_tasks = False else: view_past_tasks = request.cookies.get("ViewPastTasks", "1") == "1" calendar_data = CalendarData(config.DATA_FOLDER) try: data = calendar_data.load_calendar(calendar_id) except FileNotFoundError: abort(404) tasks = calendar_data.tasks_from_calendar(year, month, data) tasks = calendar_data.add_repetitive_tasks_from_calendar( year, month, data, tasks) if not view_past_tasks: calendar_data.hide_past_tasks(year, month, tasks) return cast( Response, render_template("calendar.html", calendar_id=calendar_id, year=year, month=month, month_name=month_name, current_year=current_year, current_month=current_month, current_day=current_day, month_days=GregorianCalendar.month_days(year, month), previous_month_link=previous_month_link(year, month), next_month_link=next_month_link(year, month), base_url=config.BASE_URL, tasks=tasks, display_view_past_button=config.SHOW_VIEW_PAST_BUTTON))