def update_from_dto(self, dto: dict): self.mapper.validate_dto_to_update(dto) product = self.product_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user()) if not product: raise ObjectNotFoundException(ErrorCode.PRODUCT_TO_UPDATE_NOT_FOUND, Product.__name__, {"id": dto.get("id")}) product = self.mapper.to_object(dto) ProductService.fill_to_create(product, datetime.utcnow(), AuthenticationContext.get_current_user()) self.product_dao.update(product) return product
def to_object(self, dto: dict, not_update: bool = False) -> Optional[object]: if not dto: return None product_type = None found = False if dto.get("id"): product_type = self.product_type_dao.find_by_id( dto.get("id"), AuthenticationContext.get_current_user()) found = product_type is not None if not found: product_type = ProductType() product_type.id = dto.get("id") product_type.created_by = self.user_mapper.to_object( dto.get("created_by")) product_type.created_at = dto.get("created_at") product_type.is_private = dto.get("is_private") if not found or not not_update: product_type.name = dto.get("name") product_type.parent_product_type = self.to_object( dto.get("parent_product_type"), not_update=True) product_type.description = dto.get("description") return product_type
def delete_by_id(self, product_id: int): product = self.product_dao.find_by_id(product_id, AuthenticationContext.get_current_user()) if not product: raise ObjectNotFoundException(ErrorCode.PRODUCT_TO_DELETE_NOT_FOUND, Product.__name__, {"id": product_id}) self.product_dao.delete(product)
def to_object(self, dto: dict, not_update: bool = False) -> Optional[Purchase]: if dto is None: return None purchase = None found = False if dto.get("id"): purchase = self.purchase_dao.find_by_id( dto.get("id"), AuthenticationContext.get_current_user()) found = purchase is not None if not found: purchase = Purchase() purchase.id = dto.get("id") purchase.created_at = dto.get("created_at") purchase.created_by = self.user_mapper.to_object( dto.get("created_by"), not_update=True) if not found or not not_update: purchase.name = dto.get("name") purchase.purchase_list = self.purchase_list_mapper.to_object( dto.get("purchase_list"), not_update=True) self.__products_to_obj(dto.get("products"), purchase) return purchase
def to_object(self, dto: dict, not_update: bool = False) -> Optional[Product]: if not dto: return None product = None found = False if dto.get("id"): product = self.product_dao.find_by_id( dto.get("id"), AuthenticationContext.get_current_user()) found = product is not None if not found: product = Product() product.id = dto.get("id") product.created_at = dto.get("created_at") product.created_by = self.user_mapper.to_object( dto.get("created_by"), not_update=True) product.is_private = dto.get("is_private") if not found or not not_update: product.name = dto.get("name") product.image_url = dto.get("image_url") product.brand = self.brand_mapper.to_object(dto.get("brand"), not_update=True) product.product_type = self.product_type_mapper.to_object( dto.get("product_type"), not_update=True) return product
def to_object(self, dto: dict, not_update: bool = False) -> Optional[Brand]: if not dto: return None brand = None found = False if dto.get("id"): brand = self.brand_dao.find_by_id( dto.get("id"), AuthenticationContext.get_current_user()) found = brand is not None if not found: brand = Brand() brand.id = dto.get("id") brand.created_at = dto.get("created_at") brand.is_private = dto.get("is_private") brand.created_by = self.user_mapper.to_object( dto.get("created_by"), not_update=True) if not found or not not_update: brand.name = dto.get("name") return brand
def create(self, product_type: ProductType): ProductTypeService.fill_to_create( product_type, datetime.utcnow(), AuthenticationContext.get_current_user()) self.product_type_dao.add(product_type) return product_type
def find_or_create_from_dto(self, dto: dict) -> Optional[Product]: if not dto.get("id"): return None product = self.product_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user()) if not product: product = self.create_from_dto(dto) return product
def create_from_dto(self, dto: dict): self.mapper.validate_dto_to_insert(dto) purchase_list = self.mapper.to_object(dto) PurchaseListService.fill_to_create( purchase_list, datetime.utcnow(), AuthenticationContext.get_current_user()) self.purchase_list_dao.add(purchase_list) return purchase_list
def update_from_dto(self, dto: dict): self.mapper.validate_dto_to_update(dto) brand = self.brand_dao.find_by_id(dto.get("id"), AuthenticationContext.get_current_user()) if not brand: raise ObjectNotFoundException(ErrorCode.BRAND_TO_UPDATE_NOT_FOUND, Brand.__name__, {"id": dto.get("id")}) brand = self.mapper.to_object(dto) self.brand_dao.update(brand) return brand
def update_from_dto(self, dto: dict): self.mapper.validate_dto_to_update(dto) purchase_list = self.find_by_id(dto.get("id")) if not purchase_list: raise ObjectNotFoundException( ErrorCode.PURCHASE_LIST_TO_UPDATE_NOT_FOUND, PurchaseList.__name__, {"id": dto.get("id")}) self.mapper.to_object(dto) PurchaseListService.fill_to_create( purchase_list, datetime.utcnow(), AuthenticationContext.get_current_user()) self.purchase_list_dao.update(purchase_list) return purchase_list
def update_from_dto(self, dto: dict): self.mapper.validate_dto_to_update(dto) user = self.find_by_id(dto.get("id")) if not user: raise ObjectNotFoundException(ErrorCode.USER_TO_UPDATE_NOT_FOUND, User.__name__, {"id": dto.get("id")}) if user != AuthenticationContext.get_current_user(): raise PermissionException(ErrorCode.UPDATE_USER_PERMISSION, User.__name__, Actions.UPDATE) user = self.mapper.to_object(dto) if dto.get("password"): user.password = PasswordEncryption.encrypt_password( dto.get("password")) self.user_dao.update(user, commit=True) return user
def test_get_current_user(self): self.assertIsNotNone(AuthenticationContext.get_current_user())
def find_by_id(self, brand_id: int) -> Brand: return self.brand_dao.find_by_id(brand_id, AuthenticationContext.get_current_user())
def find_all(self) -> List[Brand]: return self.brand_dao.find_all(AuthenticationContext.get_current_user())
def find_by_id(self, product_id: int) -> Product: return self.product_dao.find_by_id(product_id, AuthenticationContext.get_current_user())
def find_all(self) -> List[Product]: return self.product_dao.find_all(AuthenticationContext.get_current_user())
def find_all(self): return self.product_type_dao.find_allowed( AuthenticationContext.get_current_user())
def find_by_id(self, purchase_list_id: int) -> Optional[PurchaseList]: return self.purchase_list_dao.find_by_id( purchase_list_id, AuthenticationContext.get_current_user())
def find_all(self) -> List[PurchaseList]: return self.purchase_list_dao.find_all_by_user( AuthenticationContext.get_current_user())
def get_current_user(user_mapper: UserMapper): return jsonify(user_mapper.to_dto( AuthenticationContext.get_current_user()))
def create(self, brand: Brand) -> Brand: BrandService.fill_to_create(brand, datetime.utcnow(), AuthenticationContext.get_current_user()) self.brand_dao.add(brand) return brand