import logging
from django.core.management.base import BaseCommand
from apps.processing.ala.util import util
from dateutil.parser import parse
from datetime import date, timedelta

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = 'Import data from ALA stations, optionally you can pass date. Otherwise it will fetch yesterday data'

    def add_arguments(self, parser):
        parser.add_argument('date', nargs='?', type=parse, default=None)

    def handle(self, *args, **options):

        stations = util.get_or_create_stations()
        day = options['date']
        if day is None:
            day = date.today() - timedelta(2)

        logger.info(
            'Importing observations of {} ALA stations from {}.'.format(
                len(stations),
                day
            )
        )

        try:
            for station in stations:
                util.load(station, day)
                util.create_avgs(station, day)
        except Exception as e:
            self.stdout.write(self.style.ERROR(e))

import logging
from django.core.management.base import BaseCommand
from apps.processing.ala.util import util
from dateutil.parser import parse
from dateutil import relativedelta
from datetime import date, timedelta

logger = logging.getLogger(__name__)


def parse_date_range(date_str):
    if len(date_str) == 4:
        day_from = parse(date_str).replace(day=1, month=1)
        day_to = day_from + relativedelta.relativedelta(years=1)
    elif len(date_str) == 7:
        day_from = parse(date_str).replace(day=1)
        day_to = day_from + relativedelta.relativedelta(months=1)
    else:
        day_from = parse(date_str)
        day_to = day_from + timedelta(1)
    return [day_from, day_to]


class Command(BaseCommand):
    help = 'Import data from ALA stations. Optionally you can pass date, ' \
           'otherwise it will fetch the day before yesterday data.'

    def add_arguments(self, parser):
        parser.add_argument('date_range', nargs='?', type=parse_date_range,
                            default=[None, None])

    def handle(self, *args, **options):

        stations = util.get_or_create_stations()
        day_from, day_to = options['date_range']
        if day_from is None:
            day_from = date.today() - timedelta(2)
            day_to = day_from + timedelta(1)

        day = day_from
        while(day < day_to):
            logger.info(
                'Importing observations of {} ALA stations from {}.'.format(
                    len(stations),
                    day
                )
            )
            for station in stations:
                util.load(station, day)
                util.create_avgs(station, day)
            day += timedelta(1)

Enable ALA import by month or year
Enable ALA import by month or year

  import logging
  from django.core.management.base import BaseCommand
  from apps.processing.ala.util import util
  from dateutil.parser import parse
+ from dateutil import relativedelta
  from datetime import date, timedelta
  
  logger = logging.getLogger(__name__)
  
  
+ def parse_date_range(date_str):
+     if len(date_str) == 4:
+         day_from = parse(date_str).replace(day=1, month=1)
+         day_to = day_from + relativedelta.relativedelta(years=1)
+     elif len(date_str) == 7:
+         day_from = parse(date_str).replace(day=1)
+         day_to = day_from + relativedelta.relativedelta(months=1)
+     else:
+         day_from = parse(date_str)
+         day_to = day_from + timedelta(1)
+     return [day_from, day_to]
+ 
+ 
  class Command(BaseCommand):
-     help = 'Import data from ALA stations, optionally you can pass date. Otherwise it will fetch yesterday data'
+     help = 'Import data from ALA stations. Optionally you can pass date, ' \
+            'otherwise it will fetch the day before yesterday data.'
  
      def add_arguments(self, parser):
-         parser.add_argument('date', nargs='?', type=parse, default=None)
+         parser.add_argument('date_range', nargs='?', type=parse_date_range,
+                             default=[None, None])
  
      def handle(self, *args, **options):
  
          stations = util.get_or_create_stations()
-         day = options['date']
+         day_from, day_to = options['date_range']
-         if day is None:
+         if day_from is None:
-             day = date.today() - timedelta(2)
+             day_from = date.today() - timedelta(2)
+             day_to = day_from + timedelta(1)
  
+         day = day_from
+         while(day < day_to):
-         logger.info(
+             logger.info(
-             'Importing observations of {} ALA stations from {}.'.format(
+                 'Importing observations of {} ALA stations from {}.'.format(
-                 len(stations),
+                     len(stations),
-                 day
+                     day
+                 )
              )
-         )
- 
-         try:
              for station in stations:
                  util.load(station, day)
                  util.create_avgs(station, day)
+             day += timedelta(1)
-         except Exception as e:
-             self.stdout.write(self.style.ERROR(e))
  
Enable ALA import by month or year
## Code Before:
import logging
from django.core.management.base import BaseCommand
from apps.processing.ala.util import util
from dateutil.parser import parse
from datetime import date, timedelta

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = 'Import data from ALA stations, optionally you can pass date. Otherwise it will fetch yesterday data'

    def add_arguments(self, parser):
        parser.add_argument('date', nargs='?', type=parse, default=None)

    def handle(self, *args, **options):

        stations = util.get_or_create_stations()
        day = options['date']
        if day is None:
            day = date.today() - timedelta(2)

        logger.info(
            'Importing observations of {} ALA stations from {}.'.format(
                len(stations),
                day
            )
        )

        try:
            for station in stations:
                util.load(station, day)
                util.create_avgs(station, day)
        except Exception as e:
            self.stdout.write(self.style.ERROR(e))

## Instruction:
Enable ALA import by month or year
## Code After:
import logging
from django.core.management.base import BaseCommand
from apps.processing.ala.util import util
from dateutil.parser import parse
from dateutil import relativedelta
from datetime import date, timedelta

logger = logging.getLogger(__name__)


def parse_date_range(date_str):
    if len(date_str) == 4:
        day_from = parse(date_str).replace(day=1, month=1)
        day_to = day_from + relativedelta.relativedelta(years=1)
    elif len(date_str) == 7:
        day_from = parse(date_str).replace(day=1)
        day_to = day_from + relativedelta.relativedelta(months=1)
    else:
        day_from = parse(date_str)
        day_to = day_from + timedelta(1)
    return [day_from, day_to]


class Command(BaseCommand):
    help = 'Import data from ALA stations. Optionally you can pass date, ' \
           'otherwise it will fetch the day before yesterday data.'

    def add_arguments(self, parser):
        parser.add_argument('date_range', nargs='?', type=parse_date_range,
                            default=[None, None])

    def handle(self, *args, **options):

        stations = util.get_or_create_stations()
        day_from, day_to = options['date_range']
        if day_from is None:
            day_from = date.today() - timedelta(2)
            day_to = day_from + timedelta(1)

        day = day_from
        while(day < day_to):
            logger.info(
                'Importing observations of {} ALA stations from {}.'.format(
                    len(stations),
                    day
                )
            )
            for station in stations:
                util.load(station, day)
                util.create_avgs(station, day)
            day += timedelta(1)
