47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
from datetime import datetime, timedelta
|
|
from .models import InAppNotification
|
|
from module_iam.models import IAmPrincipal, IAmPrincipalType
|
|
from module_activity.models import MealRecord, Bowel, MealSymptomRecord, Medication
|
|
|
|
|
|
def notification_for_meal_and_medication():
|
|
current_date = datetime.now()
|
|
fifteen_days_ago = current_date - timedelta(days=20)
|
|
|
|
users = IAmPrincipal.objects.filter(
|
|
last_login__gte=fifteen_days_ago,
|
|
principal_type=IAmPrincipalType.get_principal_user(),
|
|
).values_list("id", flat=True)
|
|
|
|
meal_obj = MealRecord.objects.filter(date=current_date).values_list(
|
|
"principal", flat=True
|
|
)
|
|
medication_obj = Medication.objects.filter(date=current_date).values_list(
|
|
"principal", flat=True
|
|
)
|
|
|
|
# Remove IDs of users who have recorded meals for the current day
|
|
users_without_meals = set(users) - set(meal_obj)
|
|
users_without_medications = set(users) - set(medication_obj)
|
|
print(f"user id {set(users)}")
|
|
print(
|
|
f"userwithoutmeal {users_without_meals} and users_without_medication {users_without_medications}"
|
|
)
|
|
|
|
notifications_to_create = []
|
|
for user_id in users_without_meals:
|
|
message = "Have you eaten yet? it's been a whiile since you logged a meal."
|
|
notifications_to_create.append(
|
|
InAppNotification(user_id=user_id, message=message)
|
|
)
|
|
|
|
for user_id in users_without_medications:
|
|
message = "Have you taken your medication today? Remember to log your medication to stay on track with your treatment!"
|
|
notifications_to_create.append(
|
|
InAppNotification(user_id=user_id, message=message)
|
|
)
|
|
|
|
# Bulk create notifications
|
|
if notifications_to_create:
|
|
InAppNotification.objects.bulk_create(notifications_to_create)
|