예제 #1
0
def add_business_review(business_id):
    """
        Add Review
    """
    if business_id in [business['id'] for business in Store.businesses]:
        sent_data = request.get_json(force=True)
        valid = validate(sent_data, REVIEW_RULES)
        if valid != True:
            response = jsonify(
                status='error', message='Please provide valid details', errors=valid)
            response.status_code = 400
            return response
        user_id = token_id(request.headers.get('Authorization'))
        data = {
            'id': uuid.uuid4().hex,
            'user_id': user_id,
            'review': sent_data['review'],
            'created_at': f"{datetime.datetime.now():%Y-%m-%d %H:%M}",
        }
        Review.save(business_id, data)
        response = jsonify({
            'status': 'ok',
            'message': "Your review has been submitted"
        })
        response.status_code = 201
        return response
    response = jsonify({
        'status': 'error',
        'message': "This business doesn't exist"
    })
    response.status_code = 400
    return response
예제 #2
0
def register():
    """
        User Registration
    """
    valid = validate(request.get_json(force=True), REGISTER_RULES)
    sent_data = request.get_json(force=True)
    if valid != True:
        response = jsonify(
            status='error', message="Please provide valid details", errors=valid)
        response.status_code = 400
        return response
    data = {
        'id': uuid.uuid4().hex,
        'username': sent_data['username'],
        'email': sent_data['email'],
        'password': sent_data['password'],
    }
    if User.user_exists(data['email']):
        response = jsonify({
            'status': 'error',
            'message': "Sorry the email address has been taken"
        })
        response.status_code = 400
        return response
    User.save(data)
    response = jsonify({
        'status': 'ok',
        'message': "You have been successfully registered"
    })
    response.status_code = 201
    return response
예제 #3
0
def reset_password():
    """
        User password reset
    """
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, RESET_PWD_RULES)
    if valid != True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    user_id = token_id(request.headers.get('Authorization'))
    if User.check_password(user_id, sent_data['old_password']) != True:
        response = jsonify({
            'status': 'error',
            'message': "Invalid old password"
        })
        response.status_code = 400
        return response
    User.change_password(user_id, sent_data['new_password'])
    response = jsonify({
        'status': 'ok',
        'message': "You have successfully changed your password"
    })
    response.status_code = 201
    return response
예제 #4
0
def register_business():
    """
        Register business
    """
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, REGISTER_BUSINESS_RULES)
    if valid != True:
        response = jsonify(
            status='error', message="Please provide required info", errors=valid)
        response.status_code = 400
        return response
    user_id = token_id(request.headers.get('Authorization'))
    data = {
        'id': uuid.uuid4().hex,
        'user_id': user_id,
        'name': sent_data['name'],
        'description': sent_data['description'],
        'country': sent_data['country'],
        'city': sent_data['city'],
    }
    if Business.has_same_business(user_id, sent_data['name']):
        response = jsonify(
            status='error', message="You have already registered this business")
        response.status_code = 400
        return response
    Business.save(data)
    response = jsonify({
        'status': 'ok',
        'message': "Your business has been successfully registered"
    })
    response.status_code = 201
    return response
예제 #5
0
def reset_link():
    '''
        Reset link
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, RESET_LINK_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    user = User.query.filter_by(email=sent_data['email']).first()
    if user is None:
        response = jsonify({
            'status': 'error',
            'message': "Email doesn't exist"
        })
        response.status_code = 400
        return response
    PasswordReset.query.filter_by(user_id=user.id).delete()
    gen_token = generate_reset_token()
    PasswordReset.save(user.id, gen_token)
    origin_url = request.headers.get('Origin') or ''
    reset_link = '{}/auth/reset-password/{}'.format(origin_url, gen_token)
    email = render_template('emails/reset.html',
                            name=user.username,
                            url=reset_link)
    send_mail(user.email, email)
    response = jsonify({
        'status': 'ok',
        'message': "Check your email to reset password"
    })
    response.status_code = 201
    return response
예제 #6
0
def confirm_email(token):
    '''
        Confirm email address
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, CONFIRM_EMAIL_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    token = User.query.filter_by(activation_token=token,
                                 email=sent_data['email']).first()
    if token is None:
        response = jsonify({
            'status': 'error',
            'message': "Invalid confirm link token or email"
        })
        response.status_code = 400
        return response
    user = User.query.filter_by(id=token.id, email=sent_data['email']).first()
    User.activate(user.id)
    response = jsonify({'status': 'ok', 'message': "Your email was confirmed"})
    response.status_code = 200
    return response
