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 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, 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, winery_id): """ >>> v = Winery() >>> key = v.create({'name':'winery'}) >>> ndb.Key.load_get(v ) >>> h = WineryHandler() >>> h.request.POST = {'location':'Canada'} >>> h.post('12345') >>> h.response.content_type 'application/json' >>> h.response.last_write '{..."country": "Canada"...}' >>> h.response.last_write '{..."name": "winery"...}' >>> h.response.last_write '{..."key":...}' """ post = self.request.POST winery_key = ndb.Key(Winery, int(winery_id)) winery = winery_key.get() try: winery.modify(post) wines = Wine.winery_query(winery) winery.update(wines) Event.update(self.request.remote_addr, "Winery", winery_key) except YouNeedATokenForThat as e: self.response.write(str(e)) self.response.status = "401 Unauthorized" return json_response(self, winery)
def delete(self, winery_id, wine_id, userwine_id, tasting_id): tasting_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id), UserWine, int(userwine_id), WineTasting, int(tasting_id)) tasting = tasting_key.get() tasting.delete() Event.delete(self.request.remote_addr, "WineTasting", tasting_key) json_response(self, {"success": True})
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, tasting_id): post = self.request.POST tasting_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id), UserWine, int(userwine_id), WineTasting, int(tasting_id)) tasting = tasting_key.get() tasting.modify(post) Event.update(self.request.remote_addr, "WineTasting", tasting_key) json_response(self, tasting)
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, 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 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.wine = wine.key key = bottle.modify(post) Event.update(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 })
def post(self, cellar_id): post = self.request.POST 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 cellar.modify(post) Event.update(self.request.remote_addr, "WineCellar", cellar_key) json_response(self, cellar)
def post(self, winery_id, wine_id, userwine_id): post = self.request.POST userwine_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id), UserWine, int(userwine_id)) userwine = userwine_key.get() if not userwine: self.response.write("404 Not Found") self.response.status = "404 Not Found" return if userwine.user != User.get_current_user().key: self.response.write("403 Forbidden") self.response.status = "403 Forbidden" return userwine.modify(post) Event.update(self.request.remote_addr, "UserWine", userwine_key) json_response(self, userwine)
def delete(self, 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 qry = WineBottle.query(ancestor=cellar.key) results = qry.fetch(MAX_RESULTS) for bottle in results: bottle.delete() cellar.delete() user = User.get_current_user() user.cellar = None user.put() Event.delete(self.request.remote_addr, "WineCellar", cellar_key) json_response(self, {"success": True})
def post(self, winery_id, wine_id): winery_key = ndb.Key(Winery, int(winery_id)) winery = winery_key.get() wine_key = ndb.Key(Winery, int(winery_id), Wine, int(wine_id)) wine = wine_key.get() if not winery or not wine: self.response.status = "404 Not Found" self.response.write("404 Not found.") return post = self.request.POST try: wine.modify(post, winery) wine.update(winery) Event.update(self.request.remote_addr, "Wine", wine_key) except YouNeedATokenForThat as e: self.response.write(str(e)) self.response.status = "401 Unauthorized" return json_response(self, wine)