Skip to content

Instantly share code, notes, and snippets.

@pferreir
Created December 8, 2016 15:22
Show Gist options
  • Save pferreir/b11185ecb9ddd9df98a8beb89a6d52dd to your computer and use it in GitHub Desktop.
Save pferreir/b11185ecb9ddd9df98a8beb89a6d52dd to your computer and use it in GitHub Desktop.
CSV list of booking occurrences for a set of rooms
from io import BytesIO
import csv
import sys
from pytz import timezone
from indico.modules.rb.models.reservations import RepeatMapping
tz = timezone('Europe/Zurich')
room_ids = {96, 336, 97, 98}
out = BytesIO()
occurrences = ReservationOccurrence.find(
Reservation.room_id.in_(room_ids),
ReservationOccurrence.start_dt >= tz.localize(datetime(2016, 1, 1)),
ReservationOccurrence.end_dt < tz.localize(datetime(2018, 1, 1)),
ReservationOccurrence.is_valid,
_join=ReservationOccurrence.reservation,
_eager=ReservationOccurrence.reservation
).options(ReservationOccurrence.NO_RESERVATION_USER_STRATEGY).order_by(ReservationOccurrence.start_dt).all()
writer = csv.writer(out)
writer.writerow(['Room', 'Reason', 'Booked for', 'Frequency', 'Date', 'Start time', 'End time', 'URL'])
for occur in occurrences:
reservation = occur.reservation
data = [reservation.room.full_name.encode('utf-8'), reservation.booking_reason.encode('utf-8'),
reservation.booked_for_name.encode('utf-8'),
RepeatMapping.get_short_name(reservation.repeat_frequency, reservation.repeat_interval),
occur.start_dt.date(), occur.start_dt.time(), occur.end_dt.time(), reservation.details_url]
writer.writerow(data)
out.seek(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment