示例#1
0
def login():
    try:
        required_fields = ['email', 'password']
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422
    email = validate_auth_data_null(data.get('email'))
    password = validate_auth_data_null(data.get('password'))

    if not email or not password:
        return jsonify({'message':
                        'Enter Valid Data: Email and password'}), 400
    user = User.query.filter_by(email=email).first()
    # print('Hellow owrld', password, user.password)
    if not user:
        return jsonify(
            {'message':
             'Invalid Email: Enter right credentions to login'}), 401
    elif not user.verify_password(password):
        return jsonify(
            {'message':
             'Invalid password: Enter right password to login'}), 401
    current_user = user.id
    return token_generator(current_user)
示例#2
0
def register():
    try:
        required_fields = ['first_name', 'username', 'email', 'password']
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422

    email = validate_auth_data_null(data.get('email'))
    username = validate_auth_data_null(data.get('username'))
    first_name = validate_auth_data_null(data.get('first_name'))
    password = validate_auth_data_null(data.get('password'))

    if not username or not email or not password:
        return jsonify(
            {'message':
             'You need email, username and password to register'}), 400

    if not validate_email(email):
        return jsonify(
            {'message': 'Invalid Email. Enter valid email to register'}), 400
    existant_user = User.query.filter_by(email=email).first()
    if existant_user:
        return jsonify({'message':
                        'This email is registered, login instead'}), 404
    new_user = User(email=email,
                    username=username,
                    first_name=first_name,
                    password=password)
    new_user.save()
    return jsonify(
        {'message': "{}'s account succesfully created".format(username)}), 201
示例#3
0
def reset_password():
    try:
        required_fields = [
            'email',
        ]
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422
    email = validate_auth_data_null(data.get('email'))
    if not email:
        return jsonify({'message': 'Enter Valid Email'}), 400
    user = User.query.filter_by(email=email).first()
    if not user:
        return jsonify({
            'message':
            'Invalid Email: Enter right credentions for reset password'
        }), 401
    n_password = generate_string()
    User.update(User, user.id, password=n_password)
    msg = Message(subject="Weconect reset password",
                  body="This is a mail to reset your weconnect password",
                  html="Your new password is {}".format(n_password),
                  recipients=[email])
    mail.send(msg)
    return jsonify({'message':
                    'Check your email address for new password'}), 201
示例#4
0
def make_businessreview(business_id):
    current_user = get_jwt_identity()
    business_to_review = Business.query.filter_by(id=business_id).first()
    print(business_to_review)
    if request.method == 'POST':
        if not business_to_review:
            return jsonify(
                {'message': 'You can only review an existing business'}), 409
        try:
            required_fields = ['value', 'comments']
            data = check_blank_key(request.get_json(), required_fields)
        except AssertionError as err:
            msg = err.args[0]
            return jsonify({"message": msg}), 422
        value = validate_buss_data_null(data['value'])
        comments = validate_buss_data_null(data['comments'])
        if not value or not comments:
            return jsonify(
                {'message':
                 'You have to enter a review value and comment'}), 400
        if current_user == business_to_review.user_id:
            return jsonify({'message':
                            'You can not review your own business'}), 403
        new_review = Review(value=value,
                            comments=comments,
                            business_id=business_id,
                            user_id=current_user)
        new_review.save()
        display_review = Review.query.filter_by(id=new_review.id).first()
        info = display_review.accesible()

        return jsonify({
            'message': 'You have successfully created a review',
            'Review': info
        }), 201

    # retreving a single business's reviews
    if not business_to_review:
        return jsonify({'message': 'Enter an existing business'}), 409
    all_reviews = Review.get_all(Review)

    # using lambda and map
    def func(review):
        return review.accesible(
        ) if review.business_id == business_id else False

    all_reviews = filter(lambda item: item and True, map(func, all_reviews))
    data = list(all_reviews)
    if len(data) == 0:
        return jsonify({'message': 'No review for this business'}), 201

    return jsonify({
        'message':
        'Reviews for business with id {} are :'.format(business_id),
        'reviews ':
        data
    }), 201
