def post(self, winery_id): """ POST /winery/12345/wine => create a wine for winery """ winery_key = ndb.Key(Winery, int(winery_id)) winery = winery_key.get() if not winery: self.response.status = "404 Not Found" self.response.write("404 Not found.") return post = self.request.POST wine = Wine(parent=winery_key) try: key = wine.create(post, winery) wine.update(winery) Event.create(self.request.remote_addr, "Wine", key) except ValueError as e: self.response.status = "400 Bad Request" self.response.write(str(e)) return json_response(self, wine)
def post(self): post = self.request.POST cellar = WineCellar() try: user = User.get_current_user() if user.cellar is not None: self.response.write("403 Forbidden - User already has cellar") self.response.status = "403 Forbidden" return key = cellar.create(post) user.cellar = key user.put() # refresh the user cache user = User.get_current_user() if not user.has_access(key): print "see? do you SEE what I put up with?" Event.create(self.request.remote_addr, "WineCellar", key) except ValueError as e: self.response.status = "400 Bad Request" self.response.write(str(e)) return json_response(self, cellar)
def post(self): """ >>> v = WineryBaseHandler() >>> v.request.POST = {'name':'Winery'} >>> v.post() >>> v.response.content_type 'application/json' >>> v.response.last_write '{..."key": "stub-key"...}' """ post = self.request.POST winery = Winery() try: key = winery.create(post) winery.update() Event.create(self.request.remote_addr, "Winery", key) except ValueError as e: self.response.status = "400 Bad Request" self.response.write(str(e)) return json_response(self, winery)
def delete(self, cellar_id, winebottle_id=None): if winebottle_id is None: winebottle_id = cellar_id cellar_id = User.get_current_user().cellar.id() bottle_key = ndb.Key(WineCellar, int(cellar_id), WineBottle, int(winebottle_id)) bottle = bottle_key.get() if not bottle: self.response.write("404 Not Found") self.response.status = "404 Not Found" return if not User.has_access(ndb.Key(WineCellar, int(cellar_id))): self.response.write("403 Forbidden") self.response.status = "403 Forbidden" return bottle.delete() Event.create(self.request.remote_addr, "WineBottle", bottle_key) json_response(self, {"success": True})
def post(self, winery_id, wine_id, userwine_id): userwine_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id), UserWine, int(userwine_id)) post = self.request.POST tasting = WineTasting(parent=userwine_key) try: key = tasting.create(post) Event.create(self.request.remote_addr, "WineTasting", key) except ValueError as e: self.response.status = "400 Bad Request" self.response.write(str(e)) return json_response(self, tasting)
def post(self, winery_id, wine_id): wine_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id)) post = self.request.POST userwine = UserWine(parent=wine_key) try: post['user'] = User.get_current_user() key = userwine.create(post) Event.create(self.request.remote_addr, "UserWine", key) except ValueError as e: self.response.status = "400 Bad Request" self.response.write(str(e)) return json_response(self, userwine)
def post(self, cellar_id=None): if cellar_id is None: cellar_id = User.get_current_user().cellar.id() cellar_key = ndb.Key(WineCellar, int(cellar_id)) cellar = cellar_key.get() if not cellar: self.response.write("404 Not Found") self.response.status = "404 Not Found" return if not User.has_access(cellar_key): self.response.write("403 Forbidden") self.response.status = "403 Forbidden" return post = self.request.POST if 'wine_id' in post and 'winery_id' in post: wine_key = ndb.Key(Winery, int(post['winery_id']), Wine, int(post['wine_id'])) wine = wine_key.get() del post['wine_id'] del post['winery_id'] bottle = WineBottle(parent=cellar_key) bottle.wine = wine.key key = bottle.create(post) Event.create(self.request.remote_addr, "WineBottle", key) self.response.content_type = "application/json" json_response(self, bottle) else: json_response(self, { "error": "there was no wine_id", "post": self.request.body })