-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (66 loc) · 2.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from sqlalchemy.exc import IntegrityError
import os
# Initialize Flask app, SQLAlchemy database, and Marshmallow serializer
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL') or 'sqlite:///cultural_destinations.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
# Define Destination database model and schema for serialization/deserialization
class Destination(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
description = db.Column(db.Text, nullable=False)
image_url = db.Column(db.String(200), nullable=True)
location = db.Column(db.String(100), nullable=True)
contact = db.Column(db.String(100), nullable=True)
website_url = db.Column(db.String(200), nullable=True)
category = db.Column(db.String(100), nullable=True)
def __repr__(self):
return f'<Destination {self.id}: {self.name}>'
class DestinationSchema(ma.SQLAlchemySchema):
class Meta:
model = Destination
fields = ('id', 'name', 'description', 'image_url', 'location', 'contact', 'website_url', 'category')
# Define endpoints for creating and retrieving cultural destinations
@app.route('/destination', methods=['POST'])
def create_destination():
destination_data = request.get_json()
destination_schema = DestinationSchema()
try:
# Deserialize and validate destination data from request
destination = destination_schema.load(destination_data, session=db.session)
# Add destination to database
db.session.add(destination)
db.session.commit()
# Serialize and return created destination
result = destination_schema.dump(destination)
return jsonify(result), 201
except IntegrityError as e:
# Handle unique constraint violation error
db.session.rollback()
return jsonify({'error': 'Destination name must be unique.'}), 400
except Exception as e:
# Handle any other error
db.session.rollback()
return jsonify({'error': str(e)}), 400
@app.route('/destination', methods=['GET'])
def get_destinations():
destination_schema = DestinationSchema(many=True)
destinations = Destination.query.all()
result = destination_schema.dump(destinations)
return jsonify(result), 200
@app.route('/destination/<destination_id>', methods=['GET'])
def get_destination(destination_id):
destination_schema = DestinationSchema()
destination = Destination.query.get(destination_id)
if destination:
result = destination_schema.dump(destination)
return jsonify(result), 200
else:
return jsonify({'error': 'Destination not found.'}), 404
if __name__ == '__main__':
app.run(debug=True)