def set_default_image(self, request): ''' Set a front image of a product as its default image ''' # retrieve the requested product product = Product.get_by_urlsafe_key(request.product_key) if not product: message = 'No product with the key "%s" exists.' % request.product_key raise endpoints.NotFoundException(message) # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) # retrieve the image image = Image.get_by_urlsafe_key(request.image_key) if not image: message = 'No image with the key "%s" exists.' % request.image_key raise endpoints.NotFoundException(message) # the image must belong to the product and it must be a front image if not image.is_front_image() or not product.has_image(image): message = 'The given image either not a front image or it does not belong to the given product.' raise endpoints.BadRequestException(message) # set the image as default (featured) image product.set_default_image(image) # register the user as editor of the product ProductEditor.add_or_update(product, user) return message_types.VoidMessage()
def put(self): if not self.request.body: self.response.out.write(json.dumps({'text': 'Invalid request!'})) return data = json.loads(self.request.body) if data is None: self.response.out.write(json.dumps({'text': 'No data provided!'})) return image_key = data.get('imageId') ocr_result = data.get('text') image = Image.get_by_urlsafe_key(image_key) if not image: self.response.out.write( json.dumps({ 'text': 'The image with Id=%s could not be located.' % image_key })) image.ocr_result = ocr_result image.put() image.read() product = image.product.get() product.change_state(ProductState.UserCreated) self.response.out.write( json.dumps({'text': 'Successfully received the OCR result.'}))