示例#1
0
def create(data):
    product_id= data.get('product_id', '')
    product = Product.get_by_id(product_id)
    if not product:
        res = { 'api_success':False,
                'api_msg':'Product %s not found' % product_id,
                'api_function': create.__name__}
        logging.warning(res)
        return render_json_response(res)
    image64 = data.get('image_data', '')
    if not image64:
        res = {'api_success': False, 'api_msg': 'Image data not found'}
        logging.warning(res)
        return render_json_response(res)
    image64 = base64.decodestring(image64)
    im = Image.create_from_data(image64, title=product.title)
    if im.get_cached_url():
        if product.master_image is None:
            im.is_master = True
        product.images.append(im)
        product.put()
        res = {'api_success': True, 'api_msg': 'Image uploaded'}
        logging.info(res)
    else:
        res = {'api_success':False,
                 'api_msg':'Invalid image'}
        logging.warning(res)
    return render_json_response(res)
示例#2
0
def brand_add_image(request, brand_key):
    if request.method == 'POST':
        try:
            brand = Brand.get_by_id (brand_key)
            img = Image.create_from_data(
                blob_data=request.files['file'].read(),
                format=request.files['file'].content_type)
            if img.get_cached_url():
                brand.images.append(img)
                brand.put()
                return render_json_response({'api_success': True,
                                         'api_msg': 'BrandImage created'})
            else:
                return render_json_response(
                            {'api_success':False,
                             'api_msg':'Invalid image'})
        except apiproxy_errors.RequestTooLargeError:
            return render_json_response({'api_success': False,
                             'api_msg': u'Файл слишком большой. '
                                        u'Размер файла должен быть мешьше 1 мегабайта.'})
        except Exception, e:
            return render_json_response({'api_success': False,
                                         'api_msg': str(e)})