예제 #7
0
def reset_password(token):
    '''
        Reset password reset
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, RESET_PWD_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    token = PasswordReset.query.filter_by(reset_token=token).first()
    if token is None:
        response = jsonify({
            'status': 'error',
            'message': "Invalid reset token"
        })
        response.status_code = 400
        return response
    user = User.query.filter_by(id=token.user_id).first()
    User.update_password(user.id,
                         generate_password_hash(sent_data['password']))
    PasswordReset.delete(token.id)
    response = jsonify({
        'status': 'ok',
        'message': "You have successfully reset your password"
    })
    response.status_code = 201
    return response
예제 #8
0
def change_password():
    '''
        Change password
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, CHANGE_PWD_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    user_id = token_id(request.headers.get('Authorization'))
    user = User.query.filter_by(id=user_id).first()
    if check_password_hash(user.password, sent_data['old_password']) is False:
        response = jsonify({
            'status': 'error',
            'message': "Invalid old password"
        })
        response.status_code = 400
        return response
    User.update_password(user.id,
                         generate_password_hash(sent_data['new_password']))
    response = jsonify({
        'status': 'ok',
        'message': "You have successfully changed your password"
    })
    response.status_code = 201
    return response
예제 #9
0
def add_business_review(business_id):
    '''
        Add Review
    '''
    user_id = token_id(request.headers.get('Authorization'))
    business = Business.get(business_id)
    if business is not None:
        sent_data = request.get_json(force=True)
        valid = validate(sent_data, REVIEW_RULES)
        if valid is not True:
            response = jsonify(status='error',
                               message='Please provide valid details',
                               errors=valid)
            response.status_code = 400
            return response
        review = Review.save({
            'user_id': user_id,
            'description': sent_data['review'],
            'business_id': business.id
        })
        response = jsonify({
            'status': 'ok',
            'message': 'Your review has been sent',
            'review': review.serialize_one,
        })
        response.status_code = 201
        return response
    response = jsonify({
        'status': 'error',
        'message': 'This business doesn\'t exist'
    })
    response.status_code = 400
    return response
예제 #10
0
def login():
    '''
        User login
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, LOGIN_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    data = {
        'email': sent_data['email'],
        'password': sent_data['password'],
    }
    # Check if email exists
    logged_user = User.get_user(data['email'])
    if logged_user is not None:
        # Check password
        if check_password_hash(logged_user.password, data['password']):
            if logged_user.activation_token is not None:
                response = jsonify({
                    'status':
                    'error',
                    'message':
                    "Please confirm your email address"
                })
                response.status_code = 401
                return response
            token_ = get_token(logged_user.id)
            Token.save({
                'user_id': logged_user.id,
                'access_token': token_,
            })
            response = jsonify({
                'status': 'ok',
                'message': 'You have been successfully logged in',
                'access_token': token_,
                'user': {
                    'username': logged_user.username,
                    'email': logged_user.email
                }
            })
            response.status_code = 200
            # response.headers['auth_token'] = token
            return response
    response = jsonify({
        'status': 'error',
        'message': "Invalid email or password"
    })
    response.status_code = 401
    return response
예제 #11
0
def update_business(business_id):
    '''
        Update business
    '''
    sent_data = request.get_json(force=True)
    user_id = token_id(request.headers.get('Authorization'))
    business = Business.get_by_user(business_id, user_id)
    if business is not None:
        valid = validate(sent_data, REGISTER_BUSINESS_RULES)
        if valid is not True:
            response = jsonify(status='error',
                               message="Please provide required info",
                               errors=valid)
            response.status_code = 400
            return response
        data = {
            'name': sent_data['name'],
            'description': sent_data['description'],
            'category': sent_data['category'],
            'country': sent_data['country'],
            'city': sent_data['city'],
        }
        if Business.has_two_same_business(user_id, sent_data['name'],
                                          business_id):
            response = jsonify(status='error',
                               message=("You have already registered"
                                        " a business with same name"))
            response.status_code = 400
            return response
        Business.update(business_id, data)
        response = jsonify({
            'status':
            'ok',
            'message':
            "Your business has been successfully updated"
        })
        response.status_code = 202
        return response
    response = jsonify(status='error',
                       message=("This business doesn't exist or you"
                                " don't have privileges to it"))
    response.status_code = 400
    return response
예제 #12
0
def login():
    """
        User login
    """
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, LOGIN_RULES)
    if valid != True:
        response = jsonify(
            status='error', message="Please provide valid details", errors=valid)
        response.status_code = 400
        return response
    data = {
        'email': sent_data['email'],
        'password': sent_data['password'],
    }
    # Check if email exists in the store
    logged_user = User.get_user(data['email'])
    if logged_user:
        # Check password
        if check_password_hash(logged_user['password'], data['password']):
            token = get_token(logged_user['id'])
            User.add_token(token)
            response = jsonify({
                'status': 'ok',
                'message': 'You have been successfully logged in',
                'access_token': token,
            })
            response.status_code = 200
            # response.headers['auth_token'] = token
            return response
        response = jsonify({
            'status': 'error',
            'message': "Invalid password"
        })
        response.status_code = 401
        return response
    response = jsonify({
        'status': 'error',
        'message': "Invalid email or password"
    })
    response.status_code = 401
    return response
예제 #13
0
def register_business():
    '''
        Register business
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, REGISTER_BUSINESS_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide required info",
                           errors=valid)
        response.status_code = 400
        return response
    user_id = token_id(request.headers.get('Authorization'))
    if Business.query.order_by(desc(Business.created_at)).filter(
            Business.user_id == user_id,
            func.lower(Business.name) == func.lower(
                sent_data['name'])).first() is not None:
        response = jsonify(status='error',
                           message=("You have already "
                                    "registered business with the same name"))
        response.status_code = 400
        return response
    data = {
        'user_id': user_id,
        'name': sent_data['name'],
        'description': sent_data['description'],
        'category': sent_data['category'],
        'country': sent_data['country'],
        'city': sent_data['city']
    }
    Business.save(data)
    response = jsonify({
        'status':
        'ok',
        'message':
        "Your business has been successfully registered"
    })
    response.status_code = 201
    return response
