Exemplo n.º 1
0
    def delete(self, bucketlist_id, item_id):
        '''.. :quickref: Bucketlist; Delete this single bucket list

        .. sourcecode:: http

          DELETE /bucketlists/1/items/1/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json
          Authentication: <token>

        :resheader Content-Type: application/json
        :status 204: bucketlist deleted
        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            return {'message': 'Unauthorized Access!'}
        if current_user:
            bucketlist = Bucketlist.query.filter_by(created_by=current_user.id,
                                                    id=bucketlist_id).first()

            if bucketlist:
                item = Item.query.filter_by(
                    id=item_id, bucketlist_id=bucketlist_id).first()
                if item:
                    item.delete()
                    response = {'message': 'Successfully deleted Item'}
                    return response, 200
                else:
                    abort(message='Item not found')
        else:
            abort(message='Expired or invalid token')
Exemplo n.º 2
0
    def delete(self, bucketlist_id):
        '''.. :quickref: Bucketlist; Delete this single bucket list

        .. sourcecode:: http

          DELETE /bucketlists/1/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json
          Authentication: <token>

        :resheader Content-Type: application/json
        :status 204: bucketlist deleted
        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            return {'message': 'Unauthorized Access!'}
        if current_user:
            bucketlist = Bucketlist.query.filter_by(created_by=current_user.id,
                                                    id=bucketlist_id).first()

            if bucketlist:
                bucketlist.delete()
                return {'message': 'Bucketlist successfully deleted'}
            else:
                return {'message': 'Could not find bucketlist'}
        else:
            return {'message': 'Expired or invalid token'}
Exemplo n.º 3
0
    def put(self, bucketlist_id):
        '''.. :quickref: Bucketlist; Update this bucket list
        .. sourcecode:: http

           PUT /bucketlists/1/ HTTP/1.1
           Host: localhost:5000
           Accept: application/json
           Authentication: <token>

        :<json string name: Edited bucketlist name

        :resheader Content-Type: application/json
        :status 200: bucketlist updated
        :status 422: invalid parameters

        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            return {'message': 'Unauthorized Access!'}
        if current_user:
            arguments = request.get_json(force=True)
            name = arguments.get('name')

            bucketlist = Bucketlist.query.filter_by(created_by=current_user.id,
                                                    id=bucketlist_id).first()
            if bucketlist:
                bucketlist.name = name
                bucketlist.save()
                return {'message': 'Successfully updated the bucketlist'}
            else:
                return {'message': 'Could not find bucketlist'}
        else:
            return {'message': 'Expired or invalid token'}
Exemplo n.º 4
0
    def get(self, bucketlist_id):
        '''.. :quickref: Bucketlist; Get single bucket list

        **Example request**:

        .. sourcecode:: http

          GET /bucketlists/1/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json


        **Example response**:

        .. sourcecode:: http

          HTTP/1.1 200 OK
          Vary: Accept
          Content-Type: application/json
          Authentication: Token

          [
            {
                "id": 1,
                "name": "Before 50",
                "items": [
                    {
                        "id": 1,
                        "name": "Watch F1",
                        "date_created": "2017-07-30T22:29:10.044464",
                        "date_modified": "2017-07-31T10:13:44.926045",
                        "done": true
                    }

                ],
                "date_created": "2017-07-30T21:36:32.754289",
                "date_modified": "2017-07-30T21:36:32.754289",
                "created_by": 1
            },
          ]

        :resheader Content-Type: application/json
        :status 200: bucketlist found
        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            abort(400, message='Unauthorized Access!')
        if current_user:
            bucketlistitem = db.session.query(Bucketlist).filter_by(
                created_by=current_user.id, id=bucketlist_id).first()
            if not bucketlistitem:
                abort(404, message='Bucketlist not found')
            else:
                return bucketlistitem
        else:
            abort(400, message='Expired or invalid token')
Exemplo n.º 5
0
    def post(self):
        """.. :quickref: Bucketlists Collection; Create a new bucket list.

        .. sourcecode:: http

          POST /bucketlists/1/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json
          Authentication: <token>


        :reqheader Accept: application/json
        :reqheader Authentication: <token>

        :<json string name: bucketlist name


        :resheader Content-Type: application/json
        :status 201: bucketlist created
        :status 422: invalid parameters
        """

        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            abort(400, message='Unauthorized Access!')
        if current_user:
            arguments = request.get_json(force=True)
            try:
                name = arguments['name']
            except:
                return {'message': 'Invalid parameter entered'}
            bucketlists = Bucketlist.query.filter_by(
                created_by=current_user.id)
            current_bucketlists = []

            if not name:
                # we return bad request since we require name
                return {'message': 'Missing required parameters.'}, 400
            for bucketlist in bucketlists:
                current_bucketlists.append(bucketlist.name)
            if name not in current_bucketlists:
                new_bucketlist = Bucketlist(name=name,
                                            created_by=current_user.id)
                new_bucketlist.save()

                return {'message': 'successfully added a new bucketlist'}
            return {'message': 'bucketlist already exists'}
        else:
            abort(400, message='Expired or invalid token')
Exemplo n.º 6
0
    def verify_token(*args, **kwargs):
        # check if token in request headers
        if 'token' in request.headers:
            token = request.headers['token']

            # authenticate token
            user = User.verify_auth_token(token)
            if not user:
                abort(403, message='authentication failed')
            else:
                # set user in g
                g.user = user
                return f(*args, **kwargs)
        else:
            abort(401, message='token missing from header')
