Skip to content

Commit

Permalink
Merge pull request #698 from nearbeach/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
robotichead authored Oct 23, 2024
2 parents 929e8e3 + 0bf11ed commit 4160d47
Show file tree
Hide file tree
Showing 158 changed files with 2,660 additions and 369 deletions.
1,613 changes: 1,436 additions & 177 deletions NearBeach/fixtures/NearBeach_basic_setup.json

Large diffs are not rendered by default.

81 changes: 47 additions & 34 deletions NearBeach/management/commands/runscheduler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.db.models import Q, F

from NearBeach.views.tools.internal_functions import lookup_choice_from_key
from NearBeach.models import (
OBJECT_TEMPLATE_TYPE,
ObjectAssignment,
Expand Down Expand Up @@ -50,7 +49,7 @@

class Command(BaseCommand):
help = "Run this command to run the scheduled jobs within NearBeach. Setup a daily cron job"

def handle(self, *args, **kwargs):
scheduled_objects = ScheduledObject.objects.none()

Expand All @@ -66,6 +65,10 @@ def handle(self, *args, **kwargs):
# Loop through all objects and create them
for single_object in scheduled_objects:
self.create_object(single_object)

@staticmethod
def get_today():
return datetime.datetime.today()

@staticmethod
def create_object(scheduled_object, *args, **kwargs):
Expand Down Expand Up @@ -143,12 +146,13 @@ def create_object(scheduled_object, *args, **kwargs):
scheduled_object.last_run = datetime.date.today()
scheduled_object.save()

# TODO: For those repeat tasks, we might need to add the run count :)

return

@staticmethod
def run_set_day_of_the_week():
def run_set_day_of_the_week(self):
# Get today's date and day of the week
todays_date = datetime.date.today()
todays_date = self.get_today()
todays_day = calendar.day_name[todays_date.weekday()].lower()
last_run = todays_date - datetime.timedelta(days=1)

Expand Down Expand Up @@ -176,10 +180,17 @@ def run_set_day_of_the_week():
) | Q(
last_run__gte=last_run,
)
) & Q(
Q(
number_of_repeats=-1,
) | Q(
number_of_repeats__gte=0,
run_count__lte=F('number_of_repeats'),
)
)
)

# Loop through the potential and then deterine if they should be added to the return results
# Loop through the potential and then deterine if they should be added to the return result
for scheduled_object in potential_scheduled_objects:
# Get JSON value
frequency_attribute = scheduled_object.frequency_attribute
Expand All @@ -195,9 +206,8 @@ def run_set_day_of_the_week():
# Return
return query_set_results

@staticmethod
def run_weekly():
todays_date = datetime.date.today()
def run_weekly(self):
todays_date = self.get_today()
day_of_the_week = calendar.day_name[todays_date.weekday()]
last_run = todays_date - datetime.timedelta(days=7)

Expand All @@ -206,7 +216,7 @@ def run_weekly():
Q(
is_deleted=False,
frequency=SCH_WEEKLY,
frequency_attribute__day_of_the_week=day_of_the_week,
frequency_attribute__day_of_the_week=day_of_the_week.lower(),
start_date__lte=todays_date,
is_active=True,
) & Q(
Expand All @@ -221,12 +231,18 @@ def run_weekly():
) | Q(
last_run__gte=last_run,
)
) & Q(
Q(
number_of_repeats=-1,
) | Q(
number_of_repeats__gte=0,
run_count__lte=F('number_of_repeats'),
)
)
)

@staticmethod
def run_fortnightly():
todays_date = datetime.date.today()
def run_fortnightly(self):
todays_date = self.get_today()
day_of_the_week = calendar.day_name[todays_date.weekday()]
last_run = todays_date - datetime.timedelta(days=14)

Expand All @@ -235,8 +251,7 @@ def run_fortnightly():
Q(
is_deleted=False,
frequency=SCH_FORTNIGHTLY,
frequency_attribute__day_of_the_week=day_of_the_week,
# last_run__gte=last_run,
frequency_attribute__day_of_the_week=day_of_the_week.lower(),
start_date__lte=todays_date,
is_active=True,
) & Q(
Expand All @@ -249,14 +264,13 @@ def run_fortnightly():
Q(
last_run__isnull=True
) | Q(
last_run__gte=last_run
last_run__lte=last_run
)
)
)

@staticmethod
def run_monthly():
todays_date = datetime.date.today()
def run_monthly(self):
todays_date = self.get_today()
last_run = todays_date - datetime.timedelta(days=14)

# Get data and process
Expand All @@ -265,6 +279,7 @@ def run_monthly():
is_deleted=False,
frequency=SCH_MONTHLY,
start_date__lte=todays_date,
start_date__day=todays_date.day,
is_active=True,
) & Q(
Q(
Expand All @@ -276,15 +291,14 @@ def run_monthly():
Q(
last_run__isnull=True
) | Q(
last_run__gte=last_run
last_run__lte=last_run
)
)
)

@staticmethod
def run_start_of_the_month():
def run_start_of_the_month(self):
# If today is not the 1st - we will just leave
todays_date = datetime.date.today()
todays_date = self.get_today()
last_run = todays_date - datetime.timedelta(days=14)
if todays_date.day != 1:
return ScheduledObject.objects.none()
Expand All @@ -306,15 +320,14 @@ def run_start_of_the_month():
Q(
last_run__isnull=True,
) | Q(
last_run__gte=last_run,
last_run__lte=last_run,
)
)
)

@staticmethod
def run_end_of_the_month():
def run_end_of_the_month(self):
# If today is not the end of the month - we will just leave
todays_date = datetime.date.today()
todays_date = self.get_today()
end_month_date = calendar.monthrange(todays_date.year, todays_date.month)[1]
last_run = todays_date - datetime.timedelta(days=14)
if todays_date.day != end_month_date:
Expand All @@ -329,23 +342,23 @@ def run_end_of_the_month():
is_active=True,
) & Q(
Q(
end_date__gte=todays_date,
end_date__year__gte=todays_date.year,
end_date__month__gte=todays_date.month,
) | Q(
end_date__isnull=True,
)
) & Q(
Q(
last_run__isnull=True,
) | Q(
last_run__gte=last_run,
last_run__lte=last_run,
)
)
)

@staticmethod
def run_x_days_before_end_of_the_month():
def run_x_days_before_end_of_the_month(self):
# If today's date is earlier than the 14th, just leave
todays_date = datetime.date.today()
todays_date = self.get_today()
last_run = todays_date - datetime.timedelta(days=14)
if todays_date.day < 14:
return ScheduledObject.objects.none()
Expand All @@ -372,7 +385,7 @@ def run_x_days_before_end_of_the_month():
Q(
last_run__isnull=True,
) | Q(
last_run__gte=last_run,
last_run__lte=last_run,
)
)
)
Loading

0 comments on commit 4160d47

Please sign in to comment.