def api_movie_view(request, id=None): #SELECT * if request.method == 'GET' and id == None: try: movie = Movie.objects.all() except Movie.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = MovieSerializer(movie, many=True) return Response(serializer.data) #SELECT id if request.method == 'GET' and id != None: try: movie = Movie.objects.get(id = id) except Movie.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = MovieSerializer(movie) return Response(serializer.data) #UPDATE if request.method == 'PUT': try: movie = Movie.objects.get(id=id) except Movie.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = MovieSerializer(movie, data=request.data) data = {} if serializer.is_valid(): serializer.save() data["success"] = "update succesful" return Response(data=data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) #INSERT if request.method == 'POST': movie = Movie() serializer = MovieSerializer(movie, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) #DELETE if request.method == 'DELETE': try: movie = Movie.objects.get(id=id) except Movie.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) operation = movie.delete() data = {} if operation: data['success'] = 'delete succesful' else: data['failure'] = 'delete failes' return Response(data=data)
def forge(): db.create_all() name = "aaa" movies = [ {'title':'功夫之王','year':'2010'}, {'title':'机器之血','year':'2015'}, {'title':'复仇者联盟3','year':'2017'}, {'title':'微微一笑很倾城','year':'2018'}, {'title':'百鸟朝凤','year':'2019'}, {'title':'唐人街探案3','year':'2020'}, {'title':'杀破狼','year':'2010'}, {'title':'扫毒','year':'2010'}, {'title':'机器之血','year':'2010'}, {'title':'分手大师','year':'2010'}, {'title':'这个杀手不太冷','year':'2010'}, {'title':'邻里的人们','year':'2010'}, {'title':'釜山行','year':'2010'}, {'title':'拯救大兵瑞恩','year':'2010'}, {'title':'我的特工爷爷','year':'2010'}, {'title':'战狼','year':'2010'} ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'],year=m['year']) db.session.add(movie) db.session.commit() click.echo('导入数据完成')
def setUp(self): #调用app app.config.update( #处于测试环境 TESTING=True, #用缓存数据库 memory 内存数据库 SQLALCHEMY_DATABASE_URI="sqlite:///:memory:") # 往库中 db.create_all() user = User(name="Test", username="******") user.set_password("123456") movie = Movie(title="Test Movie Title", year="2020") #add一条上传 add_all 多条列表上传 db.session.add_all([user, movie]) db.session.commit() # 创建一个测试用的客户端模拟 浏览器 self.client = app.test_client() #创建测试命令运行器 self.runner = app.test_cli_runner()
def forge(): db.create_all() movies = [ { 'title': 'My Neighbor Totoro', 'year': '1988' }, { 'title': 'Dead Poets Society', 'year': '1989' }, { 'title': 'A Perfect World', 'year': '1993' }, { 'title': 'Leon', 'year': '1994' }, { 'title': 'Mahjong', 'year': '1996' }, { 'title': 'Swallowtail Butterfly', 'year': '1996' }, { 'title': 'King of Comedy', 'year': '1999' }, { 'title': 'Devils on the Doorstep', 'year': '1999' }, { 'title': 'WALL-E', 'year': '2008' }, { 'title': 'The Pork of Music', 'year': '2012' }, ] for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() # 输出提示信息 click.echo('Done')
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') 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=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 index(): # user = User.query.first() # 读取用户记录 if request.method == 'POST': if not current_user.is_authenticated: return redirect(url_for('index')) title = request.form.get('title') year = request.form.get('year') # 验证数据不为空,year长度不能超过4,title长度不能超过60 if not title or not year or len(year) > 4 or len(title) > 60: flash('输入错误') return redirect(url_for('index')) movie = Movie(title=title, year=year) db.session.add(movie) db.session.commit() flash('数据插入成功') return redirect(url_for('index')) movies = Movie.query.all() # 读取所有的电影记录 return render_template('index.html', movies=movies)
def setUp(self): # 更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据 user = User(name='Test1', username='******') user.set_password('123456') 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 forge(): db.create_all() name = "ZY" movies = [{ 'title': '杀破狼', 'year': '2003' }, { 'title': '扫毒', 'year': '2018' }, { 'title': '捉妖记', 'year': '2016' }, { 'title': '囧妈', 'year': '2020' }, { 'title': '葫芦娃', 'year': '1989' }, { 'title': '玻璃盒子', 'year': '2020' }, { 'title': '调酒师', 'year': '2020' }, { 'title': '釜山行', 'year': '2017' }, { 'title': '导火索', 'year': '2005' }, { 'title': '叶问', 'year': '2015' }] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo('导入数据完成')
def index(): if request.method == 'POST': if not current_user.is_authenticated: return redirect(url_for('index')) # request在请求触发的时候才会包含数据 title = request.form.get('title') year = request.form.get('year') # 验证数据 if not title or not year or len(year) > 4 or len(title) > 60: flash('不能为空或超过最大长度') return redirect(url_for('index')) # 保存表单数据 movie = Movie(title=title, year=year) db.session.add(movie) db.session.commit() flash('创建成功') return redirect(url_for('index')) movies = Movie.query.all() return render_template('index.html', movies=movies)
def index(): if request.method == 'POST': if not current_user.is_authenticated: return redirect(url_for('index')) # 获取表单的数据 title = request.form.get('title') year = request.form.get('year') # 验证title,year不为空,并且title长度不大于60,year的长度不大于4 if not title or not year or len(year) > 4 or len(title) > 60: flash('输入错误') # 错误提示 return redirect(url_for('index')) # 重定向回主页 movie = Movie(title=title, year=year) # 创建记录 db.session.add(movie) # 添加到数据库会话 db.session.commit() # 提交数据库会话 flash('数据创建成功') return redirect(url_for('index')) movies = Movie.query.all() return render_template('index.html', movies=movies)
def forge(): db.create_all() name = "Bruce" movies = [ {'title':'杀破狼','year':'2010'}, {'title':'扫毒','year':'2010'}, {'title':'机器之血','year':'2010'}, {'title':'分手大师','year':'2010'}, {'title':'这个杀手不太冷','year':'2010'}, {'title':'邻里的人们','year':'2010'}, {'title':'釜山行','year':'2010'}, {'title':'拯救大兵瑞恩','year':'2010'}, {'title':'我的特工爷爷','year':'2010'}, {'title':'战狼','year':'2010'} ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'],year=m['year']) db.session.add(movie) db.session.commit() click.echo('导入数据完成')
def index(): if request.method == 'POST': print(current_user.is_authenticated) if not current_user.is_authenticated: # 如果当前用户未认证 return redirect(url_for('index')) # 获取表单信息 title = request.form.get('title') year = request.form.get('year') if not title or not year or len(year) > 4 or len(title) > 60: flash('Invalid input.') return redirect(url_for('index')) movie = Movie(title=title, year=year) db.session.add(movie) db.session.commit() flash('Item created.') return redirect(url_for('index')) movies = Movie.query.all() return render_template('blog/index.html', active_page='home', movies=movies)
def movie_post(request): if request.method == 'POST': title = request.POST['movie_title'].title() genre = request.POST['genre'] director = request.POST['director'] cast = request.POST['cast'] val = requests.get('http://www.omdbapi.com/?s=' + title + '&apikey=514904fe') if val: movie = val.content.decode("utf-8") search = literal_eval(movie) search_list = search.get("Search") if search_list: year = '' poster_image = '' type_ = '' for search in search_list: if search["Title"] == title: poster_image = search.get('Poster') year = search.get('Year') type_ = search.get('Type') obj = Movie() obj.title = title obj.year = year obj.type = type_ obj.image = poster_image obj.director = director obj.genre = genre obj.cast_member = cast if Movie.objects.filter(title=title): messages.warning(request, "Movie Already Exist!!") return redirect("movie:home") else: obj.save() return redirect('movie:dd') else: messages.warning(request, 'Movie not found!!!') return redirect('movie:home')