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)