def get(self, collection_uid=None): """Get a list of a user's collections, or """ request_args = get_current_request_args() user_uid = request_args.get('user_uid') if user_uid is not None: user = User.get_not_deleted(uid=user_uid) if user is None: raise ResourceNotFound('User not found') else: user = get_current_user() if collection_uid is not None: collection = Collection.get_not_deleted(uid=collection_uid, user_id=user.id) if collection is None: raise ResourceNotFound('Collection not found') return api_success_response(collection.as_json()) pagination = user.collections.paginate() return api_success_response( data=[collection.as_json() for collection in pagination.items()], meta=pagination.meta)
def put(self, collection_uid, post_uid): """Add a post to a collection""" collection = Collection.get_not_deleted(uid=collection_uid) if collection is None: raise ResourceNotFound('Collection not found') post = Post.get_not_deleted(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') post.update(collection_id=collection.id) return api_success_response()
def delete(self, collection_uid, post_uid): """Remove a post from a collection""" collection = Collection.get_not_deleted(uid=collection_uid) if collection is None: raise ResourceNotFound('Collection not found') post = Post.get_not_deleted(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') if post.collection_id != collection.id: raise ResourceNotFound('Post not found') post.update(collection_id=None) return api_deleted_response()