product_response.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import typing
  2. import collections.abc
  3. from .directory_image import directory_image
  4. from .product import product
  5. from .exception import not_ready_exception
  6. class product_response:
  7. def __init__(self, images: directory_image) -> None:
  8. self.__images = images
  9. @property
  10. def images(self) -> directory_image:
  11. return self.__images
  12. def single(self, target: product) -> dict:
  13. if not target.ready:
  14. raise not_ready_exception(target)
  15. covers = directory_image.server_path() + "/"
  16. return {
  17. "name": target.name,
  18. "description": target.description,
  19. "author": target.author,
  20. "image": covers + self.images.get_full_name(target),
  21. "thumbnail": covers + self.images.get_thumbnail_name(target),
  22. "stock_count": target.stock_count,
  23. "on_stock": target.on_stock,
  24. "barcode": target.barcode
  25. }
  26. def collection(self, targets: typing.Iterable[product]) -> list:
  27. result = list()
  28. for count in targets:
  29. if not isinstance(count, product):
  30. raise TypeError("Product list must contain only products.")
  31. result.append(self.single(count))
  32. return result