def update(lesson_id): # Check for valid json if not request.is_json: return error_401("Reponse is not JSON") # Check if user exists and signed in jwt_user = get_jwt_identity() user = User.get_or_none(User.name == jwt_user) if not user: return error_401("Unauthorized action") # Retrieve data from json data = request.get_json() title = data['title'] description = data['description'] # Check title and description and then create new lesson if title and description: lesson = Lesson.update(title=title, description=description).where(Lesson.id==lesson_id).execute() if lesson: return success_201("Update lesson successfully", lesson) else: return error_401("Update lesson failed") else: return error_401("Invalid title or description")
def add_image(): jwt_user = get_jwt_identity() user = User.get_or_none(User.name == jwt_user) if user: image_file = request.files.get('image') request_data = request.get_json() if request_data: lesson_id = request_data.lesson_id query = Lesson.update(image = image_file.filename).where(Lesson.id == lesson_id) if query.execute() and upload_file_to_s3(image_file): return success_201('Image successfully saved and uploaded!') else: return error_401('Error when saving image to S3 or database!') else: return error_401('Requested data is not JSON or not found!') else: error_401('User not found!')