def get(self, trip_key): errors = [] output = "" user = users.get_current_user() authz = Authz(user) self.response.headers["Content-type"] = "application/json" # verify access to the trip try: # get the trip trip = Trip.get(trip_key) # verify the user is authorized to read the trip authz.readTrip(trip) except PermissionError: errors.append( {"message": "You are not authorized to view that trip"}) except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error loading trip"}) # if errors encountered so far, bail if len(errors) > 0: self.response.set_status(400) output = json.dumps({"error": errors}) self.response.out.write(output) return # next, get the list of expenses try: expenses = Expense.all() expenses.ancestor(trip) output = GqlEncoder().encode(expenses.fetch(limit=200)) except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error listing expenses"}) if len(errors) > 0: self.response.set_status(400) output = json.dumps({"errors": errors}) self.response.headers["Content-type"] = "application/json" self.response.out.write(output)
def get(self, trip_key): errors = [] output = "" user = users.get_current_user() authz = Authz(user) self.response.headers["Content-type"] = "application/json" # verify access to the trip try: # get the trip trip = Trip.get(trip_key) # verify the user is authorized to read the trip authz.readTrip(trip) except PermissionError: errors.append({"message": "You are not authorized to view that trip"}) except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error loading trip"}) # if errors encountered so far, bail if len(errors) > 0: self.response.set_status(400) output = json.dumps({"error": errors}) self.response.out.write(output) return # next, get the list of expenses try: expenses = Expense.all() expenses.ancestor(trip) output = GqlEncoder().encode(expenses.fetch(limit=200)) except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error listing expenses"}) if len(errors) > 0: self.response.set_status(400) output = json.dumps({"errors": errors}) self.response.headers["Content-type"] = "application/json" self.response.out.write(output)
def delete(self, trip_key): errors = [] output = "" user = users.get_current_user() authz = Authz(user) self.response.headers["Content-type"] = "application/json" try: # get the trip trip = Trip.get(trip_key) # verify the user is authorized to delete the trip authz.deleteTrip(trip) # delete related expenses try: expenses = Expense.all() expenses.ancestor(trip) expenses.fetch(limit=200) db.delete(expenses) except Exception as e: logging.exception(e) errors.append({ "message": "Unexpected error deleting associated expenses" }) # delete the trip trip.delete() except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except PermissionError: errors.append( {"message": "You are not authorized to delete that trip"}) except db.NotSavedError: errors.append({"message": "Unable to delete trip"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error deleting trip"}) if len(errors) > 0: self.response.set_status(400) output = json.dumps({"error": errors}) self.response.out.write(output)
def delete(self, trip_key): errors = [] output = "" user = users.get_current_user() authz = Authz(user) self.response.headers["Content-type"] = "application/json" try: # get the trip trip = Trip.get(trip_key) # verify the user is authorized to delete the trip authz.deleteTrip(trip) # delete related expenses try: expenses = Expense.all() expenses.ancestor(trip) expenses.fetch(limit=200) db.delete(expenses) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error deleting associated expenses"}) # delete the trip trip.delete() except db.BadKeyError: errors.append({"message": "Invalid trip key"}) except PermissionError: errors.append({"message": "You are not authorized to delete that trip"}) except db.NotSavedError: errors.append({"message": "Unable to delete trip"}) except Exception as e: logging.exception(e) errors.append({"message": "Unexpected error deleting trip"}) if len(errors) > 0: self.response.set_status(400) output = json.dumps({"error": errors}) self.response.out.write(output)