Exemple #1
0
 def test_from_date_and_strftime(self):
     python2_eol = Month.from_date(date(2020, 1, 1))
     self.assertEqual(python2_eol, Month(2020, 1))
     leap_month = Month.from_date(date(2000, 2, 29))
     self.assertEqual(leap_month, Month(2000, 2))
     self.assertEqual(python2_eol.strftime('%Y-%m'), "2020-01")
     with set_locale('C'):
         self.assertEqual(leap_month.strftime('%b %Y'), "Feb 2000")
         self.assertEqual(python2_eol.strftime('%b %Y'), "Jan 2020")
    def validate(self, data):
        """
        Check that:
          (1) month has a rotation (?)
          (2) intern has enough remaining days of the selected leave type
          (3) start and end date are actually within the selected month
          (4) end date is after (or equal to) start date
        """
        intern = data['intern']
        internship = intern.profile.intern.internship

        leave_type = data['type']
        leave_setting = intern.leave_settings.get(type=leave_type)

        month = data['month']
        start_date = data['start_date']
        end_date = data['end_date']

        errors = []

        if not internship.rotations.current_for_month(month):
            errors.append("This month has no rotation.")

        leave_length = (end_date - start_date).days + 1
        remaining_days = leave_setting.remaining_days
        if leave_length > remaining_days:
            errors.append("You are requesting %d days of %s, but you only have %d days available." % (
                leave_length,
                leave_type.name.lower(),
                remaining_days,
            ))

        if Month.from_date(start_date) != month:
            errors.append("Start date should be within the selected month.")

        if Month.from_date(end_date) != month:
            errors.append("End date should be within the selected month.")

        if not end_date >= start_date:
            errors.append("End date should be after or equal to start date.")

        if errors:
            raise serializers.ValidationError(errors)

        return data
 def to_python(self, value):
     if isinstance(value, Month):
         month = value
     elif isinstance(value, datetime.date):
         month = Month.from_date(value)
     elif isinstance(value, basestring):
         month = Month.from_string(value)
     else:
         month = None
     return month
Exemple #4
0
 def to_python(self, value):
     if isinstance(value, Month):
         month = value
     elif isinstance(value, datetime.date):
         month = Month.from_date(value)
     elif isinstance(value, basestring):
         month = Month.from_string(value)
     else:
         month = None
     return month
 def to_python(self, value):
     if isinstance(value, Month):
         month = value
     elif isinstance(value, datetime.date):
         month = Month.from_date(value)
         if len(str(month.year)) < 4:
             raise exceptions.ValidationError(
                 self.error_messages['invalid_year'],
                 code='invalid_year',
                 params={'value': value},
             )
     elif isinstance(value, string_type):
         month = Month.from_string(value)
     else:
         month = None
     return month
def gen_month_field():
    return Month.from_date(gen_date())
Exemple #7
0
 def get_display_starting_month(self, request):
     return Response({'id': int(Month.from_date(datetime(timezone.now().year, 1, 1)))})