Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. from flask import Flask, request, jsonify, abort, make_response, request
  2. from api import app
  3. from api.userrequests.models import requests
  4.  
  5.  
  6.  
  7. @app.route('/makerequest', methods=['POST'])
  8. def add_request():
  9. user_request = request.json
  10. items = []
  11.  
  12. username = user_request.get('username')
  13. department = user_request.get('department')
  14. items = user_request.get('items')
  15.  
  16. if not username:
  17. return jsonify({"error":"username is required"}), 422
  18.  
  19.  
  20.  
  21. if not department:
  22. return jsonify({"error":"department is required"}), 422
  23.  
  24. if not items:
  25. return jsonify({"error": "please enter items you need from the store"}), 422
  26.  
  27. items[items] = user_request
  28.  
  29. return jsonify({"message": f"requests submitted to store by '{username}' are '{items}'"}), 201
  30.  
  31.  
  32. @app.route('/item/<username>',methods=['GET', 'POST','PUT', 'PATCH', 'DELETE'])
  33. def edit(username):
  34. if request.method == 'GET':
  35. for each_request in requests:
  36. if each_request.get('username') == username:
  37. return jsonify({'requests': each_request})
  38.  
  39.  
  40. @app.route('/item',methods=['POST'])
  41. def edits():
  42. user_data = request.json
  43.  
  44. username = user_data.get('username')
  45. department = user_data.get('department')
  46. requests[username, department] = user_data
  47.  
  48. return jsonify({"message": f"requests modified'{user_data}'"}), 201
  49. return jsonify({'error':'User Not Found'}), 404
  50.  
  51. @app.route('/item/<department>',methods=['POST'])
  52. def sortrequestsbydepartmentbb(department):
  53. for each_request in requests:
  54. if each_request.get('department') == department:
  55. return jsonify({'requests': each_request})
  56.  
  57. return jsonify({'error':'department Not Found'}), 404
  58.  
  59. @app.route('/update-details/<int:detail_id>', methods=['PUT'])
  60. def update_request(requests_id):
  61. requests = [requests for requests in requests if requests['id'] == requests_id]
  62. if len(requests) == 0:
  63. abort(404)
  64. if not request.get_json():
  65. abort(400)
  66.  
  67. if 'done' in request.get_json() and type(request.get_json()['done']) is not bool:
  68. abort(400)
  69. requests[0]['fullname'] = request.get_json().get('fullname', requests[0]['fullname'])
  70. requests[0]['email'] = request.get_json().get('email', requests[0]['email'])
  71. requests[0]['phone'] = request.get_json().get('phone',requests[0]['phone'])
  72. requests[0]['department'] = request.get_json().get('department', requests[0]['department'])
  73. requests[0]['computerID'] = request.get_json().get(
  74. 'computerID', requests[0]['computerID'])
  75. requests[0]['description'] = request.get_json().get(
  76. 'description', requests[0]['description'])
  77. requests[0]['done'] = request.get_json().get('done', requests[0]['done'])
  78. return jsonify({'requests': requests})
  79.  
  80.  
  81.  
  82. return jsonify({"message": "f'userdata'"}), 201
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement