import sys
from django.apps import AppConfig
from .cron import fetchNews
from backdev.settings import DEBUG


class NewsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'News'
    iteration = 0

    def ready(self):
        """
        This runs when the News app is loaded to Django, it calls fetchNews to populate the News table if DEBUG = False (running in dev mode)
        """
        if 'runserver' not in sys.argv or NewsConfig.iteration != 0 or not DEBUG:
            return True
        fetchNews()
        NewsConfig.iteration += 1
