def _display_list(self, list_id): """ Displays the list with the given id if it belongs to the current user """ try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) if current_list is None: raise ValueError self._api_display_list_(current_list) except (TypeError, ValueError, ProtocolBufferEncodeError) as exc: logging.error(str(exc)) error_message = self.gettext("There's not such list, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def put(self, api, list_id): """ PUT method is used to change item rankings """ try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) items_json = self.request.get('items') self.response.out.write('Items json: ' + str(items_json)+ '\n') items_check = json.loads(items_json) current_list.items = items_json current_list.put() self.response.out.write('Ran with success') except (BadValueError, AttributeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext('Error while storing new order.') self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def delete(self, api=None, list_id=None, item_id=None): """ DELETE request handler """ if api is not None: try: current_user = User.getUser(self.user_email) current_list = ShoppingList.get_by_id(int(list_id), current_user) current_list.delete_item(item_id) except (TypeError, ValueError, BadKeyError, BadValueError, ProtocolBufferEncodeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext("There's no such item, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")
def post(self, api=None, list_id=None): """ POST request handler """ current_user = User.getUser(self.user_email) if api is not None: try: if list_id is None: list_name = self.request.get('name', None) new_list = ShoppingList.create_list(current_user, list_name) self._api_display_list_(new_list) except (ValueError) as exc: self.set_error(constants.STATUS_BAD_REQUEST) if list_id is not None: # Add item to list try: current_list = ShoppingList.get_by_id(int(list_id), current_user) if current_list is None: raise ValueError item = current_list.add_item(self.request.get('description', None), self.request.get('key', None), int(self.request.get('quantity', 1))) self.response.out.write(json.dumps(item.to_dict())) self.response.headers['Content-Type'] = 'application/json' self.ok('/Lists/'+str(list_id)) except (TypeError, ValueError, BadKeyError, BadValueError, ProtocolBufferEncodeError) as exc: logging.error('Exception: ' + str(exc)) error_message = self.gettext("There's not such list, sorry.") self.set_error(constants.STATUS_BAD_REQUEST, message=error_message, url="/")