from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.forms.fields import Field, ValidationError

from tempo.django.widgets import ScheduleSetWidget
from tempo.schedule import Schedule
from tempo.scheduleset import ScheduleSet


class ScheduleSetField(Field):
    widget = ScheduleSetWidget

    def to_python(self, value):
        if value is None:
            return None

        repeats = value['repeats']

        if repeats == 'monthly':
            return ScheduleSet(include=[
                Schedule(days=[int(value['repeatOn'])])
            ])
        elif repeats == 'weekly':
            schedules = []
            for repeat_on in value['repeatOn']:
                if Decimal(repeat_on['from']) > Decimal(repeat_on['to']):
                    raise ValidationError(_('"From" is greater than "to".'),
                                          code='invalid')

                schedule = Schedule(
                    weekdays=[int(repeat_on['weekday'])],
                    days=[],
                    seconds_of_the_day=list(range(
                        int(Decimal(repeat_on['from']) * 60 * 60),
                        int(Decimal(repeat_on['to']) * 60 * 60) + 1)
                    ),
                    seconds=[], minutes=[], hours=[]
                )
                schedules.append(schedule)

            return ScheduleSet(include=schedules)
        else:
            raise ValueError

from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.forms.fields import Field, ValidationError

from tempo.django.widgets import ScheduleSetWidget
from tempo.schedule import Schedule
from tempo.scheduleset import ScheduleSet


class ScheduleSetField(Field):
    widget = ScheduleSetWidget

    def to_python(self, value):
        if value is None:
            return None

        repeats = value['repeats']

        if repeats == 'monthly':
            return ScheduleSet(include=[
                Schedule(days=[int(value['repeatOn'])])
            ])
        elif repeats == 'weekly':
            schedules = []
            for repeat_on in value['repeatOn']:
                if Decimal(repeat_on['from']) >= Decimal(repeat_on['to']):
                    raise ValidationError(_('"From" is greater than "to" '
                                            'or equal to it.'),
                                          code='invalid')

                schedule = Schedule(
                    weekdays=[int(repeat_on['weekday'])],
                    days=[],
                    seconds_of_the_day=list(range(
                        int(Decimal(repeat_on['from']) * 60 * 60),
                        int(Decimal(repeat_on['to']) * 60 * 60))
                    ),
                    seconds=[], minutes=[], hours=[]
                )
                schedules.append(schedule)

            return ScheduleSet(include=schedules)
        else:
            raise ValueError

Make "to" not inclusive in widget
Make "to" not inclusive in widget

To avoid exceeding possible values.

  from decimal import Decimal
  
  from django.utils.translation import ugettext_lazy as _
  from django.forms.fields import Field, ValidationError
  
  from tempo.django.widgets import ScheduleSetWidget
  from tempo.schedule import Schedule
  from tempo.scheduleset import ScheduleSet
  
  
  class ScheduleSetField(Field):
      widget = ScheduleSetWidget
  
      def to_python(self, value):
          if value is None:
              return None
  
          repeats = value['repeats']
  
          if repeats == 'monthly':
              return ScheduleSet(include=[
                  Schedule(days=[int(value['repeatOn'])])
              ])
          elif repeats == 'weekly':
              schedules = []
              for repeat_on in value['repeatOn']:
-                 if Decimal(repeat_on['from']) > Decimal(repeat_on['to']):
+                 if Decimal(repeat_on['from']) >= Decimal(repeat_on['to']):
-                     raise ValidationError(_('"From" is greater than "to".'),
+                     raise ValidationError(_('"From" is greater than "to" '
+                                             'or equal to it.'),
                                            code='invalid')
  
                  schedule = Schedule(
                      weekdays=[int(repeat_on['weekday'])],
                      days=[],
                      seconds_of_the_day=list(range(
                          int(Decimal(repeat_on['from']) * 60 * 60),
-                         int(Decimal(repeat_on['to']) * 60 * 60) + 1)
+                         int(Decimal(repeat_on['to']) * 60 * 60))
                      ),
                      seconds=[], minutes=[], hours=[]
                  )
                  schedules.append(schedule)
  
              return ScheduleSet(include=schedules)
          else:
              raise ValueError
  
Make "to" not inclusive in widget
## Code Before:
from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.forms.fields import Field, ValidationError

from tempo.django.widgets import ScheduleSetWidget
from tempo.schedule import Schedule
from tempo.scheduleset import ScheduleSet


class ScheduleSetField(Field):
    widget = ScheduleSetWidget

    def to_python(self, value):
        if value is None:
            return None

        repeats = value['repeats']

        if repeats == 'monthly':
            return ScheduleSet(include=[
                Schedule(days=[int(value['repeatOn'])])
            ])
        elif repeats == 'weekly':
            schedules = []
            for repeat_on in value['repeatOn']:
                if Decimal(repeat_on['from']) > Decimal(repeat_on['to']):
                    raise ValidationError(_('"From" is greater than "to".'),
                                          code='invalid')

                schedule = Schedule(
                    weekdays=[int(repeat_on['weekday'])],
                    days=[],
                    seconds_of_the_day=list(range(
                        int(Decimal(repeat_on['from']) * 60 * 60),
                        int(Decimal(repeat_on['to']) * 60 * 60) + 1)
                    ),
                    seconds=[], minutes=[], hours=[]
                )
                schedules.append(schedule)

            return ScheduleSet(include=schedules)
        else:
            raise ValueError

## Instruction:
Make "to" not inclusive in widget
## Code After:
from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.forms.fields import Field, ValidationError

from tempo.django.widgets import ScheduleSetWidget
from tempo.schedule import Schedule
from tempo.scheduleset import ScheduleSet


class ScheduleSetField(Field):
    widget = ScheduleSetWidget

    def to_python(self, value):
        if value is None:
            return None

        repeats = value['repeats']

        if repeats == 'monthly':
            return ScheduleSet(include=[
                Schedule(days=[int(value['repeatOn'])])
            ])
        elif repeats == 'weekly':
            schedules = []
            for repeat_on in value['repeatOn']:
                if Decimal(repeat_on['from']) >= Decimal(repeat_on['to']):
                    raise ValidationError(_('"From" is greater than "to" '
                                            'or equal to it.'),
                                          code='invalid')

                schedule = Schedule(
                    weekdays=[int(repeat_on['weekday'])],
                    days=[],
                    seconds_of_the_day=list(range(
                        int(Decimal(repeat_on['from']) * 60 * 60),
                        int(Decimal(repeat_on['to']) * 60 * 60))
                    ),
                    seconds=[], minutes=[], hours=[]
                )
                schedules.append(schedule)

            return ScheduleSet(include=schedules)
        else:
            raise ValueError
