-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
225 lines (190 loc) · 6.77 KB
/
classes.py
File metadata and controls
225 lines (190 loc) · 6.77 KB
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
215
216
217
218
219
220
221
222
223
224
225
import os
import base64
from dataclasses import dataclass
from typing import Optional
from jinja2 import Environment, FileSystemLoader
class HasID:
"""
Abstract class for items with an ID
"""
instances: dict[str, "HasID"]
id_field: str = "name"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if getattr(self, self.id_field) in self.instances:
raise ValueError(f"Duplicate {self.id_field} value")
self.instances[getattr(self, self.id_field)] = self
@property
def id(self):
return getattr(self, self.id_field)
@classmethod
def get_by_id(cls, id_value):
if id_value not in cls.instances:
raise ValueError(f"No {cls.__name__} with {cls.id_field} {id_value}")
return cls.instances.get(id_value)
@classmethod
def create(cls, **kwargs):
instance = cls(**kwargs)
cls.instances[getattr(instance, cls.id_field)] = instance
return instance
@classmethod
def get_or_create(cls, **kwargs):
id_value = kwargs.get(cls.id_field)
if id_value in cls.instances:
return cls.instances.get(id_value)
return cls.create(**kwargs)
class HasTemplate:
"""
Abstract class for items with a template
"""
template: str
def format_for_template(self) -> dict:
raise NotImplementedError
def get_html(self, page_number: Optional[int] = None) -> str:
env = Environment(loader=FileSystemLoader(searchpath="templates"))
template = env.get_template(self.template)
template_vars = self.format_for_template()
if page_number:
template_vars["page_number"] = page_number
return template.render(template_vars)
@dataclass
class Category(HasTemplate, HasID):
"""
Category dataclass
Attributes:
- name: name of the category
- arabic_name: name of the category in arabic
- recipes: list of recipes
"""
instances = {}
name: str
arabic_name: str
order: int
template: str = "category.html"
@classmethod
def batch_create(cls, data: list[dict]):
instances = []
for category in data:
instance = cls.create(
name=category["name"],
arabic_name=category["arabic_name"],
order=category["order"]
)
instances.append(instance)
return instances
def format_for_template(self) -> dict:
return {
"name": self.name,
"arabic_name": self.arabic_name,
}
@dataclass
class Ingredient(HasID):
"""
Ingredient dataclass
Attributes:
- name: name of the ingredient
"""
instances = {}
name: str
@dataclass
class IngredientQuantity:
"""
IngredientQuantity dataclass
Attributes:
- ingredient: ingredient
- quantity: quantity of the ingredient
"""
ingredient: Ingredient
quantity: str
@classmethod
def batch_create(cls, data: list[dict]):
instances = []
for ingredient_quantity in data:
instance = cls(
ingredient=Ingredient.get_or_create(
name=ingredient_quantity["name"]
),
quantity=ingredient_quantity["quantity"]
)
instances.append(instance)
return instances
@dataclass
class Recipe(HasTemplate, HasID):
"""
Recipe dataclass
Attributes:
- name: name of the meal
- arabic_name: name of the meal in arabic
- servings: number of servings
- ingredients: list of ingredients and their quantities
- instructions: list of instructions
"""
instances = {}
order: int
category: Category
name: str
arabic_name: str
image_path: str
description: str
servings: int
ingredients: list[IngredientQuantity]
instructions: list[str]
template: str = "recipe.html"
def __str__(self):
return self.name
def format_for_template(self) -> dict:
image_path = os.path.abspath(self.image_path)
with open(image_path, "rb") as file:
encoded_string = base64.b64encode(file.read()).decode("utf-8")
image_path = f"data:image/jpeg;base64,{encoded_string}"
return {
"name": self.name,
"arabic_name": self.arabic_name,
"image_path": image_path,
"description": self.description,
"servings": self.servings,
"ingredients": [i.quantity for i in self.ingredients],
"instructions": self.instructions,
}
def has_ingredient(self, ingredient: Ingredient) -> bool:
return any(
ingredient_quantity.ingredient == ingredient
for ingredient_quantity in self.ingredients
)
def is_in_category(self, category: Category) -> bool:
return self.category == category
@classmethod
def get_by_categories(cls) -> list[dict]:
categories = Category.instances.values()
recipes_by_category = {category.name: {"category": category, "recipes": []} for category in categories}
for recipe in sorted(list(cls.instances.values()), key=lambda x: x.order):
recipes_by_category[recipe.category.name]["recipes"].append(recipe)
recipes_by_category = list(recipes_by_category.values())
return sorted(recipes_by_category, key=lambda x: x["category"].order)
@classmethod
def get_by_ingredients(cls) -> list[dict]:
ingredients = Ingredient.instances.values()
recipes_by_ingredient = {ingredient.name: {"ingredient": ingredient, "recipes": []} for ingredient in ingredients}
for recipe in sorted(cls.instances.values(), key=lambda x: x.name):
for ingredient in ingredients:
if recipe.has_ingredient(ingredient):
recipes_by_ingredient[ingredient.name]["recipes"].append(recipe)
recipes_by_ingredient = list(recipes_by_ingredient.values())
return sorted(recipes_by_ingredient, key=lambda x: x["ingredient"].name)
@classmethod
def batch_create(cls, data: list[dict]):
instances = []
for recipe in data:
instance = cls.create(
order=recipe["order"],
category=Category.get_by_id(recipe["category"]),
name=recipe["name"],
arabic_name=recipe["arabic_name"],
image_path=recipe["image_path"],
description=recipe["description"],
servings=recipe["servings"],
ingredients=IngredientQuantity.batch_create(recipe["ingredients"]),
instructions=recipe["instructions"],
)
instances.append(instance)
return instances