def update_task_action(calendar_id: str, year: str, month: str, day: str, task_id: str) -> Response: # Logic is same as save + delete, could refactor but can wait until need to change any save/delete logic calendar_data = CalendarData(current_app.config["DATA_FOLDER"], current_app.config["WEEK_STARTING_DAY"]) # For creation of "updated" task use only form data title = request.form["title"].strip() date = request.form.get("date", "") if len(date) > 0: fragments = re.split("-", date) updated_year = int(fragments[0]) # type: Optional[int] updated_month = int(fragments[1]) # type: Optional[int] updated_day = int(fragments[2]) # type: Optional[int] else: updated_year = updated_month = updated_day = None is_all_day = request.form.get("is_all_day", "0") == "1" due_time = request.form["due_time"] details = request.form["details"].replace("\r", "").replace("\n", "<br>") color = request.form["color"] has_repetition = request.form.get("repeats", "0") == "1" repetition_type = request.form.get("repetition_type", "") repetition_subtype = request.form.get("repetition_subtype", "") repetition_value = int(request.form["repetition_value"]) # type: int calendar_data.create_task(calendar_id=calendar_id, year=updated_year, month=updated_month, day=updated_day, title=title, is_all_day=is_all_day, due_time=due_time, details=details, color=color, has_repetition=has_repetition, repetition_type=repetition_type, repetition_subtype=repetition_subtype, repetition_value=repetition_value) # For deletion of old task data use only url data calendar_data.delete_task(calendar_id=calendar_id, year_str=year, month_str=month, day_str=day, task_id=int(task_id)) if updated_year is None: return redirect("{}/{}/".format(current_app.config["BASE_URL"], calendar_id), code=302) else: return redirect("{}/{}/?y={}&m={}".format( current_app.config["BASE_URL"], calendar_id, updated_year, updated_month), code=302)
def save_task_action(calendar_id: str) -> Response: account = request.form["account_select"].strip() amount = '{0:.2f}'.format(float(request.form["amount"].strip())) credit_debit = request.form["credit_debit"].strip() date = request.form.get("date", "") if len(date) > 0: date_fragments = re.split("-", date) year = int(date_fragments[0]) # type: Optional[int] month = int(date_fragments[1]) # type: Optional[int] day = int(date_fragments[2]) # type: Optional[int] else: year = month = day = None details = request.form["details"].replace("\r", "").replace("\n", "<br>") color = request.form["color"] has_repetition = request.form.get("repeats", "0") == "1" repetition_type = request.form.get("repetition_type") repetition_subtype = request.form.get("repetition_subtype") repetition_value = int(request.form["repetition_value"]) print("credit_debit: ", credit_debit, file=sys.stderr) print("amount: ", amount, file=sys.stderr) calendar_data = CalendarData(current_app.config["DATA_FOLDER"], current_app.config["WEEK_STARTING_DAY"]) calendar_data.create_task( calendar_id=calendar_id, year=year, month=month, day=day, account=account, amount=amount, credit_debit=credit_debit, details=details, color=color, has_repetition=has_repetition, repetition_type=repetition_type, repetition_subtype=repetition_subtype, repetition_value=repetition_value, ) if year is None: return redirect("{}/{}/".format(current_app.config["BASE_URL"], calendar_id), code=302) else: return redirect( "{}/{}/?y={}&m={}".format(current_app.config["BASE_URL"], calendar_id, year, month), code=302, )
def save_task_action(calendar_id: str) -> Response: title = request.form["title"].strip() date = request.form.get("date", "") if len(date) > 0: date_fragments = re.split("-", date) year = int(date_fragments[0]) # type: Optional[int] month = int(date_fragments[1]) # type: Optional[int] day = int(date_fragments[2]) # type: Optional[int] else: year = month = day = None is_all_day = request.form.get("is_all_day", "0") == "1" start_time = request.form["start_time"] end_time = request.form.get("end_time", None) details = request.form["details"].replace("\r", "").replace("\n", "<br>") color = request.form["color"] has_repetition = request.form.get("repeats", "0") == "1" repetition_type = request.form.get("repetition_type") repetition_subtype = request.form.get("repetition_subtype") repetition_value = int(request.form["repetition_value"]) calendar_data = CalendarData(current_app.config["DATA_FOLDER"], current_app.config["WEEK_STARTING_DAY"]) calendar_data.create_task( calendar_id=calendar_id, year=year, month=month, day=day, title=title, is_all_day=is_all_day, start_time=start_time, end_time=end_time, details=details, color=color, has_repetition=has_repetition, repetition_type=repetition_type, repetition_subtype=repetition_subtype, repetition_value=repetition_value, ) if year is None: return redirect("{}/{}/".format(current_app.config["BASE_URL"], calendar_id), code=302) else: return redirect( "{}/{}/?y={}&m={}".format(current_app.config["BASE_URL"], calendar_id, year, month), code=302, )
def test_creates_task_with_start_and_end_dates( save_calendar_mock: MagicMock, calendar_data: CalendarData) -> None: year = 2017 month = 12 day = 10 title = "an irrelevant title" is_all_day = False start_time = "12:00" end_time = "13:00" details = "" color = "an_irrelevant_color" has_repetition = False repetition_type = "" repetition_subtype = "" repetition_value = 0 calendar_id = "sample_empty_data_file" result = calendar_data.create_task( calendar_id=calendar_id, year=year, month=month, day=day, title=title, is_all_day=is_all_day, start_time=start_time, end_time=end_time, details=details, color=color, has_repetition=has_repetition, repetition_type=repetition_type, repetition_subtype=repetition_subtype, repetition_value=repetition_value, ) assert result is True save_calendar_mock.assert_called_once_with(ANY, filename=calendar_id) call_args, _ = save_calendar_mock.call_args data = call_args[0] assert "tasks" in data assert "normal" in data["tasks"] assert str(year) in data["tasks"]["normal"] assert str(month) in data["tasks"]["normal"][str(year)] assert str(day) in data["tasks"]["normal"][str(year)][str(month)] assert len(data["tasks"]["normal"][str(year)][str(month)][str(day)]) == 1 task_data = data["tasks"]["normal"][str(year)][str(month)][str(day)][0] assert "title" in task_data assert task_data["title"] == title assert task_data["is_all_day"] is False assert task_data["start_time"] == start_time assert task_data["end_time"] == end_time
def save_task_action(calendar_id: str) -> Response: title = request.form["title"].strip() startdate = request.form.get("date", "") enddate = request.form.get("enddate", "") if len(startdate) > 0: date_fragments = re.split("-", startdate) year = int(date_fragments[0]) # type: Optional[int] month = int(date_fragments[1]) # type: Optional[int] day = int(date_fragments[2]) # type: Optional[int] else: year = month = day = None is_all_day = request.form.get("is_all_day", "0") == "1" start_time = request.form["start_time"] end_time = request.form.get("end_time", None) details = request.form["details"].replace("\r", "").replace("\n", "<br>") color = request.form["color"] has_repetition = request.form.get("repeats", "0") == "1" repetition_type = request.form.get("repetition_type") repetition_subtype = request.form.get("repetition_subtype") repetition_value = int(request.form["repetition_value"]) calendar_data = CalendarData(current_app.config["DATA_FOLDER"], current_app.config["WEEK_STARTING_DAY"]) dates_to_create = [ ] # type: List[Tuple[Optional[int], Optional[int], Optional[int]]] # repetitive tasks not supported if startdate != enddate and not has_repetition: startdate_fragments = re.split("-", startdate) enddate_fragments = re.split("-", enddate) sdate = date(int(startdate_fragments[0]), int(startdate_fragments[1]), int(startdate_fragments[2])) edate = date(int(enddate_fragments[0]), int(enddate_fragments[1]), int(enddate_fragments[2])) delta = edate - sdate for i in range(delta.days + 1): currentdate = re.split("-", str(sdate + timedelta(days=i))) year = int(currentdate[0]) month = int(currentdate[1]) day = int(currentdate[2]) dates_to_create.append((year, month, day)) else: dates_to_create.append((year, month, day)) for date_tuple in dates_to_create: year, month, day = date_tuple calendar_data.create_task( calendar_id=calendar_id, year=year, month=month, day=day, title=title, is_all_day=is_all_day, start_time=start_time, end_time=end_time, details=details, color=color, has_repetition=has_repetition, repetition_type=repetition_type, repetition_subtype=repetition_subtype, repetition_value=repetition_value, ) if year is None: return redirect("{}/{}/".format(current_app.config["BASE_URL"], calendar_id), code=302) else: return redirect( "{}/{}/?y={}&m={}".format(current_app.config["BASE_URL"], calendar_id, year, month), code=302, )