def update_item(self, item_id, item_json): old_item = None try: old_item = self.read_item(item_id) except Category.DoesNotExist: raise components.ResourceNotFoundError() old_parent_id = old_item.parent.id if old_item.parent else None old_order = old_item.order parent = None if "parent" in item_json and item_json["parent"]: if item_json["parent"] == item_id: raise components.BadRequestError( payload={ "reason": "You can't be your own parent, you moron." }) try: parent = self.read_item(item_json["parent"]) except Category.DoesNotExist: raise components.BadRequestError() del item_json["parent"] parent_id = parent.id if parent else None item_json = self.sanitize_fields(item_json) item = dict_to_model(Category, item_json) path = self._fetch_path(parent.id if parent else None) path = "%s.%d" % (parent, item.order) if path else str(item.order) item.id = item_id item.parent = parent item.path = path item.save(only=item.dirty_fields) if (item.order != old_order) or (item.parent and item.parent.id != old_parent_id): user_id = components.current_user_id() self._reorder_branch(item, user_id=user_id, parent_id=parent_id) self._reorder_path(user_id=user_id, parent_id=parent_id) if item.parent and item.parent.id != old_parent_id: self._reorder_branch(None, user_id=user_id, parent_id=old_parent_id) self._reorder_path(user_id=user_id, parent_id=old_parent_id) # self._flatten_tree_order(user_id) return item
def create_item(self, item_json): parent = None if "parent" in item_json and item_json["parent"]: try: parent = self.read_item(item_json["parent"]) except Category.DoesNotExist: raise components.BadRequestError() del item_json["parent"] user = components.current_user() count = 0 if parent: count = len(parent.children) else: count = Category.select().join( components.BaseUser, on=(Category.owner == components.BaseUser.id)).where( Category.parent.is_null(), Category.is_deleted == False, components.BaseUser.id == user.id).count() path = self._fetch_path(parent.id if parent else None) path = "%s.%d" % (parent, count) if path else str(count) item = dict_to_model(Category, item_json) item.parent = parent item.order = count item.owner = user item.path = path item.save() return item
def create_item(self, user_json): user_json = self.sanitize_fields(user_json) if "name" not in user_json or "password" not in user_json: raise components.BadRequestError("Username or password missing") user_json["password"] = auth.hash_password(user_json["password"]) item = dict_to_model(User, user_json) item.save(force_insert=True) return item
def category_filter_helper(self, clazz, user_id, category_filter): if (str.isdigit(category_filter)): category_tree = self.fetch_subtree(user_id, int(category_filter)) if not category_tree: raise components.ResourceNotFoundError() return [clazz.category_id << category_tree] elif (category_filter == "unassigned"): return [clazz.category_id.is_null()] elif (category_filter != "all"): raise components.BadRequestError() return []
def create_item(self, item_json): (item_json, tags) = self._select_and_sanitize_tags(item_json) # Check if user has ownership over the category given if ("category" in item_json and item_json["category"] and not categoryService.read_item(item_json["category"])): raise components.BadRequestError() item = dict_to_model(Note, item_json) item.content = markupsafe.escape(markupsafe.Markup(item.content)) item.owner = components.current_user() item.save(force_insert=True) item.tags.add(tags) return item
def _dispatch(self, service): if service not in self.services: raise components.BadRequestError("No such service %s" % service) return self.services[service]
def get_profile(self): if not hasattr(g, "current_user") or not g.current_user: raise components.BadRequestError() user = g.current_user return (self.serialize_item(user), 200)