kwabenasapong

Untitled

Mar 2nd, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. '''API to using CRUD operations on images in React frontend.'''
  3.  
  4. from flask import Blueprint, request, jsonify, abort
  5. from flask_jwt_extended import jwt_required, get_jwt_identity
  6. from app.models import User, Image
  7. from app import db
  8. from werkzeug.utils import secure_filename
  9. import os
  10. import uuid
  11. from datetime import datetime
  12.  
  13.  
  14. images = Blueprint('images', __name__, url_prefix='/api/v1/images')
  15.  
  16. '''Create empty folder on server for images.'''
  17. if not os.path.exists('app/static/images'):
  18.     os.makedirs('app/static/images')
  19.  
  20.  
  21. @images.route('/', methods=['GET'])
  22. @jwt_required
  23. def get_images():
  24.     '''Get all images from database.'''
  25.     images = Image.query.all()
  26.     return jsonify([image.serialize() for image in images]), 200
  27.  
  28.  
  29. @images.route('/<int:image_id>', methods=['GET'])
  30. @jwt_required
  31. def get_image(image_id):
  32.     '''Get image by id from database.'''
  33.     image = Image.query.get(image_id)
  34.     if not image:
  35.         abort(404)
  36.     return jsonify(image.serialize()), 200
  37.  
  38.  
  39. @images.route('/', methods=['POST'])
  40. @jwt_required
  41. def create_image():
  42.     '''create image in database and upload to server using upload method
  43.    in models.image.'''
  44.     data = request.get_json()
  45.     if not data:
  46.         abort(400)
  47.     image = Image(
  48.         name=data.get('name'),
  49.         mimetype=data.get('mimetype'),
  50.         user_id=get_jwt_identity()
  51.     )
  52.     '''using upload method in models.image.'''
  53.     image.name = image.upload(request.files['image'])
  54.    
  55.     db.session.add(image)
  56.     db.session.commit()
  57.     return jsonify(image.serialize()), 201
  58.  
  59.  
  60.  
  61. @images.route('/<int:image_id>', methods=['PUT'])
  62. @jwt_required
  63. def update_image(image_id):
  64.     '''Update image in database.'''
  65.     data = request.get_json()
  66.     if not data:
  67.         abort(400)
  68.     image = Image.query.get(image_id)
  69.     if not image:
  70.         abort(404)
  71.     if image.user_id != get_jwt_identity():
  72.         abort(403)
  73.     image.name = data.get('name', image.name)
  74.     image.mimetype = data.get('mimetype', image.mimetype)
  75.     image.updated_at = datetime.utcnow()
  76.     db.session.commit()
  77.     return jsonify(image.serialize()), 200
  78.  
  79.  
  80. @images.route('/<int:image_id>', methods=['DELETE'])
  81. @jwt_required
  82. def delete_image(image_id):
  83.     '''Delete image from database and server.'''
  84.     image = Image.query.get(image_id)
  85.     if not image:
  86.         abort(404)
  87.     if image.user_id != get_jwt_identity():
  88.         abort(403)
  89.     '''Delete image from server.'''
  90.     os.remove(os.path.join('app/static/images', image.name))
  91.     '''Delete image from database.'''
  92.     db.session.delete(image)
  93.     db.session.commit()
  94.     return jsonify(image.serialize()), 200
  95.  
Add Comment
Please, Sign In to add comment