Пример #1
0
    def test_is_holiday(self):
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2017, month=3, day=16)),
            [self.easter_2017.to_json()])

        self.assertFalse(Holiday.is_holiday(datetime.date(year=2017, month=3, day=15)))
        self.assertFalse(Holiday.is_holiday(datetime.date(year=2017, month=3, day=17)))
Пример #2
0
    def test_holidays_between_dates(self):
        start_date = datetime.date(year=2017, month=1, day=1)
        end_date = datetime.date(year=2017, month=12, day=31)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 4)
        self.assertListEqual(holidays, [
            self.new_years.to_json(2017),
            self.christmas_eve.to_json(2017),
            self.christmas.to_json(2017),
            self.new_years_eve.to_json(2017),
        ])

        end_date = datetime.date(year=2018, month=12, day=24)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 6)
        self.assertListEqual(holidays, [
            self.new_years.to_json(2017),
            self.christmas_eve.to_json(2017),
            self.christmas.to_json(2017),
            self.new_years_eve.to_json(2017),
            self.new_years.to_json(2018),
            self.christmas_eve.to_json(2018),
        ])

        start_date = datetime.date(year=2016, month=1, day=2)
        end_date = datetime.date(year=2016, month=12, day=23)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 0)
Пример #3
0
    def test_get_holidays_year(self):
        holidays = Holiday.get_holidays_for_year(2017)
        self.assertEqual(len(holidays), 4)
        self.assertListEqual(holidays, [
            self.new_years.to_json(2017),
            self.christmas_eve.to_json(2017),
            self.christmas.to_json(2017),
            self.new_years_eve.to_json(2017),
        ])

        holidays = Holiday.get_holidays_for_year(2018)
        self.assertEqual(len(holidays), 4)
        self.assertListEqual(holidays, [
            self.new_years.to_json(2018),
            self.christmas_eve.to_json(2018),
            self.christmas.to_json(2018),
            self.new_years_eve.to_json(2018),
        ])

        holidays = Holiday.get_holidays_for_year(2019)
        self.assertEqual(len(holidays), 4)
        self.assertListEqual(holidays, [
            self.new_years.to_json(2019),
            self.christmas_eve.to_json(2019),
            self.christmas.to_json(2019),
            self.new_years_eve.to_json(2019),
        ])
Пример #4
0
    def test_holidays_between_dates(self):
        start_date = datetime.date(year=2017, month=1, day=1)
        end_date = datetime.date(year=2017, month=12, day=31)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 3)
        self.assertListEqual(holidays, [
            self.mlkday.to_json(2017),
            self.laborday.to_json(2017),
            self.thanksgiving.to_json(2017),
        ])

        end_date = datetime.date(year=2018, month=9, day=4)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 5)
        self.assertListEqual(holidays, [
            self.mlkday.to_json(2017),
            self.laborday.to_json(2017),
            self.thanksgiving.to_json(2017),
            self.mlkday.to_json(2018),
            self.laborday.to_json(2018),
        ])

        start_date = datetime.date(year=2016, month=1, day=19)
        end_date = datetime.date(year=2016, month=9, day=4)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 0)
Пример #5
0
    def save(self, *args, **kwargs):
        user_profile = UserProfile.objects.get(user=self.created_by)
        super(RecurringTicket, self).save(*args, **kwargs)
        today = datetime.date.today()
        if self.form_id is None:
            self.form_id = self.get_unique_id(time=self.created_at,
                business=user_profile.business.short_name)
            self.save()

        no_current_tickets = not(bool(self.generalticket_set.filter(
            status__terminal=False).count()))

        if ((self.repeat_type == self.DAILY) and
            no_current_tickets and
            (today.weekday()<=4) and
            (not Holiday.is_holiday(today))):

            self.create_general_ticket()

        elif ((self.repeat_type == self.WEEKLY) and 
            no_current_tickets and
            (today.weekday() in self.get_weekdays) and
            (not Holiday.is_holiday(today))):

            self.create_general_ticket()

        elif ((self.repeat_type == self.MONTHLY) and
            no_current_tickets and
            (today.day in self.get_month_days) and
            (not Holiday.is_holiday(today))):

            self.create_general_ticket()

        elif ((self.repeat_type == self.NTH_WEEKDAY) and
            no_current_tickets and
            (today.weekday() in self.get_weekdays) and
            (not Holiday.is_holiday(today))):

            # do a second check to make sure Ordinal matches
            count = 0
            d = today.replace(day=1)
            while d <= today:
                if d.weekday() in self.get_weekdays:
                    count += 1
            if count == self.ordinal:
                self.create_general_ticket()

        elif ((self.repeat_type == self.YEARLY) and
            no_current_tickets and
            (today == self.start_date)):

            self.create_general_ticket()

        else:
            # determine when the next ticket should be created and save that date
            self.next_task_created = self.get_next_day()
            if self.next_task_created < datetime.date.today():
                self.create_general_task()
            super(RecurringTicket, self).save(*args, **kwargs)
