|
15 | 15 |
|
16 | 16 | OPEN_DATA_URL = "https://3scale-public-prod-open-data.apps.k8s.upenn.edu/api/v1/dining/" |
17 | 17 | OPEN_DATA_ENDPOINTS = {"VENUES": OPEN_DATA_URL + "venues", "MENUS": OPEN_DATA_URL + "menus"} |
| 18 | +DINING_ICON_IDS = { |
| 19 | + "vegetarian": "1", |
| 20 | + "vegan": "4", |
| 21 | + "kosher": "11", |
| 22 | + "jain": "141", |
| 23 | + "ask_us": "262", |
| 24 | + "peanut": "253", |
| 25 | + "tree_nut": "254", |
| 26 | + "fish": "255", |
| 27 | + "wheat_gluten": "257", |
| 28 | + "milk": "258", |
| 29 | + "egg": "259", |
| 30 | + "soy": "260", |
| 31 | +} |
| 32 | +# Sesame appears on the website but is absent from the venue cor_icons fixture, |
| 33 | +# so we do not have a stable upstream ID for it yet??? |
| 34 | +# Halal (10) and shellfish (256) are stable upstream IDs but are not tracked here |
| 35 | +# because they are also not on the website. |
18 | 36 |
|
19 | 37 |
|
20 | 38 | class DiningAPIWrapper: |
@@ -44,6 +62,7 @@ def request(self, *args, **kwargs): |
44 | 62 | self.update_token() |
45 | 63 |
|
46 | 64 | headers = {"Authorization": f"Bearer {self.token}"} |
| 65 | + print(headers) |
47 | 66 |
|
48 | 67 | # add authorization headers |
49 | 68 | if "headers" in kwargs: |
@@ -203,23 +222,36 @@ def load_stations(self, station_response, dining_menu): |
203 | 222 | station.items.add(*items) |
204 | 223 | station.save() |
205 | 224 |
|
| 225 | + def _build_dining_item(self, key, value): |
| 226 | + icon_ids = value["cor_icon"] or {} |
| 227 | + return DiningItem( |
| 228 | + item_id=key, |
| 229 | + name=value["label"], |
| 230 | + description=value["description"], |
| 231 | + ingredients=value["ingredients"], |
| 232 | + vegetarian=DINING_ICON_IDS["vegetarian"] in icon_ids, |
| 233 | + vegan=DINING_ICON_IDS["vegan"] in icon_ids, |
| 234 | + kosher=DINING_ICON_IDS["kosher"] in icon_ids, |
| 235 | + jain=DINING_ICON_IDS["jain"] in icon_ids, |
| 236 | + ask_us=DINING_ICON_IDS["ask_us"] in icon_ids, |
| 237 | + peanut=DINING_ICON_IDS["peanut"] in icon_ids, |
| 238 | + tree_nut=DINING_ICON_IDS["tree_nut"] in icon_ids, |
| 239 | + sesame=False, |
| 240 | + fish=DINING_ICON_IDS["fish"] in icon_ids, |
| 241 | + wheat_gluten=DINING_ICON_IDS["wheat_gluten"] in icon_ids, |
| 242 | + milk=DINING_ICON_IDS["milk"] in icon_ids, |
| 243 | + egg=DINING_ICON_IDS["egg"] in icon_ids, |
| 244 | + soy=DINING_ICON_IDS["soy"] in icon_ids, |
| 245 | + nutrition_info=json.dumps( |
| 246 | + { |
| 247 | + x["label"]: f"{x['value']}{x['unit']}" |
| 248 | + for x in value["nutrition_details"].values() |
| 249 | + } |
| 250 | + ), |
| 251 | + ) |
| 252 | + |
206 | 253 | def load_items(self, item_response): |
207 | | - item_list = [ |
208 | | - DiningItem( |
209 | | - item_id=key, |
210 | | - name=value["label"], |
211 | | - description=value["description"], |
212 | | - ingredients=value["ingredients"], |
213 | | - allergens=", ".join(value["cor_icon"].values()) if value["cor_icon"] else "", |
214 | | - nutrition_info=json.dumps( |
215 | | - { |
216 | | - x["label"]: f"{x['value']}{x['unit']}" |
217 | | - for x in value["nutrition_details"].values() |
218 | | - } |
219 | | - ), |
220 | | - ) |
221 | | - for key, value in item_response.items() |
222 | | - ] |
| 254 | + item_list = [self._build_dining_item(key, value) for key, value in item_response.items()] |
223 | 255 | # Ignore conflicts because possibility of duplicate items |
224 | 256 | DiningItem.objects.bulk_create( |
225 | 257 | item_list, |
|
0 commit comments