Esempio n. 1
0
def booked(db: PeeweeSession, user: hug.directives.user, start_date: hug.types.text,
           end_date: hug.types.text):
    user_name = user.user_name
    with db.atomic():
        try:
            user_role = user.role
            start_day_object = date.fromisoformat(start_date)
            end_day_object = date.fromisoformat(end_date)
            if user_role != UserRoles.ADMIN:
                start_day_object = date.fromisoformat('2021-01-01') #FIXME: hack to show users all their bookings
                end_day_object = date.fromisoformat('2022-12-31') #FIXME: hack to show users all their bookings
            bookings = []
            for timeslot in TimeSlot.select().where((TimeSlot.start_date_time >= start_day_object) &
                                                    (TimeSlot.start_date_time < end_day_object + timedelta(days=1))) \
                    .order_by(TimeSlot.start_date_time.desc()):
                for appointment in Appointment.select().where(
                        (Appointment.time_slot == timeslot) & (Appointment.booked == True)):
                    try:
                        booking = Booking.get(
                            Booking.appointment == appointment)
                        if user_role != UserRoles.ADMIN:
                            booking = Booking.select().where((Booking.appointment == appointment) &
                                                             (Booking.booked_by == user_name)).get()
                        bookings.append({'start_date_time': timeslot.start_date_time, 'first_name': booking.first_name,
                                         'surname': booking.surname, 'phone': booking.phone, 'office': booking.office,
                                         'secret': booking.secret, 'booked_by': booking.booked_by,
                                         'booked_at': booking.booked_at, 'booking_id': booking.get_id()})
                    except DoesNotExist as e:
                        pass
            return bookings
        except DoesNotExist as e:
            raise hug.HTTPGone
        except ValueError as e:
            raise hug.HTTPBadRequest
Esempio n. 2
0
def list_for_day(db: PeeweeSession, user: hug.directives.user,
                 date_of_day: hug.types.text = None):
    if not date_of_day:
        date_of_day = (date.today() + timedelta(days=1)).isoformat()
    user_name = user.user_name
    with db.atomic():
        try:
            user_role = user.role
            requested_day_object = date.fromisoformat(date_of_day)
            result = io.StringIO()
            writer = csv.DictWriter(result,
                                    fieldnames=['start_date_time', 'first_name', 'surname', 'phone', 'office', 'secret',
                                                'booked_by'])
            writer.writeheader()
            for timeslot in TimeSlot.select().where(
                    (TimeSlot.start_date_time > requested_day_object - timedelta(days=1)) &
                    (TimeSlot.start_date_time < requested_day_object + timedelta(days=1))):
                for appointment in Appointment.select().where(
                        (Appointment.time_slot == timeslot) & (Appointment.booked == True)):
                    try:
                        booking = Booking.get(Booking.appointment == appointment)
                        if user_role != UserRoles.ADMIN:
                            booking = Booking.select().where((Booking.appointment == appointment) &
                                                             (Booking.booked_by == user_name)).get()

                        writer.writerow({'start_date_time': timeslot.start_date_time, 'first_name': booking.first_name,
                                         'surname': booking.surname, 'phone': booking.phone, 'office': booking.office,
                                         'secret': booking.secret, 'booked_by': booking.booked_by})
                    except DoesNotExist as e:
                        pass
            return result.getvalue().encode('utf8')
        except DoesNotExist as e:
            raise hug.HTTPGone
        except ValueError as e:
            raise hug.HTTPBadRequest
Esempio n. 3
0
def list_for_day(db: PeeweeSession, user: hug.directives.user,
                 start_date: hug.types.text, end_date: hug.types.text):
    user_name = user.user_name
    with db.atomic():
        try:
            user_role = user.role
            start_day_object = date.fromisoformat(start_date)
            end_day_object = date.fromisoformat(end_date)
            result = io.BytesIO()
            workbook = xlsxwriter.Workbook(result)
            worksheet = workbook.add_worksheet()
            bold = workbook.add_format({'bold': 1})
            date_format = workbook.add_format({'num_format': 'dd.mm.yyyy'})
            time_format = workbook.add_format({'num_format': 'hh:mm'})
            worksheet.set_column('A:A', 15)
            worksheet.set_column('B:B', 8)
            worksheet.set_column('C:C', 18)
            worksheet.set_column('D:D', 15)
            worksheet.set_column('E:E', 18)
            worksheet.set_column('F:F', 15)
            worksheet.set_column('G:G', 15)
            worksheet.set_column('H:H', 15)
            worksheet.set_column('I:I', 15)
            worksheet.set_column('J:J', 15)
            worksheet.set_column('K:K', 15)
            worksheet.set_column('L:L', 15)
            worksheet.set_column('M:M', 15)
            worksheet.set_column('N:N', 15)
            worksheet.set_column('O:O', 15)
            worksheet.write('A1', 'Termin', bold)
            worksheet.write('B1', 'Uhrzeit', bold)
            worksheet.write('C1', 'Vorname', bold)
            worksheet.write('D1', 'Nachname', bold)
            worksheet.write('E1', 'Telefon', bold)
            worksheet.write('F1', 'Straße', bold)
            worksheet.write('G1', 'Hausnummer', bold)
            worksheet.write('H1', 'PLZ', bold)
            worksheet.write('I1', 'Stadt', bold)
            worksheet.write('J1', 'Geburtdatum', bold)
            worksheet.write('K1', 'Risikokategorie 1', bold)
            worksheet.write('L1', 'Berechtigungscode', bold)
            worksheet.write('M1', 'Behörde', bold)
            worksheet.write('N1', 'Gebucht von', bold)
            worksheet.write('O1', 'Gebucht am', bold)
            row = 1
            col = 0
            for timeslot in TimeSlot.select().where(
                (TimeSlot.start_date_time >= start_day_object)
                    & (TimeSlot.start_date_time < end_day_object +
                       timedelta(days=1))).order_by(
                           TimeSlot.start_date_time.desc()):
                for appointment in Appointment.select().where(
                    (Appointment.time_slot == timeslot)
                        & (Appointment.booked == True)):
                    try:
                        booking = Booking.get(
                            Booking.appointment == appointment)
                        if user_role != UserRoles.ADMIN:
                            booking = Booking.select().where(
                                (Booking.appointment == appointment)
                                & (Booking.booked_by == user_name)).get()

                        worksheet.write_datetime(row, col,
                                                 timeslot.start_date_time,
                                                 date_format)
                        worksheet.write_datetime(row, col + 1,
                                                 timeslot.start_date_time,
                                                 time_format)
                        worksheet.write_string(row, col + 2,
                                               booking.first_name)
                        worksheet.write_string(row, col + 3, booking.surname)
                        worksheet.write_string(row, col + 4, booking.phone)
                        worksheet.write_string(
                            row, col + 5, booking.street
                            if booking.street is not None else "")
                        worksheet.write_string(
                            row, col + 6, booking.street_number
                            if booking.street_number is not None else "")
                        worksheet.write_string(
                            row, col + 7, booking.post_code
                            if booking.post_code is not None else "")
                        worksheet.write_string(
                            row, col + 8,
                            booking.city if booking.city is not None else "")
                        if booking.birthday is None:
                            worksheet.write_string(row, col + 9, "")
                        else:
                            worksheet.write_datetime(row, col + 9,
                                                     booking.birthday,
                                                     date_format)
                        worksheet.write_string(
                            row, col + 10, booking.reason
                            if booking.reason is not None else "")
                        worksheet.write_string(row, col + 11, booking.secret)
                        worksheet.write_string(row, col + 12, booking.office)
                        worksheet.write_string(row, col + 13,
                                               booking.booked_by)
                        worksheet.write_datetime(row, col + 14,
                                                 booking.booked_at,
                                                 date_format)
                        row += 1
                    except DoesNotExist as e:
                        pass
            workbook.close()
            result.flush()
            return result.getvalue()
        except DoesNotExist as e:
            raise hug.HTTPGone
        except ValueError as e:
            raise hug.HTTPBadRequest