Add routine management features for child and parent views
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 3m8s
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 3m8s
- Implemented routine child-mode flow tests to ensure proper functionality of routine assignment and task completion. - Created notification tests for parent view to verify routine completion notifications for children. - Developed ChildRoutineOverlay component for displaying routine tasks and handling user interactions. - Added RoutineApproveDialog component for approving or rejecting completed routines. - Created unit tests for ChildRoutineOverlay and RoutineEditView components to ensure correct behavior and rendering. - Enhanced RoutineEditView with proper handling of task addition and form submission. Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from time import sleep
|
||||
from datetime import datetime, timezone
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from flask import Blueprint, request, jsonify
|
||||
from tinydb import Query
|
||||
@@ -43,15 +44,49 @@ child_api = Blueprint('child_api', __name__)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _is_iso_timestamp_today_utc(timestamp: str | None, today_utc: str) -> bool:
|
||||
return bool(timestamp) and timestamp[:10] == today_utc
|
||||
def _get_user_timezone(user_id: str) -> str | None:
|
||||
user = users_db.get(Query().id == user_id)
|
||||
if not user:
|
||||
return None
|
||||
return user.get('timezone')
|
||||
|
||||
|
||||
def _is_epoch_timestamp_today_utc(epoch_ts, today_utc: str) -> bool:
|
||||
def _get_user_today_local(user_id: str) -> tuple[str, str | None]:
|
||||
tz_str = _get_user_timezone(user_id)
|
||||
try:
|
||||
now_local = datetime.now(ZoneInfo(tz_str)) if tz_str else datetime.now(timezone.utc)
|
||||
except Exception:
|
||||
tz_str = None
|
||||
now_local = datetime.now(timezone.utc)
|
||||
return now_local.strftime('%Y-%m-%d'), tz_str
|
||||
|
||||
|
||||
def _is_iso_timestamp_on_local_day(timestamp: str | None, local_day: str, tz_str: str | None) -> bool:
|
||||
if not timestamp:
|
||||
return False
|
||||
try:
|
||||
normalized = timestamp.replace('Z', '+00:00')
|
||||
parsed = datetime.fromisoformat(normalized)
|
||||
if parsed.tzinfo is None:
|
||||
parsed = parsed.replace(tzinfo=timezone.utc)
|
||||
try:
|
||||
tz = ZoneInfo(tz_str) if tz_str else timezone.utc
|
||||
except Exception:
|
||||
tz = timezone.utc
|
||||
return parsed.astimezone(tz).strftime('%Y-%m-%d') == local_day
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _is_epoch_timestamp_on_local_day(epoch_ts, local_day: str, tz_str: str | None) -> bool:
|
||||
if epoch_ts is None:
|
||||
return False
|
||||
try:
|
||||
return datetime.fromtimestamp(float(epoch_ts), timezone.utc).strftime('%Y-%m-%d') == today_utc
|
||||
tz = ZoneInfo(tz_str) if tz_str else timezone.utc
|
||||
except Exception:
|
||||
tz = timezone.utc
|
||||
try:
|
||||
return datetime.fromtimestamp(float(epoch_ts), tz).strftime('%Y-%m-%d') == local_day
|
||||
except (TypeError, ValueError, OSError):
|
||||
return False
|
||||
|
||||
@@ -305,6 +340,7 @@ def list_child_tasks(id):
|
||||
task_ids = child.get('tasks', [])
|
||||
|
||||
TaskQuery = Query()
|
||||
today_local, tz_str = _get_user_today_local(user_id)
|
||||
child_tasks = []
|
||||
for tid in task_ids:
|
||||
task = task_db.get((TaskQuery.id == tid) & ((TaskQuery.user_id == user_id) | (TaskQuery.user_id == None)))
|
||||
@@ -339,12 +375,10 @@ def list_child_tasks(id):
|
||||
status = pending.get('status')
|
||||
approved_at = pending.get('approved_at')
|
||||
created_at = pending.get('created_at')
|
||||
today_utc = datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
||||
|
||||
if status == 'approved' and _is_iso_timestamp_today_utc(approved_at, today_utc):
|
||||
if status == 'approved' and _is_iso_timestamp_on_local_day(approved_at, today_local, tz_str):
|
||||
ct_dict['pending_status'] = 'approved'
|
||||
ct_dict['approved_at'] = approved_at
|
||||
elif status == 'pending' and _is_epoch_timestamp_today_utc(created_at, today_utc):
|
||||
elif status == 'pending' and _is_epoch_timestamp_on_local_day(created_at, today_local, tz_str):
|
||||
ct_dict['pending_status'] = 'pending'
|
||||
ct_dict['approved_at'] = None
|
||||
else:
|
||||
@@ -892,6 +926,7 @@ def reward_status(id):
|
||||
reward_ids = child.rewards
|
||||
|
||||
RewardQuery = Query()
|
||||
today_local, tz_str = _get_user_today_local(user_id)
|
||||
statuses = []
|
||||
for reward_id in reward_ids:
|
||||
reward_dict = reward_db.get((RewardQuery.id == reward_id) & ((RewardQuery.user_id == user_id) | (RewardQuery.user_id == None)))
|
||||
@@ -912,8 +947,7 @@ def reward_status(id):
|
||||
)
|
||||
redeeming = False
|
||||
if pending and pending.get('status') == 'pending':
|
||||
today_utc = datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
||||
if _is_epoch_timestamp_today_utc(pending.get('created_at'), today_utc):
|
||||
if _is_epoch_timestamp_on_local_day(pending.get('created_at'), today_local, tz_str):
|
||||
redeeming = True
|
||||
else:
|
||||
pending_id = pending.get('id')
|
||||
@@ -975,8 +1009,8 @@ def request_reward(id):
|
||||
(DupQuery.user_id == user_id)
|
||||
)
|
||||
if duplicate:
|
||||
today_utc = datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
||||
if _is_epoch_timestamp_today_utc(duplicate.get('created_at'), today_utc):
|
||||
today_local, tz_str = _get_user_today_local(user_id)
|
||||
if _is_epoch_timestamp_on_local_day(duplicate.get('created_at'), today_local, tz_str):
|
||||
return jsonify({'error': 'Reward request already pending', 'code': 'DUPLICATE_REWARD_REQUEST'}), 409
|
||||
pending_id = duplicate.get('id')
|
||||
if pending_id:
|
||||
@@ -1106,7 +1140,7 @@ def list_pending_confirmations():
|
||||
if not user_id:
|
||||
return jsonify({'error': 'Unauthorized', 'code': 'UNAUTHORIZED'}), 401
|
||||
PendingQuery = Query()
|
||||
today_utc = datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
||||
today_local, tz_str = _get_user_today_local(user_id)
|
||||
pending_items = pending_confirmations_db.search(
|
||||
(PendingQuery.user_id == user_id) & (PendingQuery.status == 'pending')
|
||||
)
|
||||
@@ -1120,7 +1154,7 @@ def list_pending_confirmations():
|
||||
for pr in pending_items:
|
||||
pending = PendingConfirmation.from_dict(pr)
|
||||
|
||||
if not _is_epoch_timestamp_today_utc(pending.created_at, today_utc):
|
||||
if not _is_epoch_timestamp_on_local_day(pending.created_at, today_local, tz_str):
|
||||
pending_confirmations_db.remove(PendingQuery.id == pending.id)
|
||||
continue
|
||||
|
||||
@@ -1200,16 +1234,16 @@ def confirm_chore(id):
|
||||
(PendingQuery.entity_type == 'chore') & (PendingQuery.user_id == user_id)
|
||||
)
|
||||
if existing:
|
||||
today_utc = datetime.now(timezone.utc).strftime('%Y-%m-%d')
|
||||
today_local, tz_str = _get_user_today_local(user_id)
|
||||
if existing.get('status') == 'pending':
|
||||
if _is_epoch_timestamp_today_utc(existing.get('created_at'), today_utc):
|
||||
if _is_epoch_timestamp_on_local_day(existing.get('created_at'), today_local, tz_str):
|
||||
return jsonify({'error': 'Chore already pending confirmation', 'code': 'CHORE_ALREADY_PENDING'}), 400
|
||||
pending_id = existing.get('id')
|
||||
if pending_id:
|
||||
pending_confirmations_db.remove(PendingQuery.id == pending_id)
|
||||
if existing.get('status') == 'approved':
|
||||
approved_at = existing.get('approved_at', '')
|
||||
if _is_iso_timestamp_today_utc(approved_at, today_utc):
|
||||
if _is_iso_timestamp_on_local_day(approved_at, today_local, tz_str):
|
||||
return jsonify({'error': 'Chore already completed today', 'code': 'CHORE_ALREADY_COMPLETED'}), 400
|
||||
pending_id = existing.get('id')
|
||||
if pending_id:
|
||||
|
||||
Reference in New Issue
Block a user