Esempio n. 1
0
def parse_google_calendar_recurrence_rule(repeat_day_week_number,
                                          repeat_day_name):
    """
		Returns (repeat_on) exact date for combination eg 4TH viz. 4th thursday of a month
	"""
    if repeat_day_week_number < 0:
        # Consider a month with 5 weeks and event is to be repeated in last week of every month, google caledar considers
        # a month has 4 weeks and hence itll return -1 for a month with 5 weeks.
        repeat_day_week_number = 4

    weekdays = get_weekdays()
    current_date = now_datetime()
    isset_day_name, isset_day_number = False, False

    # Set the proper day ie if recurrence is 4TH, then align the day to Thursday
    while not isset_day_name:
        isset_day_name = True if weekdays[
            current_date.weekday()].lower() == repeat_day_name else False
        current_date = add_days(current_date,
                                1) if not isset_day_name else current_date

    # One the day is set to Thursday, now set the week number ie 4
    while not isset_day_number:
        week_number = get_week_number(current_date)
        isset_day_number = True if week_number == repeat_day_week_number else False
        # check if  current_date week number is greater or smaller than repeat_day week number
        weeks = 1 if week_number < repeat_day_week_number else -1
        current_date = add_to_date(
            current_date,
            weeks=weeks) if not isset_day_number else current_date

    return current_date
Esempio n. 2
0
    def check_support_and_resolution(self):
        week = get_weekdays()
        support_days = []

        for support_and_resolution in self.support_and_resolution:
            # Check if start and end time is set for every support day
            if not (support_and_resolution.start_time
                    or support_and_resolution.end_time):
                frappe.throw(
                    _("Set Start Time and End Time for  \
					Support Day {0} at index {1}.".format(support_and_resolution.workday,
                                           support_and_resolution.idx)))

            support_days.append(support_and_resolution.workday)
            support_and_resolution.idx = week.index(
                support_and_resolution.workday) + 1

            if support_and_resolution.start_time >= support_and_resolution.end_time:
                frappe.throw(
                    _("Start Time can't be greater than or equal to End Time \
					for {0}.".format(support_and_resolution.workday)))

        # Check for repeated workday
        if not len(set(support_days)) == len(support_days):
            repeated_days = get_repeated(support_days)
            frappe.throw(
                _("Workday {0} has been repeated.").format(repeated_days))
Esempio n. 3
0
def get_expected_time_for(parameter, service_level, start_date_time):
    current_date_time = start_date_time
    expected_time = current_date_time
    start_time = None
    end_time = None

    if parameter == "response":
        allotted_seconds = service_level.get("response_time")
    elif parameter == "resolution":
        allotted_seconds = service_level.get("resolution_time")
    else:
        frappe.throw(_("{0} parameter is invalid").format(parameter))

    expected_time_is_set = 0

    support_days = {}
    for service in service_level.get("support_and_resolution"):
        support_days[service.workday] = frappe._dict({
            "start_time":
            service.start_time,
            "end_time":
            service.end_time,
        })

    holidays = get_holidays(service_level.get("holiday_list"))
    weekdays = get_weekdays()

    while not expected_time_is_set:
        current_weekday = weekdays[current_date_time.weekday()]

        if not is_holiday(current_date_time,
                          holidays) and current_weekday in support_days:
            start_time = current_date_time - datetime(current_date_time.year, current_date_time.month, current_date_time.day) \
             if getdate(current_date_time) == getdate(start_date_time) and get_time_in_timedelta(current_date_time.time()) > support_days[current_weekday].start_time \
             else support_days[current_weekday].start_time
            end_time = support_days[current_weekday].end_time
            time_left_today = time_diff_in_seconds(end_time, start_time)

            # no time left for support today
            if time_left_today <= 0: pass
            elif allotted_seconds:
                if time_left_today >= allotted_seconds:
                    expected_time = datetime.combine(
                        getdate(current_date_time), get_time(start_time))
                    expected_time = add_to_date(expected_time,
                                                seconds=allotted_seconds)
                    expected_time_is_set = 1
                else:
                    allotted_seconds = allotted_seconds - time_left_today

        if not expected_time_is_set:
            current_date_time = add_to_date(current_date_time, days=1)

    if end_time and allotted_seconds >= 86400:
        current_date_time = datetime.combine(getdate(current_date_time),
                                             get_time(end_time))
    else:
        current_date_time = expected_time

    return current_date_time
Esempio n. 4
0
def get_expected_time_for(parameter, service_level, start_date_time):
    current_date_time = start_date_time
    expected_time = current_date_time
    start_time = end_time = None
    expected_time_is_set = 0

    allotted_seconds = get_allotted_seconds(parameter, service_level)
    support_days = get_support_days(service_level)
    holidays = get_holidays(service_level.get("holiday_list"))
    weekdays = get_weekdays()

    while not expected_time_is_set:
        current_weekday = weekdays[current_date_time.weekday()]

        if not is_holiday(current_date_time,
                          holidays) and current_weekday in support_days:
            if (getdate(current_date_time) == getdate(start_date_time)
                    and get_time_in_timedelta(current_date_time.time()) >
                    support_days[current_weekday].start_time):
                start_time = current_date_time - datetime(
                    current_date_time.year, current_date_time.month,
                    current_date_time.day)
            else:
                start_time = support_days[current_weekday].start_time

            end_time = support_days[current_weekday].end_time
            time_left_today = time_diff_in_seconds(end_time, start_time)
            # no time left for support today
            if time_left_today <= 0:
                pass

            elif allotted_seconds:
                if time_left_today >= allotted_seconds:
                    expected_time = datetime.combine(
                        getdate(current_date_time), get_time(start_time))
                    expected_time = add_to_date(expected_time,
                                                seconds=allotted_seconds)
                    expected_time_is_set = 1
                else:
                    allotted_seconds = allotted_seconds - time_left_today

        if not expected_time_is_set:
            current_date_time = add_to_date(current_date_time, days=1)

    if end_time and allotted_seconds >= 86400:
        current_date_time = datetime.combine(getdate(current_date_time),
                                             get_time(end_time))
    else:
        current_date_time = expected_time

    return current_date_time
