def post(self): """register a user""" try: username = str(request.json.get("username")) email = request.json.get("email") password = request.json.get("password") if not (str.isalpha(username) or str.isalnum(username)): return "username should be a string!", 200 except AttributeError: return "Attributes not found!" is_valid = validate_email(email) and self.validate_password(password) if is_valid: user = User.create_user(username, email, password) if isinstance(user, User): result = { 'message': "user registered successfully", 'user': { 'user_id': user.id, 'username': user.username, 'email': user.email } } return make_response(jsonify(result), 201) else: return user else: return "use strong passwords and correct email format", 200
def delete(self, id, item_id): """" deletes a bucket list item given its id """ access_token = request.headers.get('token') if not access_token: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401) if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) buckets = Bucketlist.query.filter_by(id=id).first() if isinstance(user_id, int): if buckets: output = Bucketlistitem.delete_bucketlistitem(item_id, id) return output else: result = { "message": "bucketlist not available" } return make_response(jsonify(result), 404) else: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401)
def put(self, id, item_id): """ updates a bucket list given id and the data """ access_token = request.headers.get('Token') try: name = request.json.get('name') done = request.json.get('done') except AttributeError: return "attributes not found!" if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) buckets = Bucketlist.query.filter_by(id=id).first() if isinstance(user_id, int): if buckets: output = Bucketlistitem.update_bucketlistitem( item_id, id, name, done) if isinstance(output, Bucketlistitem): result = { "id": output.id, "name": output.name, "done": output.done, "date_created": output.date_created } return make_response(jsonify(result), 200) else: return output else: result = {"message": "bucketlist not available"} return make_response(jsonify(result), 404) else: result = {"message": "unauthorized action"} return make_response(jsonify(result), 401)
def put(self, id): """ updates a bucket list given id and the data """ try: name = request.json.get('name') except AttributeError: return "attribute name not found", 400 access_token = request.headers.get('Token') if not access_token: result = {"message": "unauthorized action"} return make_response(jsonify(result), 401) if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): output = Bucketlist.update_bucketlist(id, user_id, name) if isinstance(output, Bucketlist): result = { "id": output.id, "name": output.name, "created_by": output.created_by, "date_created": output.date_created } return make_response(jsonify(result), 200) else: return output else: result = {"message": "unauthorized action"} return make_response(jsonify(result), 401)
def get(self, id): """ List all tasks' """ access_token = request.headers.get('token') if not access_token: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401) if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): bucket = Bucketlist.query.filter_by( id=id, created_by=user_id).first() if bucket: bucket_list = [] items = Bucketlistitem.query.filter_by( bucketlist_id=id).all() for item in items: an_item = { "id": item.id, "name": item.name, "date_created": item.date_created, "done": item.done, "date_modified": item.date_modified } bucket_list.append(an_item) result = { "id": bucket.id, "name": bucket.name, "items": bucket_list, "date_created": bucket.date_created, "date_modified": bucket.date_modified, "created_by": bucket.created_by } return make_response(jsonify(result), 200) else: result = { "message": "Bucketlist not found" } return make_response(jsonify(result), 404) else: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401)
def post(self, id): """ creates a bucketlist item """ access_token = request.headers.get('token') bucket = Bucketlist.query.filter_by(id=id).first() if not bucket: return "Bucketlist not found!", 404 if not access_token: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401) try: name = request.json.get('name') if not name: return "name attribute not found!", 400 done = request.json.get('done') if done is None: return "done attribute not found!", 400 except AttributeError: return "attributes not found!", 400 if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): output = Bucketlistitem.create_bucketlistitem(id, name, done) if isinstance(output, Bucketlistitem): result = { "id": output.id, "name": output.name, "done": output.done, "date_created": output.date_created } return make_response(jsonify(result), 200) else: return output else: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401)
def get(self, id): """ List all items of a given bucketlist """ bucket = Bucketlist.query.filter_by(id=id).first() if not bucket: return "Bucketlist not found!", 404 access_token = request.headers.get('token') if not access_token: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401) if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): items = Bucketlistitem.query.filter_by(bucketlist_id=id).all() if items: bucket_item_list = [] for item in items: result = { "id": item.id, "name": item.name, "done": item.done, "date_created": item.date_created, "date_modified": item.date_modified } bucket_item_list.append(result) return make_response(jsonify(bucket_item_list), 200) else: result = { "message": "No items found" } return make_response(jsonify(result), 200) else: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401)
def delete(self, id): """" deletes a bucket list given its id """ access_token = request.headers.get('Token') if not access_token: result = {"message": "unauthorized action"} return make_response(jsonify(result), 401) if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): output = Bucketlist.delete_bucketlist(id) return output else: result = {"message": "unauthorized action"} return make_response(jsonify(result), 401)
def get(self): """ List all bucket list """ args = pagination_arguments.parse_args() page = args['page'] limit = args['limit'] search_words = args['q'] if limit > 100: limit = 100 access_token = request.headers.get('token') if not access_token: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401) else: # Attempt to decode the token and get the User ID user_id = User.decode_auth_token(access_token) if isinstance(user_id, int): if search_words: bucketlists_page = Bucketlist.query.filter( Bucketlist.created_by == user_id, Bucketlist.name.contains( search_words + "%")).paginate( page, limit, False) if bucketlists_page: total = bucketlists_page.pages has_next = bucketlists_page.has_next has_previous = bucketlists_page.has_prev if has_next: next_page = str(request.url_root) + 'bucketlists?' + \ 'q=' + str(search_words) + '&page=' + str(page + 1) else: next_page = 'None' if has_previous: previous_page = request.url_root + 'bucketlists?' + \ 'q=' + str(search_words) + '&page=' + str(page - 1) else: previous_page = 'None' bucketlists = bucketlists_page.items if bucketlists: items = [] for item in bucketlists: buc_items = [] bucketlist_items = Bucketlistitem.query.filter_by( bucketlist_id=item.id).all() for any_item in bucketlist_items: an_item = { 'id': any_item.id, 'name': any_item.name, 'done': any_item.done, 'date_created': any_item.date_created, 'date_modified': any_item.date_modified } buc_items.append(an_item) a_bucket = { 'id': item.id, 'name': item.name, 'items': buc_items, 'created_by': item.created_by, 'date_created': item.date_created, 'date_modified': item.date_modified } items.append(a_bucket) result = {'bucketlists': items, 'has_next': has_next, 'pages': total, 'previous_page': previous_page, 'next_page': next_page } return make_response(jsonify(result), 200) else: result = { "message": "No bucketlist item found" } return make_response(jsonify(result), 200) bucketlists_page = Bucketlist.query.filter_by( created_by=user_id).paginate( page=page, per_page=limit, error_out=False) if not bucketlists_page: result = { "message": "No bucketlist item found" } return make_response(jsonify(result), 204) total = bucketlists_page.pages has_next = bucketlists_page.has_next has_previous = bucketlists_page.has_prev if has_next: next_page = str(request.url_root) + 'bucketlists?' + \ 'limit=' + str(limit) + '&page=' + str(page + 1) else: next_page = 'None' if has_previous: previous_page = request.url_root + 'bucketlists?' + \ 'limit=' + str(limit) + '&page=' + str(page - 1) else: previous_page = 'None' bucketlists = bucketlists_page.items items = [] if bucketlists: for item in bucketlists: buc_items = [] bucketlist_items = Bucketlistitem.query.filter_by( bucketlist_id=item.id).all() for any_item in bucketlist_items: an_item = { 'id': any_item.id, 'name': any_item.name, 'done': any_item.done, 'date_created': any_item.date_created, 'date_modified': any_item.date_modified } buc_items.append(an_item) a_bucket = { 'id': item.id, 'name': item.name, 'items': buc_items, 'created_by': item.created_by, 'date_created': item.date_created, 'date_modified': item.date_modified } items.append(a_bucket) result = {'bucketlists': items, 'has_next': has_next, 'pages': total, 'previous_page': previous_page, 'next_page': next_page } return make_response(jsonify(result), 200) else: result = { "message": "No bucketlist item found" } return make_response(jsonify(result), 200) else: result = { "message": "unauthorized action" } return make_response(jsonify(result), 401)