-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.py
81 lines (70 loc) · 2.93 KB
/
recipe.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
#!/usr/bin/python
"""
This Module contains the Recipe class
Author: Peter Ekwere
"""
import models
from models.base_model import BaseModel, Base
from os import getenv
from sqlalchemy import Column, String, Integer, Float, ForeignKey, Table
from sqlalchemy.orm import relationship
if models.storage_type == 'db':
recipe_ingredients = Table('recipe_ingredients', Base.metadata,
Column('recipe_id', String(60),
ForeignKey('recipe.id', onupdate='CASCADE',
ondelete='CASCADE'),
primary_key=True),
Column('ingredient_id', String(60),
ForeignKey('ingredient.id', onupdate='CASCADE',
ondelete='CASCADE'),
primary_key=True))
class Recipe(BaseModel, Base):
"""Representation of a Recipe """
if models.storage_type == 'db':
__tablename__ = 'recipe'
ingredient_id = Column(String(60), ForeignKey('ingredient.id'), nullable=False)
user_id = Column(String(60), ForeignKey('users.id'), nullable=False)
name = Column(String(128), nullable=False)
description = Column(String(1024), nullable=True)
ingredients = relationship("Ingredient",
backref="place",
cascade="all, delete, delete-orphan",
single_parent=True)
else:
user_id = ""
name = ""
description = ""
ingredient_ids = []
def __init__(self, *args, **kwargs):
"""initializes Place"""
super().__init__(*args, **kwargs)
if models.storage_type != 'db':
@property
def ingredients(self):
"""getter attribute returns the list of ingredient instances"""
return self.ingredients
def process_recipe_data(recipe_data):
""" This method takes a spooncular recipe data ane processes it
Args:
recipe_data (str): This is a json response
"""
response_data = {
'recipe': recipe_data[0]['name'],
'instructions': [],
'ingredients': []
}
#Extract instructions
for instruction_step in recipe_data[0]['steps']:
step_info = {
'step': instruction_step['step']
}
for ingredient in instruction_step.get('ingredients', []):
ingredient_info = {
'id': ingredient['id'],
'name': ingredient['name'],
'amount': ingredient.get('amount'),
'unit': ingredient.get('unit'),
}
response_data['ingredients'].append(ingredient_info)
response_data['instructions'].append(step_info)
return response_data