예제 #14
0
def confirm_token_existance():
    '''
        Confirm email address
    '''
    sent_data = request.get_json(force=True)
    valid = validate(sent_data, CONFIRM_TOKEN_RULES)
    if valid is not True:
        response = jsonify(status='error',
                           message="Please provide valid details",
                           errors=valid)
        response.status_code = 400
        return response
    token = User.query.filter_by(activation_token=sent_data['token']).first()
    if token is None:
        response = jsonify({
            'status': 'error',
            'message': "Invalid confirm link token"
        })
        response.status_code = 400
        return response
    response = jsonify({'status': 'success', 'message': "Token exists!"})
    response.status_code = 200
    return response
예제 #15
0
def register():
    '''
        User Registration
    '''
    errors = {}
    valid = validate(request.get_json(force=True), REGISTER_RULES)
    sent_data = request.get_json(force=True)
    if valid is not True:
        errors = valid

    # Check if if there exists same confirmed email
    email_check = User.query.filter_by(email=sent_data.get('email')).first()
    if email_check is not None:
        if email_check.activation_token is None:
            errors['email'] = errors.get('email') or []
            errors['email'].append('Email was taken')

    # Check if if there exists same username
    username_check = User.query.filter_by(
        username=sent_data.get('username')).first()
    if username_check is not None:
        if email_check is not None:
            if (username_check.email is email_check.email) and (
                    email_check.activation_token is not None):
                pass
            else:
                errors['username'] = errors.get('username') or []
                errors['username'].append('Username was taken')
        else:
            errors['username'] = errors.get('username') or []
            errors['username'].append('Username was taken')

    if errors:
        response = jsonify({
            'status': 'error',
            'errors': errors,
            'message': "Please provide valid details"
        })
        response.status_code = 400
        return response

    gen_token = get_confirm_email_token()
    origin_url = request.headers.get('Origin') or ''
    confirm_link = '{}/auth/confirm-password/{}'.format(origin_url, gen_token)
    if email_check is not None:
        User.update_token(email_check.id, gen_token)
        message = '''This account is already registered,
                    Check your email to confirm'''
    else:
        User.save({
            'username': sent_data['username'],
            'email': sent_data['email'],
            'activation_token': gen_token,
            'password': generate_password_hash(sent_data['password'])
        })
        message = '''You have been successfully registered,
                    Please confirm email address'''
    email = render_template('emails/reset.html',
                            name=sent_data['username'],
                            url=confirm_link)
    send_mail(sent_data['email'], email)
    response = jsonify({'status': 'ok', 'message': message})
    response.status_code = 201
    return response