Exemple #1
0
    def post(self):
        """handles registering a user """

        args = register_parser.parse_args()
        new_user = args
        is_not_valid_input = validate_user_payload(args)

        if is_not_valid_input:
            return is_not_valid_input

        # Check for an already existent username
        db_user_with_same_name = db.filter_by(User, 'user_name',
                                              new_user['user_name'])
        db_user_with_same_email = db.filter_by(User, 'email',
                                               new_user['email'])

        if (db_user_with_same_name or db_user_with_same_email) is not None:
            return {'message': 'User Already exists'}, 400

        # Register the User
        user_name = new_user['user_name'].strip()
        email = new_user['email'].strip()
        password = new_user['password']

        user_object = User(user_name, email, password)
        db.commit(user_object)
        return {'result': 'You Are Registered'}, 201
Exemple #2
0
    def decorated(*args, **kwargs):

        token_auth_header = request.headers.get('Authorization')
        current_user = ''
        token = ''
        if token_auth_header:
            try:
                token = token_auth_header if not 'Bearer' in token_auth_header else token_auth_header.split(
                    ' ')[1]
            except IndexError:
                return {'message': 'Token is missing!'}, 401

            token_blacklisted = db.filter_by(Blacklist, 'token', token)

            if token_blacklisted is not None:
                return {'message': 'Token is expired!'}, 401

            try:
                data = jwt.decode(token, app.config['SECRET_KEY'])
                matched_user = db.filter_by(User, 'user_name', data['user'])
                current_user = matched_user['user_name']

            except jwt.exceptions.InvalidTokenError:
                return {'message': 'Token is invalid!'}, 401

        else:
            return {'message': 'unauthorised'}, 401

        return f(*args, current_user, token, **kwargs)
Exemple #3
0
    def post(self):
        """ handles posting login data"""

        args = login_parser.parse_args()
        token = ''
        verified = False
        user_name = args['user_name']
        password = args['password']

        # Check if the user exists
        db_user = db.filter_by(User, 'user_name', user_name)

        if db_user is not None:
            if check_password_hash(db_user['password_hash'], password):
                token = jwt.encode(
                    {
                        'user':
                        db_user['user_name'],
                        'exp':
                        datetime.datetime.utcnow() +
                        datetime.timedelta(minutes=100)
                    }, app.config['SECRET_KEY'])
                verified = True

        if verified:
            return {'token': token.decode('UTF-8')}, 200
        else:
            return {'message': 'user does not exist'}, 403
Exemple #4
0
    def post(self, current_user, token):
        """resets user's password """

        args = reset_parser.parse_args()
        is_not_valid_input = validate_reset_payload(args)

        if is_not_valid_input:
            return is_not_valid_input

        password_payload = args

        db_user = db.filter_by(User, 'user_name', current_user)

        if db_user is not None:
            if check_password_hash(db_user['password_hash'],
                                   password_payload['current_password']):
                db_user['password_hash'] = generate_password_hash(
                    password_payload['new_password'])
                db.update(User, db_user)
                return {'message': 'password is reset'}, 201
Exemple #5
0
    def post(self, current_user, token):
        """ posts a business """

        self.current_user = current_user
        args = business_parser.parse_args()
        new_business = args
        is_not_valid_input = validate_business_payload(args)

        if is_not_valid_input:
            return is_not_valid_input

        db_user = db.filter_by(User, 'user_name', self.current_user)

        # Adding a business
        added_business = BusinessModel(new_business['business_name'],
                                       new_business['category'],
                                       new_business['location'],
                                       new_business['profile'], db_user['id'])
        db.commit(added_business)

        return {'message': 'business added sucessfully'}, 201
Exemple #6
0
    def delete(self, current_user, token, businessId):
        """ deletes a specific businesses """

        business_to_change = db.get(BusinessModel, businessId)
        db_user = db.filter_by(User, 'user_name', current_user)

        if business_to_change is None:
            return {
                'message':
                'There is no Business with ID : {}'.format(businessId)
            }, 400

        else:
            if business_to_change['created_by'] != db_user['id']:
                return {
                    'message': 'You are not authorised to Change this business'
                }, 403

            else:
                db.delete(BusinessModel, business_to_change)
                return {'message': 'business deleted'}, 201
Exemple #7
0
    def put(self, current_user, token, businessId):
        """ updates a specific businesses data """

        business_to_change = db.get(BusinessModel, businessId)
        args = update_business_parser.parse_args()
        busines_payload = args
        is_not_valid_input = validate_business_update_payload(args, businessId)

        if is_not_valid_input:
            return is_not_valid_input

        if business_to_change is None:
            return {
                'message':
                'There is no Business with ID : {}'.format(businessId)
            }, 400

        else:

            db_user = db.filter_by(User, 'user_name', current_user)
            if business_to_change['created_by'] != db_user['id']:
                return {
                    'message': 'You are not authorised to Change this business'
                }, 403

                # change business name
            for key in PAYLOAD_KEYS:
                if key == 'business_name':
                    test_business = db.filter_by(BusinessModel,
                                                 'business_name',
                                                 args['business_name'])

                    if test_business is not None and test_business[
                            'business_name'].lower(
                            ) != business_to_change['business_name'].lower():
                        return {
                            'message': 'business name is already taken'
                        }, 400

                if busines_payload[key] is not None:

                    if busines_payload[key] != business_to_change[key]:
                        business_to_change[key] = busines_payload[key]

            # if busines_payload['business_name'] is not None:

            #     test_business = db.filter_by(BusinessModel,'business_name', args['business_name'])

            #     if test_business is not None and test_business['business_name'].lower() != business_to_change['business_name'].lower():
            #         return {'message': 'business name is already taken'}, 400

            #     if busines_payload['business_name'] != business_to_change['business_name']:
            #         business_to_change['business_name'] = busines_payload['business_name']

            #     # change category
            # if busines_payload['category'] is not None:

            #     if busines_payload['category'] != business_to_change['category']:
            #         business_to_change['category'] = busines_payload['category']

            #     # change location
            # if busines_payload['location'] is not None:

            #     if busines_payload['location'] != business_to_change['location']:
            #         business_to_change['location'] = busines_payload['location']

            #     # change profile
            # if busines_payload['profile'] is not None:

            #     if busines_payload['profile'] != business_to_change['profile']:
            #         business_to_change['profile'] = busines_payload['profile']

            db.update(BusinessModel, business_to_change)

            return {'result': 'business changed successfully'}, 201