def addMovie(): if request.method == 'POST': title = request.form['title'] director = request.form['director'] actors = request.form['actors'] description = request.form['description'] error = None if not title: error = "Title is required" elif not director: error = "Director is required" elif not actors: error = "Actors are required" elif not description: error = "Description is required" if error is None: movie = Movie(title=title, director=director, actors=actors, description=description) db.session.add(movie) db.session.commit() return redirect(url_for('admin.viewMovies')) return render_template('admin/movie_form.html')
def test_movie_create(self): m = Movie.get_or_create("Monty Python and the Holy Test") db.session.add(m) db.session.commit() ms = Movie.query.filter_by(title="Monty Python and the Holy Test").all() assert len(ms) == 1 assert ms[0].title == "Monty Python and the Holy Test"
def init_db(): db.drop_all() db.create_all() df = process_csv(os.path.join(dirname, "fake_data.csv")) for _, row in df.iterrows(): movie = Movie(name=row["name"], description=row["description"], img="https://source.unsplash.com/random", price=row["price"]) db.session.add(movie) db.session.commit()
def _populate_db(): #Populate database with 3 movies and 1 uploader: for i in range(1, 4): u = Uploader(uploader_name="Niko", email="gmail") s = Movie(name="RAMBO{}".format(i), genre="action", uploader=u) db.session.add(s) db.session.commit()
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', username='******') user.set_password('123') movie = Movie(title='Test Movie Title', year='2019') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端 self.runner = app.test_cli_runner() # 创建测试命令运行器
def test_comment_create(self): m = Movie.get_or_create("Monty Python and the Holy Test") u = User.create("commenter", "*****@*****.**", "asdfasdf") db.session.add(m) db.session.add(u) db.session.commit() c = Comment(user_id=u.id, contents="4/5. Lots of good testing moments.") m.add_comment(c) comments = Comment.query.filter_by(user_id=u.id, movie_id=m.id).all() assert len(comments) == 1 assert "good testing moments" in comments[0].contents
def setUp(self) -> None: # update config app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', account_name='admin_test') user.set_password('456') movie = Movie(title='Test Movie Title', year=2020) db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() self.runner = app.test_cli_runner()
def setUp(self): # 更新配置 app.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影条目 user = User(name='Nicolas Xiong', username='******') user.set_password('19990909xzc') movie = Movie(title='Test Movie Title', year='2019') # 使用 add_all() 方法一次添加多个模型类实例,传入列表 db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端 self.runner = app.test_cli_runner() # 创建测试命令运行器
def setUp(self) -> None: # 更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABSE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影条目 user = User(name="Test", username='******') user.set_password('123') movie = Movie(title='Test Movie Title', year='2019') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() self.runner = app.test_cli_runner()
def setUp(self): # 更新配置 在开发和测试时候通常配置是不一样 app.config.update( TESTING=True, # 开启测试模式 出错时候不会输出多余的信息 SQLALCHEMY_DATABASE_URI= 'sqlite:///:memory:' # SQLite内存型数据库,不会干扰开发时使用的数据库文件 ) # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影信息 user = User(name='Test', username='******') user.set_password('123456') movie = Movie(title='测试电影名称', year='2020') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端(模拟客户端请求) self.runner = app.test_cli_runner() # 创建测试命令运行器(触发自定义命令)
def setUp(self): # 更新配置 app.config.update( # 測試時狀態TESTING打開可以過濾一些多餘的訊息 TESTING=True, # 改使用memory當作db做測試,不會用到開發時的db SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 用來創建db和表 db.create_all() user = User(name='Test', username='******') user.set_password('123') movie = Movie(title='Test Movie Title', year='2019') db.session.add_all([user, movie]) db.session.commit() # 創建測試客服端,來模擬瀏覽器,如果調用get()相當於對server發送GET請求 self.client = app.test_client() self.runner = app.test_cli_runner() # 創建測試命令行
def setUp(self): # 更新配置 app.config.update( # 开启测试模式 TESTING=True, # 启用SQLite内存型数据库,不会干扰开发时使用的数据文件 SQLALCEHMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据:一个用户和一个电影条目 user = User(name='Test', username='******') user.set_password('123') movie = Movie(title='Test Movie Title', year='2019') # 使用add_all()方法一次添加多个模型实例 db.session.add_all([user, movie]) db.session.commit() # 创建测试客户端, 用来模拟客户端请求 self.client = app.test_client() # 创建测试命令运行器,用来触发自定义命令 self.runner = app.test_cli_runner()
from app import User, Movie from app import db user = User(name='gao') m1 = Movie(title='1', year='1994') m2 = Movie(title='2', year='1994') # 就是因为之前的错误引用导致的读取错误! db.session.add(user) db.session.add(m1) db.session.add(m2) # 注意这里只是加入了会话 db.session.commit()
from app import db, Movie, Genre from scrape import movie_dict, Genres genres_dict = {} ind = 1 # create genre table for genre in Genres: genres_dict[genre] = str(ind) ind += 1 to_add = Genre(name=genre) db.session.add(to_add) db.session.commit() for key, value in movie_dict.items(): director = value['director'] #dire = director.split("\")[0] movie = Movie(name=value['name'], released_year=value['released_year'], director=director, one_sentence=value['one_sentence']) genre_list = [] for genre in value['genres']: id_table = int(genres_dict[genre]) item = Genre.query.filter_by(id=id_table).one_or_none() genre_list.append(item) #print(len(genre_list)) movie.genres = genre_list db.session.add(movie) db.session.commit()
from app import User, Movie, db # 导入是从项目根目录开始的,要在加一级文件夹 user = User(name='JMJ') # 创建一个 User 记录 m1 = Movie(title='Leon', year='1994') # 创建一个 Movie 记录 m2 = Movie(title='Mahjong', year='1996') # 再创建一个 Movie 记录 db.session.add(user) # 把新创建的记录添加到数据库会话 db.session.add(m1) db.session.add(m2) db.session.commit() # 提交数据库会话,只需要在最后调用一次即可
from app import db, User, Movie user = User(name='Grey Li') m1 = Movie(title='Leon',year='1994') m2 = Movie(title='Mahjong', year='1996') db.session.add(user) db.session.add(m1) db.session.add(m2) db.session.commit()
#initializing Genre class before commiting _genre = {} try: db.create_all() except Exception as e: print(e) pass for i in a: df = {i: Genre(which_genre=i)} _genre.update(df) #initializing Movie class and adding to session ready for commit for i in _raw_data: kl = Movie(name=i["name"], director=i["director"], imdb_score=i["imdb_score"], popularity=i["99popularity"]) db.session.add(kl) #initializing Genre class and adding to session ready for commit for p, q in _genre.items(): db.session.add(q) #commiting everything in session db.session.commit() #committing Relationship for i in _raw_data: kl = Movie(name=i["name"], director=i["director"], imdb_score=i["imdb_score"], popularity=i["99popularity"]) for l in i["genre"]:
from app import db from app import Movie import pandas as pd df = pd.read_csv('movies.csv', sep=',') db.drop_all() db.create_all() for d in df.values: try: id, title = d[0], d[1] movie = Movie(id, title) db.session.add(movie) except: print('failed in {}'.format(d[0])) db.session.commit() print("Done loading data")