def upload_session_logo(session_id): """ Endpoint for uploading the logo of a session. :param session_id: Id of the session. :return: - HTTP 204, if the session logo could be stored. - HTTP 400, if the uploaded image is not supported, if the user didn't specify a logo or if the session id is not valid. - HTTP 404, if the session does not exist. """ LOGGER.info("Received request for uploading session logo.") if 'image' not in request.files: raise BadRequest("Expected a file.") file = request.files['image'] try: FilesValidator.validate_image_format(file) SessionManager.get_instance().set_session_logo(session_id=session_id, image_file=file) return "", 204 except ValueError as ve: LOGGER.exception("Upload logo request finished with errors: ") raise BadRequest(ve) except SessionValidationException as sv: LOGGER.exception("Upload logo request finished with errors: ") raise NotFound(sv)
def initialize_session(session_id): """ Endpoint for initialzing an existing Rumba session. :param session_id: Id of the session to initialize :return: - HTTP 204, if the session could be successfully initialized. - HTTP 404, if the session does not exist. - HTTP 409, if the session is not in the expected state before initializing it. """ LOGGER.info("Received request for initializing a session") try: SessionManager.get_instance().initialize_session(session_id=session_id) LOGGER.info("Session sucessfully initialized.") return "", 204 except IllegalSessionStateException as ie: LOGGER.exception( "Session initialization request finished with errors.") raise Conflict(ie) except NotExistingResource as ne: LOGGER.exception( "Session initialization request finished with errors.") raise NotFound(ne) except Exception as ex: raise InternalServerError(ex)
def delete_session(session_id): """ Endpoint for removing a Rumba session. Rumba sessions can only be removed if they are no longer active. Active sessions should be stopped before calling to this method. :param session_id: Id of the session to remove. :return: - HTTP 400, if the provided id is not valid - HTTP 404, if there's no network with such id. - HTTP 409, if the network is active and can not be deleted. """ LOGGER.info("Received request for deleting a session.") try: SessionManager.get_instance().delete_session(session_id) LOGGER.info("Session removal request sucessfully finished.") return "", 204 except ValueError as ve: LOGGER.exception("Session removal request finished with errors: ") raise BadRequest(ve) except IllegalSessionStateException as ie: LOGGER.exception("Session removal request finished with errors: ") raise Conflict(ie) except SessionValidationException as se: LOGGER.exception("Session removal request finished with errors: ") raise NotFound(se)
def list_all_session_videos(self, session_id): """ :param session_id: :return: """ LOGGER.info( "Listing all session videos. [session_id={}]".format(session_id)) GenericValidator.validate_id(session_id) session = SessionManager.get_instance().get_session( session_id=session_id) videos = Video.objects(session=session['id']) session_videos = DataTransformer.generate_video_list_view( session=session, db_videos=videos) for session_video in session_videos: try: session_video['ts'] = VideoEditorHelper.get_initial_ts( video_id=session_video['video_id']) except Exception: LOGGER.warn( "Could not retrieve timestmap of video. Skipping it...") session_video['ts'] = -1 ordered_list = sorted(session_videos, key=lambda x: float(x['ts'])) LOGGER.info("Retrieved {} videos for session {}".format( len(ordered_list), session_id)) return ordered_list
def add_video(self, session_id, user_id): """ :param session_id: :param user_id: :return: """ LOGGER.info("Adding video to session.") session = SessionManager.get_instance().get_session( session_id=session_id) if session['state'] != SessionStatus.ACTIVE.value: raise IllegalSessionStateException( "Can only add videos to active sessions.") user_videos = Video.objects(session=session['id'], user_id=user_id).count() video_name = "video{}".format(user_videos) session = RumbaSession.objects(id=session_id).first() video_path = FileSystemService.get_instance().create_video_directory( session_folder=session['folder_url'], user_id=user_id, video_name=video_name) video = Video(session=session['id'], user_id=user_id, name=video_name, video_path=video_path).save() LOGGER.info("Video successfully added: [id={}]".format(video)) return str(video['id'])
def get_session(session_id): """ Endpoint for retrieving the information of a Rumba session. A Rumba session is identified by its id, which was returned to the user when creating the Rumba session. :param session_id: String containing the id of the Rumba session to retrieve. :return: Json containing the information of the session. It would contain following format: { "id": "r32423gs-44d-t456g-32423" "concert": "muse-live-2018", "band": "muse", "date": 1521545253, "is_public": true, "vimeo": { "username": "******", "password": "******" } "location": "Palau Sant Jordi, Barcelona" } """ LOGGER.info("Received request for retrieving a session.") try: session = SessionManager.get_instance().get_session(session_id) LOGGER.info("Session retrieval request successfully finished.") return jsonify(session), 200 except ValueError as ve: LOGGER.exception("Session creation request finished with errors: ") raise BadRequest(ve) except NotExistingResource as sv: LOGGER.exception("Session creation request finished with errors: ") raise NotFound(sv)
def stop_session(session_id): """ Endpoint for stopping an active session. :param session_id: Id of the session to stop. :return: - """ LOGGER.info("Received request for stopping a session.") try: SessionManager.get_instance().stop_session(session_id) LOGGER.info("Stop request successfully finished.") return "", 204 except ValueError as ve: LOGGER.exception("Stop request finished with errors: ") raise BadRequest(ve) except NotExistingResource as ie: LOGGER.exception("Stop request finished with errors: ") raise NotFound(ie) except IllegalSessionStateException as ise: LOGGER.exception("Stop request finished with errors: ") raise Conflict(ise)
def get_current_session(): """ HTTP endpoint for retrieving the current session. :return: - HTTP 200 with the id in the body. - HTTP 404, if there's no created nor initialized session. """ LOGGER.info("Received request for getting current session.") try: current_session = SessionManager.get_instance().get_current_session() LOGGER.info("Get current session request succesfully finished.") return jsonify(current_session), 200 except NotExistingResource as ne: LOGGER.exception("Get current session request finished with errors: ") raise NotFound(ne)
def add_video_to_active_session(self, user_id): """ :param user_id: :return: """ LOGGER.info("Adding video to active session.") session = SessionManager.get_instance().get_current_session() if session is None or session['state'] != SessionStatus.ACTIVE.value: raise IllegalSessionStateException("There's no active session.") video_id = self.add_video(session_id=session['id'], user_id=user_id) video_mongo = Video.objects(id=video_id).first() video_dict = MongoHelper.to_dict(video_mongo) video_dict['session'] = str(video_dict['session']) LOGGER.info("Video successfully added to active session") return video_dict
def list_session_videos(self, session_id, user_id): """ :param session_id: :param user_id: :return: """ LOGGER.info( "Listing all session videos of the user. [session_id={}, user_id={}]" .format(session_id, user_id)) GenericValidator.validate_id(session_id) GenericValidator.validate_id(user_id) session = SessionManager.get_instance().get_session( session_id=session_id) videos = Video.objects(session=session['id'], user_id=user_id) session_videos = DataTransformer.generate_video_list_view( session=session, db_videos=videos) LOGGER.info("Retrieved {} videos for session {} and user {}".format( len(session_videos), session_id, user_id)) return session_videos
def download_logo(session_id): """ Endpoint for downloading the logo of a session. :param session_id: Id of the session. :return: - HTTP 200, if the logo could be retrieved. The logo is sent in the body of the message. - HTTP 400, if the given session is not a valid session id. - HTTP 404, if the session does not exist or it exists but has no associated logo. """ LOGGER.info("Received request for downloading session logo.") try: image_url = SessionManager.get_instance().get_session_logo_url( session_id=session_id) return send_file(image_url), 200 except ValueError as ve: LOGGER.exception("Download logo request finished with errors: ") raise BadRequest(ve) except SessionValidationException as sv: LOGGER.exception("Download logo request finished with errors: ") raise NotFound(sv)
def create_session(): """ Endpoint for creating Rumba sessions. This method should be used by applications on top of the Rumba bakcned in order to create Rumba sessions. The rumba session speceficiacion should be provided in the body of the message with following structure: { "concert": "muse-live-2018", "band": "muse", "date": 1521545253, "is_public": true, "vimeo": { "username": "******", "password": "******" } "location": "Palau Sant Jordi, Barcelona" } Mandatory fields are "concert", "band", "date" and "is_public." The date should be provided as UNIX timestamp, that is, the number of seconds that have elapsed since 1st January 1970. :return: - HTTP 201, with the id of the new Rumba session in the body, if the sessio could be created. Example: { "id": "{session-id}" } - HTTP 400, if some of the provided parameters were not correct. """ LOGGER.info("Received request for creating a new session.") session = request.get_json(force=True) LOGGER.debug("Request body: {0}".format(session)) try: SessionValidator.validate_new_session(session) session_id = SessionManager.get_instance().create_session(session) LOGGER.info("Session creation request successfully finished.") return jsonify({'id': session_id}), 201 except SessionValidationException as sv: LOGGER.exception("Session creation request finished with errors: ") raise BadRequest(sv) except ValueError as ve: LOGGER.exception("Session creation request finished with errors: ") raise BadRequest(ve)
def list_sessions(): """ Endpoint for listing all Rumba sessions. :return: Json containing a list of sesions. It would contain following format: [ { "id": "r32423gs-44d-t456g-32423" "concert": "muse-live-2018", "band": "muse", "date": 1521545253, "is_public": true, "vimeo": { "username": "******", "password": "******" } "location": "Palau Sant Jordi, Barcelona" } ] """ LOGGER.info("Received request for listing all sessions.") session_list = SessionManager.get_instance().list_sessions() LOGGER.info("Sessions retrieval request successfully finished.") return jsonify(session_list), 200