示例#5
0
def registerBusiness():
    """ This is to register a business"""
    current_user = get_jwt_identity()
    if request.method == 'POST':
        # Check for blank key
        try:
            required_fields = ['name', 'description', 'category', 'location']
            data = check_blank_key(request.get_json(), required_fields)
        except AssertionError as err:
            msg = err.args[0]
            return jsonify({"message": msg}), 422
        # Check if user entered a name and location
        name = validate_buss_data_null(data['name'])
        description = validate_buss_data_null(data['description'])
        location = validate_buss_data_null(data['location'])
        category = validate_buss_data_null(data['category'])
        if not location or not description or not name:
            return jsonify(
                {'message': 'You need a business name' +
                                ' and location to Register'}), 403
        # Check if business is registered
        exist_business = Business.query.filter_by(name=name).first()
        if exist_business:
            return jsonify(
                {'message': 'This Business is already registered'}), 409

        new_business = Business(
            name=name,
            description=description,
            category=category,
            location=location,
            user_id=current_user)
        new_business.save()
        current_business = Business.query.filter_by(
            name=name).first()
        new_business_id = current_business.id
        new_business_name = current_business.name

        return jsonify(
            {'message': 'New business has been created',
             'businesses ID': new_business_id,
             'business name': new_business_name
             }), 201

    # Get all businesses
    all_businesses = Business.get_all(Business)

    def func(business): return business.accesible()
    all_businesses = map(func, all_businesses)
    data = list(all_businesses)
    if data == []:
        return jsonify({'message': 'No businesses available',
                        }), 200
    return jsonify({'message': 'These are the businesses',
                    'businesses': data
                    }), 200
示例#6
0
def editBusiness(business_id):
    current_user = get_jwt_identity()
    exist_business = Business.query.filter_by(id=business_id).first()
    # Check if business exists
    if not exist_business:
        return jsonify({'message': 'Bussniess does not exist'}), 404
    if request.method == 'GET':
        business_info = exist_business.accesible()
        return jsonify({'business': business_info,
                        'message': 'Here is the searched business'
                        }), 200
    if current_user != exist_business.user_id:
        return jsonify(
            {'message': 'You can only change your own business'}), 403

    elif request.method == 'DELETE':
        exist_business.delete()
        return jsonify(
            {'message': 'Business successfully deleted'}), 202

    # Check for blank key
    try:
        required_fields = ['name', 'description', 'category', 'location']
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422
    # Check for null user data
    name = validate_buss_data_null(data['name'])
    description = validate_buss_data_null(data['description'])
    location = validate_buss_data_null(data['location'])
    category = validate_buss_data_null(data['category'])
    if not name or not description or not location:
        return jsonify(
            {'message': 'Business name and Location have to be entred'}), 403

    exist_name_business = Business.query.filter_by(name=name).first()
    if exist_name_business:
        return jsonify(
            {'message': 'This Business is name is already used'}), 409

    Business.update(Business, business_id, name=name, description=description,
                    location=location, category=category, user_id=current_user)
    display_business = Business.query.filter_by(name=name).first()
    info = display_business.accesible()
    return jsonify({'Edited business is': info,
                    'message': 'Business edited successfully'
                    }), 201
示例#7
0
def change_password():
    try:
        required_fields = ['old_password', 'new_password']
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422
    old_password = validate_auth_data_null(data.get('old_password'))
    new_password = validate_auth_data_null(data.get('new_password'))
    jti = get_raw_jwt()['jti']

    if not old_password or not new_password:
        return jsonify({'message':
                        'Enter Valid Data: Email and password'}), 400
    current_user = get_jwt_identity()
    user = User.query.filter_by(id=current_user).first()
    if not user.verify_password(old_password):
        return jsonify(
            {'message': 'Enter Valid Password: Old password is wrong'}), 401
    User.update(User, current_user, password=new_password)
    tokenlist = TokenBlacklist(token=jti, user_identity=current_user)
    tokenlist.save()
    return jsonify({'message': 'Password Successfully Changed'}), 201