Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. from flask import Flask, request, jsonify, json
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_marshmallow import Marshmallow
  4. import os
  5.  
  6. app = Flask(__name__)
  7.  
  8. basedir = os.path.abspath(os.path.dirname(__file__))
  9.  
  10. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
  11.     os.path.join(basedir, 'db.sqlite')
  12. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  13.  
  14. db = SQLAlchemy(app)
  15.  
  16. ma = Marshmallow(app)
  17.  
  18.  
  19. class Animal(db.Model):
  20.     type = db.Column(db.String(64))
  21.     name = db.Column(db.String(64), unique=True)
  22.     price = db.Column(db.Float)
  23.     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  24.  
  25.     def __init__(self, type, name, price):
  26.         self.name = name
  27.         self.type = type
  28.         self.price = price
  29.  
  30.  
  31. class AnimalSchema(ma.Schema):
  32.     class Meta:
  33.         fields = ('id', 'type', 'name', 'price')
  34.  
  35.  
  36. animal_schema = AnimalSchema()
  37. animal_schema = AnimalSchema(many=True)
  38.  
  39.  
  40. # Add new animal
  41. @app.route('/animals/add_animal', methods=['POST'])
  42. def add_animal():
  43.     data = request.get_json()
  44.     type = data['type']
  45.     name = data['name']
  46.     price = data['price']
  47.  
  48.     new_animal = Animal(type, name, price)
  49.  
  50.     db.session.add(new_animal)
  51.     db.session.commit()
  52.     return json.dumps('Added'), 200
  53.  
  54.  
  55. # See all animals in shop
  56. @app.route('/animals', methods=['GET'])
  57. def get_animals():
  58.     all_animals = Animal.query.all()
  59.     result = animal_schema.dump(all_animals)
  60.     return jsonify(result)
  61.  
  62. # See all animals of current type
  63. @app.route('/animals/<type>', methods=['GET'])
  64. def type_animals(type):
  65.     animals = []
  66.     animals = Animal.query.filter_by(type=type).all()
  67.     result = animal_schema.dump(animals)
  68.     return jsonify(result)
  69.  
  70.  
  71. # See information about current animal
  72. @app.route('/animals/<id>', methods=['GET'])
  73. def get_animal(id):
  74.     animal = Animal.query.get(id)
  75.     print(animal)
  76.     return animal_schema.jsonify(animal)
  77.  
  78.  
  79. # Update information about animal
  80. @app.route('/animals/edit_price/<id>', methods=['PUT'])
  81. def update_price_animal(id):
  82.     data = request.get_json()
  83.     new_price = data['price']
  84.     animal = Animal.query.filter_by(id=id).all()[0]
  85.     animal.price = new_price
  86.     db.session.commit()
  87.     return json.dumps('Edited'), 200
  88.  
  89. # Update information about animal
  90. @app.route('/animals/edit_name/<id>', methods=['PUT'])
  91. def update_name_animal(id):
  92.     data = request.get_json()
  93.     new_name = data['name']
  94.     animal = Animal.query.filter_by(id=id).all()[0]
  95.     animal.name = new_name
  96.     db.session.commit()
  97.     return json.dumps('Edited'), 200
  98.  
  99.  
  100. # Buy the animal
  101. @app.route('/animals/buy/<id>', methods=['DELETE'])
  102. def buy_animal(id):
  103.     animal = Animal.query.get(id)
  104.     db.session.delete(animal)
  105.     db.session.commit()
  106.     return json.dumps('Deleted'), 200
  107.  
  108.  
  109. if __name__ == '__main__':
  110.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement