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 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)