def post(self): data = request.get_json() mandatory_fields = ["imdb_id"] if any(data.get(item) is None for item in mandatory_fields): return ResponseGenerator.mandatory_field(fields=mandatory_fields) fav_movie = UserRepository.add_user_favorite_movie(user=current_user, imdb_id=data["imdb_id"]) if not fav_movie: return ResponseGenerator.generate_response(data={"msg": "disliked"}, code=204) fav_movie = MovieRepository.get_movie_info(fav_movie) return ResponseGenerator.generate_response(data=fav_movie, code=201)
def update_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() data = request.get_json() if not user: return ResponseGenerator.not_found() if data['admin'] == "True": admin = True else: admin = False user.name = data['name'] user.password = data['password'] user.email = data['email'] user.admin = admin update_user = User(name=user.name, email=user.email, password=user.password, admin=user.admin) User.update(update_user) return ResponseGenerator.generate_response( f"{update_user.name} has been successfully updated", 200)
def create_todo(current_user): data = request.get_json() new_todo = Todo(text=data['text'], complete=False, user_id=current_user.id) Todo.save(new_todo) return ResponseGenerator.generate_response( f"{new_todo.text} has been created!", 200)
def todo_create(): data = request.get_json() new_todo = TodoRepository.create_todo(data['title'], data['isCompleted']) return ResponseGenerator.generate_response( { 'title': new_todo.title, 'isCompleted': new_todo.isCompleted }, 200)
def delete_todo(current_user, todo_id): todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first() if not todo: return ResponseGenerator.not_found() Todo.delete(todo) return ResponseGenerator.generate_response( f"{todo.text} has been Deleted!", 200)
def user_register(): data = request.get_json() new_user = UserRepository.create_user(data['name'], data['email'], data['password']) print(new_user) return ResponseGenerator.generate_response( { 'name': new_user.name, 'email': new_user.email, }, 200)
def user_login(): data = request.get_json() existing_user = UserRepository.check_password(data['email'], data['password']) if existing_user: token = TokenGenerator.generate_jwt({ 'name': existing_user.name, 'email': existing_user.email }) return ResponseGenerator.generate_response({'token': token}, 200) return ResponseGenerator.user_login_failed()
def get(self): mandatory_fields = ["q"] if any( request.args.get(item, default=None) is None for item in mandatory_fields): return ResponseGenerator.mandatory_field(fields=mandatory_fields) query = request.args.get("q", default=None, type=str) page = request.args.get("page", default=1, type=int) movies = MovieSearchRepository.search(search_query=query, page=page) return ResponseGenerator.generate_response(data=movies, code=200)
def get_one_todo(current_user, todo_id): todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first() if not todo: return jsonify({'message': 'No todo found!'}) todo_data = {} todo_data['id'] = todo.id todo_data['text'] = todo.text todo_data['complete'] = todo.complete todo_data['user_id'] = todo.user_id return ResponseGenerator.generate_response(todo_data, 200)
def delete_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() if not user: return ResponseGenerator.not_found() User.delete(user) return ResponseGenerator.generate_response( f"{user.name} deleted successfully from DB.", 200)
def get_all_todos(current_user): todos = Todo.query.filter_by(user_id=current_user.id).all() output = [] for todo in todos: todo_data = {} todo_data['id'] = todo.id todo_data['text'] = todo.text todo_data['complete'] = todo.complete todo_data['user_id'] = todo.user_id output.append(todo_data) return ResponseGenerator.generate_response(output, 200)
def get_one_user(current_user, email): if not current_user.admin: return ResponseGenerator.not_authorized() user = User.query.filter_by(email=email).first() if not user: return ResponseGenerator.not_found() user_data = {} user_data['id'] = user.id user_data['name'] = user.name user_data['email'] = user.email user_data['admin'] = user.admin return ResponseGenerator.generate_response(user_data, 200)
def get_all_users(current_user): if not current_user.admin: return ResponseGenerator.not_authorized() users = User.query.all() output = [] for user in users: user_data = {} user_data['id'] = user.id user_data['name'] = user.name user_data['email'] = user.email user_data['admin'] = user.admin output.append(user_data) return ResponseGenerator.generate_response(output, 200)
def post(self): data = request.get_json() mandatory_fields = ["name", "email", "password"] if any(data.get(item) is None for item in mandatory_fields): return ResponseGenerator.mandatory_field(fields=mandatory_fields) name, email, password = data.pop("name"), data.pop("email"), data.pop("password") user = UserRepository.create_user(name=name, email=email, password=password, **data) if not user: return ResponseGenerator.error_response(msg="Email is already registered, try to login", code=400) access_token = UserToken.create_user_access_token(user=user) return ResponseGenerator.generate_response({ "access_token": access_token }, code=201)
def post(self): data = request.get_json() email = data["email"] password = data["password"] user = UserRepository.get_by_email(email=email) if not user: return ResponseGenerator.not_found(msg="user not found") if not user.check_password(password=password): return ResponseGenerator.forbidden( msg="email/password combination is invalid") access_token = UserRepository.create_user_access_token(user=user) return ResponseGenerator.generate_response( {"access_token": access_token}, code=200)
def post(self): data = request.get_json() mandatory_fields = ["email", "password"] if any(data.get(item) is None for item in mandatory_fields): return ResponseGenerator.mandatory_field(fields=mandatory_fields) email = data["email"] password = data["password"] user = UserRepository.get_by_email(email=email) if not user: return ResponseGenerator.not_found(msg="user not found") if not user.check_password(password=password): return ResponseGenerator.forbidden(msg="email/password combination is invalid") access_token = UserToken.create_user_access_token(user=user) return ResponseGenerator.generate_response({ "access_token": access_token }, code=200)
def create_user(): data = request.get_json() email = data['email'] user = User.query.filter_by(email=email).first() if user: return ResponseGenerator.error_response( f"Already {email} exist in DB. Try using other email address", 409) if data['admin'] == "True": admin = True else: admin = False new_user = User(name=data['name'], email=data['email'], password=data['password'], admin=admin) User.save(new_user) return ResponseGenerator.generate_response(data, 200)
def home(): return ResponseGenerator.generate_response("hello", 200)
def about(): return ResponseGenerator.generate_response("About", 200)
def get(self, imdb_id: str): movie = MovieSearchRepository.search_movie_by_imdb_id(imdb_id=imdb_id) movies = UserRepository.add_attribute_to_favorite_movies( current_user, movie) return ResponseGenerator.generate_response(data=movies, code=200)
def get(self): fav_movies = UserRepository.get_users_favorite_movie_details(current_user) return ResponseGenerator.generate_response(fav_movies, code=200)
def home(): return ResponseGenerator.generate_response( "welcome to your personal movie database", 200)
def get(self): return ResponseGenerator.generate_response(current_user.json, code=200)