Esempio n. 5
0
def repeat_on_to_google_calendar_recurrence_rule(doc):
	"""
	Returns event (repeat_on) in Google Calendar format ie RRULE:FREQ=WEEKLY;BYDAY=MO,TU,TH
	"""
	recurrence = framework_frequencies.get(doc.repeat_on)
	weekdays = get_weekdays()

	if doc.repeat_on == "Weekly":
		byday = [framework_days.get(day.lower()) for day in weekdays if doc.get(day.lower())]
		recurrence = recurrence + "BYDAY=" + ",".join(byday)
	elif doc.repeat_on == "Monthly":
		week_number = str(get_week_number(get_datetime(doc.starts_on)))
		week_day = weekdays[get_datetime(doc.starts_on).weekday()].lower()
		recurrence = recurrence + "BYDAY=" + week_number + framework_days.get(week_day)

	return [recurrence]
Esempio n. 6
0
    def check_support_and_resolution(self):
        week = get_weekdays()
        support_days = []

        for support_and_resolution in self.support_and_resolution:
            support_days.append(support_and_resolution.workday)
            support_and_resolution.idx = week.index(
                support_and_resolution.workday) + 1

            if to_timedelta(support_and_resolution.start_time) >= to_timedelta(
                    support_and_resolution.end_time):
                frappe.throw(
                    _("Start Time can't be greater than or equal to End Time for {0}."
                      ).format(support_and_resolution.workday))

        # Check for repeated workday
        if not len(set(support_days)) == len(support_days):
            repeated_days = get_repeated(support_days)
            frappe.throw(
                _("Workday {0} has been repeated.").format(repeated_days))
Esempio n. 7
0
    def validate(self):
        week = get_weekdays()
        indexes = []

        self.check_response_and_resolution_time()

        for support_and_resolution in self.support_and_resolution:
            indexes.append(week.index(support_and_resolution.workday))
            support_and_resolution.idx = week.index(
                support_and_resolution.workday) + 1
            start_time, end_time = (datetime.strptime(
                support_and_resolution.start_time, '%H:%M:%S').time(),
                                    datetime.strptime(
                                        support_and_resolution.end_time,
                                        '%H:%M:%S').time())
            if start_time > end_time:
                frappe.throw(
                    _("Start Time can't be greater than End Time for {0}.".
                      format(support_and_resolution.workday)))
        if not len(set(indexes)) == len(indexes):
            frappe.throw(_("Workday has been repeated twice"))
Esempio n. 8
0
def get_expected_time_for(parameter, service_level, start_date_time):
    current_date_time = start_date_time
    expected_time = current_date_time
    start_time = None
    end_time = None

    # lets assume response time is in days by default
    if parameter == 'response':
        allotted_days = service_level.response_time
        time_period = service_level.response_time_period
    elif parameter == 'resolution':
        allotted_days = service_level.resolution_time
        time_period = service_level.resolution_time_period
    else:
        frappe.throw(_("{0} parameter is invalid".format(parameter)))

    allotted_hours = 0
    if time_period == 'Hour':
        allotted_hours = allotted_days
        allotted_days = 0
    elif time_period == 'Week':
        allotted_days *= 7

    expected_time_is_set = 1 if allotted_days == 0 and time_period in [
        'Day', 'Week'
    ] else 0

    support_days = {}
    for service in service_level.support_and_resolution:
        support_days[service.workday] = frappe._dict({
            'start_time':
            service.start_time,
            'end_time':
            service.end_time,
        })

    holidays = get_holidays(service_level.holiday_list)
    weekdays = get_weekdays()

    while not expected_time_is_set:
        current_weekday = weekdays[current_date_time.weekday()]

        if not is_holiday(current_date_time,
                          holidays) and current_weekday in support_days:
            start_time = current_date_time - datetime(
                current_date_time.year, current_date_time.month,
                current_date_time.day
            ) if getdate(current_date_time) == getdate(
                start_date_time) else support_days[current_weekday].start_time
            end_time = support_days[current_weekday].end_time
            time_left_today = time_diff_in_hours(end_time, start_time)

            # no time left for support today
            if time_left_today < 0: pass
            elif time_period == 'Hour':
                if time_left_today >= allotted_hours:
                    expected_time = datetime.combine(
                        getdate(current_date_time), get_time(start_time))
                    expected_time = add_to_date(expected_time,
                                                hours=allotted_hours)
                    expected_time_is_set = 1
                else:
                    allotted_hours = allotted_hours - time_left_today
            else:
                allotted_days -= 1
                expected_time_is_set = allotted_days <= 0

        current_date_time = add_to_date(current_date_time, days=1)

    if end_time and time_period != 'Hour':
        current_date_time = datetime.combine(getdate(current_date_time),
                                             get_time(end_time))
    else:
        current_date_time = expected_time

    return current_date_time