def create_movie(jwt): """ POST /movies it should create a new row in the movies table it should require the 'post:movies' permission it should contain the movie.long() data representation returns status code 200 and json {"success": True, "movies": movie} where drink an array containing only the newly created drink or appropriate status code indicating reason for failure """ try: data = request.get_json() movie = Movies(title=data.get('title', None), duration=data.get('duration', None), release_year=data.get('release_year', None)) movie.insert() movies = list(map(Movies.long, Movies.query.all())) return jsonify({ 'success': True, 'movie': movies, }), 200 except Exception: print(sys.exc_info()) abort(422)
def add_movie(payload): body = request.get_json() # get all the user's inputs new_title = body.get('title', None) new_date = body.get('date', None) # check the user's inputs if new_title == '' or new_date == '': # unprocessable error abort(422) try: movie = Movies(title=new_title, date=new_date) movie.insert() total = len(Movies.query.all()) return jsonify({ 'success': True, 'created': movie.id, 'totalMovies': total }) except BaseException: # unprocessable error abort(422)
def add_movie(payload): body = request.get_json() movie_title = body.get('title', None) movie_release_date = body.get('release_date', None) if not movie_title: abort(422, {'message': 'Title of movie not provided'}) if not movie_release_date: abort(422, {'message': 'Release date of movie not provided'}) try: movie = Movies(title=movie_title, release_date=movie_release_date) movie.insert() movies = Movies.query.order_by(Movies.id).all() total_movies = len(movies) last_movie = movies[total_movies - 1].format() # movies_formatted = [movie.format() for movie in movies] return jsonify({'success': True, 'movie_added': last_movie}) except BaseException: db.session.rollback() print(sys.exc_info()) abort(422, {'message': 'Failed to add new movie to the database'}) finally: db.session.close()
def insert_movie(payload): # Get data new_title = request.get_json()['title'] new_release_date = request.get_json()['release_date'] # check movie title for duplicate check_movie_title = Movies.query.filter( Movies.title.ilike('%{}%'.format(new_title))).all() if len(check_movie_title) > 0: raise Exception("Sorry, no duplicate movie titles allowed") abort(409) else: try: # Prepare INSERT transaction new_movie = Movies(title=new_title, release_date=new_release_date) new_movie.insert() # Display latest data selection = Movies.query.filter( Movies.title == new_title).first() return jsonify({ 'id': selection.id, 'title': selection.title, 'release_date': selection.release_date, 'success': True }), 200 except Exception: abort(422)
def create_movie(token): abort_code = None new_movie_name = None new_movie_releasedate = None body = request.get_json() if body is not None: new_movie_name = body.get('name', None) new_movie_releasedate = body.get('releasedate', None) try: if (new_movie_name is None or new_movie_releasedate is None): abort_code = 422 if abort_code is None: movie_to_insert = Movies( name=new_movie_name, releasedate=new_movie_releasedate) movie_to_insert.insert() return jsonify({ 'success': True, 'created': movie_to_insert.id, }) except BaseException: db.session.rollback() abort_code = 422 finally: db.session.close if abort_code: abort(abort_code)
def create_movie(jwt): ''' Create an endpoint to POST a new movie, which will require the title and release date ''' # Get the request body body = request.get_json() new_title = body.get('title') new_release_date = body.get('release_date') if ((new_title is None) or (new_release_date is None)): abort(422) try: # create the new movie movie = Movies(title=new_title, release_date=new_release_date) movie.insert() # return the new created movie if successfully created return jsonify({ 'success': True, 'movie': movie.format() }) # raise exception for any error during deleting the movie except: db.session.rollback() print(sys.exc_info()) abort(404)
def add_movies(jwt): #---get json data--# body = request.get_json() if body is None: abort(401) try: new_title = body.get('title', '') new_release_date = body.get('release_date', '') if not (new_title and new_release_date): abort(400) movie = Movies(title=new_title, release_date=new_release_date) movie.insert() return jsonify( { 'success': True, 'title': new_title, 'message': new_title + " Added successfully", 'id': movie.id }, 200) except Exception as e: print(e) abort(422)
def post_movie(payload): body = request.get_json() if not ('title' in body and 'release_date' in body): abort(422) new_title = body.get('title', None) new_release_date = body.get('release_date', None) if new_title is None or new_release_date is None: abort(400) try: movie = Movies(title=new_title, release_date=new_release_date) if Movies.query.filter_by(title=movie.title).count(): abort(409) movie.insert() return jsonify({ 'success': True, 'movie': movie.format(), 'total_movies': len(Movies.query.all()) }) except AttributeError: abort(422)
def post_movie(token): post_data = request.get_json() if 'title' in post_data and 'release_date' in post_data: try: new_movie = Movies(title=post_data['title'], release_date=post_data['release_date']) if 'actors' in post_data: actors_list = post_data['actors'] for actor in actors_list: actor_id = actor['id'] actor_data = Actors.query.get(actor_id) if actor_data: movie_set = Sets(movie_id=new_movie.id, actor_id=actor_id) movie_set.insert() new_movie.insert() return jsonify({ 'id': new_movie.id, 'success': True }) except SQLAlchemyError: print(sys.exc_info()) db.session.rollback() abort(400) abort(400)
def create_movie(): try: required_props = [ 'title', 'release_date' ] optional_props = [ 'actors' ] movie_data = {} for prop in required_props: movie_data[prop] = request.get_json()[prop] for prop in optional_props: if prop in request.get_json(): movie_data[prop] = request.get_json()[prop] movie_data = validate_movie(**movie_data) movie = Movies(**movie_data) movie.insert() response = { 'success': True, 'movies': [movie.format()] } except UnprocessableEntity: raise except Exception: print(sys.exc_info()) raise BadRequest return jsonify(response)
def add_movie(): data = request.get_json() new_title = data.get('title', None) new_release_date = data.get('release_date', None) new_image_link = data.get('image_link', None) new_description = data.get('description', None) new_genres = data.get('genres', None) try: movie = Movies(title=new_title, release_date=new_release_date, description=new_description, image_link=new_image_link, genres=new_genres) movie.insert() selection = Movies.query.order_by('id').all() flash(movie.title + 'successfully added') return jsonify({ 'success': True, 'message': 'Movie ' + movie.title + ' successfully added', 'movies': len(selection) }) except Exception: return json.dumps({ 'success': False, 'error': 'Not able to add movie at this time', }), 422
def post_movie(token): data = request.get_json() new_movie = Movies(title=data['title'], release_date=data['release_date']) new_movie.insert() return jsonify({'movie ': new_movie.format(), 'success': True})
def create_movie_submission(): # TODO: insert form data as a new Movie record in the db, instead # TODO: modify data to be the data object returned from db insertion title = request.form['title'] release_date = request.form['release_date'] facebook_link = request.form['facebook_link'] image_link = request.form['image_link'] website = request.form['website'] seeking_talent = request.form['seeking_talent'] seeking_description = request.form['seeking_description'] if (seeking_talent == "True"): seeking_talent = bool(seeking_talent) else: seeking_talent = bool() error = False try: movie = Movies(title=title, release_date=release_date) movie.insert() except: error = True print(sys.exc_info()) if error: flash('An error occurred. Movie ' + movie.title + ' could not be listed.') else: flash('Movie ' + request.form['title'] + ' was successfully listed!') # on successful db insert, flash success # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ return render_template('pages/home.html')
def test_get_movies(self): movie = Movies(title='The Last Man Standing', release_date='12-21-23 12:00 pm') movie.insert() response = self.client().get('/movies', headers=self.assistant) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertEqual(data['success'], True)
def create_movie(payload): body = request.get_json() new_movie = Movies(title=body.get('title', ''), release_date=body.get('release_date', '')) new_movie.insert() return jsonify({'status': 'successful'}), 200
def patch(self, user_type): """" Casting Assistants cannot edit actors nor movies """ data = ['actors', 'movies'] for entity_type in data: if entity_type == 'actors': actor_data = {"name": "Clarece", "gender": "f", "age": 88} actor = Actors(**actor_data) actor.insert() actors_response = self.user_patch(user_type, entity_type, 1, {"name": "Amanda"}) if user_type is 'CA': self.assertEqual(actors_response.json['code'], 401) else: self.assertEqual( actors_response.json, { f'{entity_type}': [{ 'name': 'Amanda', 'gender': 'Female', 'id': 1, 'age': 88, 'movies': [] }], 'success': True }) if entity_type == 'movies': movie_data = { "title": "The Big One", "release_date": datetime.fromisoformat('2020-03-22 22:23:11') } movie = Movies(**movie_data) movie.insert() movies_response = self.user_patch(user_type, entity_type, 1, {"title": "The Little One"}) if user_type is 'CA': self.assertEqual(movies_response.json['code'], 401) else: self.assertEqual( movies_response.json, { 'success': True, 'movies': [{ 'id': 1, 'title': "The Little One", 'release_date': 'Sun Mar 22 22:23:11 2020', 'actors': [] }] })
def test_get_movies(self): movie = Movies(title='the walking dead', release_date='23-01-22') movie.insert() res = self.client().get( '/movies', headers={'Authorization': 'Bearer ' + ASS_TOKEN}) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def test_delete_movie(self): movie = Movies(title='the walking dead S4', release_date='23-07-22') movie.insert() res = self.client().delete( f'/movies/{movie.id}', headers={'Authorization': 'Bearer ' + EXE_TOKEN}) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def create_movie(payload): body = request.get_json() if "title" not in body: abort(422) try: new_movie = Movies(title=body["title"], release_date=body["release_date"]) new_movie.insert() return jsonify({'success': True, 'movie': new_movie.format()}), 200 except BaseException: abort(400)
def test_delete_movie(self): movie = Movies(title='The Last Man Standing', release_date='12-21-23 12:00 pm') movie.insert() movie_id = movie.id response = self.client().delete('/movies/' + str(movie_id) + '', headers=self.producer) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertEqual(data['success'], True)
def add_movie(): data = request.get_json() new_title = data.get('title', None) new_release_date = data.get('release_date', None) new_genre = data.get('genre', None) new_movie = Movies(title=new_title, release_date=new_release_date, genre=new_genre) Movies.insert(new_movie) list_of_movies = Movies.query.all() return jsonify({'success': True, 'movies': list_of_movies})
def test_400_update_movie(self): movie = Movies(title='The Last Man Standing', release_date='12-21-23 12:00 pm') movie.insert() movie_id = movie.id response = self.client().patch('/movies/' + str(movie_id) + '', headers=self.director) data = json.loads(response.data) self.assertEqual(response.status_code, 400) self.assertEqual(data['success'], False)
def test_delete_movies(self): new_movie = Movies(title='the girl', release='1988') new_movie.insert() movie_id = new_movie.id res = self.client().delete( '/movies/{}'.format(movie_id), headers={'Authorization': 'Bearer ' + test_token}) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def insert_Movies(payload): body = request.get_json() try: movie = Movies(name=body['name'], length=body['length'], genre=body['genre']) movie.insert() return jsonify({'success': True}) except Exception: abort(404)
def test_update_movie(self): movie = Movies(title='the walking dead', release_date='23-01-22') movie.insert() new_movie = {'title': 'whatever0', 'release_date': '28-1-21'} res = self.client().patch( '/movies/2', json=new_movie, headers={'Authorization': 'Bearer ' + DIR_TOKEN}) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def add_movie(permission): data = request.get_json() movie = Movies(title=data['title'], release_date=data['release_date'], image_link=data['image_link']) movie.insert() movie = Movies.query.order_by(Movies.id.desc()).first() return jsonify({ 'success': True, 'id': movie.id, 'message': 'Movie ' + movie.title + ' added successfully.' })
def add_movie(payload, *args, **kwargs): body = request.get_json() title = body.get('title') release_date = body.get('release_date') try: new_movie = Movies(title=title, release_date=release_date) new_movie.insert() movies = get_movies() return jsonify({"success": True, "movies": movies}) except Exception: abort(400)
def post_movie(payload): body = request.get_json() name = body.get('name') rdate = body.get('rdate') try: movie = Movies(name=name, rdate=rdate) Movies.insert(movie) except BaseException: abort(404) return jsonify({ 'status': 'Successfully added a movie', 'success': True }), 201
def add_new_movie(): try: if request.method == 'PUT': abort(405) else: body = request.get_json() title = body.get('title') release_date = body.get('release_date') movie = Movies(title=title, release_date=release_date) movie.insert() except Exception: abort(422) return jsonify({'success': True, 'added_movie': movie.format()})
def post_movie_details(payload): body = request.get_json() title = body['title'] release_date = body['release_date'] rating = body['rating'] actor = body['actor'] new_movie = Movies(title=title, release_date=release_date, actor=actor, rating=rating) new_movie.insert() new_movie_data = Movies.query.order_by(-Movies.id).first().format() return jsonify({'success': True, 'movies': new_movie_data})