-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlask.py
More file actions
29 lines (21 loc) · 819 Bytes
/
Copy pathFlask.py
File metadata and controls
29 lines (21 loc) · 819 Bytes
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
from flask import Flask
from markupsafe import escape
from pymongo import MongoClient
app_flask = Flask(__name__)
client = MongoClient('localhost:27017',
username='root',
password='Qwerty123!',
authMechanism='SCRAM-SHA-256')
database = client['hua-python']
collection = database['user']
@app_flask.route('/user/<name>', methods=['GET'])
def find_products_by_username(name):
username = escape(name)
response = collection.find({'name': username}, {"products": 1})
response_list = list(response)
if len(response_list) == 0:
return {'errorCode': 404, 'message': 'User not found'}
elif len(response_list) == 1:
return {'products': response_list[0].get('products')}
else:
return {'error': 'Generic error'}