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
def test_func_get_all(self): business1 = Business(name='name', password='******', email='email', confirmed=True) business2 = Business(name='name2', password='******', email='email2', confirmed=False) db.session.add(business1) db.session.add(business2) db.session.commit() businesses = Business.get_all() self.assertEqual(businesses[0], business1) self.assertEqual(businesses[1], business2)
def all_business(): """this will get all the businesses""" businesses = Business.get_all() final_result = [] for business in businesses: obj = { 'id': business.id, 'name': business.name, 'description': business.description, 'location': business.location, 'contact': business.contact, 'category': business.category } final_result.append(obj) response = jsonify(final_result) response.status_code = 200 return response