Update calendar_app.py
Browse files- calendar_app.py +72 -68
calendar_app.py
CHANGED
|
@@ -114,71 +114,75 @@ class Calendar:
|
|
| 114 |
print_counter+=1
|
| 115 |
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
def check_valid_menu_choice(choice):
|
| 119 |
-
"""Input sanitation for valid menu choice from 1 and 7."""
|
| 120 |
-
if not choice.isdigit() or int(choice) < 1 or int(choice) > 7:
|
| 121 |
-
print(f"Invalid choice: {choice}. Please choose an option number from 1 to 7.")
|
| 122 |
-
return "invalid_choice"
|
| 123 |
-
else:
|
| 124 |
-
return "valid_choice"
|
| 125 |
-
|
| 126 |
-
def check_valid_date(date):
|
| 127 |
-
"""Input sanitation for valid date format"""
|
| 128 |
-
date_parts = date.split("-")
|
| 129 |
-
count_date_parts = len(date_parts)
|
| 130 |
-
|
| 131 |
-
# check 3 date parts are found: YYYY, MM, DD
|
| 132 |
-
if count_date_parts<3:
|
| 133 |
-
print(f"You entered {date} containing only {count_date_parts} date parts. Please enter date in YYYY-MM-DD format")
|
| 134 |
-
return "invalid_date"
|
| 135 |
-
|
| 136 |
-
# check separator of date parts
|
| 137 |
-
elif "." in date:
|
| 138 |
-
print(f"You entered {date} split on '.'. Please enter date in YYYY-MM-DD format split by '-'")
|
| 139 |
-
return "invalid_date"
|
| 140 |
-
elif "/" in date:
|
| 141 |
-
print(f"You entered {date} split on '/'. Please enter date in YYYY-MM-DD format split by '-'")
|
| 142 |
-
return "invalid_date"
|
| 143 |
-
|
| 144 |
-
# check length of date parts
|
| 145 |
-
elif len(date_parts[0])!=4:
|
| 146 |
-
print(f"You entered {date} with year value {date_parts[0]}. Please enter date in YYYY-MM-DD format, starting with a 4-digit year")
|
| 147 |
-
return "invalid_date"
|
| 148 |
-
elif len(date_parts[1])!=2:
|
| 149 |
-
print(f"You entered {date} with month value {date_parts[1]}. Please enter date in YYYY-MM-DD format, with a 2-digit month in the middle")
|
| 150 |
-
return "invalid_date"
|
| 151 |
-
elif len(date_parts[2])!=2:
|
| 152 |
-
print(f"You entered {date} with day value {date_parts[2]}. Please enter date in YYYY-MM-DD format, with a 2-digit day at the end")
|
| 153 |
-
return "invalid_date"
|
| 154 |
-
|
| 155 |
-
# in this last scenario, we have a valid date
|
| 156 |
-
else:
|
| 157 |
-
return "valid_date"
|
| 158 |
-
|
| 159 |
-
def check_valid_date_range(date_start, date_end):
|
| 160 |
-
"""Input sanitation for start date before OR equal to the end date."""
|
| 161 |
-
if datetime.strptime(date_start, '%Y-%m-%d') > datetime.strptime(date_end, '%Y-%m-%d'):
|
| 162 |
-
print(f"Error: Start date {date_start} is after end date {date_end}.\nPlease enter end date after the start date {date_start}.")
|
| 163 |
-
return "invalid_range"
|
| 164 |
-
else:
|
| 165 |
-
return "valid_range"
|
| 166 |
|
| 167 |
-
def check_valid_month(month):
|
| 168 |
-
"""Input sanitation for month string between 1 and 12."""
|
| 169 |
-
if not month.isdigit() or int(month) < 1 or int(month) > 12:
|
| 170 |
-
print(f"Invalid month: {month}. Please enter a month from 01 to 12.")
|
| 171 |
-
return "invalid_month"
|
| 172 |
-
else:
|
| 173 |
-
return "valid_month"
|
| 174 |
-
|
| 175 |
-
def check_valid_reminder(reminder):
|
| 176 |
-
"""Input sanitation for reminder description."""
|
| 177 |
-
if len(reminder)<1 or reminder is None:
|
| 178 |
-
print("Error: Reminder description cannot be empty.")
|
| 179 |
-
return "invalid_reminder"
|
| 180 |
-
else:
|
| 181 |
-
return "valid_reminder"
|
| 182 |
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
|
@@ -225,10 +229,10 @@ if __name__ == "__main__":
|
|
| 225 |
elif choice == "2":
|
| 226 |
while True:
|
| 227 |
date_start = input("Enter start date (YYYY-MM-DD): ")
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
|
| 233 |
while True:
|
| 234 |
date_end = input("Enter end date (YYYY-MM-DD): ")
|
|
@@ -279,7 +283,7 @@ if __name__ == "__main__":
|
|
| 279 |
elif choice == "6":
|
| 280 |
while True:
|
| 281 |
user_mth = input("Enter month number (MM): ")
|
| 282 |
-
if check_valid_month(
|
| 283 |
continue
|
| 284 |
else:
|
| 285 |
break
|
|
|
|
| 114 |
print_counter+=1
|
| 115 |
|
| 116 |
|
| 117 |
+
#############################
|
| 118 |
+
# Input validation functions
|
| 119 |
+
def check_valid_menu_choice(choice):
|
| 120 |
+
"""Input sanitation for valid menu choice from 1 and 7."""
|
| 121 |
+
if not choice.isdigit() or int(choice) < 1 or int(choice) > 7:
|
| 122 |
+
print(f"Invalid choice: {choice}. Please choose an option number from 1 to 7.")
|
| 123 |
+
return "invalid_choice"
|
| 124 |
+
else:
|
| 125 |
+
return "valid_choice"
|
| 126 |
+
|
| 127 |
+
def check_valid_date(date):
|
| 128 |
+
"""Input sanitation for valid date format"""
|
| 129 |
+
date_parts = date.split("-")
|
| 130 |
+
count_date_parts = len(date_parts)
|
| 131 |
+
|
| 132 |
+
# check 3 date parts are found: YYYY, MM, DD
|
| 133 |
+
if count_date_parts<3:
|
| 134 |
+
print(f"You entered {date} containing only {count_date_parts} date parts. Please enter date in YYYY-MM-DD format")
|
| 135 |
+
return "invalid_date"
|
| 136 |
+
|
| 137 |
+
# check separator of date parts
|
| 138 |
+
elif "." in date:
|
| 139 |
+
print(f"You entered {date} split on '.'. Please enter date in YYYY-MM-DD format split by '-'")
|
| 140 |
+
return "invalid_date"
|
| 141 |
+
elif "/" in date:
|
| 142 |
+
print(f"You entered {date} split on '/'. Please enter date in YYYY-MM-DD format split by '-'")
|
| 143 |
+
return "invalid_date"
|
| 144 |
+
|
| 145 |
+
# check length of date parts
|
| 146 |
+
elif len(date_parts[0])!=4:
|
| 147 |
+
print(f"You entered {date} with year value {date_parts[0]}. Please enter date in YYYY-MM-DD format, starting with a 4-digit year")
|
| 148 |
+
return "invalid_date"
|
| 149 |
+
elif len(date_parts[1])!=2:
|
| 150 |
+
print(f"You entered {date} with month value {date_parts[1]}. Please enter date in YYYY-MM-DD format, with a 2-digit month in the middle")
|
| 151 |
+
return "invalid_date"
|
| 152 |
+
elif len(date_parts[2])!=2:
|
| 153 |
+
print(f"You entered {date} with day value {date_parts[2]}. Please enter date in YYYY-MM-DD format, with a 2-digit day at the end")
|
| 154 |
+
return "invalid_date"
|
| 155 |
+
|
| 156 |
+
# in this last scenario, we have a valid date
|
| 157 |
+
else:
|
| 158 |
+
return "valid_date"
|
| 159 |
+
|
| 160 |
+
def check_valid_date_range(date_start, date_end):
|
| 161 |
+
"""Input sanitation for start date before OR equal to the end date."""
|
| 162 |
+
if datetime.datetime.strptime(date_start, '%Y-%m-%d') > datetime.datetime.strptime(date_end, '%Y-%m-%d'):
|
| 163 |
+
print(f"Error: Start date {date_start} is after end date {date_end}.\nPlease enter end date after the start date {date_start}.")
|
| 164 |
+
return "invalid_range"
|
| 165 |
+
else:
|
| 166 |
+
return "valid_range"
|
| 167 |
+
|
| 168 |
+
def check_valid_month(month):
|
| 169 |
+
"""Input sanitation for month string between 1 and 12."""
|
| 170 |
+
if not month.isdigit() or int(month) < 1 or int(month) > 12:
|
| 171 |
+
print(f"Invalid month: {month}. Please enter a month from 01 to 12.")
|
| 172 |
+
return "invalid_month"
|
| 173 |
+
else:
|
| 174 |
+
return "valid_month"
|
| 175 |
+
|
| 176 |
+
def check_valid_reminder(reminder):
|
| 177 |
+
"""Input sanitation for reminder description."""
|
| 178 |
+
if len(reminder.strip())<1 or reminder is None:
|
| 179 |
+
print("Error: Reminder description cannot be empty.")
|
| 180 |
+
return "invalid_reminder"
|
| 181 |
+
else:
|
| 182 |
+
return "valid_reminder"
|
| 183 |
+
#############################
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
|
|
|
| 229 |
elif choice == "2":
|
| 230 |
while True:
|
| 231 |
date_start = input("Enter start date (YYYY-MM-DD): ")
|
| 232 |
+
if check_valid_date(date_start) == "invalid_date":
|
| 233 |
+
continue
|
| 234 |
+
else:
|
| 235 |
+
break
|
| 236 |
|
| 237 |
while True:
|
| 238 |
date_end = input("Enter end date (YYYY-MM-DD): ")
|
|
|
|
| 283 |
elif choice == "6":
|
| 284 |
while True:
|
| 285 |
user_mth = input("Enter month number (MM): ")
|
| 286 |
+
if check_valid_month(user_mth) == "invalid_month":
|
| 287 |
continue
|
| 288 |
else:
|
| 289 |
break
|