-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
79 lines (67 loc) · 2.82 KB
/
backend.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
75
76
77
78
79
from flask import Flask, render_template, jsonify, request
from pymongo import MongoClient
from flask_cors import CORS
cluster = MongoClient("mongodb+srv://newuser:[email protected]/CommunitySupplierDatabase?retryWrites=true&w=majority")
db = cluster["CommunitySupplierDatabase"]
collection = db["Requests"]
"""collection.insert_one(
{
"_id":0,
"hospitalName":"Memorial West",
"itemType":"Ventilator",
"quantityAsked": 5,
"hasFile":True,
"date": d1
}
)"""
app = Flask(__name__)
CORS(app) #enables
"""@app.route('/')
def root():
# For the sake of example, use static information to inflate the template.
# This will be replaced with real information in later steps.
return render_template('index.html')""" # pointless function now as rendering is no longer needed.
# @app.route("/", methods=["GET, POST"])
# def get():
# """GET in server"""
# # x = collection.find()
# response = jsonify(message="Simple server is running")
#
# # Enable Access-Control-Allow-Origin
# response.headers.add("Access-Control-Allow-Origin", "*")
# return response
@app.route("/api", methods=["GET, POST"])
def post():
if request.method == "POST":
try:
hospitalName = request.get_json()['hospitalName']
itemType = request.get_json()['itemType']
quantityAsked = request.get_json()['quantityAsked']
address = request.get_json()['address']
collection.insert_one(
{
"hospitalName":hospitalName,
"address":address,
"itemType":itemType,
"quantityAsked": quantityAsked
}
)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
return jsonify(message=message)
return jsonify(message="POST request returned")
else:
response = jsonify(message="Simple server is running")
# Enable Access-Control-Allow-Origin
response.headers.add("Access-Control-Allow-Origin", "*")
return response
if __name__ == '__main__':
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
# Flask's development server will automatically serve static files in
# the "static" directory. See:
# http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
# App Engine itself will serve those files as configured in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)