def removeReservation(author, date, slug, period): try: classroom = Classroom.objects.get(slug = slug) except: raise ValueNotFoundError('classroom - %s is not valid' % slug) try: reservation = Reservation.objects.get(period=period, classroom=classroom, input_date=date) if reservation.author == author: reservation.delete() log.debug('removeReservation: removed successfully') else: raise PermissionDeniedError( 'User does not have permission to remove this reservation') except Reservation.DoesNotExist: raise ValueNotFoundError( 'User does not have a reservation for %s for %s' % (classroom.name, date) )
def addReservation(author, date, slug, period): try: classroom = Classroom.objects.get(slug = slug) log.debug('addReservation: classroom id: %s' % classroom.id) except: log.error('addReservation: classroom with slug: %s not found' % slug) raise ValueNotFoundError('class room - %s is not valid' % slug) roomsReserved = Reservation.objects.filter(classroom=classroom, input_date=date) if roomsReserved.count() >= MAX_RESERVATION: log.error('addReservation: exceeded max reservation capacity, classroom [%s] on [%s] has [%s] reservations' % (classroom.name, date, roomsReserved)) raise ExceedingLimitationError('Exceeding maximum limit for %s on %s' % (classroom.name, date)) else: try: reservation = Reservation.objects.get(classroom=classroom, input_date=date, period=period) log.error('addReservation: duplicated reservation: classroom [%s] for period [%s] on [%s] has already been reserved' % (classroom.name, period, date)) raise DuplicatedEntryError( 'User already has a reservation for %s for %s' % (classroom.name, date) ) except Reservation.DoesNotExist: reservation = Reservation(author=author, input_date=date, classroom=classroom, period=period) reservation.save() log.debug('addReservation: saved successfully')