Ejemplo n.º 1
0
def load_data(row):
    """populate db with data from csv file"""

    name, address, categories = row[1:4]

    lat_lng = get_lat_lng(address)

    business = Business(name=name,
                        address=address,
                        lat=lat_lng["lat"],
                        lng=lat_lng["lng"])

    for category in categories.split(", "):

        # getting all existing category objects in the db
        categories_in_db = Category.get_all_categories()

        list_of_categories_in_db = [c.category for c in categories_in_db]

        # checking if category is already in db
        if category not in list_of_categories_in_db:
            category = Category(category=category)

        else:
            category = Category.get_category_by_name(category)

        # adding data to the association table
        if isinstance(category, list):
            for single_category in category:
                single_category.categories_business.append(business)
                db.session.add(single_category)
        else:
            category.categories_business.append(business)
            db.session.add(category)

        db.session.commit()
Ejemplo n.º 2
0
def get_business_info():
    """return a json element with businesses associated to the given category"""

    category = request.args.get("searchTerm", "")

    try:
        # get category object with search term
        category_object = Category.get_category_by_name(category)

    except NoResultFound:
        return jsonify({"data": "Can't find matches"})

    # getting businesses associated with elected category
    try:
        businesses = category_object.categories_business

    # when we have multiple matches, we get a list -> categories_business throws
    # AttributeError (list does not have that attribute
    except AttributeError:

        # todo: turn list of objects into one big object to pass to JS
        return jsonify({"data": "Can't find matches"})

    return jsonify(Business.serialize_business_object(businesses))