예제 #1
0
    def post(self):
        """Handle POST request for this resource. Url ---> /auth/register"""

        # Query to see if the user already exists
        post_data = request.json
        email = post_data.get('email')
        password = post_data.get('password')

        user = User.query.filter_by(email=email).first()
        if not user:
            # There is no user so we'll try to register them
            try:
                # Register the user
                user = User(email=email, password=password)
                user.save()

                response = {
                    'message': 'You registered successfully. Please log in.'
                }
                # return a response notifying the user that they registered successfully
                return response, 201
            except Exception as e:
                # An error occured, therefore return a string message containing the error
                response = {'message': str(e)}
                return response, 401
        else:
            # There is an existing user. We don't want to register users twice
            # Return a message to the user telling them that they they already exist
            response = {'message': 'User already exists. Please login.'}

            return response, 409
예제 #2
0
    def delete(self, id):
        """Handle DELETE request for this resource. 
            Url ---> /bucketlists/<id>"""
        # get the access token from the authorization header
        access_token = request.headers.get('Authorization')

        if access_token:
            # Get the user id related to this access token
            user_id = User.decode_token(access_token)

            if not isinstance(user_id, str):
                # If the id is not a string(error), we have a user id
                # Get the bucketlist with the id specified from the URL (<int:id>)
                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)

                # delete the bucketlist using our delete method
                bucketlist.delete()
                return {
                    "message": "bucketlist {} deleted".format(bucketlist.id)
                }, 200
        abort(401, user_id)
예제 #3
0
    def put(self, id, item_id):
        """Handle POST request for this resource. 
            Url ---> /bucketlists/<id>/items/"""
        # get the access token from the authorization header
        access_token = request.headers.get('Authorization')

        if access_token:
            # Get the user id related to this access token
            user_id = User.decode_token(access_token)

            if not isinstance(user_id, str):
                # If the id is not a string(error), we have a user id
                # Get the bucketlist with the id specified from the URL (<int:id>)
                bucketitem = BucketItem.query.filter_by(id=item_id,
                                                        bucket_id=id).first()
                if not bucketitem:
                    # There is no bucketlist with this ID for this User, so
                    # Raise an HTTPException with a 404 not found status code
                    abort(404)

                # Obtain the new name of the bucketlist from the request data
                name = request.json.get('name')
                done = request.json.get('done')
                if not done:
                    done = False
                bucketitem.name = name
                bucketitem.done = done
                bucketitem.save()
                return bucketitem, 200
        abort(401, user_id)
예제 #4
0
    def get(self, id):
        """Handle GET request for this resource. 
            Url ---> /bucketlists/<id>"""
        # get the access token from the authorization header
        access_token = request.headers.get('Authorization')

        if access_token:
            # Get the user id related to this access token
            user_id = User.decode_token(access_token)

            if not isinstance(user_id, str):
                # If the id is not a string(error), we have a user id
                # Get the bucketlist with the id specified from
                # the URL (<int:id>)
                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)

                # Handle GET request, sending back the bucketlist to the user
                bucketitems = BucketItem.query.filter_by(
                    bucket_id=bucketlist.id)
                items = [item for item in bucketitems]
                bucket = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'items': items,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified,
                    'created_by': bucketlist.created_by,
                }
                return bucket, 200
        abort(401, user_id)
예제 #5
0
 def post(self):
     """Handle POST request for this resource. Url ---> /bucketlists/"""
     # Get the access token from the header
     access_token = request.headers.get('Authorization')
     post_data = request.json
     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):
             name = post_data.get('name')
             bucketlist = Bucketlist(name=name, created_by=user_id)
             bucketlist.save()
             return bucketlist, 201
         abort(401, user_id)
예제 #6
0
 def post(self, id):
     """Handle POST request for this resource. 
         Url ---> /bucketlists/<id>/items/"""
     # Get the access token from the header
     access_token = request.headers.get('Authorization')
     post_data = request.json
     if access_token:
         # Attempt to decode the token and get the User ID
         user_id = User.decode_token(access_token)
         #use = User.query.filter_by(id=user_id).first()
         if not isinstance(user_id, str):
             name = post_data.get('name')
             bucketitem = BucketItem(name=name, bucket_id=id)
             bucketitem.save()
             return bucketitem, 201
         abort(401, user_id)
예제 #7
0
 def get(self):
     """Handle GET request for this resource. Url ---> /bucketlists/"""
     # Get the access token from the header
     access_token = request.headers.get('Authorization')
     if access_token:
         # Attempt to decode the token and get the User ID
         user_id = User.decode_token(access_token)
         bucketlists = Bucketlist.query.filter_by(created_by=user_id)
         if not isinstance(user_id, str):
             # Go ahead and handle the request, the user is authenticated
             # GET all the bucketlists created by this user
             args = pagination_and_search_arguments.parse_args(request)
             page = args.get('page', 1)
             per_page = args.get('per_page', 20)
             q = args.get('q')
             if q:
                 bucketlists = Bucketlist.query.filter_by(
                     created_by=user_id).filter(
                         Bucketlist.name.ilike('%' + q + '%')).paginate(
                             page, per_page, False)
             else:
                 bucketlists = Bucketlist.query.filter_by(
                     created_by=user_id).paginate(page, per_page, False)
             results = []
             if not bucketlists:
                 # There is no bucketlist with this ID for this User, so
                 # Raise an HTTPException with a 404 not found status code
                 abort(404)
             for bucketlist in bucketlists.items:
                 bucketitems = BucketItem.query.filter_by(
                     bucket_id=bucketlist.id)
                 items = [item for item in bucketitems]
                 bucket = {
                     'id': bucketlist.id,
                     'name': bucketlist.name,
                     'items': items,
                     'date_created': bucketlist.date_created,
                     'date_modified': bucketlist.date_modified,
                     'created_by': bucketlist.created_by,
                 }
                 results.append(bucket)
             return results, 200
     abort(401, user_id)