Files
Schulung_Docker_k8s/carpool/python_rest.py
nutzer26 bed22c6720 added
2022-12-13 17:55:43 +01:00

105 lines
3.0 KiB
Python

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow, fields
from flask_cors import CORS
import os
# Init app
app = Flask(__name__)
user = os.environ.get('POSTGRES_USER')
db = os.environ.get('POSTGRES_DB')
passwd = os.environ.get('POSTGRES_PASSWORD')
host = os.environ.get('POSTGRES_HOST')
#enable CORS
CORS(app)
# connect to already existing and running Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + user + ':' + passwd +'@' + host + ':5432/' + db
# not important but otherwise we get a warning every time
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#Init db
db = SQLAlchemy(app)
#Init ma
ma = Marshmallow(app)
# Pool Car Class/Model
class Pool_Car(db.Model):
#define table, in our case already existing
__tablename__ = 'cars'
car_id = db.Column(db.Integer, primary_key=True)
license_plate = db.Column(db.String(30), unique=True)
car_type = db.Column(db.String(20))
fuel = db.Column(db.String(20))
number_of_seats =db.Column(db.Integer)
# set class attributes
def __init__(self, license_plate, car_type, fuel, number_of_seats):
self.license_plate = license_plate
self.car_type = car_type
self.fuel = fuel
self.number_of_seats = number_of_seats
class Pool_CarSchema(ma.Schema):
class Meta:
fields = ('car_id','license_plate','car_type','fuel','number_of_seats')
# Init schema
pool_car_schema = Pool_CarSchema()
pool_cars_schema = Pool_CarSchema(many=True)
# Create a Pool_Car
@app.route('/car', methods=['POST'])
def add_pool_car():
# Get request data as json
car_entity = request.get_json()
license_plate= car_entity.get('license_plate')
car_type = car_entity.get('car_type')
fuel = car_entity.get('fuel')
number_of_seats = car_entity.get('number_of_seats')
new_pool_car = Pool_Car(license_plate, car_type, fuel, number_of_seats)
db.session.add(new_pool_car)
db.session.commit()
return pool_car_schema.jsonify(new_pool_car)
# Get car_id,license_plate,car_type of all products of the table
@app.route('/car', methods=['GET'])
def get_pool_cars():
all_pool_cars = Pool_Car.query.all()
result = pool_cars_schema.dump(all_pool_cars)
car_list= []
for item in result:
car_details = { "car_id":None, "license_plate":None, "car_type":None}
car_details['car_id'] = item['car_id']
car_details['license_plate'] = item['license_plate']
car_details['car_type'] = item['car_type']
car_list.append(car_details)
return jsonify(car_list)
# Get Single Products
@app.route('/car/<car_id>', methods=['GET'])
def get_pool_car(car_id):
pool_car = Pool_Car.query.get(car_id)
return pool_car_schema.jsonify(pool_car)
# Delete Product
@app.route('/car/<car_id>', methods=['DELETE'])
def delete_pool_car(car_id):
pool_car = Pool_Car.query.get(car_id)
db.session.delete(pool_car)
db.session.commit()
return pool_car_schema.jsonify(pool_car)
if __name__ == '__main__':
app.run(debug=True) #port='5002'