def create_value(request, property_key): if request.method == 'POST': model = simplejson.loads(request.data) property_obj = ProductProperty.get_by_id(property_key) value = ProductPropertyValue(title=model['title'], property=property_obj.key) value.put() return render_json_response(value.to_json())
def is_value_exist(title, property_key): value = ProductPropertyValue.query()\ .filter(ProductPropertyValue.property == property_key)\ .filter(ProductPropertyValue.title == title) if value.count(): return True return False
def filter_value(request): is_available = bool(request.values.get('available')) value_key = request.values.get('value') products = [] if value_key: value_ = ProductPropertyValue.get_by_id(int(value_key)) if is_available: products = value_.get_available_products() else: products = value_.get_not_available_products() else: if is_available: products_tmp = Product.available_list() products = [] for prop in products_tmp: if not prop.properties_: products.append(prop) else: products_tmp = Product.not_available_list() products = [] for prop in products_tmp: if not prop.properties_: products.append(prop) return render_to_response('shop/admin/product_filter.html', { 'products':products, 'available':is_available })
def create(data, is_unique_title=False): title = data.get('title', '') if not title: res = {'api_success':False, 'api_msg':'"Field title" is required', 'api_function':create.__name__} logging.warning(res) return render_json_response(res) property = ProductProperty.get_by_id(data.get('property', '')) if is_unique_title and is_value_exist(title, property.key): res = {'api_success':False, 'api_msg':'Field "title" must be unique', 'api_function':create.__name__} logging.warning(res) return render_json_response(res) value = ProductPropertyValue(title=title, property=property.key) value.put() res = {'api_success': True, 'api_msg': 'Value "%s" created' % title, 'property': value.to_json()} logging.info(res) return render_json_response(res)
def set_properties(request, key): product_ = Product.get_by_id(key) if not product_: return redirect(url_for('shop/admin/product')) if request.method == 'POST': selected_values = request.form.getlist('values') product_.properties_ = [] for value in selected_values: val_obj = ProductPropertyValue.get_by_id(value) if val_obj: product_.properties_.append(val_obj.key()) product_.put() return redirect(url_for('shop/admin/product/edit', key=key))
def get_value(request, property_key, value_key): if request.method == 'GET': try: value = ProductPropertyValue.get_by_id(value_key) if not value: data = {'success': False, 'msg': 'Value %s not found' % value_key} return render_json_response(data) except Exception, e: data = {'api_success': False, 'api_msg': str(e)} return render_json_response(data) data = {'api_success': True, 'api_msg': 'Value created'} data.update(value.to_json) return render_json_response(data)
def put_product(request, product, 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: product = Product(title='') title = model.get('title') change_string(product, 'title', title) id_1c = model.get('id_1c') change_string(product, 'id_1c', id_1c) original_title = model.get('original_title') change_string(product, 'original_title', original_title) short_description = model.get('short_description') change_string(product, 'short_description', short_description) rating = model.get('rating') if rating: rating = int(rating) change_string(product, 'rating', rating) price = model.get('price') if price: if isinstance(price, unicode): price = model['price'].replace(",",'.') price = float(price) change_string(product, 'price', price) real_price = model.get('real_price') if real_price: if isinstance(real_price, unicode): real_price = model['real_price'].replace(',', '.') real_price = float(real_price) change_string(product, 'real_price', real_price) leftovers_on_way = model.get('leftovers_on_way') if leftovers_on_way: if isinstance(leftovers_on_way, unicode): leftovers_on_way = leftovers_on_way.replace(",",'.') leftovers_on_way = int(leftovers_on_way) change_string(product, 'leftovers_on_way', leftovers_on_way) leftovers = model.get('leftovers') if leftovers: if isinstance(leftovers, unicode): leftovers = leftovers.replace(",",'.') leftovers = int(leftovers) change_string(product, 'leftovers', leftovers) available = model.get('available') if available: available = bool(available) change_string(product, 'available', available) in_main_page = model.get('in_main_page', False) change_string(product, 'in_main_page', in_main_page) wrn = [] def set_key_property(obj, pname, value, cls): if type(value) == dict: value_id = value.get('id') if value_id: pval = getattr(obj, pname) if not pval or (pval and pval.id() != value_id): cls_obj = cls.get_by_id(value_id) if cls_obj: setattr(obj, pname, cls_obj.key) else: wrn.append('Invalid %s ID: %s' % (pname, value_id)) else: if value == {}: setattr(obj, pname, None) else: wrn.append('Invalid %s object: %s' % (pname, value)) if value is not None and type(value) != dict: wrn.append('Invalid %s object: %s' % (pname, value)) # category category = model.get('category') set_key_property(product, 'category', category, Category) # brand brand = model.get('brand') set_key_property(product, 'brand', brand, Brand) # properties properties_ = model.get('properties') if type(properties_) == list and len(properties_): prop_ids = [] for prop in properties_: if type(prop) != dict: wrn.append("Invalid property - must be a dict: %s" % prop) continue prop_id = prop.get("id") if not prop_id: wrn.append("Invalid property data - ID not found: %s" % prop) continue prop_ids.append(prop_id) # remove properties for prop in product.properties_: if prop.id not in prop_ids: product.properties_.remove(prop) for prop_id in prop_ids: prop = ProductPropertyValue.get_by_id(prop_id) if prop and prop.key not in product.properties_: product.properties_.append(prop.key) if not prop: wrn.append("Property with ID: %s - not found" % prop_id) if type(properties_) == list and not len(properties_): if product.properties_: del product.properties_[:] if properties_ is not None and type(properties_) != list: wrn.append("Invalid properties - must be a list: %s" % properties_) product.put() if is_new: msg = "Product %s created" else: msg = "Product %s updated" data = { 'api': { 'success': True, 'msg': msg % product.id, 'wrn':wrn } } data.update(product.to_json()) return render_json_response(data)
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 get_values(request, property_key): property_obj = ProductProperty.get_by_id(property_key) values = ProductPropertyValue.query( ProductPropertyValue.property == property_obj.key ) values = [value.to_json() for value in values] return render_json_response(values)
def get_value(request, property_key, value_key): if request.method == 'GET': try: value = ProductPropertyValue.get_by_id(value_key) if not value: data = {'success': False, 'msg': 'Value %s not found' % value_key} return render_json_response(data) except Exception, e: data = {'api_success': False, 'api_msg': str(e)} return render_json_response(data) data = {'api_success': True, 'api_msg': 'Value created'} data.update(value.to_json) return render_json_response(data) if request.method == 'PUT': model = simplejson.loads(request.data) value = ProductPropertyValue.get_by_id(value_key) value.title = model['title'] value.put() data = {'api_success': True, 'api_msg': 'Value %s updated' % value_key} data.update(value.to_json()) return render_json_response(data) if request.method == 'DELETE': value = ProductPropertyValue.get_by_id(value_key) value.key.delete() data = {'api_success': True, 'api_msg': 'Value %s deleted' % value_key} return render_json_response(data)
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_: if prop.id() not in properties_: product_obj.properties_.remove(prop) for prop in properties_: if prop: prop = ProductPropertyValue.get_by_id(prop) if prop and prop.key not in product_obj.properties_: product_obj.properties_.append(prop.key) else: if product_obj.properties_: del product_obj.properties_[:] product_obj.put() data = {'api_success':True, 'api_msg': 'Product %s updated' % name} data.update(product_obj.to_json()) return render_json_response(data) except Exception, e: data = {'api_success':False, 'api_msg':str(e)} return render_json_response(data, status=400) @admin_required