def validate_date_ranges(event_args):
    # Check that the end dates and times come after the start
    start = datetime.combine(event_args['start_date'],
                             event_args['start_time'].time())
    end = datetime.combine(event_args['end_date'],
                           event_args['end_time'].time())

    if end <= start:
        message = 'End date must be after the start date'
        raise errors.ResourceValidationError(
            messages={'date_range': [message]})
def set_occurrences(event_args):
    """
    Create the list of occurrences using the original event date details
    For example:
        DAILY recurrence 10 times will set values for the next 10 days
        including the date specified in the request.  The start and end times for
        them will be the same
    :param event_args:
    :return:
    """
    start_date = event_args['start_date']
    end_date = event_args['end_date']

    # Check to see if the recurrence details is set
    if event_args['is_recurring']:
        recurrence_details = event_args.get('recurrence_details')
        if recurrence_details is None:
            message = 'Missing data for required field when is_recurring is true'
            raise errors.ResourceValidationError(
                messages={'recurrence_details': [message]})

        # Check that a daily recurrence type does not span more than a day
        recurrence = recurrence_details['recurrence']
        date_delta = end_date - start_date
        if recurrence == constants.RecurrenceType.DAILY.value and date_delta.days > 0:
            message = 'Daily recurrence events cannot be longer than a day'
            raise errors.ResourceValidationError(
                messages={'daily_event_too_long': [message]})
    else:
        event_args['recurrence_details'] = None
        recurrence_details = set_default_recurrence_details()

    # Populate the occurrences list and last end date
    last_end_date, occurrences = populate_occurrences(start_date, end_date,
                                                      recurrence_details)

    event_args['end_date'] = last_end_date
    event_args['occurrences'] = occurrences
Exemplo n.º 3
0
def set_occurrences(event_args):
    # Check to see if the recurrence details is set
    if event_args['is_recurring']:
        recurrence_details = event_args.get('recurrence_details')
        if recurrence_details is None:
            message = 'Missing data for required field when is_recurring is true'
            raise errors.ResourceValidationError(
                messages={'recurrence_details': [message]})
    else:
        event_args['recurrence_details'] = None
        recurrence_details = set_default_recurrence_details()

    # Populate the occurrences list and last end date
    last_end_date, occurrences = populate_occurrences(event_args['start_date'],
                                                      event_args['end_date'],
                                                      recurrence_details)

    event_args['end_date'] = last_end_date
    event_args['occurrences'] = occurrences
def populate_occurrences(start_date, end_date, recurrence_details):
    occurrence_type = recurrence_details['occurrence_type']

    if occurrence_type == constants.OccurrenceType.NEVER.value:
        last_end_date, occurrences = generate_occurrence_type_never_events(
            start_date, end_date, recurrence_details)

    elif occurrence_type == constants.OccurrenceType.AFTER.value:
        last_end_date, occurrences = generate_occurrence_type_after_events(
            start_date, end_date, recurrence_details)

    elif occurrence_type == constants.OccurrenceType.ON.value:
        last_end_date, occurrences = generate_occurrence_type_on_events(
            start_date, end_date, recurrence_details)

    else:
        message = 'Invalid value, must be one of {}'.format(
            constants.OccurrenceType.values())
        raise errors.ResourceValidationError(
            messages={'occurrence_type': [message]})

    return last_end_date, occurrences