Пример #6
0
    def test_is_holiday(self):
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2017, month=1, day=1)),
            [self.new_years.to_json(2017)])

        self.assertFalse(
            Holiday.is_holiday(datetime.date(year=2017, month=1, day=2)))
        self.assertFalse(
            Holiday.is_holiday(datetime.date(year=2017, month=12, day=23)))
Пример #7
0
    def test_is_paid_holiday(self):
        self.assertFalse(Holiday.is_paid_holiday(datetime.date(year=2016, month=1, day=18)))
        self.assertFalse(Holiday.is_paid_holiday(datetime.date(year=2017, month=1, day=16)))

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=9, day=4)),
            [self.laborday.to_json(2017)])

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=11, day=23)),
            [self.thanksgiving.to_json(2017)])
Пример #8
0
    def test_is_paid_holiday(self):
        self.assertFalse(Holiday.is_paid_holiday(datetime.date(year=2016, month=11, day=8)))
        self.assertFalse(Holiday.is_paid_holiday(datetime.date(year=2017, month=11, day=7)))

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=11, day=24)),
            [self.blackfriday.to_json(2017)])

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2018, month=11, day=23)),
            [self.blackfriday.to_json(2018)])
Пример #9
0
    def test_get_holidays_year(self):
        holidays = Holiday.get_holidays_for_year(2017)
        self.assertEqual(len(holidays), 1)
        self.assertDictEqual(holidays[0], self.easter_2017.to_json())

        holidays = Holiday.get_holidays_for_year(2018)
        self.assertEqual(len(holidays), 1)
        self.assertDictEqual(holidays[0], self.easter_2018.to_json())

        holidays = Holiday.get_holidays_for_year(2019)
        self.assertEqual(len(holidays), 0)
Пример #10
0
    def available_hours(self):
        end_date = self.cached_due_date
        if end_date is None or self.effort is None:
            # TODO: what is an appropriate amount of effort (man-days) to set
            #       for a task that has no effort or no due date?  Right now
            #       we set at 2 hours (a quarter-day)
            return 0.25 # or 0.0?
        else:
            if type(end_date) is datetime.datetime:
                end_date = end_date.date()
            # get holidays in years covered across today through
            # requested/scheduled date
            holidays = []
            ptor_time = 0.0
            for year in range(datetime.date.today().year, end_date.year+1):
                holiday_dates = [h['date'] for h in Holiday.get_holidays_for_year(year, {'paid_holiday':True})]
                holidays.extend(holiday_dates)
            
                # also add paid time off requests that have been approved as non working days
                if self.assignee:
                    for ptor in PaidTimeOffRequest.objects.filter(Q(pto_start_date__year=year)|Q(pto_end_date__year=year)
                        ).filter(Q(status=PaidTimeOffRequest.APPROVED)|Q(status=PaidTimeOffRequest.PROCESSED)
                        ).filter(user_profile=self.assignee.profile):
                        for i in range((ptor.pto_end_date-ptor.pto_start_date).days):
                            holidays.append(ptor.pto_start_date+datetime.timedelta(days=i))
                        if float(ptor.amount) < 8.0:
                            ptor_time += 8.0 - float(ptor.amount)

            # get number of workdays found in between the today and end_date
            num_workdays = workdays.networkdays(datetime.date.today(),
                                                end_date,
                                                holidays)
            num_workdays = num_workdays - (float(ptor_time)/8.0)
            return 8.0 * max(0.5,num_workdays)
Пример #11
0
    def test_holidays_between_dates(self):
        start_date = datetime.date(year=2017, month=3, day=16)
        end_date = datetime.date(year=2018, month=3, day=31)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 1)
        self.assertDictEqual(holidays[0], self.easter_2017.to_json())

        end_date = datetime.date(year=2018, month=4, day=1)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 2)
        self.assertListEqual(holidays, [self.easter_2017.to_json(), self.easter_2018.to_json()])

        start_date = datetime.date(year=2016, month=3, day=28)
        end_date = datetime.date(year=2017, month=3, day=15)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 0)
Пример #12
0
    def test_is_paid_holiday(self):
        self.assertFalse(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=12,
                                                  day=24)))
        self.assertFalse(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=12,
                                                  day=31)))

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=1, day=1)),
            [
                self.new_years.to_json(2017),
            ])

        self.assertEqual(
            Holiday.is_paid_holiday(datetime.date(year=2017, month=12,
                                                  day=25)),
            [
                self.christmas.to_json(2017),
            ])
