def get_brand(request, brand_key): if request.method == 'GET': brand = Brand.get_by_id(brand_key) if not brand: data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key} return render_json_response(data) return render_json_response(brand.to_json()) if request.method == 'PUT': model = simplejson.loads(request.data) brand = Brand.get_by_id(brand_key) if not brand: data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key} return render_json_response(data) brand.title = model['title'] brand.available = model['available'] brand.toggle_available() brand.put() data = {'api_success': True, 'api_msg': 'Brand %s updated' % brand_key} return render_json_response(data) if request.method == 'DELETE': brand = Brand.get_by_id(brand_key) if not brand: data = {'api_success': False, 'api_msg': 'Brand %s not found' % brand_key} return render_json_response(data) brand.pre_delete() brand.key.delete() data = {'api_success': True, 'api_msg': 'Brand %s deleted' % brand_key} return render_json_response(data)
def filter_brand(request): is_available = bool(request.values.get('available', False)) brand_key = request.values.get('brand') if brand_key: brand = Brand.get_by_id(int(brand_key)) products = Product.query().filter(Product.brand == brand.key).filter(Product.available == is_available) else: products = Product.query().filter(Product.brand == None).filter(Product.available == is_available) return render_to_response('shop/admin/product_filter.html', { 'products':products, 'available':is_available })
def get_brand_image(request, brand_key, image_key): if request.method == 'DELETE': brand = Brand.get_by_id(brand_key) if brand: for image in brand.images: if image.uid == image_key: image.pre_delete() brand.images.remove(image) brand.put() data = {'api_success': True, 'api_msg': 'BrandImage %s deleted' % image_key} return render_json_response(data) data = {'api_success': False, 'api_msg': 'BrandImage %s not found' % image_key} return render_json_response(data)
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)})
def get_brand_images(request, brand_key): brand = Brand.get_by_id(brand_key) images = [image.to_json() for image in brand.images] return render_json_response(images)
def create(data): id_1c = data.get('id_1c', '') if id_1c and is_product_exist_id_1c(id_1c): res = {'api_success':False, 'api_msg':'Field "id_1c" must be unique', 'api_function':create.__name__} logging.warning(res) return render_json_response(res) title = data.get('title','') if not title: res = {'api_success':False, 'api_msg':'"title" field is required', 'api_function':create.__name__} logging.warning(res) return render_json_response(res) original_title = data.get('original_title', '') short_description = data.get('short_description', '') rating = int(data.get('rating', 0)) leftovers = int(data.get('leftovers',0)) price = data.get('price', 0.0) if price: if isinstance(price, unicode): price = price.replace(",",'.') price = float(price) available = data.get('available', False) category = data.get('category', None) if category: category_id = category.get('id', None) category = Category.get_by_id(category_id).key brand = data.get('brand', None) if brand: brand_id = brand.get('id', None) brand = Brand.get_by_id(brand_id).key properties_raw = data.get('properties_', None) properties_ = [] if properties_raw: properties_raw = [prop.get('id', None) for prop in properties_raw] for prop in properties_raw: if prop: prop = ProductPropertyValue.get_by_id(prop) if prop: properties_.append(prop.key) product = Product( title=title, id_1c=id_1c, original_title=original_title, short_description=short_description, brand=brand, category=category, price=price, available=available, leftovers=leftovers, rating=rating) if properties_: for prop in properties_: product.properties_.append(prop) product.put() res = {'api_success': True, 'api_msg': 'Product created', 'product': product.to_json()} logging.info(res) return render_json_response(res)
category = category_obj.key else: category = None else: category = product_obj.category else: category = None product_obj.category = category brand = model.get('brand', None) if brand: brand = brand.get('id', None) if not product_obj.brand or (product_obj.brand and product_obj.brand.id() != brand): if brand: brand_obj = Brand.get_by_id(brand) if brand_obj: brand = brand_obj.key else: brand = None else: brand = product_obj.brand else: brand = None product_obj.brand = brand properties_ = model.get('properties', None) if properties_: properties_ = [prop.get('id', None) for prop in properties_] for prop in product_obj.properties_: