def create_category(request): if request.method == 'POST': try: model = simplejson.loads(request.data) title = model.get('title', '') available = model.get('available', False) only_end_products = model.get('only_end_products') parent_category = model.get('parent_category', None) if parent_category: parent_id = parent_category.get('id', None) if parent_id: parent_category = Category.get_by_id(int(parent_id)) if parent_category: parent_category = parent_category.key category = Category( title=title, available=available, parent_category=parent_category, only_end_products=only_end_products) category.put() res = {'api_success': True, 'api_msg': 'Category "%s" created' % category.key.id()} res.update(category.to_json()) return render_json_response(res) except Exception, e: res = {'api_success': False, 'api_msg': str(e)} return render_json_response(res)
def get_category(request, id): cat_obj = Category.get_by_id(id) if not cat_obj: return redirect(url_for('shop/index')) categories = Category.query(Category.available == True, Category.only_end_products == True).fetch() if request.user and request.user.is_admin: products_obj = Product.query().filter(Product.category == cat_obj.key).order(-Product.rating).fetch() else: products_obj = Product.query().filter(Product.category == cat_obj.key).filter(Product.available == True).order(-Product.rating).fetch() return render_to_response('shop/category.html', {'category':cat_obj, 'products':products_obj, 'categories':categories})
def products(request): categories = Category.query(Category.available == True, Category.only_end_products == True).order(Category.title).fetch() if request.user and request.user.is_admin: products_obj = Product.query().order(-Product.rating).fetch(20) else: products_obj = Product.query().filter(Product.available == True).order(-Product.rating).fetch(20) return render_to_response('shop/products.html', {'products':products_obj, 'categories':categories})
def category_json(request, category_id): category = Category.get_by_id(category_id) if not category: return render_json_response({ 'api':{ 'success': False, 'msg': 'Category %s not found' % category_id } }, status=400) if request.method == 'GET': return get_category(category) if request.method == 'DELETE': return delete_category(request, category) if request.method == 'PUT': return put_category(request, category) return render_json_response({ 'api':{ 'success':False, 'msg': 'Request method %s not supported' % request.method } }, status=400)
def constructor(request): categories = Category.query(Category.available == True, Category.only_end_products == False).order(Category.title) products_obj = None if categories.count(): if request.user and request.user.is_admin: products_obj = Product.query(Product.category==categories.get().key).order(-Product.rating).fetch(20) else: products_obj = Product.query(Product.category==categories.get().key).filter(Product.available == True).order(-Product.rating).fetch(20) return render_to_response('shop/constructor.html', {'products':products_obj, 'categories':categories.fetch()})
def images(request, uid): category = Category.get_by_id(uid) if not category: return render_to_response('empty.html') url = blobstore.create_upload_url(url_for('shop/admin/category/images/add', uid=category.key.id())) return render_to_response('shop/admin/category_images.html',{ 'images': category.images, 'category': category, 'url': url })
def create(data, is_unique_title=False): try: 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) if is_unique_title and is_category_exist(title): res = {"api_success": False, "api_msg": 'Field "title" must be unique', "api_function": create.__name__} logging.warning(res) return render_json_response(res) available = data.get("available", False) parent = data.get("parent", None) if parent: parent_id = parent.get("id", None) if parent_id: parent = Category.get_by_id(parent_id) if parent: category = Category(title=title, parent_category=parent.key, available=available) else: category = Category(title=title, available=available) category.put() res = {"api_success": True, "api_msg": "Category created", "category": category.to_json()} logging.info(res) return render_json_response(res) except Exception, e: res = {"api_success": False, "api_msg": str(e), "api_function": create.__name__} logging.error(res) return render_json_response(res)
def get_category(request, category_id): if request.method == 'GET': try: category = Category.get_by_id(category_id) if not category: data = {'api_success': False, 'api_msg': 'Category %s not found' % category_id} return render_json_response(data) return render_json_response(category.to_json()) except Exception, e: data = {'api_success': False, 'api_msg': str(e)} return render_json_response(data)
def desc(request, id): category = Category.get_by_id(id) if not category: return redirect('/shop/admin/show_categories/') form = CategoryDescForm(initial={'description':category.description}) if request.method == 'POST': if form.validate(request.form): category.description = form['description'] category.put() return redirect('shop/admin/show_categories') return render_to_response('shop/admin/category_desc.html', { 'form':form.as_widget(), 'category': category })
def delete_image(request, uid, img_uid): category = Category.get_by_id(uid) if not category or not category.images: return redirect(url_for('shop/admin/category/images', uid=uid)) for image in category.images: if image.uid == img_uid: master = False if image.is_master: master = True image.pre_delete() category.images.remove(image) if master and len(category.images): new_master = category.images[0] new_master.is_master = True category.put() return redirect(url_for('shop/admin/category/images', uid=uid))
def product(request): all_product = Product.available_list() all_hide_product = Product.not_available_list() available_product_without_properties_count = 0 for pr in all_product: if not len(pr.properties_): available_product_without_properties_count += 1 unavailable_product_without_properties_count = 0 for pr in all_hide_product: if not len(pr.properties_): unavailable_product_without_properties_count += 1 brand_all_available_product = Product.available_list().filter(Product.brand != None) logging.warning(brand_all_available_product.count()) brand_all_not_available_product = Product.not_available_list().filter(Product.brand != None) no_brand_all_available_product_count = Product.available_list().filter(Product.brand == None).count() no_brand_all_not_available_product_count = Product.not_available_list().filter(Product.brand == None).count() available_brand = {} for product_ in brand_all_available_product: if not product_.brand in available_brand: count = Product.available_list().filter(Product.brand == product_.brand).count() available_brand[product_.brand] = count not_available_brand = {} for product_ in brand_all_not_available_product: if not product_.brand in not_available_brand: count = Product.not_available_list().filter(Product.brand == product_.brand).count() not_available_brand[product_.brand] = count categories = Category.query(Category.parent_category == None).order(Category.title) properties = ProductProperty.query().order(ProductProperty.title) brands = Brand.query().order(Brand.title) available_uncategories = all_product available_uncategories = available_uncategories.filter(Product.category == None) unavailable_uncategories = all_hide_product unavailable_uncategories = unavailable_uncategories.filter(Product.category == None) return render_to_response('shop/admin/product.html', locals() )
def put_category(request, category, is_new=False): model = simplejson.loads(request.data) if type(model) != dict: return render_json_response({ 'api': { 'success': False, 'msg': 'Invalid data', 'data': request.data } }, status=500) if is_new: category = Category(title='') change_string(category, 'title', model.get('title')) change_string(category, 'available', model.get('available')) change_string(category, 'only_end_products', model.get('only_end_products')) wrn = [] parent_category = model.get('parent_category') if type(parent_category) == dict: parent_id = parent_category.get('id') if parent_id: parent_category_obj = Category.get_by_id(int(parent_id)) if parent_category_obj: category.parent_category = parent_category_obj.key else: wrn.append('Category with ID: %s not found' % parent_id) else: if parent_category == {}: category.parent_category = None else: wrn.append('Invalid category object: %s' % parent_category) category.put() if is_new: msg = 'Category %s created' else: msg = 'Category %s updated' data = {'api':{ 'success': True, 'msg': msg % category.id, 'wrn': wrn } } data.update(category.to_json()) return render_json_response(data)
def filter_category(request): is_available = request.values.get('available', '0') is_available = is_available.lower() in ["1", "true", "yes"] category_id = request.values.get('category') if category_id: category = Category.get_by_id(int(category_id)) if is_available: products = category.get_available_products() else: products = category.get_not_available_products() else: if is_available: products = Product.query().filter(Product.available == True).filter(Product.category == None) else: products = Product.query().filter(Product.available != True).filter(Product.category == None) return render_to_response('shop/admin/product_filter.html', { 'products':products, 'available':is_available })
def category_json(request): key_id = request.values.get('id') if not key_id: raise ApiException('Invalid request: category "id" not found.') category = Category.get_by_id(int(key_id)) if not category: raise ApiException( 'Invalid request: Category with "id" == %s not found' % key_id ) category_j = category.to_json_api() id_1c = request.values.get('id_1c') if id_1c is not None: category_j['products_by_id_1c'] = [product.id_1c for product in category.get_products().fetch(projection=Product.id_1c)] return render_json_response( { 'success': True, 'result': category_j } )
def add_image(request, uid): category = Category.get_by_id(uid) if not category: return render_to_response('empty.html') if request.method == 'POST': try: upload_files = get_uploads(request, 'file') if len(upload_files): blob_info = upload_files[0] if blob_info.size: im = Image.create(blob_info.key(), title=category.title) if not len(category.images): im.is_master = True if im.get_cached_url(): category.images.append(im) category.put() return images(request, uid) else: blob_info.delete() except Exception, e: return render_to_response('empty.html', {'msg': msg})
def not_end(request): categories = Category.query(Category.available == True, Category.only_end_products == False).order(Category.title) products_obj = None if categories.count(): products_obj = Product.query(Product.category==categories.get().key).order(-Product.rating).fetch(21) return render_to_response('shop/not_end.html', {'products':products_obj, 'categories':categories})
def categories_json(request): categories = [key.id() for key in Category.query().fetch(keys_only=True)] return render_json_response({ 'success': True, 'result': categories })
def categories_count_json(request): return render_json_response({ 'success': True, 'result': Category.query().count() })
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)
def available_category(request, key): category = Category.get(key) if category: category.set_available(request.user) return redirect('/shop/admin/show_categories/')
def is_category_exist(title): category = Category.query().filter(Category.title == title) if category.count(): return True return False
def delete_category(request, key): category = Category.get(key) if category: category.delete() return redirect('/shop/admin/show_categories/')
def get_product(request, id): product = Product.get_by_id(id) if not product: return redirect(url_for('page/404')) categories = Category.query(Category.available == True, Category.only_end_products == True) return render_to_response('shop/product.html', {'product':product, 'categories':categories})
def categories_json(request): categories = [category.to_json() for category in Category.query().order(Category.title)] return render_json_response(categories)
if leftovers: if isinstance(leftovers, unicode): leftovers = leftovers.replace(",",'.') product_obj.leftovers = int(leftovers) else: product_obj.leftovers = 0 product_obj.available = model['available'] product_obj.in_main_page = model['in_main_page'] category = model.get('category', None) if category: category = category.get('id', None) if not product_obj.category or (product_obj.category and product_obj.category.id() != category): if category: category_obj = Category.get_by_id(category) if category_obj: 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):
def get_not_end(request, id): cat_obj = Category.get_by_id(id) categories = Category.query(Category.available == True, Category.only_end_products == False).order(Category.title).fetch() products_obj = Product.query().filter(Product.category == cat_obj.key).filter(Product.available == True).order(-Product.rating).fetch() return render_to_response('shop/not_end.html', {'category':cat_obj, 'products':products_obj, 'categories':categories})
def get_categories(request): categories = [category.to_json() for category in Category.query()] return render_json_response(categories)
def collection(request): categories = Category.query(Category.available == True, Category.only_end_products == True).order(Category.title) products = Product.query().order(-Product.rating).fetch(21) return render_to_response('page/collection.html', {'products':products, 'categories':categories})
def get_category(request, id): cat_obj = Category.get_by_id(id) categories = Category.query().filter(Category.available == True, Category.only_end_products == True).order(Category.title) products = Product.query().filter(Product.category == cat_obj.key).filter(Product.available == True) return render_to_response('page/collection.html', {'category':cat_obj, 'products':products, 'categories':categories})
try: category = Category.get_by_id(category_id) if not category: data = {'api_success': False, 'api_msg': 'Category %s not found' % category_id} return render_json_response(data) return render_json_response(category.to_json()) except Exception, e: data = {'api_success': False, 'api_msg': str(e)} return render_json_response(data) if request.method == 'PUT': try: model = simplejson.loads(request.data) category = Category.get_by_id(category_id) if not category: data = {'api_success': False, 'api_msg': 'Category %s not found' % category_id} return render_json_response(data) category.title = model['title'] category.available = model['available'] category.only_end_products = model['only_end_products'] parent_category = model.get('parent_category', None) if parent_category: parent_id = parent_category.get('id', None) if parent_id: parent_category = Category.get_by_id(int(parent_id)) if parent_category: category.parent_category = parent_category.key category.put()