forked from fyxd-dev/tayda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.py
56 lines (43 loc) · 1.45 KB
/
product.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
# built-in librairies
from __future__ import annotations
from typing import List
from dataclasses import dataclass
from typing import ClassVar
# additional librairies
from currency_converter import CurrencyConverter
@dataclass
class Offer:
_TVA: ClassVar[float] = 1.2
quantity: int
currency: str
price_ht: float
def __repr__(self) -> str:
"""We want : self = eval(repr(self))"""
return f"Price(price_ht={self.price_ht}, currency={self.currency}" \
f"quantity={self.quantity})"
def __str__(self) -> str:
"""Returns a readable representation of the object"""
# We need to use the property to access price_eur_ttc to init it
return f"{self.quantity:>3} pcs for " \
f"{self.price_eur_ttc:8.2f}€ TTC"
@property
def price_eur_ht(self) -> float:
if self.currency == "EUR":
return self.price_ht
# "EUR" is the default for new_currency in convert(...)
return CurrencyConverter().convert(self.price_ht, self.currency)
@property
def price_eur_ttc(self) -> float:
return self.price_eur_ht * self._TVA
@dataclass
class Product:
sku: str
name: str
description: str
offers_list: List[Offer]
@property
def formatted_name(self):
return f"{self.sku} | {self.name}"
def __str__(self):
offers_str = '\n'.join(map(str, self.offers_list))
return f"{self.formatted_name}\n{offers_str}"