예제 #1
0
def timerange_delete_callback(sender, instance, **kwargs):
    """
    delete all time slots within a time range object.
    """
    # iterate over days in date range set in the form
    for single_date in daterange(instance.start_date, instance.end_date):

        # add a time object only when the day in range
        # is a weekday that was checked by the user
        weekday = single_date.weekday()

        if (weekday == 0 and instance.monday) \
                or (weekday == 1 and instance.tuesday) \
                or (weekday == 2 and instance.wednesday) \
                or (weekday == 3 and instance.thursday) \
                or (weekday == 4 and instance.friday) \
                or (weekday == 5 and instance.saturday) \
                or (weekday == 6 and instance.sunday):

            # add the start time & end time from the form in order to
            # create a datetime object, as required by the Time model
            start_time = datetime.combine(single_date, instance.start_time)
            end_time = datetime.combine(single_date, instance.end_time)

            # delete time
            try:
                Time.objects.filter(
                    start_time=start_time,
                    end_time=end_time)[0].delete()
            except IndexError:
                pass
예제 #2
0
def timerange_save_callback(sender, instance, **kwargs):
    """
    create single time slots based on the creation of a time range object.
    """
    # empty list for bulk_create
    timeList = []

    # iterate over days in date range set in the form
    for single_date in daterange(instance.start_date, instance.end_date):

        # add a time object only when the day in range
        # is a weekday that was checked by the user
        weekday = single_date.weekday()

        if (weekday == 0 and instance.monday) \
                or (weekday == 1 and instance.tuesday) \
                or (weekday == 2 and instance.wednesday) \
                or (weekday == 3 and instance.thursday) \
                or (weekday == 4 and instance.friday) \
                or (weekday == 5 and instance.saturday) \
                or (weekday == 6 and instance.sunday):

            # add the start time & end time from the form in order to
            # create a datetime object, as required by the Time model
            start_time = datetime.combine(single_date, instance.start_time)
            end_time = datetime.combine(single_date, instance.end_time)

            # now do timezone conversion
            current_tz = timezone.get_current_timezone()
            utc = pytz.timezone('UTC')

            localized_start_time = current_tz.localize(start_time)
            localized_end_time = current_tz.localize(end_time)

            normalized_start_time = utc.normalize(
                localized_start_time.astimezone(utc))

            normalized_end_time = utc.normalize(
                localized_end_time.astimezone(utc))

            #aware_start_time = timezone.make_aware(start_time, timezone.utc)
            #aware_end_time   = timezone.make_aware(end_time, timezone.utc)

            now = timezone.now()

            # append a time object to the list so
            # all of them can be inserted in one query
            timeList.append(Time(
                start_time=normalized_start_time,
                end_time=normalized_end_time,
                branch=instance.branch,
                venue=instance.venue,
                created=now,
                updated=now)
            )

    # save time slots
    Time.objects.bulk_create(timeList)