pythondjangodjango-modelsjobsapscheduler

Django Custom Commands Not Creating New Entries


I'm pretty new to Django, and I'm working on a simple app that generates json forecast data for a list of locations daily. This data is then used to display the forecasts to the user. I have a startjobs.py set up to run some functions that generate the json data I need for my app.

I'm running into a problem where the jobs are successfully running, however, no new entries are added and no new json files created. I've looked through some similar questions, but couldn't find any solutions that work for my case.

Here are my models:

class Location(models.Model):
    location = models.CharField(max_length = 75, unique = True)

    def __str__(self):
        return f"{self.location}"


INTERVAL_CHOICES = [
    ('today', 'Today'),
    ('hourly', 'Hourly'),
    ('daily', 'Daily'),
]

class Forecast(models.Model):
    location =  models.ForeignKey(Location, on_delete = models.CASCADE)
    date = models.DateField()
    interval = models.CharField(max_length = 6, choices = INTERVAL_CHOICES, default = 'today')
    filename = models.FileField(upload_to = 'test/')

    def __str__(self):
        return f"Forecast for max wave height - {self.interval} in {self.location} on {self.date}"

And my startjobs.py (I have my job running every 2 minutes for testing):

locations = Location.objects.values_list("location", flat=True)

def save_new_forecasts():

    date = dt.date.today().strftime("%Y-%m-%d")

    for location in locations:
        if not Forecast.objects.filter(date = date).exists:
            daily_data = create_daily_forecast(location)
            hourly_data = create_hourly_forecast(location)
            daily_forecast = Forecast(
                location = location,
                date = date,
                interval = daily_data[0],
                filename = daily_data[1]
            )
            daily_forecast.save()
            hourly_forecast = Forecast(
                location_id = location,
                date = date,
                interval = hourly_data[0],
                filename = hourly_data[1]
            )
            hourly_forecast.save()

def create_daily_forecast(location):

    interval = "daily"
    filename = fg_daily.generate_daily_forecast(location, 10)

    return [interval, filename]

def create_hourly_forecast(location):
   
    location_id = location
    date = dt.date.today().strftime("%Y.%m.%d")
    interval = "hourly"
    filename = fg_hourly.generate_hourly_forecast(location, 24)

    return [location_id, date, interval, filename]

def delete_old_job_executions(max_age=604_800):
    DjangoJobExecution.objects.delete_old_job_executions(max_age)

class Command(BaseCommand):
    help = "Runs apscheduler."

    def handle(self, *args, **options):
        scheduler = BlockingScheduler(timezone=settings.TIME_ZONE)
        scheduler.add_jobstore(DjangoJobStore(), "default")

        scheduler.add_job(
            save_new_forecasts,
            trigger="interval",
            minutes=2,
            id="Forecasts",
            max_instances=1,
            replace_existing=True,
        )
        logger.info("Added job: Save New Forecasts")

        scheduler.add_job(
            delete_old_job_executions,
            trigger=CronTrigger(
                day_of_week="mon", hour="00", minute="00"
            ),  # Midnight on Monday, before start of the next work week.
            id="Delete Old Job Executions",
            max_instances=1,
            replace_existing=True,
        )
        logger.info("Added weekly job: Delete Old Job Executions.")

        try:
            logger.info("Starting scheduler...")
            scheduler.start()
        except KeyboardInterrupt:
            logger.info("Stopping scheduler...")
            scheduler.shutdown()
            logger.info("Scheduler shut down successfully!")

EDIT:

I thought it might also be helpful to see some of the functions that create the json:

def generate_json(location, interval, data):
    df = pandas.DataFrame(data=data)
    filename = generate_filename(location, interval)
    df.to_json(f'{PATH}/{filename}', orient='records')
    return filename
def generate_daily_forecast(location, length):
    date_list = generate_date_list(length)
    height_list = generate_height_list(length)
    keys = ["date", "max_wave_height"]
    values = ['', '']
    data = []

    for dates in date_list:
        i = date_list.index(dates)
        values[0] = date_list[i]
        values[1] = height_list[i]
        pre_data = dict(zip(keys, values))
        data.append(pre_data)

    filename = generate_json(location, 'daily', data)
    return filename

Any help would be greatly appreciated! Thanks!


Solution

  • You need to filter date and location in the if Statement:

    def save_new_forecasts():
    
        date = dt.date.today().strftime("%Y-%m-%d")
    
        for location in locations:
            if not Forecast.objects.filter(date = date).exists: