This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
214 lines (175 loc) · 8.66 KB
/
server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""BLOCK PARTY Server File"""
# from jinja2 import StrictUndefined
from flask import (Flask, jsonify, render_template, redirect, request,
flash, session, abort, url_for)
from flask_debugtoolbar import DebugToolbarExtension
#libraries for API requests
from sys import argv
from pprint import pprint, pformat
from model import db, connect_to_db, Donor, Address, Receiver, Food
# from passlib.hash import bcrypt
import os
from datetime import datetime, timedelta
from sqlalchemy import and_
from geopy.geocoders import Nominatim
app = Flask(__name__)
app.secret_key = "ABC"
# Raises an error in Jinja
# app.jinja_env.undefined = StrictUndefined
@app.route('/donor-info.json')
def fetch_donor_info(donor_id):
# donor = Donor.query.get(donor_id)
donor_dict = {"id": 1,
"name": "Chez Panisse",
"phone": "(510) 548-5525",
"email": "alice@chezpanisse.com",
"address": {
"address": "1517 Shattuck Ave, Berkeley, CA 94709",
"lat": 37.8796128,
"lng": -122.2710068,
}
}
return jsonify(donor_dict)
@app.route('/receiver-info.json')
def fetch_receiver_info(receiver_id):
receiver = Receiver.query.get(receiver_id)
receiver_dict = {"id": 1,
"name": "SF-Marin Food Bank",
"phone": "(415)282-1900",
"email": "paul@sf-marinfoodbank.org",
"address": {
"address": "900 Pennsylvania Ave., SF, CA 94107",
"lat": 37.7544355,
"lng": -122.395706,
}
}
return jsonify(receiver_dict)
@app.route('/pickups', methods=['GET', 'POST'])
def fetch_pickup_info():
"""Dictionary of food items that have not been claimed"""
available_pickups = {1: {{"name": "Chez Panisse",
"phone": "(510) 548-5525",
"email": "alice@chezpanisse.com",
"address": {
"address": "1517 Shattuck Ave, Berkeley, CA 94709",
"lat": 37.8796128,
"lng": -122.2710068
}
},
"foodItems": [
{"id": 1,
"name": "Fort Bragg rockfish",
"servings": 20,
"expiration_date": datetime.now() + timedelta(days=2)
},
{"id": 2,
"name": "Chantrelle mushrooms",
"servings": 40,
"expiration_date": datetime.now() + timedelta(days=15)
},
{"id": 3,
"name": "Leeks",
"servings": 35,
"expiration_date": datetime.now() + timedelta(days=10)
}
]},
2: {{"name": "Jupiter Taproom",
"phone": "(510)843-8277",
"email": "bob@jupitertaproom.com",
"address": {
"address": "2181 Shattuck Ave, Berkeley, CA 94704",
"lat": 37.8697972,
"lng": -122.2697709
}
},
"foodItems": [
{"id": 1,
"name": "Fort Bragg rockfish",
"servings": 20,
"expiration_date": datetime.now() + timedelta(days=2)
},
{"id": 2,
"name": "Chantrelle mushrooms",
"servings": 40,
"expiration_date": datetime.now() + timedelta(days=15)
},
{"id": 3,
"name": "Leeks",
"servings": 35,
"expiration_date": datetime.now() + timedelta(days=10)
}]},
3: {{"name": "Whitechapel",
"phone": "(415)292-5800",
"email": "unicorn@whitechapel.com",
"address": {
"address": "600 Polk St., SF, CA 94102",
"lat": 37.7823999,
"lng": -122.4210646
}},
"foodItems": [
{"id": 1,
"name": "Hamburgers",
"servings": 20,
"expiration_date": datetime.now() + timedelta(days=2)
},
{"id": 2,
"name": "Chantrelle mushrooms",
"servings": 40,
"expiration_date": datetime.now() + timedelta(days=15)
},
{"id": 3,
"name": "Leeks",
"servings": 35,
"expiration_date": datetime.now() + timedelta(days=10)
}
]}}
if request.method = "GET":
# food_lst = Food.query.filter(receiver_id = None).all()
return jsonify(available_pickups)
elif request.method = "POST":
available_pickups = available_pickups[1]["foodItems"] = {"id": 4,
"name": "Carrots",
"servings": 20,
"expiration_date": datetime.now() + timedelta(days=10)
}
return jsonify(available_pickups)
@app.route('/claims')
def fetch_claim_info():
"""Dictionary of food items that have not been claimed"""
# food_lst = Food.query.filter(receiver_id = None).all()
claimed_items = {1: {{"name": "Chez Panisse",
"phone": "(510) 548-5525",
"email": "alice@chezpanisse.com",
"address": {
"address": "1517 Shattuck Ave, Berkeley, CA 94709",
"lat": 37.8796128,
"lng": -122.2710068,
}},
"foodItems": [
{"id": 4,
"name": "Duck breast",
"servings": 12,
"expiration_date": datetime.now() + timedelta(days=5)
},
{"id": 5,
"name": "Hazelnut ice cream",
"servings": 23,
"expiration_date": datetime.now() + timedelta(days=30)
}
]}
}
return jsonify(claimed_items)
##HELPER FUNCTION
def address_to_latlng(formatted_add):
geolocator = Nominatim()
location = geolocator.geocode(formatted_add)
return jsonify("address": formatted_add: {"lat": location.latitude, "lng":location.longitude})
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
# app.jinja_env.auto_reload = app.debug # make sure templates, etc. are not cached in debug mode
connect_to_db(app)
# Use the DebugToolbar
# DebugToolbarExtension(app)
app.run(port=5000, host='0.0.0.0')