from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from database import Restaurant, Base, MenuItem, User engine = create_engine('postgresql://*****:*****@localhost/catalog') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() restaurant1 = Restaurant(name="Zaza Burger") restaurant2 = Restaurant(name="Nana pizza") restaurant3 = Restaurant(name="seka pasta") session.add(restaurant1) session.add(restaurant2) session.add(restaurant3) session.commit()
def add_restaurant_to_db(name): restaurant_name = Restaurant(name = name) session.add(restaurant_name) session.commit() session.close() return restaurant_name
# Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Menu for UrbanBurger restaurant1 = Restaurant(name="Urban Burger") session.add(restaurant1) session.commit() menuItem2 = MenuItem( name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) session.add(menuItem2) session.commit() menuItem1 = MenuItem(name="French Fries",
def update_restaurant_info(data): """ update the restaurant infos """ # put in model from json for better validation, debug, test update_restaurant = Restaurant() update_restaurant.name = data["name"] update_restaurant.lat = data["lat"] update_restaurant.lon = data["lon"] update_restaurant.phone = data["phone"] update_restaurant.covid_measures = data["covid_measures"] update_restaurant.avg_time = data["avg_time"] update_restaurant.rating = data["rating"] update_restaurant.owner_email = data["owner_email"] update_restaurant.id = data["id"] db_session = current_app.config["DB_SESSION"] q = (db_session.query(Restaurant).filter_by( id=update_restaurant.id).update({ "name": update_restaurant.name, "lat": update_restaurant.lat, "lon": update_restaurant.lon, "phone": update_restaurant.phone, "covid_measures": update_restaurant.covid_measures, "avg_time": update_restaurant.avg_time, "rating": update_restaurant.rating, "owner_email": update_restaurant.owner_email, })) db_session.commit() db_session.flush() # return True if a restaurant was modified return q != 0
def create_restaurant(data, max_seats): """ This method creates a new restaurant """ rest = data["restaurant"] rest_name = rest["name"] lat = rest["lat"] lon = rest["lon"] rest_phone = rest["phone"] # add in restaurant table new_restaurant = Restaurant() new_restaurant.name = rest_name new_restaurant.lat = lat new_restaurant.lon = lon new_restaurant.phone = rest_phone new_restaurant.covid_measures = data["restaurant"]["covid_measures"] new_restaurant.avg_time = data["restaurant"]["avg_time"] new_restaurant.rating = data["restaurant"]["rating"] new_restaurant.owner_email = data["restaurant"]["owner_email"] db_session = current_app.config["DB_SESSION"] db_session.add(new_restaurant) db_session.commit() # add tables in RestaurantTable table number_tables = data["restaurant_tables"] for i in range(number_tables): RestaurantService.create_table("", max_seats, new_restaurant.id) # insert opening hours list_openings = data["opening"] for opening in list_openings: new_opening = OpeningHours() new_opening.restaurant_id = new_restaurant.id new_opening.week_day = opening["week_day"] time_info = opening["open_lunch"].split(":") new_opening.open_lunch = datetime.time(int(time_info[0]), int(time_info[1])) time_info = str(opening["close_lunch"]).split(":") new_opening.close_lunch = datetime.time(int(time_info[0]), int(time_info[1])) time_info = str(opening["open_dinner"]).split(":") new_opening.open_dinner = datetime.time(int(time_info[0]), int(time_info[1])) time_info = str(opening["close_dinner"]).split(":") new_opening.close_dinner = datetime.time(int(time_info[0]), int(time_info[1])) db_session.add(new_opening) db_session.commit() # insert menus for menu in data["menu"]: new_menu = Menu() new_menu.restaurant_id = new_restaurant.id new_menu.cusine = menu new_menu.description = "" db_session.add(new_menu) db_session.commit() return (db_session.query(Restaurant).filter_by( name=rest_name, lat=lat, lon=lon, phone=rest_phone).first())
# Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Menu for UrbanBurger restaurant1 = Restaurant(name="Urban Burger") session.add(restaurant1) session.commit() menuItem1 = MenuItems(name="French Fries", description="with garlic and parmesan", price="$2.99", course="Appetizer", restaurant=restaurant1) session.add(menuItem1) session.commit() menuItem2 = MenuItems( name="Chicken Burger",
from database import db_session, User, MenuItem, Restaurant, RestaurantAddress # Create fake user User1 = User(name="Robo Barista", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png') db_session.add(User1) db_session.commit() # Menu for UrbanBurger restaurant1 = Restaurant(user_id=1, name="Urban Burger", phone="1111112222", email="*****@*****.**", food_type="mix", website="www.test.com", description="test") db_session.add(restaurant1) db_session.commit() restaurantaddres = RestaurantAddress(restaurant_id=1, street="1 road", city="lansing", state="Michigan", zip_code="48910") db_session.add(restaurantaddres) db_session.commit() menuItem2 = MenuItem(user_id=1, name="Veggie Burger", description="Juicy grilled veggie patty with tomato mayo and lettuce", price="$7.50", course="Entree", restaurant=restaurant1) db_session.add(menuItem2) db_session.commit() menuItem1 = MenuItem(user_id=1, name="French Fries", description="with garlic and parmesan", price="$2.99", course="Appetizer", restaurant=restaurant1)
def add_restaurant(name): restaurant_name = Restaurant(name=name) session.add(restaurant_name) session.commit() print('Adding Success: %s' % name)