Exemplo n.º 7
0
    def post(self, bucketlist_id):
        """.. :quickref: Bucketlists Collection; Add a new bucketlist item.

        .. sourcecode:: http

          POST /bucketlists/1/items/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json
          Authentication: <token>

        :reqheader Accept: application/json

        :<json string id: bucketlist id
        :<json string name: bucketlist name


        :resheader Content-Type: application/json
        :status 201: bucketlist created
        :status 422: invalid parameters
        """
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            abort(message='Unauthorized Access!')
        if current_user:
            arguments = request.get_json(force=True)
            name = arguments.get('name')

            bucketlist = db.session.query(Bucketlist).filter_by(
                created_by=current_user.id, id=bucketlist_id)
            if bucketlist:
                try:
                    item = Item(name=name, bucketlist_id=bucketlist_id)
                    item.save()

                    response = {
                        'message': 'Item successfully added to bucketlist'
                    }
                    return response
                except:
                    abort(message='Failed to create item')

            else:
                abort(message='Bucketlist not found')
        else:
            abort(message='Expired or invalid token')
Exemplo n.º 8
0
def before_request():
    """Set global attributes."""
    if request.endpoint in [
            "createitem", "updateitem", "deleteitem", "createbucketlist",
            "getallbucketlists", "getsinglebucketlist", "updatebucketlist",
            "deletebucketlist", "getallitems", "changeusername"
    ]:
        token = request.headers.get("token")
        if token is not None:
            user = User.verify_auth_token(token)
            if user == "Expired":
                return jsonify({"message": "Error: Expired Token"}), 401
            if user == "Invalid":
                return jsonify({"message": "Error: Invalid Token"}), 401
            g.user = user
        else:
            return jsonify({"message": "Error: Please enter a token"}), 401
Exemplo n.º 9
0
    def put(self, bucketlist_id, item_id):
        '''.. :quickref: Bucketlist; Update this bucket list item
        .. sourcecode:: http

           PUT /bucketlists/1/items/1/ HTTP/1.1
           Host: localhost:5000
           Accept: application/json
           Authentication: <token>

        :<json string name: New item name

        :resheader Content-Type: application/json
        :status 200: Item updated
        :status 422: invalid parameters
        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            abort(message='Unauthorized Access!!')
        if current_user:
            arguments = request.get_json(force=True)
            name, done = arguments.get('name'), arguments.get('done')

            bucketlist = Bucketlist.query.filter_by(created_by=current_user.id,
                                                    id=bucketlist_id).first()

            if bucketlist:
                item = Item.query.filter_by(
                    id=item_id, bucketlist_id=bucketlist_id).first()
                if item:
                    item.name = name if name is not None else item.name
                    item.done = done if done is not None else item.done
                    item.save()
                    return {'message': 'Successfully updated the item'}
                else:
                    abort(message='Item not found')
            else:
                abort(message='Bucketlist not found')
        else:
            abort(message='Expired or invalid token')
Exemplo n.º 10
0
    def get(self):
        '''

        .. :quickref: Bucketlists Collection; List all the created bucket lists

        **Example request**:

        .. sourcecode:: http

          GET /bucketlists/ HTTP/1.1
          Host: localhost:5000
          Accept: application/json
          Authentication: <token>

        **Example response**:

        .. sourcecode:: http

          HTTP/1.1 200 OK
          Vary: Accept
          Content-Type: application/json

          [
            {
                "id": 1,
                "name": "Before 30",
                "items": [
                    {
                        "id": 1,
                        "name": "Sky Dive",
                        "date_created": "2017-07-30T22:29:10.044464",
                        "date_modified": "2017-07-31T10:13:44.926045",
                        "done": false
                    }
                ],
                "date_created": "2017-07-30T21:36:32.754289",
                "date_modified": "2017-07-30T21:36:32.754289",
                "created_by": 1
            },
            {
                "id": 2,
                "name": "Before 50",
                "items": [
                    {
                        "id": 2,
                        "name": "Climb Mt.Everest",
                        "date_created": "2017-08-07T06:39:22.466605",
                        "date_modified": "2017-08-07T06:39:22.466537",
                        "done": false
                    }

                ],
                "date_created": "2017-07-30T22:28:54.824647",
                "date_modified": "2017-07-30T22:28:54.824647",
                "created_by": 1
            },
          ]

        :query q: full text search query
        :query limit: number of bucket lists per page
        :query page: select page
        :resheader Content-Type: application/json
        :status 200: bucketlists found


        '''
        token = request.headers.get('Authorization')
        if token:
            current_user = User.verify_auth_token(token)
        else:
            abort(401, message='Unauthorized Access!')

        if not isinstance(current_user, User):
            abort(401, current_user)

        # get arguments
        parser = reqparse.RequestParser()
        parser.add_argument('q', type=str, required=False, location='args')
        parser.add_argument('limit',
                            type=int,
                            required=False,
                            default=20,
                            help='Results per page',
                            location='args')
        parser.add_argument('page',
                            type=int,
                            default=1,
                            help='Page number',
                            required=False,
                            location='args')

        arguments = parser.parse_args(request)
        q = arguments.get("q")
        limit = arguments.get("limit")
        page = arguments.get("page")

        if q:
            bucketlists = Bucketlist.query.filter(
                Bucketlist.name.ilike('%' + q + '%'),
                Bucketlist.created_by == current_user.id).paginate(
                    page, limit, False)
        else:
            bucketlists = Bucketlist.query.filter_by(
                created_by=current_user.id).paginate(page, limit, False)

        if bucketlists.items:
            # return marshal(bucketlists)
            return bucketlists.items
        abort(400, message='Bucketlists not found')