def test_checkin_fail_choice(self): bike = Bike.objects.get(name="1") make_ride(self.student, bike) body = "checkin PSA" expected = "Station not found" response = handle_checkin(self.student, body) print(expected, response) self.assertTrue(expected in response) self.assertLess(len(response), 161)
def test_checkin_success(self): bike = Bike.objects.get(name="1") make_ride(self.student, bike) body = "checkin rodin" expected = "You have successfully returned" response = handle_checkin(self.student, body) print(response) self.assertTrue(expected in response) self.assertLess(len(response), 161)
def test_checkout_fail_busy(self): other_student = Student.objects.get(name="Other Student") make_ride(other_student, self.bike) timenow_string = timezone.localtime(datetime.datetime.now(pytz.utc)).strftime("%H:%M on %D") body = "checkout 1" expected1 = "still in use" expected2 = "checked out at "+timenow_string response = handle_checkout(self.student, body) print "PRINT TIME_NOW: " + timenow_string print(response) self.assertTrue(expected1 in response) self.assertTrue(expected2 in response) self.assertLess(len(response), 161)
def checkout(request): data = request.POST bike = data.get("bike") penncard = data.get("penncard") pin = data.get("pin") try: student = Student.objects.get(penncard=penncard) except Student.DoesNotExist: return json_failure("Student does not exist.") if student.pin != pin: email_razzi("pin mismatch! {}".format(locals())) return HttpResponseForbidden() if not student.can_ride: message = "" current_rides = student.ride_set.filter(checkin_time=None) if len(current_rides) > 0: bike = current_rides[0].bike.name message += "You can't check bikes out until you check bike {} in. ".format(bike) if not student.waiver_signed: message += "You need to fill out a waiver. Click the 'account' button at the bottom and accept the waiver there. " if not student.current_payments: message = "You don't currently have any PennCycle plans. Log on to penncycle.org to add one." return json_failure(message) try: bike = Bike.objects.get(name=bike) except Exception: return json_failure( "Bike {} is unavailable." .format(bike) ) if bike.status == 'available': make_ride(student, bike) else: return json_failure( "Bike {} is unavailable with status '{}'." .format(bike.name, bike.status) ) return http_json({"combo": bike.combo})
def handle_checkout(student, body): if not student.can_ride: current_rides = student.ride_set.filter(checkin_time=None) if not student.waiver_signed: message = "You need to accept our waiver. Go to penncycle.org/signin to do so." elif not student.current_payments: message = ( "You don't currently have any PennCycle plans. " "Log on to penncycle.org/signin to add one." ) elif len(current_rides) > 0: bike = current_rides[0].bike.name message = "You can't check bikes out until you check bike {} back in. ".format(bike) else: message = "You are currently unable to ride. Log in to your penncycle account at penncycle.org/signin to fix this." email_razzi("Strange cannot ride case. {}".format(locals())) return message try: bike_number = re.search("\d+", body).group() except: return "Command not understood. Text 'help' for a list of commands. Example of checking out a bike would be: Checkout 10" try: bike = Bike.objects.get(name=bike_number) except Bike.DoesNotExist: message = "Bike not found. Text 'Checkout (number)', where number is 1 or 2 digits. Text 'Report (issue)' to report an issue." return message if bike.status == "available": make_ride(student, bike) message = "You have successfully checked out bike {}. The combination is {}. To return it, reply 'Checkin Hill' or any other station. Text 'Stations' for a list.".format(bike_number, bike.combo) elif bike.status == "out": checkout_time = bike.rides.latest().checkout_time time_string = timezone.localtime(checkout_time).strftime("%H:%M on %D") message = "Bike {} is still in use. It was checked out at {}. Text 'bikes' for a list of available bikes.".format(bike.name, time_string) else: message = "Bike {} is not in service. Please try another bike, or text 'bikes' for a list of available bikes.".format(bike.name) return message