From c66d8a9b5b2b7b272fddbdab161c3a32babd70d2 Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 2 Mar 2017 22:28:20 -0300 Subject: [PATCH] Adding cron to set goals for unsetted users after limit date --- amadeus/settings.py | 6 ++++-- goals/cron.py | 16 ++++++++++++++++ goals/utils.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 goals/cron.py create mode 100644 goals/utils.py diff --git a/amadeus/settings.py b/amadeus/settings.py index caa288d..0f0cb77 100644 --- a/amadeus/settings.py +++ b/amadeus/settings.py @@ -188,11 +188,13 @@ STATICFILES_DIRS = [ ] CRON_CLASSES = [ - 'notifications.cron.Notify' + 'notifications.cron.Notify', + 'goals.cron.SetGoals' ] CRONJOBS = [ - ('1 */12 * * *', 'notifications.cron.notification_cron') + ('1 */12 * * *', 'notifications.cron.notification_cron'), + ('1 */12 * * *', 'goals.cron.setgoals_cron') ] CHANNEL_LAYERS = { diff --git a/goals/cron.py b/goals/cron.py new file mode 100644 index 0000000..1f93737 --- /dev/null +++ b/goals/cron.py @@ -0,0 +1,16 @@ +import datetime +from django_cron import CronJobBase, Schedule + +from .utils import set_goals + +class SetGoals(CronJobBase): + RUN_EVERY_MINS = 1440 # every day + + schedule = Schedule(run_every_mins=RUN_EVERY_MINS) + code = 'amadeus.goals_cron' # a unique code + + def do(self): + set_goals() + +def setgoals_cron(): + set_goals() \ No newline at end of file diff --git a/goals/utils.py b/goals/utils.py new file mode 100644 index 0000000..c94650d --- /dev/null +++ b/goals/utils.py @@ -0,0 +1,18 @@ +from django.utils import timezone + +from users.models import User + +from .models import GoalItem, MyGoals + +def set_goals(): + specifications = GoalItem.objects.filter(goal__limit_submission_date__date = timezone.now()) + entries = [] + + for goal in specifications: + users = User.objects.filter(subject_student = goal.goal.topic.subject) + + for user in users: + if not MyGoals.objects.filter(user = user, item = goal).exists(): + entries.append(MyGoals(user = user, item = goal, value = goal.ref_value)) + + MyGoals.objects.bulk_create(entries) -- libgit2 0.21.2