def signup(self): parser = RequestParser(trim=True, bundle_errors=True) parser.add_argument('email', required=True, help='The email field is required.') parser.add_argument('first_name', required=True, help='The first name field is required.') parser.add_argument('last_name', required=True, help='The last name field is required.') parser.add_argument('password', required=True, help='The password field is required.') args = parser.parse_args() if not re.match( r"^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$", args.email): return api_resp(400, 'error', {'email': 'Please enter a valid email address.'}) if User.query.filter_by(email=args.email).first(): return api_resp(400, 'error', {'email': 'Email has already been taken.'}) if len(args.password) < 5: return api_resp( 400, 'error', {'password': '******'}) user = User(args.email, args.password, args.first_name, args.last_name) user.is_active = True if user.save(): return api_resp(200, 'success', {'detail': 'Account created successfully.'}) return api_resp(409, 'error', {'detail': 'There has been some error.'})
def put(self, movie_id): args = reqparse.RequestParser()\ .add_argument('name').add_argument('imdb_score')\ .add_argument('popularity').add_argument('director')\ .add_argument('genres', action='append').parse_args() movie = MovieModel.query.filter_by(id=movie_id).first() if movie: director = Director.query.filter_by(name=args['director']).first() if not director: director = Director() director.name = args['director'] director.save() genres = [] if args.get('genres'): for genre_name in args.get('genres'): if genre_name.strip(): genre = Genre.query.filter_by(name=genre_name.strip()).first() if not genre: genre = Genre() genre.name = genre_name.strip() genre.save() genres.append(genre) else: genres.append(genre) GenreMovie.query.filter_by(movie_id=movie_id).delete() movie.name = args.get('name').strip() movie.imdb_score = args.get('imdb_score') movie.popularity = args.get('popularity') movie.director = director if len(genres): movie.genres.extend(genres) if movie.save(): return api_resp(200, 'success', {'movie': movie.serialize}) return api_resp(404, 'error', {'detail': 'No record found.'})
def delete(self, movie_id): movie = MovieModel.query.filter_by(id=movie_id).first() if movie: GenreMovie.query.filter_by(movie_id=movie_id).delete() MovieModel.query.filter_by(id=movie_id).delete() db.session.commit() return api_resp(200, 'success', {'detail': 'Movie deleted successfully.'}) return api_resp(404, 'error', {'detail': 'No record found.'})
def post(self): data_file = request.files.get('import_data') if data_file: filename = secure_filename(data_file.filename) filename = random_generator(40) + '.' + filename.rsplit( '.', 1)[1].lower() file_path = os.path.join('./storage', filename) data_file.save(file_path) data = open(file_path, 'r') data = json.load(data) for row in data: director_name = row.get('director').strip() director_exist = Director.query.filter_by( name=director_name).first() if not director_exist: director = Director() director.name = director_name director.save() for genre_name in row.get('genre'): genre_exist = Genre.query.filter_by( name=genre_name.strip()).first() if not genre_exist: genre = Genre() genre.name = genre_name.strip() genre.save() for row in data: movie = Movie() movie.name = row.get('name').strip() movie.imdb_score = row.get('imdb_score') movie.popularity = row.get('99popularity') director = Director.query.filter_by( name=row.get('director').strip()).first() movie.director = director genres = [genre.strip() for genre in row.get('genre')] if len(genres): genres = Genre.query.filter(Genre.name.in_(genres)) movie.genres.extend(genres) movie.save() if os.path.isfile(file_path): os.remove(file_path) return api_resp(200, 'success', {'detail': 'Data imported successfully.'}) return api_resp(409, 'error', {'detail': 'No file found.'})
def login(self): parser = RequestParser(trim=True, bundle_errors=True) parser.add_argument('email', required=True, help='The email field is required.') parser.add_argument('password', required=True, help='The password field is required.') args = parser.parse_args() user = User.find_user(_email=args.email) if user: if user.check_password(args.password) and user.is_active: token = jwt.encode( { 'user_id': user.id, 'exp': datetime.utcnow() + timedelta(days=30), 'iat': datetime.utcnow() }, SECRET_KEY) return api_resp(200, 'success', {'access_token': token.decode('UTF-8')}) return api_resp( 400, 'error', {'email': 'Incorrect email address and / or password.'})
def get(self): args = reqparse.RequestParser().add_argument('page').add_argument('director') \ .add_argument('genre').add_argument('name').add_argument('imdb_score').parse_args() page = int(args['page']) if args['page'] else None movies = Movie.query if args['name']: movies = movies.filter(Movie.name.like("%"+args['name'].strip()+"%")) if args['imdb_score']: movies = movies.filter(Movie.imdb_score >= args['imdb_score']).order_by(Movie.imdb_score.desc()) if args['director']: movies = movies.join(Director).filter(Director.name.like("%"+args['director'].strip()+"%")) if args['genre']: movies = movies.join(GenreMovie).join(Genre).filter(Genre.name.like("%"+args['genre'].strip()+"%")) movies = movies.paginate(per_page=33, page=page, error_out=False) serialize_movies = [movie.serialize for movie in movies.items] return api_resp(200, 'success', {'movies': serialize_movies, 'total': movies.total, 'pages': movies.pages, 'current_page': movies.page, 'has_prev': movies.has_prev, 'has_next': movies.has_next})
def get(self, movie_id): movie = MovieModel.query.filter_by(id=movie_id).first() if movie: return api_resp(200, 'success', {'movie': movie.serialize}) return api_resp(404, 'error', {'detail': 'No record found.'})
def get(self): args = reqparse.RequestParser().add_argument('page').parse_args() page = int(args['page']) if args['page'] else None movies = Movie.query.paginate(per_page=3, page=page, error_out=False) serialize_movies = [movie.serialize for movie in movies.items] return api_resp(200, 'success', {'movies': serialize_movies, 'total': movies.total, 'pages': movies.pages, 'current_page': movies.page, 'has_prev': movies.has_prev, 'has_next': movies.has_next})