def create_genre(payload): data = request.get_json() type = data.get('type') if not type: abort(422) # prevent duplicate genres exists = Genre.query.filter_by(type=type).all() # what would be sutable HTTP error code ! if exists: return jsonify({ 'success': False, 'message': 'Genre already exists' }) try: new_genre = Genre(type=type) new_genre.insert() return jsonify({ 'success': True, 'message': 'New genre has been Created' }), 200 except Exception: print(sys.exc_info()) abort(422)
def add_genre(): data = request.get_json() try: new_genre = Genre(data['name']) if('description' in data): new_genre.description = data['description'] except: abort(400) success = new_genre.insert() if(success): inserted_genre = Genre.query.filter_by(name= data['name']).all()[0].format() return jsonify({ 'success': True, 'new_genre': inserted_genre }) else: abort(500)
def update(self): """ Sync the local DB with IMDB :return: None """ self.download() self.decompress() ids = self.get_ids() new_ids = self.get_new_movies(ids) # For each new movie id get it's data and store in the DB for id in new_ids: movie_data = self.get_movie_info(id) if movie_data: print(movie_data) # distribute this info to respective tables movie = Movie(db) movie.insert([movie_data['id'], movie_data['title'], movie_data['year'], movie_data['runtime'][0] if movie_data['runtime'] else None, movie_data['budget'], movie_data['rating'], movie_data['link'], movie_data['director'][0]['name'] if movie_data['director'] else None]) # Add actor to actor table actors = movie_data['actors'] # Add actor to movie actor relation table ma = MovieActor(db) if actors: for actor in actors: try: fname, lname = actor['name'].split(' ', 2) except ValueError: fname = actor['name'] lname = None id = actor.getID() a = Actor(db) try: if not a.select_one(id): a.insert([id, fname, lname]) except Exception: pass try: # for each actor in the movie add relation movieid -> actorid ma.insert([movie_data['id'], id]) except Exception: pass # Add production company to table companies = movie_data['production company'] if companies: for company in companies: id = company.getID() name = company['name'] c = Production(db) try: if not c.select_one(id): c.insert([id, name]) except Exception: pass # Add this movie to production movie relation table pm = ProductionMovie(db) try: pm.insert([id, movie_data['id']]) except Exception: pass # Add genre to table genres = movie_data['genre'] if genres: g = Genre(db) for genre in genres: try: if not g.select_one(genre): g.insert([genre]) except Exception: pass # Add movie to genre movie relation table gm = GenreMovie(db) try: gm.insert([g.select_one(genre)['GN_ID'], movie_data['id']]) except Exception: pass