def post(self): # get the info about the uploaded images upload_files_front = self.get_file_infos('front_image') upload_files_ing = self.get_file_infos('ingredients_image') if not len(upload_files_front) or not len(upload_files_ing): self.response.out.write('Images not uploaded') return front_image = upload_files_front[0] ingredients_image = upload_files_ing[0] # check whether the image has been uploaded if not front_image.gs_object_name or not ingredients_image.gs_object_name: self.response.out.write( 'Image info not found. Seems to be upload error!') return ingredients = self.request.get('ingredients') if not ingredients: self.response.out.write( 'You must provide a list of ingredients for the product to be created.' ) return user = User.load(users.get_current_user()) product = Product.create_by_barcode(self.PRODUCT_BARCODE, user) product.name = self.PRODUCT_NAME product.put() Image.create(creator=user, product=product, blob=blobstore.create_gs_key(front_image.gs_object_name), image_type=ImageType.Front, ocr_result='', featured=True) Image.create(creator=user, product=product, blob=blobstore.create_gs_key( ingredients_image.gs_object_name), image_type=ImageType.Ingredient, ocr_result=ingredients, featured=False) for ingredient_name in ingredients.splitlines(): ingredient = Ingredient.find_by_name(ingredient_name) if ingredient: ProductIngredient.add_entry(product, ingredient, user) self.redirect('/utils')
def create_product(self, request): ''' Create a product based on a barcode ''' user = User.get_by_urlsafe_key(request.creator) if not user: message = 'No user with the key "%s" exists.' % request.creator raise endpoints.NotFoundException(message) product = Product.create_by_barcode(request.barcode, user) # if there is a wrong-product-report, register it if request.wrong_product: wrong_product = Product.get_by_urlsafe_key(request.wrong_product) if not wrong_product: message = 'No product with the key "%s" exists.' % request.wrong_product raise endpoints.NotFoundException(message) wrong_product.mark_wrong(user, product) return ProductCreated(key=product.key.urlsafe(), uploadURLs=UploadHandler.create_upload_urls(4))