Ejemplo n.º 1
0
 def post(self, list_id):
     """POST request handling for /bucketlists/<int:list_id>/items
     Create a new bucketlist item
     """
     name = str(request.data.get('name', ''))
     description = str(request.data.get('description', ''))
     if name:
         bucketlist_item = BucketlistItem(list_id, name, description)
         bucketlist_item.save()
         response = jsonify({
             'id':
             bucketlist_item.id,
             'bucketlist_id':
             bucketlist_item.bucketlist_id,
             'name':
             bucketlist_item.bucketlist_item_name,
             'description':
             bucketlist_item.bucketlist_item_description
         })
         response.status_code = 201
         response.headers['Access-Control-Allow-Origin'] = "*"
         response.headers['Access-Control-Allow-Credentials'] = True
         response.headers[
             'Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
         response.headers['Access-Control-Allow-Methods'] = 'POST'
         return response
Ejemplo n.º 2
0
 def bucketlistitem(id):
     """Method for creating and retrieving bucket list items."""
     access_token = request.headers.get('Authorization')
     if access_token:
         user_id = User.decode_token(access_token)
         if not isinstance(user_id, str):
             bucketlist = Bucketlist.query.filter_by(id=id).first()
             if not bucketlist:
                 return {
                     "message": "No bucket lists exists please create some."
                 }
             if request.method == 'POST':
                 name = str(request.data.get('name', ''))
                 if name:
                     bucketlistitem = BucketlistItem(item_name=name,
                                                     bucketlist_id=id)
                     bucketlistitem.save()
                     response = jsonify({
                         'id':
                         bucketlistitem.id,
                         'name':
                         bucketlistitem.item_name,
                         'bucketlist_id':
                         bucketlistitem.bucketlist_id
                     })
                     response.status_code = 201
                     return response
                 if request.method == 'GET':
                     bucketlistitems = BucketlistItem.query.filter_by(
                         bucketlist_id=id).first()
                     results = []
                     for listitem in bucketlistitems:
                         obj = {
                             'id': listitem.id,
                             'name': listitem.item_name,
                             'status': listitem.completed
                         }
                         results.append(obj)
                     response = jsonify(results)
                     response.status_code = 200
                     return response
     return {"message": "Please login first."}
Ejemplo n.º 3
0
 def setUp(self):
     """sets up moc data for tests"""
     self.app = app.test_client()
     app.config.from_object(config.TestingConfig)
     db.create_all()
     db.session.add(User(name='testuser', email='*****@*****.**', password=generate_password_hash(
         'testpass', method='sha256')))
     db.session.add(Bucketlist(name='testbucket', owner_id=1))
     db.session.add(Bucketlist(name='testbucket2', owner_id=1))
     db.session.add(BucketlistItem(description='itemname', bucketlist_id=2))
     db.session.commit()
Ejemplo n.º 4
0
def add_item(current_user, bucketlistID):
    description = request.json.get('description')
    if not description:
        res = {"msg": "Please provide the itemname"}
        return jsonify(res)
    else:
        description = BucketlistItem(description=description,
                                     bucketlist_id=bucketlistID)
        db.session.add(description)
        db.session.commit()
        res = {"msg": "bucketlistitem added successfully"}
        return jsonify(res)
Ejemplo n.º 5
0
    def bucketlistsitem_get(id):
        '''
        Function for retireving a users bucketlist item
        '''

        auth_header = request.headers.get('Authorization')
        access_token = auth_header

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                try:
                    user_bucketlist = Bucketlist.query.filter_by(
                        bucketlist_id=id, user_id=user_id).one()
                    if not user_bucketlist:
                        response = jsonify(
                            {'message': 'Bucketlist does not exist'})
                        response.status_code = 404
                        return response
                    else:
                        bucketlistsitems = BucketlistItem.get_items(
                            user_bucketlist.id)
                        results = []

                        for bucketlistitem in bucketlistsitems:
                            obj = {
                                'id': bucketlistitem.id,
                                'name': bucketlistitem.name,
                                'date_created': bucketlistitem.date_created,
                                'date_modified': bucketlistitem.date_modified,
                                'bucketlist': bucketlistitem.bucketlist_id
                            }
                            results.append(obj)
                        response = jsonify(results)
                        response.status_code = 200
                        return response
                except:
                    response = jsonify(
                        {'message': 'Bucketlist does not exist'})
                    response.status_code = 404
                    return response
            else:
                response = jsonify({'message': 'Invalid token'})
                response.status_code = 401
                return response
Ejemplo n.º 6
0
    def bucketlist_items(bucketlist_id, **kwargs):
        # Get the access token from the passed header
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split("Bearer ")[1]
        if access_token:
            # Decode the token to get the user id
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist = Bucketlist.query.filter_by(id=bucketlist_id, created_by=user_id).first()
                if not bucketlist:
                        return jsonify({
                        "message": "Sorry, you don't have a bucketlist with that id"
                    }), 404

                if request.method == 'POST':
                    title = request.json.get('title')
                    if title:
                        if BucketlistItem.title_exists(title):
                            return jsonify({
                                "message": "The title should be unique, use a different name"
                            }), 409

                        bucketlist_item = BucketlistItem(title=title, bucketlist_id=bucketlist_id)
                        bucketlist_item.save()

                        response = jsonify({
                            'id': bucketlist_item.id,
                            'title': bucketlist_item.title,
                            'date_created': bucketlist_item.date_created,
                            'date_modified': bucketlist_item.date_modified,
                            'bucketlist_id': bucketlist_id,
                            'message': "Yaaay! Bucketlist item successfully added"
                        })
                        response.status_code = 201

                        return response

                    return jsonify({
                                "message": "Title cannot be blank"
                            }), 400

                else:    # If GET
                    q = request.args.get('q', ' ').strip()
                    if q:
                        items = BucketlistItem.query.filter(BucketlistItem.title.like("%"+q+"%"))\
                        .filter(BucketlistItem.bucketlist_id==bucketlist_id).all()
                        if items:
                            results = []

                            for bucketlist_item in items:
                                single = {
                                    'id': bucketlist_item.id,
                                    'title': bucketlist_item.title,
                                    'date_created': bucketlist_item.date_created,
                                    'date_modified': bucketlist_item.date_modified,
                                    'bucketlist_id': bucketlist_id
                                }
                                results.append(single)

                            response = jsonify(results), 200

                            if not results:
                                return jsonify({
                                "message": "Hey, this bucketlist doesn't have items yet. Please add some"
                            }), 404

                            return response

                        if not items:
                            return jsonify({"message": "Bucketlist item not found"})

                    else:
                        # Implement pagination
                        # Get the pages parameter or set it to 1
                        raw_page = request.args.get('page')
                        if raw_page:
                            try:
                                page = int(raw_page)
                            except ValueError:
                                return jsonify({"message": "The page must be an integer"})
                        else:
                            page = 1    # default page

                        # Set the limit of the no of bucketlists to be viewed
                        raw_limit = request.args.get('limit')
                        if raw_limit:
                            try:
                                limit = int(raw_limit)
                            except ValueError:
                                return jsonify({"message": "The limit must be an integer"})
                        else:
                            limit = 5    # default limit

                        # If q has not been passed / no search query made
                        bucketlist_items = BucketlistItem.get_all(bucketlist_id).paginate(page, limit, False)
                        results = []

                        # if not results:
                        #     return jsonify({
                        #     "message": "Hey, you don't have anyyy bucketlist yet, please create one"
                        # }), 404

                        if bucketlist_items.has_next:
                            next_page_url = "?page=" + str(page + 1) + "&limit=" + str(limit)
                        else: next_page_url = ""

                        if bucketlist_items.has_prev:
                            prev_page_url = "?page=" + str(page - 1) + "&limit=" + str(limit)
                        else: prev_page_url = ""

                        for bucketlist_item in bucketlist_items.items:
                            item = {
                                'id': bucketlist_item.id,
                                'title': bucketlist_item.title,
                                'date_created': bucketlist_item.date_created,
                                'date_modified': bucketlist_item.date_modified,
                                'created_by': user_id
                            }
                            results.append(item)

                        response = jsonify({
                            'next_url': next_page_url, 
                            'prev_url': prev_page_url,
                            'results': results}), 200

                        return response    

            else:
                # User_id not found, payload is an error msg
                return jsonify({
                "message": "Error, could not authenticate. Please login first"
            }), 401
        else:
                # No access token
                return jsonify({
                "message": "Error, access token not found, you need to login first"
            }), 401
Ejemplo n.º 7
0
    def buckelist_get():
        '''
        Function for retrieving a users bucketlist
        '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header
        current_limit = 0
        selected_page = 0
        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                q = request.args.get('q')
                limit = request.args.get('limit')
                page = request.args.get('page')
                if limit:
                    if limit.isdigit():
                        current_limit = int(limit)
                    else:
                        response = jsonify({'message': 'Invalid limit'})
                        response.status_code = 400
                        return response
                else:
                    current_limit = 20
                if page:
                    if page.isdigit():
                        selected_page = int(page)
                    else:
                        response = jsonify({'message': 'Invalid page number'})
                        response.status_code = 400
                        return response
                else:
                    selected_page = 1
                if q:
                    # try:
                    bucketlists = Bucketlist.query.filter(
                        Bucketlist.name.like('%{}%'.format(q))).filter_by(
                            user_id=user_id).paginate(page=selected_page,
                                                      per_page=current_limit,
                                                      error_out=True)
                    results = []
                    for bucketlist in bucketlists.items:
                        obj = {
                            'bucketlist id': bucketlist.bucketlist_id,
                            'name': bucketlist.name,
                            'date_created': bucketlist.date_created,
                            'date_modified': bucketlist.date_modified,
                            'User id': user_id,
                        }
                        results.append(obj)
                    response = jsonify(results)
                    response.status_code = 200
                    return response

                bucketlists = Bucketlist.query.filter_by(
                    user_id=user_id).paginate(page=selected_page,
                                              per_page=current_limit,
                                              error_out=True)
                results = []
                for bucketlist in bucketlists.items:
                    bucketlistresults = []
                    bucketlistsitems = BucketlistItem.get_items(bucketlist.id)
                    for bucketlistitem in bucketlistsitems:
                        obj = {
                            'id': bucketlistitem.id,
                            'item id': bucketlistitem.item_id,
                            'name': bucketlistitem.name,
                            'bucketlist': bucketlistitem.bucketlist_id
                        }
                        bucketlistresults.append(obj)
                    obj = {
                        'bucketlist id': bucketlist.bucketlist_id,
                        'name': bucketlist.name,
                        'items': bucketlistresults,
                        'date_created': bucketlist.date_created,
                        'date_modified': bucketlist.date_modified,
                        'Created By': user_id,
                    }
                    results.append(obj)
                response = jsonify(results)
                response.status_code = 200
                return response
            else:
                response = jsonify({'message': 'Invalid token or expired'})
                response.status_code = 401
                return response
        else:
            response = jsonify({'message': 'Invalid token or expired'})
            response.status_code = 401
            return response
Ejemplo n.º 8
0
    def bucketlistsitem_post(id):
        '''
        Function for adding a users bucketlist item
        '''

        auth_header = request.headers.get('Authorization')
        access_token = auth_header
        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                try:
                    user_bucketlist = Bucketlist.query.filter_by(
                        bucketlist_id=id, user_id=user_id).one()
                    if not user_bucketlist:
                        response = jsonify(
                            {'message': 'Bucket list does not exist'})
                        response.status_code = 404
                        return response
                    item_buckelists = BucketlistItem.query.filter_by(
                        bucketlist_id=user_bucketlist.id).all()
                    items_in_bucketlist = []
                    item_list = []
                    for i in item_buckelists:
                        item_list.append(i)
                        items_in_bucketlist.append(i.name)
                    item_bucket_list_id = len(item_list) + 1
                    if not user_bucketlist:
                        # Raise an HTTPException with a 404 not found status code
                        response = jsonify(
                            {'message': 'Bucketlist does not exist'})
                        response.status_code = 404
                        return response
                    else:
                        name = str(request.data.get('name'))
                        if name:
                            if name in items_in_bucketlist:
                                response = jsonify({
                                    'message':
                                    'Item already exists in this bucketlist'
                                })
                                response.status_code = 409
                                return response
                            else:
                                bucketlistitem = BucketlistItem(
                                    name=name,
                                    item_id=item_bucket_list_id,
                                    bucketlist_id=user_bucketlist.id)
                                bucketlistitem.save()
                                response = jsonify({
                                    'id':
                                    bucketlistitem.id,
                                    'name':
                                    bucketlistitem.name,
                                    'date_created':
                                    bucketlistitem.date_created,
                                    'date_modified':
                                    bucketlistitem.date_modified,
                                    'bucketlist':
                                    bucketlistitem.bucketlist_id
                                })
                                response.status_code = 201
                                return response
                        else:
                            response = jsonify({'message': 'Invalid Entry'})
                            response.status_code = 404
                            return response
                except:
                    response = jsonify(
                        {'message': 'Bucketlist does not exist'})
                    response.status_code = 404
                    return response

            else:
                response = jsonify({'message': 'Invalid token'})
                response.status_code = 401
                return response
Ejemplo n.º 9
0
 def bucketlist_modify(id):
     '''
     Function for modifying a users bucketlist
     '''
     auth_header = request.headers.get('Authorization')
     access_token = auth_header
     if access_token:
         user_id = User.decode_token(access_token)
         if not isinstance(user_id, str):
             bucketlist = Bucketlist.query.filter_by(
                 bucketlist_id=id, user_id=user_id).first()
             if not bucketlist:
                 response = jsonify(
                     {'message': 'Bucketlist does not exist'})
                 response.status_code = 404
                 return response
             if request.method == 'DELETE':
                 bucketlistsitems = BucketlistItem.get_items(bucketlist.id)
                 all_user_bucketlists = Bucketlist.get_bucketlist(user_id)
                 for bucketlist_item in all_user_bucketlists:
                     if bucketlist_item.bucketlist_id > id:
                         bucketlist_item.rearange(bucketlist_item.id)
                 for item in bucketlistsitems:
                     item.delete()
                 bucketlist.delete()
                 response = jsonify(
                     {"message": "bucketlist deleted successfully"})
                 response.status_code = 200
                 return response
             elif request.method == 'PUT':
                 name = str(request.data.get('name'))
                 bucketlist.name = name
                 bucketlist.save()
                 response = jsonify({
                     'id':
                     bucketlist.id,
                     'name':
                     bucketlist.name,
                     'date_created':
                     bucketlist.date_created,
                     'date_modified':
                     bucketlist.date_modified
                 })
                 response.status_code = 200
                 return response
             else:
                 # GET
                 response = jsonify({
                     'id':
                     bucketlist.id,
                     'name':
                     bucketlist.name,
                     'date_created':
                     bucketlist.date_created,
                     'date_modified':
                     bucketlist.date_modified
                 })
                 response.status_code = 200
                 return response
         else:
             response = jsonify({'message': 'Invalid token'})
             response.status_code = 401
             return response
Ejemplo n.º 10
0
def bucketlist_items(id):
    """
    Method that executes creation of a new bucketlist item and adding it to the given bucketlist with <id>:id
    :param id:
    :return:
    """
    # check if the header with key is present
    if 'Authorization' not in request.headers:
        # Return a message to the user telling them that they need to submit an authorization header with token
        response = {'message': 'Header with key Authorization missing.'}
        return make_response(jsonify(response)), 401
    else:
        # Get the access token from the header
        auth_header = request.headers.get('Authorization')

        # check for when authorization was not provided in header
        if not auth_header:
            # Return a message to the user telling them that they need to submit an authorization header with token
            response = {
                'message':
                'Token not provided in the header with key Authorization.'
            }
            return make_response(jsonify(response)), 401
        else:

            auth_strings = auth_header.split(" ")
            if len(auth_strings) != 2:
                response = {'message': 'Invalid token format.'}
                return make_response(jsonify(response)), 401
            else:
                access_token = auth_header.split(" ")[1]

                if access_token:
                    # Attempt to decode the token and get the User ID
                    user_id = User.decode_token(access_token)
                    if not isinstance(user_id, str):
                        # Go ahead and handle the request, the user is authenticated

                        # Get the bucketlist with the id specified from the URL (<int:id>) for the logged in user
                        bucketlist = Bucketlist.query.filter_by(
                            id=id, created_by=user_id).first()
                        if not bucketlist:
                            # There is no bucketlist with this ID for this User, so
                            # Raise an HTTPException with a 404 not found status code
                            abort(404)

                        # adding a new item to the bucketlist
                        if request.method == "POST":
                            if 'name' not in request.data:
                                # Return a message to the user telling them that they need to submit a name
                                response = {
                                    'message': 'Parameter name missing.'
                                }
                                return make_response(jsonify(response)), 400
                            else:
                                name = str(request.data.get('name', ''))
                                if name:
                                    # query whether a bucketlist item with the same name already
                                    # exists in this bucketlist
                                    bucketlist_items = BucketlistItem.query.filter(
                                        and_(BucketlistItem.belongs_to == id,
                                             BucketlistItem.name ==
                                             name)).first()
                                    if bucketlist_items:
                                        # Return a message to the user telling them that the bucketlist exists
                                        response = {
                                            'message':
                                            'Bucketlist item with this name already exists in this'
                                            ' bucketlist. Choose another name.'
                                        }
                                        return make_response(
                                            jsonify(response)), 409
                                    else:
                                        # Save the bucketlist item with the given name in the bucketlist with the id
                                        # specified from the URL (<int:id>)
                                        bucketlist_item = BucketlistItem(
                                            name=name, belongs_to=id)
                                        bucketlist_item.save()
                                        response = jsonify({
                                            'id':
                                            bucketlist_item.id,
                                            'name':
                                            bucketlist_item.name,
                                            'date_created':
                                            bucketlist_item.date_created,
                                            'date_modified':
                                            bucketlist_item.date_modified,
                                            'done':
                                            bucketlist_item.done,
                                            'belongs_to':
                                            bucketlist_item.belongs_to
                                        })

                                        return make_response(response), 201
                                else:
                                    # Return a message to the user telling them that they need to submit a name
                                    response = {
                                        'message':
                                        'Bucketlist item name should not be empty.'
                                    }
                                    return make_response(
                                        jsonify(response)), 400
                        else:
                            # GET all the bucketlist items created by this user and belonging to this bucketlist
                            bucketlist_items = BucketlistItem.query.filter_by(
                                belongs_to=id)

                            results = []

                            for bucketlist_item in bucketlist_items.items:
                                obj = {
                                    'id': bucketlist_item.id,
                                    'name': bucketlist_item.name,
                                    'date_created':
                                    bucketlist_item.date_created,
                                    'date_modified':
                                    bucketlist_item.date_modified,
                                    'done': bucketlist_item.done,
                                    'belongs_to': bucketlist_item.belongs_to
                                }
                                results.append(obj)

                            return make_response(jsonify(results)), 200
                    else:
                        # user is not legit, so the payload is an error message
                        message = user_id
                        response = {'message': message}
                        return make_response(jsonify(response)), 401
                else:
                    response = {'message': 'Empty token string'}
                    return make_response(jsonify(response)), 401