def get( self, request: Request, ) -> Response: """ Get info about product by its id. :param request: request with "id" field. :return: response whether request is successful with info about product. """ profile: Profile = get_profile_by_token(request) if not profile.has_valid_token: return INVALID_CREDENTIALS id_: str = request.query_params.get("id") try: product: Product = Product.get_by_id(id_) except (ValueError, TypeError, Product.DoesNotExist): return PRODUCT_NOT_FOUND serializer = ProductSerializer( product, many=False, ) return Response( data=serializer.data, status=HTTP_200_OK, )
def post( self, request: Request, ) -> Response: """ Edit product info. :param request: request with "id" and optional "title" and "category" fields. :return: response whether request is successful. """ profile: Profile = get_profile_by_token(request) if not profile.has_valid_token or profile.role != Admin: return INVALID_CREDENTIALS id_: str = request.data.get("id") try: product: Product = Product.get_by_id(id_) except (ValueError, TypeError, Product.DoesNotExist): return PRODUCT_NOT_FOUND title: str = request.data.get("title") if title: product.title = title category: str = request.data.get("category") if category is not None: product.category = category product.save() return Response( data={ "message": "Product info was edited successfully.", }, status=HTTP_200_OK, )