Exemple #1
0
def view_calendar(request, item_type, item_id, date=None):
    item_type = ReservationItemType(item_type)
    item = get_object_or_404(item_type.get_object_class(), id=item_id)
    if date:
        try:
            date = extract_date(date)
        except:
            render(request, 'mobile/error.html',
                   {'message': 'Invalid date requested for tool calendar'})
            return HttpResponseBadRequest()
    else:
        date = datetime.now()

    start = beginning_of_the_day(date, in_local_timezone=True)
    end = end_of_the_day(date, in_local_timezone=True)

    reservations = Reservation.objects.filter(**{
        item_type.value: item
    }).filter(cancelled=False, missed=False, shortened=False).filter(**{})
    # Exclude events for which the following is true:
    # The event starts and ends before the time-window, and...
    # The event starts and ends after the time-window.
    reservations = reservations.exclude(start__lt=start, end__lt=start)
    reservations = reservations.exclude(start__gt=end, end__gt=end)

    outages = ScheduledOutage.objects.none()
    if item_type == ReservationItemType.TOOL:
        outages = ScheduledOutage.objects.filter(
            Q(tool=item) | Q(resource__fully_dependent_tools__in=[item]))
    elif item_type == ReservationItemType.AREA:
        outages = item.scheduled_outage_queryset()

    # Exclude outages for which the following is true:
    # The outage starts and ends before the time-window, and...
    # The outage starts and ends after the time-window.
    outages = outages.exclude(start__lt=start, end__lt=start)
    outages = outages.exclude(start__gt=end, end__gt=end)

    events = list(chain(reservations, outages))
    events.sort(key=lambda x: x.start)

    dictionary = {
        'item': item,
        'item_type': item_type.value,
        'previous_day': start - timedelta(days=1),
        'current_day': start,
        'current_day_string': date.strftime('%Y-%m-%d'),
        'next_day': start + timedelta(days=1),
        'events': events,
    }

    return render(request, 'mobile/view_calendar.html', dictionary)
Exemple #2
0
def view_calendar(request, tool_id, date=None):
    tool = get_object_or_404(Tool, id=tool_id)
    if date:
        try:
            date = extract_date(date)
        except:
            render(request, 'mobile/error.html',
                   {'message': 'Invalid date requested for tool calendar'})
            return HttpResponseBadRequest()
    else:
        date = datetime.now()

    start = beginning_of_the_day(date, in_local_timezone=True)
    end = end_of_the_day(date, in_local_timezone=True)

    reservations = Reservation.objects.filter(tool=tool,
                                              cancelled=False,
                                              missed=False,
                                              shortened=False)
    # Exclude events for which the following is true:
    # The event starts and ends before the time-window, and...
    # The event starts and ends after the time-window.
    reservations = reservations.exclude(start__lt=start, end__lt=start)
    reservations = reservations.exclude(start__gt=end, end__gt=end)

    outages = ScheduledOutage.objects.filter(
        Q(tool=tool) | Q(resource__fully_dependent_tools__in=[tool]))
    outages = outages.exclude(start__lt=start, end__lt=start)
    outages = outages.exclude(start__gt=end, end__gt=end)

    events = list(chain(reservations, outages))
    events.sort(key=lambda x: x.start)

    dictionary = {
        'tool': tool,
        'previous_day': start - timedelta(days=1),
        'current_day': start,
        'current_day_string': date.strftime('%Y-%m-%d'),
        'next_day': start + timedelta(days=1),
        'events': events,
    }

    return render(request, 'mobile/view_calendar.html', dictionary)