|
@@ -12,15 +12,21 @@ from .exception import bad_request_exception
|
|
|
from .exception import not_found_exception
|
|
from .exception import not_found_exception
|
|
|
from .exception import access_denied_exception
|
|
from .exception import access_denied_exception
|
|
|
from .users_collection import users_collection
|
|
from .users_collection import users_collection
|
|
|
|
|
+from .image import image
|
|
|
|
|
+from .directory_image import directory_image
|
|
|
|
|
|
|
|
class product_app(app_route_database):
|
|
class product_app(app_route_database):
|
|
|
def __init__(
|
|
def __init__(
|
|
|
self,
|
|
self,
|
|
|
connection: sqlalchemy.engine.base.Engine,
|
|
connection: sqlalchemy.engine.base.Engine,
|
|
|
- users: users_collection
|
|
|
|
|
|
|
+ users: users_collection,
|
|
|
|
|
+ images: directory_image
|
|
|
|
|
+
|
|
|
) -> None:
|
|
) -> None:
|
|
|
super().__init__(connection)
|
|
super().__init__(connection)
|
|
|
self.__users_collection = users
|
|
self.__users_collection = users
|
|
|
|
|
+ self.__response = product_response(images)
|
|
|
|
|
+ self.__images_manager = images
|
|
|
|
|
|
|
|
def all(self) -> dict:
|
|
def all(self) -> dict:
|
|
|
try:
|
|
try:
|
|
@@ -78,16 +84,37 @@ class product_app(app_route_database):
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
return self._fail(str(error))
|
|
return self._fail(str(error))
|
|
|
|
|
|
|
|
|
|
+ def __image(self, send: dict) -> image | None:
|
|
|
|
|
+ if not "image" in send:
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ return image(send["image"])
|
|
|
|
|
+
|
|
|
def create(self, send: dict) -> dict:
|
|
def create(self, send: dict) -> dict:
|
|
|
try:
|
|
try:
|
|
|
if not self.__logged_in(send):
|
|
if not self.__logged_in(send):
|
|
|
raise access_denied_exception()
|
|
raise access_denied_exception()
|
|
|
|
|
+
|
|
|
|
|
+ target = product_builder().modify(send).result
|
|
|
|
|
+ image = self.__image(send)
|
|
|
|
|
+
|
|
|
|
|
+ if image is None:
|
|
|
|
|
+ raise bad_request_exception("not contain image")
|
|
|
|
|
|
|
|
with self.__products_database as loader:
|
|
with self.__products_database as loader:
|
|
|
- target = product_builder().modify(send).result
|
|
|
|
|
result = loader.store(target)
|
|
result = loader.store(target)
|
|
|
-
|
|
|
|
|
- return self.__modify(result, "Can nod create product.")
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if not result:
|
|
|
|
|
+ return self.__modify(result, "Can not create product.")
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ self.__images_manager.save(image, target)
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ loader.drop(target)
|
|
|
|
|
+ raise error
|
|
|
|
|
+
|
|
|
|
|
+ return self._success()
|
|
|
|
|
+
|
|
|
|
|
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
return self._fail(str(error))
|
|
return self._fail(str(error))
|
|
@@ -140,11 +167,49 @@ class product_app(app_route_database):
|
|
|
|
|
|
|
|
with self.__products_database as loader:
|
|
with self.__products_database as loader:
|
|
|
target = self.__select_by_sended(send, loader)
|
|
target = self.__select_by_sended(send, loader)
|
|
|
|
|
+
|
|
|
|
|
+ old = target.save_copy()
|
|
|
updated = product_builder(target).modify(send).result
|
|
updated = product_builder(target).modify(send).result
|
|
|
- result = loader.store(updated)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if not loader.store(updated):
|
|
|
|
|
+ return self._fail("Can not update product.")
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ self.__images_manager.update(old, updated)
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ updated.restore_copy(old)
|
|
|
|
|
+
|
|
|
|
|
+ if not loader.store(updated):
|
|
|
|
|
+ error = "Image caould be moved. Can not restore "
|
|
|
|
|
+ error = error + "previous product. Fatal error."
|
|
|
|
|
+
|
|
|
|
|
+ return self._fail(error)
|
|
|
|
|
+
|
|
|
|
|
+ raise error
|
|
|
|
|
+
|
|
|
|
|
+ return self._success()
|
|
|
|
|
|
|
|
- return self.__modify(result, "Can not update product.")
|
|
|
|
|
|
|
+ except Exception as error:
|
|
|
|
|
+ return self._fail(str(error))
|
|
|
|
|
|
|
|
|
|
+ def update_image(self, send: dict) -> dict:
|
|
|
|
|
+ try:
|
|
|
|
|
+ if not self.__logged_in(send):
|
|
|
|
|
+ raise access_denied_exception()
|
|
|
|
|
+
|
|
|
|
|
+ image = self.__image(send)
|
|
|
|
|
+
|
|
|
|
|
+ if image is None:
|
|
|
|
|
+ raise bad_request_exception("Not found image in request.")
|
|
|
|
|
+
|
|
|
|
|
+ with self.__products_database as loader:
|
|
|
|
|
+ target = self.__select_by_sended(send, loader)
|
|
|
|
|
+
|
|
|
|
|
+ self.__images_manager.drop(target)
|
|
|
|
|
+ self.__images_manager.save(image, target)
|
|
|
|
|
+
|
|
|
|
|
+ return self._success()
|
|
|
|
|
+
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
return self._fail(str(error))
|
|
return self._fail(str(error))
|
|
|
|
|
|
|
@@ -155,10 +220,15 @@ class product_app(app_route_database):
|
|
|
|
|
|
|
|
with self.__products_database as loader:
|
|
with self.__products_database as loader:
|
|
|
target = self.__select_by_sended(send, loader)
|
|
target = self.__select_by_sended(send, loader)
|
|
|
- result = loader.drop(target)
|
|
|
|
|
|
|
+ copy = target.save_copy()
|
|
|
|
|
|
|
|
- return self.__modify(result, "Can not delete product.")
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if not loader.drop(target):
|
|
|
|
|
+ return self._fail("Can not delete product.")
|
|
|
|
|
+
|
|
|
|
|
+ self.__images_manager.drop(copy)
|
|
|
|
|
+
|
|
|
|
|
+ return self._success()
|
|
|
|
|
+
|
|
|
except Exception as error:
|
|
except Exception as error:
|
|
|
return self._fail(str(error))
|
|
return self._fail(str(error))
|
|
|
|
|
|
|
@@ -175,10 +245,10 @@ class product_app(app_route_database):
|
|
|
if target is None:
|
|
if target is None:
|
|
|
return self._fail("Can not found product in database.")
|
|
return self._fail("Can not found product in database.")
|
|
|
|
|
|
|
|
- return self._success(product = product_response(target))
|
|
|
|
|
|
|
+ return self._success(product = self.__response.single(target))
|
|
|
|
|
|
|
|
def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
def __collection(self, target: typing.Iterable[product]) -> dict:
|
|
|
- return self._success(collection = product_response(target))
|
|
|
|
|
|
|
+ return self._success(collection = self.__response.collection(target))
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
|
def __users(self) -> users_collection:
|
|
def __users(self) -> users_collection:
|