コード例 #1
0
ファイル: booking.py プロジェクト: tullingedk/booking
def swish(seat_type, id):
    if not is_integer(id):
        abort(400, "Id must be integer")

    booking = (Booking.query.get(int(id)) if seat_type == "standard" else
               (ConsoleBooking.query.get(int(id)) if seat_type == "console"
                else abort(400, "Invalid seat_type")))

    if not booking:
        abort(404, "Booking does not exist")

    generate_swish_qr(
        booking.name,
        booking.school_class,
        booking.seat,
        0 if seat_type == "standard" else 1,
    )
    return send_file("static/temp_swish.png", mimetype="image/png")
コード例 #2
0
 def test_empty(self):
     self.assertFalse(is_integer(""))
コード例 #3
0
 def test_leading_space(self):
     self.assertFalse(is_integer(" 5"))
コード例 #4
0
 def test_only_minus(self):
     self.assertFalse(is_integer("-"))
コード例 #5
0
 def test_zero_decimal(self):
     self.assertFalse(is_integer("0.0"))
コード例 #6
0
 def test_leading_zero(self):
     self.assertTrue(is_integer("00"))
コード例 #7
0
 def test_leading_plus(self):
     self.assertFalse(is_integer("+999"))
コード例 #8
0
 def test_leading_minus(self):
     self.assertTrue(is_integer("-999"))
コード例 #9
0
 def test_5000(self):
     self.assertTrue(is_integer("5000"))
コード例 #10
0
 def test_trailing_letter(self):
     self.assertFalse(is_integer("5a"))
コード例 #11
0
 def test_leading_letter(self):
     self.assertFalse(is_integer("a5"))
コード例 #12
0
 def test_single_digit(self):
     self.assertTrue(is_integer("5"))
コード例 #13
0
ファイル: booking.py プロジェクト: tullingedk/booking
def book():
    seat = request.json["seat"]
    seat_type = request.json["seat_type"]

    # Validate user input, must be an integer
    if not is_integer(seat):
        abort(400, "Seat must be integer")

    # Seat integer must be within bookable range
    seat_max = (
        NUM_SEATS if seat_type == "standard" else
        (NUM_CONSOLE_SEATS if seat_type == "console" else abort(
            400, "Invalid seat_type")  # only two types of seat
         ))

    if int(seat) < 1 or int(seat) > seat_max:
        abort(400, f"Seat must be in range 1 - {seat_max}")

    # Check if this seat is already booked by querying the database

    if (Booking.query.get(int(seat)) if seat_type == "standard" else
        (ConsoleBooking.query.get(int(seat))
         if seat_type == "console" else abort(400, "Invalid seat_type"))):
        abort(400, "Seat already booked.")

    # Check if this user already has a booking
    if (len((
            Booking.query.filter_by(email=session["google_email"]).all()
            if seat_type == "standard" else
        (ConsoleBooking.query.filter_by(email=session["google_email"]).all()
         if seat_type == "console" else abort(400, "Invalid seat_type")
         # bad seat_type would have triggered abort earlier but good practice to always handle bad data
         )
    )) != 0  # realized this is whole if-statement is quite unreadable but it is very compact
        ):
        abort(
            400,
            "You have already booked a seat. Contact administrator for help with cancellation or seat movement.",
        )

    # Retrieve current user object
    user = User.query.filter_by(email=session["google_email"]).one()

    # Create new booking object
    booking = (Booking(
        seat=int(seat),
        name=session["google_name"],
        email=session["google_email"],
        school_class=user.school_class,
        paid=False,
    ) if seat_type == "standard" else (ConsoleBooking(
        seat=int(seat),
        name=session["google_name"],
        email=session["google_email"],
        school_class=user.school_class,
        paid=False,
    ) if seat_type == "console" else abort(400, "Invalid seat_type")))

    # Add to database
    db.session.add(booking)
    db.session.commit()

    return base_req()