Пример #13
0
    def test_get_holidays_year(self):
        holidays = Holiday.get_holidays_for_year(2017)
        self.assertEqual(len(holidays), 2)
        self.assertListEqual(holidays, [
            self.electionday.to_json(2017),
            self.blackfriday.to_json(2017),
        ])

        holidays = Holiday.get_holidays_for_year(2018)
        self.assertEqual(len(holidays), 2)
        self.assertListEqual(holidays, [
            self.electionday.to_json(2018),
            self.blackfriday.to_json(2018),
        ])

        holidays = Holiday.get_holidays_for_year(2019)
        self.assertEqual(len(holidays), 2)
        self.assertListEqual(holidays, [
            self.electionday.to_json(2019),
            self.blackfriday.to_json(2019),
        ])
Пример #14
0
    def test_holidays_between_dates(self):
        start_date = datetime.date(year=2017, month=1, day=1)
        end_date = datetime.date(year=2017, month=12, day=31)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 2)
        self.assertListEqual(holidays, [
            self.electionday.to_json(2017),
            self.blackfriday.to_json(2017),
        ])

        end_date = datetime.date(year=2018, month=11, day=6)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 3)
        self.assertListEqual(holidays, [
            self.electionday.to_json(2017),
            self.blackfriday.to_json(2017),
            self.electionday.to_json(2018),
        ])

        start_date = datetime.date(year=2016, month=1, day=19)
        end_date = datetime.date(year=2016, month=9, day=4)
        holidays = Holiday.holidays_between_dates(start_date, end_date)
        self.assertEqual(len(holidays), 0)
Пример #15
0
    def test_get_holidays_year(self):
        holidays = Holiday.get_holidays_for_year(2017)
        self.assertEqual(len(holidays), 3)
        self.assertListEqual(holidays, [
            self.mlkday.to_json(2017),
            self.laborday.to_json(2017),
            self.thanksgiving.to_json(2017),
        ])

        holidays = Holiday.get_holidays_for_year(2018)
        self.assertEqual(len(holidays), 3)
        self.assertListEqual(holidays, [
            self.mlkday.to_json(2018),
            self.laborday.to_json(2018),
            self.thanksgiving.to_json(2018),
        ])

        holidays = Holiday.get_holidays_for_year(2019)
        self.assertEqual(len(holidays), 3)
        self.assertListEqual(holidays, [
            self.mlkday.to_json(2019),
            self.laborday.to_json(2019),
            self.thanksgiving.to_json(2019),
        ])
Пример #16
0
    def test_is_holiday(self):
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2016, month=11, day=24)),
            [self.thanksgiving.to_json(2016)])
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2017, month=11, day=23)),
            [self.thanksgiving.to_json(2017)])
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2018, month=11, day=22)),
            [self.thanksgiving.to_json(2018)])

        self.assertFalse(Holiday.is_holiday(datetime.date(year=2016, month=11, day=23)))
        self.assertFalse(Holiday.is_holiday(datetime.date(year=2017, month=11, day=24)))
        self.assertFalse(Holiday.is_holiday(datetime.date(year=2018, month=11, day=23)))
Пример #17
0
    def test_is_holiday(self):
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2016, month=11, day=8)),
            [self.electionday.to_json(2016)])
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2017, month=11, day=7)),
            [self.electionday.to_json(2017)])
        self.assertEqual(
            Holiday.is_holiday(datetime.date(year=2018, month=11, day=6)),
            [self.electionday.to_json(2018)])

        self.assertFalse(Holiday.is_holiday(datetime.date(year=2016, month=11, day=7)))
        self.assertFalse(Holiday.is_holiday(datetime.date(year=2017, month=11, day=6)))
        self.assertFalse(Holiday.is_holiday(datetime.date(year=2018, month=11, day=7)))
Пример #18
0
    def set_cached_due_date(self):
        if self.fixed_due_date:
            self.cached_due_date = self.fixed_due_date
        else:
            delayed_days = self.relative_due_days_delta
            delta = 1 if self.relative_due_days_delta >= 0 else -1
            dates = [datetime.date.today(),
                datetime.date.today() + relativedelta(days=2*delayed_days)]
            holidays = Holiday.holidays_between_dates(min(dates), max(dates))
            
            if self.relative_due_date_ticket.cached_due_date is None:
                # we want to recursively call this to make sure everything is
                # up to date when we are setting a cached value
                self.relative_due_date_ticket.set_cached_due_date()

            due_date = self.relative_due_date_ticket.cached_due_date
            while delayed_days != 0:
                due_date = due_date + relativedelta(days=delta)
                if due_date not in holidays and due_date.weekday() < 5:
                    delayed_days  -= delta

            self.cached_due_date = due_date
