def POST(self): """ Images upload main handler Can be tested using the following command: curl -F "image_title=some_title" -F "image_descr=some_descr" \ -F "image_file=@/home/oleg/Desktop/hard.jpg" \ http://localhost:8080/api/image/upload """ request_data = web.input(image_file={}) file_obj = request_data.get('image_file') # For some reason, FileStorage object treats itself as False if type(file_obj) == dict: return e_response("Required args are missing", 400) file_path = self.save_file(file_obj) images_dict = store_image_from_filename(self.db, file_path, widths=(202, 212)) external_id = _generate_external_id() pin_id = self.create_db_record({ 'name': request_data.get("image_title"), 'description': request_data.get("image_descr"), 'user_id': self._user['id'], 'link': request_data.get("link"), 'product_url': request_data.get("product_url"), 'price': request_data.get("price"), 'price_range': request_data.get("price_range"), 'board_id': request_data.get("board_id"), 'external_id': external_id, 'image_url': images_dict[0]['url'], 'image_width': images_dict[0]['width'], 'image_height': images_dict[0]['height'], 'image_202_url': images_dict[202]['url'], 'image_202_height': images_dict[202]['height'], 'image_212_url': images_dict[212]['url'], 'image_212_height': images_dict[212]['height'] }) self.data['image_id'] = pin_id self.data['external_id'] = external_id return self.respond()
def create_pin(db, user_id, title, description, link, tags, price, product_url, price_range, image_filename=None, board_id=None, repin=None): try: if image_filename: images_dict = media.store_image_from_filename(db, image_filename, widths=(202, 212)) else: empty = {'url': None, 'width': None, 'height': None} images_dict = {0: empty, 202: empty, 212: empty} if not price: price = None external_id = _generate_external_id() pin_id = db.insert(tablename='pins', name=title, description=description, user_id=user_id, link=link, views=1, price=price, image_url=images_dict[0]['url'], image_width=images_dict[0]['width'], image_height=images_dict[0]['height'], image_202_url=images_dict[202]['url'], image_202_height=images_dict[202]['height'], image_212_url=images_dict[212]['url'], image_212_height=images_dict[212]['height'], product_url=product_url, price_range=price_range, external_id=external_id, board_id=board_id, repin=repin) if tags: tags = parse_tags(tags) values_to_insert = [{ 'pin_id': pin_id, 'tags': tag } for tag in tags] db.multiple_insert(tablename='tags', values=values_to_insert) pin = db.where(table='pins', id=pin_id)[0] return pin except: logger.error('Cannot insert a pin in the DB', exc_info=True) raise
def _save_in_database(self, file_obj, resized_size, album_id): file_path = self._save_file(file_obj) images_dict = store_image_from_filename(db, file_path, widths=[resized_size]) picture = Picture( album_id, images_dict[0]['url'], images_dict.get(resized_size, images_dict[0])\ .get('url', None) ) web.ctx.orm.add(picture) web.ctx.orm.commit() return picture
def update_pin_images(db, pin_id, user_id, image_filename): images_dict = media.store_image_from_filename(db, image_filename, widths=(202, 212)) db.update( tables='pins', where='id=$id and user_id=$user_id', vars={ 'id': pin_id, 'user_id': user_id }, image_url=images_dict[0]['url'], image_width=images_dict[0]['width'], image_height=images_dict[0]['height'], image_202_url=images_dict[202]['url'], image_202_height=images_dict[202]['height'], image_212_url=images_dict[212]['url'], image_212_height=images_dict[212]['height'], )
def PUT(self, category_id, pin_id): ''' Puts the pin in the category's cool pins ''' db = database.get_db() transaction = db.transaction() try: pin = db.where(table='pins', id=pin_id)[0] image_name, _ = urllib.urlretrieve(pin.image_202_url) image = Image.open(image_name) if image.size[0] <= image.size[1]: ratio = 72.0 / float(image.size[0]) height = int(ratio * image.size[1]) image = image.resize((72, height), Image.ANTIALIAS) margin = (height - 72) / 2 crop_box = (0, margin, 72, 72 + margin) else: ratio = 72.0 / float(image.size[1]) width = int(ratio * image.size[0]) image = image.resize((width, 72), Image.ANTIALIAS) margin = (width - 72) / 2 crop_box = (margin, 0, 72 + margin, 72) image = image.crop(crop_box) image.save(image_name) image_urls_dict = media.store_image_from_filename( db=db, filename=image_name, widths=None) db.insert(tablename='cool_pins', category_id=category_id, pin_id=pin_id, image_url=image_urls_dict[0]) transaction.commit() except: transaction.rollback() logger.error( 'Could not add pin ({}) to cool pins for category ({})'.format( pin_id, category_id), exc_info=True) raise web.NotFound('Could not add pin to cool pins') web.header('Content-Type', 'application/json') return json.dumps({'status': 'ok'})