forked from ayaankazerouni/flask-playground-db-access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_backend.py
93 lines (84 loc) · 2.71 KB
/
sample_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
import random
import string
from model_mongodb import User
app = Flask(__name__)
#CORS stands for Cross Origin Requests.
CORS(app) #Here we'll allow requests coming from any domain. Not recommended for production environment.
users = {
'users_list' :
[
{
'id' : 'xyz789',
'name' : 'Charlie',
'job': 'Janitor',
},
{
'id' : 'abc123',
'name': 'Mac',
'job': 'Bouncer',
},
{
'id' : 'ppp222',
'name': 'Mac',
'job': 'Professor',
},
{
'id' : 'yat999',
'name': 'Dee',
'job': 'Aspring actress',
},
{
'id' : 'zap555',
'name': 'Dennis',
'job': 'Bartender',
}
]
}
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/users', methods=['GET', 'POST'])
def get_users():
if request.method == 'GET':
search_username = request.args.get('name')
search_job = request.args.get('job')
if search_username and search_job:
# TODO: Replace with database access
result = find_users_by_name_job(search_username, search_job)
elif search_username:
# using list shorthand for filtering the list.
# TODO: Replace with database access
result = [user for user in users['users_list'] if user['name'] == search_username]
else:
result = User().find_all()
return {"users_list": result}
elif request.method == 'POST':
userToAdd = request.get_json() # no need to generate an id ourselves
newUser = User(userToAdd)
newUser.save() # pymongo gives the record an "_id" field automatically
resp = jsonify(newUser), 201
return resp
@app.route('/users/<id>', methods=['GET', 'DELETE'])
def get_user(id):
if request.method == 'GET':
user = User({"_id":id})
if user.reload() :
return user
else :
return jsonify({"error": "User not found"}), 404
elif request.method == 'DELETE':
user = User({"_id":id})
resp = user.remove()
# TODO: Check the resp object if the removal was successful or not.
# Return a 404 status code if it was not successful
return {}, 204
def find_users_by_name_job(name, job):
subdict = {'users_list' : []}
for user in users['users_list']:
if user['name'] == name and user['job'] == job:
subdict['users_list'].append(user)
return subdict