コード例 #1
0
ファイル: views.py プロジェクト: peterwade153/weconnect
def reviews(current_user, id):
    """ route allows user review a a business """

    if request.method == 'POST':
        ''' Reviewing a business '''
        data = request.get_json()

        is_valid_review = re.match('^[A-Za-z]+[A-Za-z0-9 ]{,200}$',
                                   data['review'])

        #if the data passes our validity check
        if not is_valid_review:
            return jsonify({
                'Status':
                'Failed',
                'Message':
                'Only characters and not beyond 200 charaters!'
            }), 403

        business = Business.get_business(id)
        if business is None:
            return jsonify({
                'Status': 'Failed',
                'Message': 'Business not registered here!'
            }), 404

        new_review = Review(review=data['review'], business_id=id)
        db.session.add(new_review)
        db.session.commit()
        return jsonify({
            'Status': 'Success',
            'Message': 'Review has been successfully added!'
        }), 201

    elif request.method == 'GET':
        ''' Get request '''
        reviews = Review.query.filter_by(business_id=id).all()
        author = User.query.filter_by(id=current_user).first()
        business = Business.get_business(id)

        review_list = []
        for review in reviews:
            review_info = {}
            review_info['review'] = review.review
            review_info['business_name'] = business.business_name
            review_info['reviewed_on'] = review.reviewed_on
            review_info['review_author'] = author.username

            review_list.append(review_info)

        return jsonify({'Status': 'Success', 'Review': review_list}), 200
コード例 #2
0
def one_business(business_id):

    current_user = get_jwt_identity()  #Current_user is username
    targetbusiness = Business.get_business(business_id)

    if request.method == 'GET':
        return make_response(jsonify(targetbusiness)), 200

    elif request.method == 'DELETE':
        if current_user == targetbusiness['username']:
            deletebusiness = Business.delete_business(business_id)
            return make_response(jsonify(deletebusiness)), 200
        else:
            return jsonify(
                {'message': 'You cannot delete a business that is not yours'})
コード例 #3
0
ファイル: views.py プロジェクト: peterwade153/weconnect
def business(current_user, id):
    """ route allows user to get, update and delete a business """

    if request.method == 'GET':
        ''' allows user view a business '''
        business = Business.get_business(id)

        if business is None:

            return jsonify({
                'Status': 'Failed',
                'Message': 'No business found'
            }), 404

        business_info = business.business_object()

        return jsonify({'Status': 'Success', 'Business': business_info}), 200

    if request.method == 'PUT':
        ''' method only allows business owner to edit it '''
        data = request.get_json()
        business = Business.get_business(id)
        if not business:
            return jsonify({
                'Status': 'Failed',
                'Message': 'No business found'
            }), 404

        if 'business_name' in data.keys():
            is_valid_name = re.match('^[A-Za-z]+[A-Za-z0-9 ]+$',
                                     data['business_name'])
            if not is_valid_name:
                return jsonify({
                    'Status': 'Failed',
                    'Message': 'characters or digits expected!'
                }), 403
            business.business_name = data['business_name']
        if 'category' in data.keys():
            is_valid_category = re.match('^[A-Za-z]+[A-Za-z0-9 ]+$',
                                         data['category'])
            if not is_valid_category:
                return jsonify({
                    'Status': 'Failed',
                    'Message': 'characters or digits expected!'
                }), 403
            business.category = data['category']
        if 'location' in data.keys():
            is_valid_location = re.match('^[A-Za-z]+[A-Za-z0-9 ]+$',
                                         data['location'])
            if not is_valid_location:
                return jsonify({
                    'Status': 'Failed',
                    'Message': 'characters or digits expected!'
                }), 403
            business.location = data['location']

        if business.user_id != current_user:
            return jsonify({
                'Status': 'Failed',
                'Message': 'You are not permitted'
            }), 403
        #save new business details
        db.session.commit()
        return jsonify({
            'Status': 'Success',
            'Message': 'Business Updated successfully'
        }), 200

    elif request.method == 'DELETE':
        """ deletes a business """

        business = Business.get_business(id)

        if business is None:
            return jsonify({
                'Status': 'Failed',
                'Message': 'No business found!'
            }), 404

        if business.user_id != current_user:
            return jsonify({
                'Status': 'Failed',
                'Message': 'You are not permitted'
            }), 403
        db.session.delete(business)
        db.session.commit()

        return jsonify({
            'Status': 'Success',
            'Message': 'Business deleted successfully'
        }), 200
コード例 #4
0
ファイル: business.py プロジェクト: tmas0/dbrepo
def get_business():
    b = Business()

    business = b.get_business()

    return jsonify({'data': business})