Пример #19
0
 def test_get_holidays(self):
     self.assertListEqual(list(Holiday.get_available_holidays()), [
         'Christmas', 'Christmas Eve', 'New Year\'s Day', 'New Year\'s Eve'
     ])
Пример #20
0
 def test_get_holidays(self):
     self.assertListEqual(
         list(Holiday.get_available_holidays()),
         ['MLK Day', 'Labor Day', 'Thanksgiving'])
Пример #21
0
    def get_next_day(self):
        """Based on the recurrence pattern defined in the instance,
        this function determines when the next occurence should be.
        """
        # assumes that today is not an option
        next_date = datetime.date.today()
        if self.last_ticket_created:
            kwarg = {'%ss'%self.due_date_unit: self.due_date_interval}
            next_date = self.last_ticket_created + relativedelta(**kwarg) + \
                relativedelta(days=1)
        
        next_date = max(next_date, datetime.date.today() + datetime.timedelta(days=1))

        if self.repeat_type == '':
            # if there is no recurrence, simply return the one-time date
            return self.start_date

        elif self.repeat_type == self.DAILY:
            # make sure it is not a weekend or a holiday
            while ((next_date.weekday() >= 5) or 
                   (Holiday.is_paid_holiday(next_date))):
                next_date += datetime.timedelta(days=1)

        elif self.repeat_type == self.WEEKLY:
            # TODO: only works for repeat_interval 1 right now
            while ((next_date.weekday() not in self.get_weekdays) or 
                   (next_date.weekday() >= 5)):
                next_date += datetime.timedelta(days=1)
            
            # after matching the recurrence pattern, make sure it
            # is not a holiday.  If it is, back track to the first
            # working day
            if Holiday.is_paid_holiday(next_date):
                while ((next_date.weekday() >= 5) or 
                   (Holiday.is_paid_holiday(next_date))):
                    next_date -= datetime.timedelta(days=1)

        elif self.repeat_type == self.MONTHLY:
            # TODO: only works for repeat_interval 1 right now
            while next_date.day not in self.get_current_month_days(next_date) or next_date < datetime.date.today():
                next_date += datetime.timedelta(days=1)
            
            # after matching the recurrence pattern, make sure it
            # is not a holiday.  If it is, back track to the first
            # working day
            if Holiday.is_paid_holiday(next_date):
                while ((next_date.weekday() >= 5) or 
                   (Holiday.is_paid_holiday(next_date))):
                    next_date -= datetime.timedelta(days=1)

        elif self.repeat_type == self.NTH_WEEKDAY:
            dow = self.get_weekdays[0]
            if self.last_ticket_created:
                next_date = self.last_ticket_created.replace(day=1)
                for i in range(self.repeat_interval):
                    next_date = (next_date + datetime.timedelta(days=31)).replace(day=1)
            else:
                next_date = next_date.replace(day=1)

            if self.ordinal < 5:
                count = 0
                while count < self.ordinal:
                    if next_date.weekday() == dow:
                        count += 1
                    next_date += relativedelta(days=1)
                next_date -= relativedelta(days=1)
                
            else:
                next_date = datetime.date(next_date.year, next_date.month, 
                    calendar.monthrange(next_date.year,next_date.month)[1])
                while next_date.weekday() != dow:
                    next_date -= relativedelta(days=1)
            
            if next_date > datetime.date.today():
                pass
            else:
                self.last_ticket_created = next_date
                self.save()
                return self.get_next_day()

        elif self.repeat_type == self.YEARLY:
            # TODO: only works for repeat_interval 1 right now
            if self.last_ticket_created:
                # TODO: this should be more robust to not use the last_ticket_created date
                next_date = self.last_ticket_created + relativedelta(years=1)

            else:
                next_date = self.start_date

        # if the interval is intended for the Due Date as opposed
        # to the create date, push the next_date back
        if self.interval_for_due == 1:
            kwarg = {'%ss'%self.due_date_unit: self.due_date_interval}
            next_date -= relativedelta(**kwarg)

        return  next_date
Пример #22
0
 def test_get_holidays(self):
     self.assertListEqual(
         list(Holiday.get_available_holidays()),
         ['Election Day', 'Black Friday'])
Пример #23
0
 def test_get_holidays(self):
     self.assertListEqual(list(Holiday.get_available_holidays()), ["Easter"])