Ejemplo n.º 1
0
    def post(self):
        """Handle POST request for this view. Url ---> /auth/register"""

        # Query to see if the user already exists
        user = User.query.filter_by(email=request.data['email']).first()

        if not user:
            # There is no user so we'll try to register them
            try:
                post_data = request.data
                # Register the user
                email = post_data['email']
                password = post_data['password']
                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 make_response(jsonify(response)), 201
            except Exception as e:
                # An error occured, therefore return a string message containing the error
                response = {'message': str(e)}
                return make_response(jsonify(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 make_response(jsonify(response)), 202
Ejemplo n.º 2
0
    def post(self):
        """Handle POST request for this view. Url ---> /auth/register"""

        # Query to see if the user already exists
        user = User.query.filter_by(email=request.data['email']).first()

        if not user:
            try:
                post_data = request.data
                # Register the user
                email = post_data['email'].strip()
                password = post_data['password'].strip()

                # check registration without password
                if not password:
                    return self.error.not_acceptable(
                        "You can't register without a password")

                # check password length
                if len(password) < 8:
                    return self.error.not_acceptable(
                        "Password is too short. Minimum is 8 characters")

                # check email is not empty
                if not email:
                    return self.error.not_acceptable(
                        "You cannot register without an email")

                # check correct format
                regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
                if not re.match(regex, email):
                    return self.error.not_acceptable(
                        "The email address input is not valid")

                # Register user finally
                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 self.success.create_resource(response)
            except Exception as e:
                # An error occured, therefore return a string message containing the error
                return self.error.internal_server_error(str(e))
        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
            return self.error.not_acceptable(
                "User already exists. Please login.")
Ejemplo n.º 3
0
    def get(self, id):
        '''
        GET request for url: /bucketlist/<bucketlist_id>/items
        Get all bucketlist items
        '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist_items = BucketlistItem.get_all(belongs_to=id)
                results = []

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

                return make_response(jsonify(results)), 200
Ejemplo n.º 4
0
    def put(self, id, item_id):
        ''' UPDATE a bucketlist '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist_item = BucketlistItem.query.filter_by(
                    belongs_to=id, id=item_id).first()
                if not bucketlist_item:
                    return make_response(
                        jsonify({"message": "The Item does not exist"})), 404
                name = str(request.data.get('name', ''))

                bucketlist_item.name = name
                bucketlist_item.save()

                response = {
                    'id': bucketlist_item.id,
                    'name': bucketlist_item.name,
                    'date_created': bucketlist_item.date_created,
                    'date_modified': bucketlist_item.date_modified,
                    'belongs_to': bucketlist_item.belongs_to
                }
                return make_response(jsonify(response)), 200
Ejemplo n.º 5
0
    def post(self):
        '''
        POST request for url: /bucketlist
        Create a bucketlist
        '''
        # Get the access token from the header
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                name = str(request.data.get('name', ''))
                public_id = str(uuid.uuid4())
                if name:
                    bucketlist = Bucketlist.query.filter_by(
                        name=name, created_by=user_id).first()
                    if bucketlist:
                        return make_response({
                            "message":
                            "A Bucketlist with the same name already exists"
                        }), 409
                    bucketlist = Bucketlist(name=name,
                                            created_by=user_id,
                                            public_id=public_id)
                    bucketlist.save()
                    response = jsonify({
                        'id': bucketlist.id,
                        'name': bucketlist.name,
                        'date_created': bucketlist.date_created,
                        'date_modified': bucketlist.date_modified,
                        'create_by': user_id
                    })
                    return make_response(response), 201
Ejemplo n.º 6
0
    def get(self):
        '''
        GET request for url: /bucketlist
        Get all bucketlists
        '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlists = Bucketlist.query.filter_by(created_by=user_id)
                results = []

                for bucketlist in bucketlists:
                    obj = {
                        'id': bucketlist.id,
                        'name': bucketlist.name,
                        'date_created': bucketlist.date_created,
                        'date_modified': bucketlist.date_modified,
                        'created_by': bucketlist.created_by
                    }
                    results.append(obj)

                return make_response(jsonify(results)), 200
Ejemplo n.º 7
0
    def put(self, id):
        ''' UPDATE a bucketlist '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist = Bucketlist.query.filter_by(
                    id=id, created_by=user_id).first()
                if not bucketlist:
                    abort(404)
                name = str(request.data.get('name', ''))

                bucketlist.name = name
                bucketlist.save()

                response = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified,
                    'created_by': bucketlist.created_by
                }
                return make_response(jsonify(response)), 200

        response = jsonify({"message": "Token is missing"})
        return response
Ejemplo n.º 8
0
    def delete(self, id):
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        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 make_response(
                        jsonify({"message":
                                 "The Bucketlist does not exist"})), 404
                bucketlist.delete()
                response = jsonify(
                    {"message": "bucketlist {} deleted".format(bucketlist.id)})
                return make_response(response), 200
Ejemplo n.º 9
0
    def delete(self, id, item_id):
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist_item = BucketlistItem.query.filter_by(
                    belongs_to=id, id=item_id).first()
                if not bucketlist_item:
                    abort(404)
                bucketlist_item.delete()
                response = jsonify({
                    "message":
                    "bucketlist {} deleted".format(bucketlist_item.id)
                })
                return make_response(response), 200
Ejemplo n.º 10
0
    def get(self, id, item_id):
        ''' READ a bucketlist item '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist_item = BucketlistItem.query.filter_by(
                    belongs_to=id, id=item_id).first()
                if not bucketlist_item:
                    abort(404)
                response = jsonify({
                    'id': bucketlist_item.id,
                    'name': bucketlist_item.name,
                    'date_created': bucketlist_item.date_created,
                    'date_modified': bucketlist_item.date_modified,
                    'belongs_to': bucketlist_item.belongs_to
                })
                return make_response(response), 200
Ejemplo n.º 11
0
    def post(self, id):
        '''
        POST request for url: /bucketlist/<id>/items
        Create a bucketlist item
        '''
        # Get the access token from the header
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                name = str(request.data.get('name', ''))
                if name:
                    # Ensuring no duplicate Bucketlist items.
                    bucketlist_item = BucketlistItem.query.filter_by(
                        belongs_to=id, name=name).first()
                    if not bucketlist_item:
                        public_id = str(uuid.uuid4())
                        bucketlist_item = BucketlistItem(name=name,
                                                         belongs_to=id,
                                                         public_id=public_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,
                            'belongs_to':
                            bucketlist_item.belongs_to
                        })
                        return make_response(response), 201
                    return make_response(
                        jsonify({"message":
                                 "Bucketlist Item already exists"})), 409
Ejemplo n.º 12
0
    def get(self, id):
        ''' READ a bucketlist '''
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist = Bucketlist.query.filter_by(
                    id=id, created_by=user_id).first()
                if not bucketlist:
                    abort(404)
                response = jsonify({
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_createad': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified,
                    'created_by': bucketlist.created_by
                })
                return make_response(response), 200

        return ''