基于Python的美术馆预约系统【附源码】-代码:

# app.py  
from flask import Flask, request, jsonify  
from flask_sqlalchemy import SQLAlchemy  
from datetime import datetime  
  
app = Flask(__name__)  
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///museum_reservation.db'  
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  
db = SQLAlchemy(app)  
  
class Reservation(db.Model):  
    id = db.Column(db.Integer, primary_key=True)  
    user_name = db.Column(db.String(80), nullable=False)  
    user_email = db.Column(db.String(120), nullable=False, unique=True)  
    reservation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)  
    exhibition_name = db.Column(db.String(100), nullable=False)  
    time_slot = db.Column(db.String(20), nullable=False)  # e.g., "10:00-12:00"  
  
    def to_dict(self):  
        return {  
            'id': self.id,  
            'user_name': self.user_name,  
            'user_email': self.user_email,  
            'reservation_date': self.reservation_date.strftime('%Y-%m-%d %H:%M:%S'),  
            'exhibition_name': self.exhibition_name,  
            'time_slot': self.time_slot  
        }  
  
@app.route('/reserve', methods=['POST'])  
def reserve():  
    data = request.get_json()  
    new_reservation = Reservation(  
        user_name=data['user_name'],  
        user_email=data['user_email'],  
        exhibition_name=data['exhibition_name'],  
        time_slot=data['time_slot']  
    )  
    try:  
        db.session.add(new_reservation)  
        db.session.commit()  
        return jsonify({'message': 'Reservation successful!', 'reservation': new_reservation.to_dict()}), 201  
    except Exception as e:  
        db.session.rollback()  
        return jsonify({'message': 'An error occurred during reservation.', 'error': str(e)}), 500  
  
@app.route('/reservations', methods=['GET'])  
def get_reservations():  
    reservations = Reservation.query.all()  
    return jsonify([reservation.to_dict() for reservation in reservations]), 200  
  
@app.route('/reservation/<int:reservation_id>', methods=['GET'])  
def get_reservation_by_id(reservation_id):  
    reservation = Reservation.query.get_or_404(reservation_id)  
    return jsonify(reservation.to_dict()), 200  
  
if __name__ == '__main__':  
    db.create_all()  
    app.run(debug=True)
上一篇:windows 64bit安装JDK以及EclipseEE


下一篇:使用springCache实现缓存-配置