refactor(manage_notification): solved the issue of cron for push notification and filter in report
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
*$py.class
|
*$py.class
|
||||||
|
.vscode
|
||||||
|
|
||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
|
|||||||
@@ -716,7 +716,7 @@ class MealDateAPIView(APIView):
|
|||||||
return ApiResponse.error(
|
return ApiResponse.error(
|
||||||
message=constants.FAILURE, errors="Invalid date format"
|
message=constants.FAILURE, errors="Invalid date format"
|
||||||
)
|
)
|
||||||
obj = self.model.objects.filter(principal=request.user.id, date=date_obj)
|
obj = self.model.objects.filter(principal=request.user.id, date=date_obj, deleted=False)
|
||||||
serializer = self.serializer_class(obj, many=True)
|
serializer = self.serializer_class(obj, many=True)
|
||||||
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
|
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
|
||||||
|
|
||||||
|
|||||||
@@ -256,13 +256,16 @@ class GoogleSignin(APIView):
|
|||||||
player_id = request.data["player_id"]
|
player_id = request.data["player_id"]
|
||||||
user_info = GoogleAuthService.get_user_info(access_token)
|
user_info = GoogleAuthService.get_user_info(access_token)
|
||||||
|
|
||||||
print(f"User Info : {user_info}")
|
print(f"User Info : {user_info} and player id is {player_id}")
|
||||||
|
|
||||||
# Authenticate user with the email provided by Google
|
# Authenticate user with the email provided by Google
|
||||||
user = IAmPrincipal.objects.filter(email=user_info['email']).first(
|
user = IAmPrincipal.objects.filter(email=user_info['email']).first(
|
||||||
) or authenticate(email=user_info['email'], password=None)
|
) or authenticate(email=user_info['email'], password=None)
|
||||||
|
|
||||||
if user is None:
|
if user:
|
||||||
|
# Update the player_id for the existing user
|
||||||
|
IAmPrincipal.objects.filter(email=user_info['email']).update(player_id=player_id)
|
||||||
|
else:
|
||||||
# Create a new user if not found
|
# Create a new user if not found
|
||||||
user = IAmPrincipal.objects.create_user(
|
user = IAmPrincipal.objects.create_user(
|
||||||
username=user_info['email'],
|
username=user_info['email'],
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ from django_datatables_view.base_datatable_view import BaseDatatableView
|
|||||||
|
|
||||||
from module_iam import iam_constant, permission
|
from module_iam import iam_constant, permission
|
||||||
from module_project import constants
|
from module_project import constants
|
||||||
from module_project.mixins import ActionMixin, DatatablesMixin
|
from module_project.mixins import ActionMixin
|
||||||
from module_project.utils import JsonResponseUtil
|
from module_project.utils import JsonResponseUtil
|
||||||
|
|
||||||
from .forms import (CustomAuthenticationForm, IAmPrincipalForm, IAmPrincipalResourceLinkForm,
|
from .forms import (CustomAuthenticationForm, IAmPrincipalForm, IAmPrincipalResourceLinkForm,
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
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)
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from ...cron_job import notification_for_meal_and_medication
|
from ...tasks import notification_for_meal_and_medication
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = 'Sends notifications to users'
|
help = 'Sends notifications to users'
|
||||||
|
|||||||
116
module_notification/tasks.py
Normal file
116
module_notification/tasks.py
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
from datetime import datetime, timedelta
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
from module_project.service import OneSignalService
|
||||||
|
from .models import InAppNotification
|
||||||
|
from module_iam.models import IAmPrincipal, IAmPrincipalType
|
||||||
|
from module_activity.models import MealRecord, Bowel, MealSymptomRecord, Medication
|
||||||
|
|
||||||
|
|
||||||
|
def notification_for_meal():
|
||||||
|
notification_message = "You haven’t filled your meal details yet."
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
# Remove IDs of users who have recorded meals for the current day
|
||||||
|
users_without_meals = set(users) - set(meal_obj)
|
||||||
|
print(f"user id {set(users)}")
|
||||||
|
print(
|
||||||
|
f"userwithoutmeal {users_without_meals}"
|
||||||
|
)
|
||||||
|
|
||||||
|
player_ids = list(
|
||||||
|
IAmPrincipal.objects.filter(
|
||||||
|
id__in=users_without_meals
|
||||||
|
).values_list("player_id", flat=True)
|
||||||
|
)
|
||||||
|
# removing none from list
|
||||||
|
player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))
|
||||||
|
|
||||||
|
notifications_to_create = []
|
||||||
|
if users_without_meals:
|
||||||
|
for user_id in users_without_meals:
|
||||||
|
message = notification_message
|
||||||
|
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)
|
||||||
|
|
||||||
|
try:
|
||||||
|
notification = OneSignalService()
|
||||||
|
response = notification.send_notification(
|
||||||
|
headings="Meal",
|
||||||
|
contents=notification_message,
|
||||||
|
include_player_ids=player_ids,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Notification error in meal {str(e)}")
|
||||||
|
pass
|
||||||
|
|
||||||
|
def notification_for_medication():
|
||||||
|
notification_message = "You haven’t filled your medication details yet. "
|
||||||
|
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)
|
||||||
|
|
||||||
|
medication_obj = Medication.objects.filter(date=current_date).values_list(
|
||||||
|
"principal", flat=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Remove IDs of users who have recorded medication for the current day
|
||||||
|
users_without_medications = set(users) - set(medication_obj)
|
||||||
|
print(f"user id {set(users)}")
|
||||||
|
print(
|
||||||
|
f"users_without_medication {users_without_medications}"
|
||||||
|
)
|
||||||
|
|
||||||
|
player_ids = list(
|
||||||
|
IAmPrincipal.objects.filter(
|
||||||
|
id__in=users_without_medications
|
||||||
|
).values_list("player_id", flat=True)
|
||||||
|
)
|
||||||
|
# removing none from list
|
||||||
|
player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))
|
||||||
|
|
||||||
|
notifications_to_create = []
|
||||||
|
if users_without_medications:
|
||||||
|
for user_id in users_without_medications:
|
||||||
|
message = notification_message
|
||||||
|
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)
|
||||||
|
|
||||||
|
try:
|
||||||
|
notification = OneSignalService()
|
||||||
|
response = notification.send_notification(
|
||||||
|
headings="Medication",
|
||||||
|
contents=notification_message,
|
||||||
|
include_player_ids=player_ids,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Notification error in medication {str(e)}")
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def testing_cron():
|
||||||
|
print("cron is working ")
|
||||||
@@ -181,6 +181,8 @@ class NotificationSendView(generic.View):
|
|||||||
# removing none from list
|
# removing none from list
|
||||||
player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))
|
player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))
|
||||||
|
|
||||||
|
print(f"player id is {player_ids}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
notification = OneSignalService()
|
notification = OneSignalService()
|
||||||
response = notification.send_notification(
|
response = notification.send_notification(
|
||||||
|
|||||||
@@ -285,8 +285,8 @@ SIMPLE_JWT = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CRONJOBS = [
|
CRONJOBS = [
|
||||||
('0 18 * * *', 'manage_notification.cron_job.notification_for_meal_and_medication'),
|
('0 18 * * *', 'module_notification.tasks.notification_for_meal', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))),
|
||||||
]
|
('0 20 * * *', 'module_notification.tasks.notification_for_medication', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))),
|
||||||
|
# ('* * * * *', 'module_notification.tasks.testing_cron', '>> {}'.format(str(BASE_DIR / 'logs/cron.log'))),
|
||||||
|
|
||||||
# Additional configuration for cron jobs
|
]
|
||||||
CRONTAB_LOCK_JOBS = True
|
|
||||||
|
|||||||
@@ -352,8 +352,10 @@
|
|||||||
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink" style="">
|
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink" style="">
|
||||||
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(7)">Last 7 days</a>
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(7)">Last 7 days</a>
|
||||||
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(20)">Last 20 days</a>
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(20)">Last 20 days</a>
|
||||||
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(30)">Last 30 days</a>
|
||||||
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(40)">Last 40 days</a>
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(40)">Last 40 days</a>
|
||||||
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(60)">Last 60 days</a>
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(60)">Last 60 days</a>
|
||||||
|
<a class="dropdown-item" href="javascript:void(0)" onclick="getReportData(90)">Last 90 days</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user