def add_repetitive_tasks_from_calendar( self, year: int, month: int, data: Dict, tasks: Dict, view_past_tasks: Optional[bool] = True) -> Dict: current_day, current_month, current_year = GregorianCalendar.current_date( ) repetitive_tasks = self._repetitive_tasks_from_calendar( year=year, month=month, month_days=GregorianCalendar.month_days_with_weekday(year=year, month=month), data=data) for day, day_tasks in repetitive_tasks.items(): if not view_past_tasks: if year < current_year: continue if year == current_year: if month < current_month or (month == current_month and int(day) < current_day): continue if day not in tasks: tasks[day] = [] for task in day_tasks: tasks[day].append(task) return tasks
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] view_past_tasks = request.cookies.get("ViewPastTasks", "1") == "1" calendar_data = CalendarData(config.DATA_FOLTER) try: data = calendar_data.load_calendar(calendar_id) except FileNotFoundError: abort(404) tasks = calendar_data.tasks_from_calendar(year=year, month=month, view_past_tasks=view_past_tasks, data=data) tasks = calendar_data.add_repetitive_tasks_from_calendar(year=year, month=month, data=data, tasks=tasks, view_past_tasks=view_past_tasks) return 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=year, month=month), previous_month_link=previous_month_link(year=year, month=month), next_month_link=next_month_link(year=year, month=month), base_url=config.BASE_URL, tasks=tasks)
def new_task(calendar_id: str, year: int, month: int) -> Response: current_day, current_month, current_year = GregorianCalendar.current_date() year = max(min(int(year), config.MAX_YEAR), config.MIN_YEAR) month = max(min(int(month), 12), 1) month_names = GregorianCalendar.MONTH_NAMES if current_month == month and current_year == year: day = current_day else: day = 1 task = { "date": CalendarData.date_for_frontend(year=year, month=month, day=day), "is_all_day": True, "repeats": False, "details": "" } return render_template("task.html", calendar_id=calendar_id, year=year, month=month, min_year=config.MIN_YEAR, max_year=config.MAX_YEAR, month_names=month_names, task=task, base_url=config.BASE_URL, editing=False)
def write(self, calendar_data: CalendarData, data: Dict) -> bool: current_day, current_month, current_year = GregorianCalendar.current_date( ) output = self._get_header(calendar_name=data["name"]) # type: str year = current_year month = current_month for index in range(self.months_to_export): if month < 12: month += 1 else: month = 1 year += 1 all_tasks = calendar_data.tasks_from_calendar(year=year, month=month, data=data) all_tasks = calendar_data.add_repetitive_tasks_from_calendar( year=year, month=month, data=data, tasks=all_tasks, view_past_tasks=False) for day, tasks in all_tasks.items(): for task in tasks: output += self._get_task(task, year, month, day) output += self._get_footer() print(output) return True
def _clear_past_hidden_entries(data: Dict) -> None: _, current_month, current_year = GregorianCalendar.current_date() task_ids_to_delete = [] for task_id in data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK]: years_to_delete = [] for year in data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id]: months_to_delete = [] for month in data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][ task_id][year]: if (int(year) < current_year) or (int(year) == current_year and int(month) < current_month): months_to_delete.append(month) for month in months_to_delete: del (data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id] [year][month]) if len(data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id] [year]) == 0: years_to_delete.append(year) for year in years_to_delete: del ( data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id][year]) if len(data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id]) == 0: task_ids_to_delete.append(task_id) for task_id in task_ids_to_delete: del (data[KEY_TASKS][KEY_REPETITIVE_HIDDEN_TASK][task_id])
def _clear_past_hidden_entries(data: Dict) -> None: _, current_month, current_year = GregorianCalendar.current_date() task_ids_to_delete = [] for task_id in data["tasks"]["hidden_repetition"]: years_to_delete = [] for year in data["tasks"]["hidden_repetition"][task_id]: months_to_delete = [] for month in data["tasks"]["hidden_repetition"][task_id][year]: if (int(year) < current_year) or (int(year) == current_year and int(month) < current_month): months_to_delete.append(month) for month in months_to_delete: del (data["tasks"]["hidden_repetition"][task_id][year] [month]) if len(data["tasks"]["hidden_repetition"][task_id][year]) == 0: years_to_delete.append(year) for year in years_to_delete: del (data["tasks"]["hidden_repetition"][task_id][year]) if len(data["tasks"]["hidden_repetition"][task_id]) == 0: task_ids_to_delete.append(task_id) for task_id in task_ids_to_delete: del (data["tasks"]["hidden_repetition"][task_id])
def tasks_from_calendar(self, year: int, month: int, view_past_tasks: Optional[bool]=True, data: Optional[Dict]=None, calendar_id: Optional[str]=None) -> Dict: if data is None: if calendar_id is None: raise ValueError("Need to provide either calendar_id or loaded data") else: data = self.load_calendar(calendar_id) if "tasks" not in data.keys(): raise ValueError("Incomplete data for calendar id '{}'".format(calendar_id)) if not all(["normal" in data["tasks"].keys(), "repetition" in data["tasks"].keys(), "hidden_repetition" in data["tasks"].keys()]): raise ValueError("Incomplete data for calendar id '{}'".format(calendar_id)) tasks = {} # type: Dict if str(year) in data["tasks"]["normal"]: if str(month) in data["tasks"]["normal"][str(year)]: tasks = data["tasks"]["normal"][str(year)][str(month)] if not view_past_tasks: current_day, current_month, current_year = GregorianCalendar.current_date() if year < current_year: tasks = {} elif year == current_year: if month < current_month: tasks = {} else: for day in tasks.keys(): if month == current_month and int(day) < current_day: tasks[day] = [] return tasks
def jsonRest(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] view_past_tasks = request.cookies.get("ViewPastTasks", "1") == "1" calendar_data = CalendarData(config.DATA_FOLTER) try: data = calendar_data.load_calendar(calendar_id) except FileNotFoundError: abort(404) tasks = calendar_data.tasks_from_calendar(year=year, month=month, view_past_tasks=view_past_tasks, data=data) tasks = calendar_data.add_repetitive_tasks_from_calendar( year=year, month=month, data=data, tasks=tasks, view_past_tasks=view_past_tasks) merged = {} merged[year] = {} merged[year][month] = tasks nextMonth = month nextYear = year if (month == 12): nextMonth = 1 nextYear = year + 1 else: nextMonth = month + 1 tasksNextMonth = calendar_data.tasks_from_calendar( year=nextYear, month=nextMonth, view_past_tasks=view_past_tasks, data=data) tasksNextMonth = calendar_data.add_repetitive_tasks_from_calendar( year=nextYear, month=nextMonth, data=data, tasks=tasksNextMonth, view_past_tasks=view_past_tasks) if (year == nextYear): merged[year][nextMonth] = tasksNextMonth else: merged[nextYear] = {} merged[nextYear][nextMonth] = tasksNextMonth return json.dumps(merged)
def next_month_link(year: int, month: int) -> str: month, year = GregorianCalendar.next_month_and_year(year=year, month=month) return "" if year < config.MIN_YEAR or year > config.MAX_YEAR else "?y={}&m={}".format(year, month)