-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chores] Added check for OPENWISP_MONITORING_WIFI_CLIENTS_CHECK_SNOOZ…
…E_SCHEDULE
- Loading branch information
Showing
3 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from datetime import datetime | ||
|
||
from django.core.checks import Error, register | ||
|
||
from . import settings as app_settings | ||
|
||
|
||
@register() | ||
def check_wifi_clients_snooze_schedule(app_configs, **kwargs): | ||
errors = [] | ||
setting_name = 'OPENWISP_MONITORING_WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE' | ||
schedule = app_settings.WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE | ||
|
||
if not isinstance(schedule, (list, tuple)): | ||
errors.append( | ||
Error( | ||
'Invalid schedule format', | ||
hint='Schedule must be a list of date-time ranges', | ||
obj=setting_name, | ||
) | ||
) | ||
return errors | ||
|
||
for item in schedule: | ||
if not isinstance(item, (list, tuple)) or len(item) != 2: | ||
errors.append( | ||
Error( | ||
f'Invalid schedule entry format: {item}', | ||
hint='Each schedule entry must be a pair of start and end times', | ||
obj=setting_name, | ||
) | ||
) | ||
continue | ||
|
||
start, end = item | ||
if not isinstance(start, str) or not isinstance(end, str): | ||
errors.append( | ||
Error( | ||
f'Invalid time format: {item}', | ||
hint='Use format "MM-DD HH:MM", "MM-DD", or "HH:MM"', | ||
obj=setting_name, | ||
) | ||
) | ||
continue | ||
|
||
try: | ||
# Check if both are time format (HH:MM) | ||
if '-' not in start and '-' not in end: | ||
datetime.strptime(start, '%H:%M') | ||
datetime.strptime(end, '%H:%M') | ||
# Check if both are date format (MM-DD) | ||
elif ':' not in start and ':' not in end: | ||
datetime.strptime(start, '%m-%d') | ||
datetime.strptime(end, '%m-%d') | ||
# Check if both are date-time format (MM-DD HH:MM) | ||
elif len(start.split()) == 2 and len(end.split()) == 2: | ||
datetime.strptime(start, '%m-%d %H:%M') | ||
datetime.strptime(end, '%m-%d %H:%M') | ||
else: | ||
errors.append( | ||
Error( | ||
f'Inconsistent format: {item}', | ||
hint='Both start and end must be in the same format (either both time or both date)', | ||
obj=setting_name, | ||
) | ||
) | ||
except ValueError: | ||
errors.append( | ||
Error( | ||
f'Invalid date-time format: {item}', | ||
hint='Use format "MM-DD HH:MM", "MM-DD", or "HH:MM"', | ||
obj=setting_name, | ||
) | ||
) | ||
|
||
return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters