def post(self): if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "API only supports json type" return item = db_defs.Item() item_id = self.request.get('id', default_value = None) if item_id is None: loaded_data = json.loads(self.request.body) item_id = loaded_data['id'] if item_id is not None: item_obj = ndb.Key(db_defs.Item, int(item_id)) q = db_defs.Business.query() q = q.filter(db_defs.Business.items == item_obj) business_results = q.fetch() rlist = [] for r in business_results: results = {'id' : r.key.id(), 'name' : r.name, 'phone' : r.phone, 'website' : r.website, 'address' : r.address} rlist.append(results) self.response.write(json.dumps(rlist)) return
def post(self): #creates an Item entity #this never fires... what the heck. if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "API only supports json type" return #create a item item using the database definition new_item = db_defs.Item() #get name from the http data name = self.request.get('name', default_value = None) #get all items from list of item keys busses = self.request.get_all('businesses[]', default_value = None) cats = self.request.get_all('category[]', default_value = None) #if JSON data, not http data... if name is None: #fetch the JSON data loaded_data = json.loads(self.request.body) #and extract name from that data name = loaded_data['name'] busses = loaded_data['businesses'] cat = loaded_data['category'] if name: new_item.name = name else: self.response.status = 400 self.response.status_message = "Invalid Request, Name is Required" if cats: for cat in cats: new_item.category.append(ndb.Key(db_defs.Category, int(cat))) if busses: #for each item being added, append its Item object to the list of items for bus in busses: new_item.businesses.append(ndb.Key(db_defs.Business, int(bus))) #write the new item to the DB key = new_item.put() #create a dictionary of the new entry out = new_item.to_dict() #write the dictionary as formatted JSON self.response.write(json.dumps(out)) return
def add_item(): if not request.json: return "Error: API only supports application/json MIME type\n\n", 406 new_item = db_defs.Item() name = request.json.get('name') price = request.json.get('price') description = request.json.get('description') location = request.json.get('location') if 'username' in session: seller = session['username'] else: return "Error: User not logged in\n\n", 406 if 'name' in request.json: new_item.name = name else: return "Error: Each item must have a name\n\n", 400 if description: new_item.description = description if 'price' in request.json: new_item.price = int(price) else: return "Error: Each item must have a price\n\n", 400 if 'location' in request.json: new_item.location = location else: return "Error: Each item must have a location\n\n", 400 if seller: new_item.seller = seller else: return "Error: Each item must have a seller\n\n", 400 key = new_item.put() out = new_item.to_dict() return jsonify(out)
def post(self): if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "API only supports json type" return #creates a blank item object item = db_defs.Item() #if getting data from a curl query... item_id = self.request.get('id', default_value=None) #if getting data from JSON, get the item id if item_id is None: loaded_data = json.loads(self.request.body) item_id = loaded_data['id'] #once we have the item id, figure out which item it represents in the DB if item_id is not None: item_obj = ndb.Key(db_defs.Item, int(item_id)) #create a query for businesses that contain that item q = db_defs.Business.query() q = q.filter(db_defs.Business.items == item_obj) business_results = q.fetch() rlist = [] for r in business_results: #returns a list of business objects associated with the item results = { 'id': r.key.id(), 'name': r.name, 'phone': r.phone, 'website': r.website, 'address': r.address } rlist.append(results) self.response.write(json.dumps(rlist)) return
def post(self): form_id = self.request.get('form_id') if form_id == 'add_category': new_cat = db_defs.Category() new_cat.name = self.request.get('name') items = self.request.get_all('add_items[]') if items: #for each item being added, append its Item object to the list of items for it in items: item_key = ndb.Key(db_defs.Item, int(it)) new_cat.items.append(item_key) cat_key = new_cat.put() if items: #for each item being added, append its Item object to the list of items for i in items: item_key = ndb.Key(db_defs.Item, int(i)) this_item = item_key.get() this_item.category.append(cat_key) this_item.put() render( self, 'success.html', { 'message': 'Success: Saved results for ' + new_cat.name + ' to the database' }) if form_id == 'add_item': new_item = db_defs.Item() new_item.name = self.request.get('name') category = self.request.get_all('add_category[]') businesses = self.request.get_all('add_businesses[]') if category: #for each item being added, append its Item object to the list of items for cat in category: new_item.category.append( ndb.Key(db_defs.Category, int(cat))) if businesses: #for each item being added, append its Item object to the list of items for bus in businesses: new_item.businesses.append( ndb.Key(db_defs.Business, int(bus))) item_key = new_item.put() if category: #for each item being added, append its Item object to the list of items for c in category: cat_key = ndb.Key(db_defs.Category, int(c)) this_cat = cat_key.get() this_cat.items.append(item_key) this_cat.put() if businesses: #for each item being added, append its Item object to the list of items for b in businesses: bus_key = ndb.Key(db_defs.Business, int(b)) this_bus = bus_key.get() this_bus.items.append(item_key) this_bus.put() render( self, 'success.html', { 'message': 'Success: Saved results for ' + str(this_bus) + ' to the database', 'return': '/add_new' })