def update_request_audit(request_id, module=None): """ .. :quickref: Requests; Update request audit_data for an instrument Update the value of the audit_data for the evaluation of a request by an instrument :reqheader Authorization: This method requires authentication based on client certificate. A valid certificate from Instrument must be provided. :param request_id: request identifier :type tesla_id: int :param instrument_id: instrument identifier :type instrument_id: int :<json audit_data progress: audit_data object :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+-------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+-------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+-------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+-------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+-------------------------------------------------------------------------------------------+ """ valid, valid_data, errors = validators.validate_request_audit_update( request) if not valid: logger.error('Invalid JSON on [POST] /request/{}/status: {}'.format( request_id, errors)) return api_response(TESLA_API_STATUS_CODE.INVALID_JSON, data={'error_message': errors}) # Get the instrument ID from certificate module instrument_id = None if module is not None and module['is_instrument']: instrument_id = module['instrument']['id'] ret_val = tesla_storage.save_request_audit_data( request_id=request_id, instrument_id=instrument_id, with_enrolment=valid_data['include_enrolment'], with_request=valid_data['include_request'], data=valid_data['audit_data']) if ret_val == 0: return api_response(TESLA_API_STATUS_CODE.SUCCESS) else: return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA)
def get_learner_activity_results(tesla_id, vle_id, activity_type, activity_id): """ .. :quickref: Learners; Get learner summarized results for all instruments used in a certain activity Get learner summarized results for all instruments used in a certain activity :reqheader Authorization: This method requires authentication based on client certificate. :param tesla_id: learner TeSLA ID following RFC4122 v4 standard :type tesla_id: uuid :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) learner_results = tesla_db.activities.get_activity_results_by_tesla_id_activity_id( tesla_id, activity.id) result = {} result['items'] = schemas.RequestResult( many=True).dump(learner_results).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, result)
def get_activity_instruments(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Get activity instruments Return the list of instruments used in this activity. The list will contain all the instruments, including the alternative instruments. :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :>json list instruments: list of active instruments for this activity :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) act_instruments = tesla_db.activities.get_activity_all_instruments( activity.id) instruments_json = schemas.ActivityInstrument( many=True).dump(act_instruments).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, {"instruments": instruments_json})
def update_request_instrument_status(request_id, instrument_id, module=None): """ .. :quickref: Requests; Update request status for an instrument Update the value of the status and progress for the evaluation of a request by an instrument :reqheader Authorization: This method requires authentication based on client certificate. A valid certificate from Instrument must be provided. :param request_id: request identifier :type tesla_id: int :param instrument_id: instrument identifier :type instrument_id: int :<json float progress: evaluation progress percentage between 0 and 1 :<json int status: evaluation status, where 0 is not stared, 1 is in progress, 2 finished and 3 failed. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ valid, valid_data, errors = validators.validate_request_status_update( request) if not valid: logger.error('Invalid JSON on [POST] /request/{}/{}/status: {}'.format( request_id, instrument_id, errors)) return api_response(TESLA_API_STATUS_CODE.INVALID_JSON, data={'error_message': errors}) progress = valid_data.get('progress') status = valid_data.get('status') if not tesla_db.requests.update_request_result_status( request_id, instrument_id, progress, status): logger.error( 'Error persisting request status for request={}, instrument={}, status={}, progress={}' .format(request_id, instrument_id, status, progress)) return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA) return api_response(TESLA_API_STATUS_CODE.SUCCESS)
def delete_activity(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Delete an activity Delete an activity from the system and all their related requests and results. :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) # TODO: Delete activity data if tep_sync: pass else: tesla_db.activities.delete_activity(activity.id) return api_response(TESLA_API_STATUS_CODE.SUCCESS, http_code=501)
def get_activity_learners(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Get activity learners Get the list of learners of an activity :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json uuid tesla_id: learner TeSLA ID :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) return api_response(TESLA_API_STATUS_CODE.SUCCESS, http_code=501)
def get_instrument_thresholds(instrument_id): """ .. :quickref: Instruments; Get thresholds information for an instrument Get thresholds information for an instrument :reqheader Authorization: This method requires authentication based on client certificate. :param instrument_id: linstrument identifier in TeSLA :type instrument_id: int :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 14 | Instrument threshold not found. | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync is True: tep_db.sync_instrument_thresholds() threshold = tesla_db.instruments.get_instrument_thresholds(instrument_id) if threshold is None: return api_response( TESLA_API_STATUS_CODE.INSTRUMENT_THRESHOLD_NOT_FOUND, http_code=404) threshold_json = schemas.InstrumentThreshold().dump(threshold).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, threshold_json)
def get_learner(tesla_id): """ .. :quickref: Learners; Get learner information Get learner data :reqheader Authorization: This method requires authentication based on client certificate. :param tesla_id: learner TeSLA ID following RFC4122 v4 standard :type tesla_id: uuid :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ return api_response(TESLA_API_STATUS_CODE.SUCCESS, http_code=501)
def get_instruments(): """ .. :quickref: Instruments; Get the list of instruments Get the list of instruments :reqheader Authorization: This method requires authentication based on client certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ instruments = tesla_db.instruments.get_instruments() if instruments is None: return api_response(TESLA_API_STATUS_CODE.INSTRUMENT_NOT_FOUND, http_code=404) instruments_json = schemas.Instrument(many=True).dump(instruments).data response = {} response['items'] = instruments_json return api_response(TESLA_API_STATUS_CODE.SUCCESS, response)
def test_any_instrument(): return api_response(TESLA_API_STATUS_CODE.SUCCESS)
def get_learner_activity_instruments(tesla_id, vle_id, activity_type, activity_id): """ .. :quickref: Learners; Get active instruments in a certain activity for a particular learner Get all instruments to activate in a certain activity for a particular learner :reqheader Authorization: This method requires authentication based on client certificate. :param tesla_id: learner TeSLA ID following RFC4122 v4 standard :type tesla_id: uuid :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ # Get informed consent information ic_info = get_learner_ic_info(tesla_id) response = { 'learner_found': ic_info['learner_found'], 'agreement_status': ic_info['ic_valid'], 'instrument_ids': [], 'send': {} } if not ic_info['ic_valid']: if not ic_info['learner_found']: return api_response(TESLA_API_STATUS_CODE.USER_NOT_FOUND, response) if ic_info['ic_current_version'] is None: return api_response( TESLA_API_STATUS_CODE.INFORMED_CONSENT_NOT_ACCEPTED, response) if ic_info['rejected_date'] is not None: return api_response( TESLA_API_STATUS_CODE.INFORMED_CONSENT_REJECTED, response) return api_response(TESLA_API_STATUS_CODE.INFORMED_CONSENT_OUTDATED, response) # BEGIN: Synchronize data with TEP act_synch = tep_db.sync_activity(vle_id, activity_id, activity_type) # END: Synchronize data with TEP # Get the activity activity = tesla_db.activities.get_activity_by_def(vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, response) # Get the instruments activated for this activity act_instruments = tesla_db.activities.get_activity_instruments(activity.id) # Get learner SEND information send_info = get_learner_send_info(tesla_id) response['send'] = send_info['send'] # Create the list of instruments instruments = [] if act_instruments is not None: for inst in act_instruments: if send_info['is_send']: if inst.instrument_id in send_info['send'][ 'disabled_instruments']: if not inst.required: if inst.alternative_instrument_id is not None and inst.alternative_instrument_id not in \ send_info['send']['disabled_instruments']: instruments.append(inst.alternative_instrument_id) else: instruments.append(inst.instrument_id) else: instruments.append(inst.instrument_id) # Create the final response response = { 'learner_found': True, 'agreement_status': True, 'instrument_ids': instruments, 'send': send_info } return api_response(TESLA_API_STATUS_CODE.SUCCESS, response)
def get_learner_instrument_enrolment(tesla_id, instrument_id): """ .. :quickref: Learners; Get learner enrolment information for an instrument Get learner enrolment information for an instrument :reqheader Authorization: This method requires authentication based on client certificate. :param tesla_id: learner TeSLA ID following RFC4122 v4 standard :type tesla_id: uuid :param instrument_id: instrument identifier in TeSLA :type tesla_id: int :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ tesla_id = str(tesla_id) # BEGIN: Synchronize data with TEP tep_db.sync_learner(tesla_id) # END: Synchronize data with TEP learner_enrolment = tesla_db.learners.get_learner_enrolment( tesla_id, instrument_id) if learner_enrolment is None: percentage = 0.0 completed_enrollments = tesla_db.learners.count_learner_completed_requests( tesla_id, True, instrument_id) pending_enrollments = tesla_db.learners.count_learner_pending_requests( tesla_id, True, instrument_id) phase = TESLA_ENROLLMENT_PHASE.NOT_STARTED if completed_enrollments > 0 or pending_enrollments > 0: phase = TESLA_ENROLLMENT_PHASE.ONGOING else: percentage = learner_enrolment.percentage phase = TESLA_ENROLLMENT_PHASE.ONGOING if percentage == 1.0: phase = TESLA_ENROLLMENT_PHASE.COMPLETED pending_requests = tesla_db.learners.get_learner_pending_enrolment_requests( tesla_id, instrument_id) response = { "enrolment_phase_id": phase, "enrolment_completion": percentage, "predicted_enrolment_completion": percentage, "deferred_enrolments": pending_requests, "status_code_source": "TEP", "status_code_message": "OK" } return api_response(TESLA_API_STATUS_CODE.SUCCESS, response)
def get_activity_results(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Get activity results for all learners and instruments Get activity results for all learners and instruments :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json uuid tesla_id: learner TeSLA ID :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) learners = tesla_db.activities.get_activity_learners_with_results( activity.id, pagination=ResultsPagination(request)) results = schemas.ActivitySummaryPagination().dump(learners).data for learner_result in results['items']: tesla_id = learner_result['tesla_id'] summary = tesla_db.activities.get_activity_learner_summary( activity.id, tesla_id) summary_json = schemas.InstrumentResultsSummary( many=True).dump(summary).data learner_result.update({"instruments": summary_json}) return api_response(TESLA_API_STATUS_CODE.SUCCESS, results)
def test_any_instrument_inject(module): return api_response(TESLA_API_STATUS_CODE.SUCCESS, data=module)
def set_activity_instruments(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Set activity instruments Set the list of instruments to be activated in a certain activity :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json list: list of instrument options in JSON format :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :>json list instruments: list of active instruments for this activity :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 400: some controlled error occurred during request processing. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ valid, data, errors = validators.validate( schemas.ActivityInstrument(many=True), request) if not valid: return api_response(TESLA_API_STATUS_CODE.INVALID_JSON, errors, http_code=400) if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type, data) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) if not tesla_db.activities.update_activity_instrument_config( activity.id, data): return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) act_instruments = tesla_db.activities.get_activity_all_instruments( activity.id) instruments_json = schemas.ActivityInstrument( many=True).dump(act_instruments).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, {"instruments": instruments_json})
def get_activity_report(vle_id, activity_type, activity_id, page = 1): """ .. :quickref: Reports; Get report from an activity for all learners and instruments Get report of an activity results for all learners and instruments :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :param page: number of page :type page: integer :<json uuid tesla_id: learner TeSLA ID :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ ''' activity = tesla_db.activities.get_activity_by_def(vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) reports = tesla_db.report.get_reports(activity.id, pagination=ReportsPagination(request)) results = schemas.ResultViewActivityPagination().dump(reports).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, results) ''' # Verify the activity activity = tesla_db.activities.get_activity_by_def(vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) # Get filter parameters from request query parameters max_per_page = 20 instrument_ids = None # Get the list of instruments that have some request for this activity activity_instruments = tesla_db.reports.get_activity_instruments_with_requests(activity.id) # Get activity statistics for involved instruments context_statistics = {} for instrument in activity_instruments: context_statistics[instrument.id] = {} context_statistics[instrument.id]['histogram'] = tesla_db.statistics.verification_activity_results_histogram( activity.id, instrument.id) context_statistics[instrument.id]['thresholds'] = schemas.InstrumentThreshold().dump(tesla_db.instruments.get_instrument_thresholds( instrument.id)).data # Get a paginated list of learners that will be in the response learners = tesla_db.reports.get_activity_learners_with_results(activity.id, page=page, max_per_page=max_per_page, instrument_ids=instrument_ids) # Get the data for each learner results = schemas.ActivitySummaryPagination().dump(learners).data for learner_result in results['items']: tesla_id = learner_result['tesla_id'] summary = tesla_db.activities.get_activity_learner_summary(activity.id, tesla_id) summary_json = schemas.LearnerInstrumentResults(many=True).dump(summary).data # Build indexed data to make easier the results visualization indexed_info = {} for inst in summary_json: indexed_info[inst['instrument_id']] = inst # Update the learner data on the paginated view learner_result.update({"instruments": indexed_info}) # Add learner level statistics learner_result.update({"stats": _get_learner_instrument_stats(learner_result, context_statistics)}) # Build the paginated iterator as a list, since items type is JSON. results['iter_pages'] = [p for p in learners.iter_pages(left_edge=1, left_current=2, right_current=3, right_edge=1)] act_json = schemas.Activity().dump(activity).data return_data = { 'results': results, 'activity': act_json, 'instruments': schemas.Instrument(many=True).dump(activity_instruments).data } return api_response(TESLA_API_STATUS_CODE.SUCCESS, return_data)
def add_activity(): """ .. :quickref: Activities; Add a new activity Create a new activity on the system :reqheader Authorization: This method requires authentication based on client certificate. :<json int vle_id: VLE identifier in TeSLA :<json string activity_type: type of the activity in the VLE :<json string activity_id: identifier of the activity in the VLE :<json string description: description for the activity :<json JSON conf: JSON object configuration options for the activity :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :>json int vle_id: VLE identifier in TeSLA :>json string activity_type: type of the activity in the VLE :>json string activity_id: identifier of the activity in the VLE :>json string description: description for the activity :>json JSON conf: JSON object configuration options for the activity :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 400: bad request. In this case, the request data is invalid. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data. | +---------+----------------------------------------------------------------------------------------------+ | 13 | The activity already exists. | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ valid, data, errors = validators.validate(schemas.Activity(), request) if not valid: return api_response(TESLA_API_STATUS_CODE.INVALID_JSON, errors, http_code=400) if tep_sync: activity = tep_db.sync_activity(data.vle_id, data.activity_id, data.activity_type) else: activity = tesla_db.activities.get_activity_by_def( data.vle_id, data.activity_type, data.activity_id) if activity is not None: act_json = schemas.Activity().dump(activity).data return api_response(TESLA_API_STATUS_CODE.DUPLICATED_ACTIVITY, act_json, http_code=400) if tep_sync: activity = tep_db.create_activity(data.vle_id, data.activity_id, data.activity_type) if not tesla_db.activities.update_activity_config( activity.id, data.conf): return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) if not tesla_db.activities.update_activity_description( activity.id, data.description): return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) else: activity = tesla_db.activities.create_activity(data.vle_id, data.activity_type, data.activity_id, data.conf, data.description) if activity is None: return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) activity = tesla_db.activities.get_activity(activity.id) act_json = schemas.Activity().dump(activity).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, act_json)
def test_tep(): return api_response(TESLA_API_STATUS_CODE.SUCCESS)
def update_activity(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Update activity information Update activity data :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :<json string description: description for the activity :<json JSON conf: JSON object configuration options for the activity :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :>json int vle_id: VLE identifier in TeSLA :>json string activity_type: type of the activity in the VLE :>json string activity_id: identifier of the activity in the VLE :>json string description: description for the activity :>json JSON conf: JSON object configuration options for the activity :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 400: some controlled error occurred during request processing. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ valid, data, errors = validators.validate(schemas.Activity(), request, partial=True) if not valid: return api_response(TESLA_API_STATUS_CODE.INVALID_JSON, errors, http_code=400) if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) # Modify provided fields if 'conf' in request.get_json(): if not tesla_db.activities.update_activity_config( activity.id, data.conf): return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) if 'description' in request.get_json(): if not tesla_db.activities.update_activity_description( activity.id, data.description): return api_response(TESLA_API_STATUS_CODE.ERROR_PERSISTING_DATA, http_code=400) activity = tesla_db.activities.get_activity(activity.id) act_json = schemas.Activity().dump(activity).data return api_response(TESLA_API_STATUS_CODE.SUCCESS, act_json)
def get_request(request_id): """ .. :quickref: Requests; Get request data Get request data :reqheader Authorization: This method requires authentication based on client certificate. :param request_id: request information :type request_id: int :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ logger.debug("GET REQUEST {}".format(request_id)) request = tesla_db.requests.get_request(request_id) if request is None: return api_response(TESLA_API_STATUS_CODE.REQUEST_NOT_FOUND) activity = None if request.activity_id is not None: activity = tesla_db.activities.get_activity(request.activity_id) request_data = tesla_storage.load_request_data(request_id) data = { "request_id": request.id, "tesla_id": request.tesla_id, "is_enrollment": request.is_enrolment, "data": request_data } if activity is not None: data["activity"] = { "vle_id": activity.vle_id, "activity_id": activity.activity_id, "activity_type": activity.activity_type } return api_response(TESLA_API_STATUS_CODE.SUCCESS, data=data)
def test_tep_inject(module): return api_response(TESLA_API_STATUS_CODE.SUCCESS, data=module)
def get_learner_activity_instrument_audit(tesla_id, vle_id, activity_type, activity_id, instrument_id): """ .. :quickref: Learners; Get learner summarized audit for an instrument in a certain activity Get learner summarized audit for an instrument in a certain activity :reqheader Authorization: This method requires authentication based on client certificate. :param tesla_id: learner TeSLA ID following RFC4122 v4 standard :type tesla_id: uuid :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :param instrument_id: identifier of the instrument in TeSLA :type activity_id: int :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) tesla_id = str(tesla_id) # Verify the learner learner = tesla_db.learners.get_learner(tesla_id) if learner is None: return api_response(TESLA_API_STATUS_CODE.LEARNER_NOT_FOUND, http_code=404) # Verify the instrument instrument = tesla_db.instruments.get_instrument_by_id(instrument_id) if instrument is None: return api_response(TESLA_API_STATUS_CODE.INSTRUMENT_NOT_FOUND, http_code=404) # Check that the instrument have audit possibilities if instrument.id not in [1, 6]: return api_response(TESLA_API_STATUS_CODE.SUCCESS, http_code=405) audit_data = tep_db.get_activity_audit(activity.vle_id, activity.activity_type, activity.activity_id, tesla_id, instrument.id) for a in audit_data: if a['start'] is not None: a['start'] = str(a['start']) if a['finish'] is not None: a['finish'] = str(a['finish']) if instrument.id == 1: audit_data = _get_fr_audit_data(audit_data) if audit_data is None: audit_data = { 'results': { 'frame_details': [], 'total_frames': 0, 'valid_frames': 0, 'frame_codes': [], 'error_frames': 0 }, 'enrollment_user_faces': [], 'version': 'TFR 1.0' } else: aux = audit_data audit_data = {} audit_data['audit_data'] = aux return api_response(TESLA_API_STATUS_CODE.SUCCESS, data=audit_data)
def test_list_modules(): return api_response(TESLA_API_STATUS_CODE.SUCCESS)
def get_activity(vle_id, activity_type, activity_id): """ .. :quickref: Activities; Get activity information Get activity data :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :>json int vle_id: VLE identifier in TeSLA :>json string activity_type: type of the activity in the VLE :>json string activity_id: identifier of the activity in the VLE :>json string description: description for the activity :>json JSON conf: JSON object configuration options for the activity :>json boolean tesla_active: this parameter is true if this activity has some TeSLA instrument active :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 400: some controlled error occurred during request processing. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ if tep_sync: activity = tep_db.sync_activity(vle_id, activity_id, activity_type) else: activity = tesla_db.activities.get_activity_by_def( vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) act_json = schemas.Activity().dump(activity).data act_json['tesla_active'] = False act_instruments = tesla_db.activities.get_activity_all_instruments( activity.id) for instrument in act_instruments: if instrument.active is True: act_json['tesla_active'] = True break return api_response(TESLA_API_STATUS_CODE.SUCCESS, act_json)
def get_activity_learner_report(vle_id, activity_type, activity_id, tesla_id): """ .. :quickref: Reports; Get report results for an activity and learner Get report results for an activity and learner :reqheader Authorization: This method requires authentication based on client certificate. :param vle_id: VLE identifier :type vle_id: int :param activity_type: type of the activity in the VLE :type activity_type: string :param activity_id: identifier of the activity in the VLE :type activity_id: string :param tesla_id: identifier of Learner :type tesla_id: guid :<json uuid tesla_id: learner TeSLA ID :<json string public_cert: public certificate for the learner. :<json string cert_alg: algorithm used to create the public certificate. :>json int status_code: indicates if the request is correctly processed or some error occurred. :>json string error_message: in case of error (status_code > 0) it provide a description of the error :status 200: request processed. In this case, check the status_code in order to verify if is correct or not. :status 401: authorization denied. There is some problem with the provided certificates :status 404: activity not found. :status 500: unexpected error processing the request **Response Status Codes** +---------+----------------------------------------------------------------------------------------------+ |**Code** | **Description** | +---------+----------------------------------------------------------------------------------------------+ | 0 | Success! | +---------+----------------------------------------------------------------------------------------------+ | 3 | Activity not found | +---------+----------------------------------------------------------------------------------------------+ | 6 | Error persisting the data | +---------+----------------------------------------------------------------------------------------------+ | 16 | Learner not found | +---------+----------------------------------------------------------------------------------------------+ | 53 | Request JSON error. It contains more data or some required fields are missing. | +---------+----------------------------------------------------------------------------------------------+ """ activity = tesla_db.activities.get_activity_by_def(vle_id, activity_type, activity_id) if activity is None: return api_response(TESLA_API_STATUS_CODE.ACTIVITY_NOT_FOUND, http_code=404) learner = tesla_db.learners.get_learner(tesla_id) if learner is None: return api_response(TESLA_API_STATUS_CODE.LEARNER_NOT_FOUND, http_code=404) # Get the list of instruments that have some request for this activity activity_instruments = tesla_db.reports.get_activity_instruments_with_requests(activity.id) # Get activity statistics for involved instruments context_statistics = {} for instrument in activity_instruments: context_statistics[instrument.id] = {} context_statistics[instrument.id][ 'histogram'] = tesla_db.statistics.verification_activity_results_histogram(activity.id, instrument.id) context_statistics[instrument.id]['thresholds'] = schemas.InstrumentThreshold().dump(tesla_db.instruments.get_instrument_thresholds( instrument.id)).data context_statistics[instrument.id]['temporal'] = _get_temporal_results(tesla_db.reports.get_activity_learner_temporal_results(activity.id, tesla_id, instrument.id)) context_statistics[instrument.id]['aronym'] = instrument.acronym summary = tesla_db.activities.get_activity_learner_summary(activity.id, tesla_id) summary_json = schemas.LearnerInstrumentResults(many=True).dump(summary).data # Build indexed data to make easier the results visualization indexed_info = {} for inst in summary_json: indexed_info[inst['instrument_id']] = inst # Update the learner data on the paginated view learner_result = { "tesla_id": tesla_id, "instruments": indexed_info, "failed_requests": _get_temporal_results( tesla_db.reports.get_activity_learner_temporal_failed_requests(activity.id, tesla_id), break_far_samples=False) } # Add learner level statistics learner_result.update({"stats": _get_learner_instrument_stats(learner_result, context_statistics)}) act_json = schemas.Activity().dump(activity).data result = { 'learner': learner_result, 'object': act_json, 'instruments': schemas.Instrument(many=True).dump(activity_instruments).data, 'context_statistics': context_statistics, 'tesla_id': str(tesla_id) } return api_response(TESLA_API_STATUS_CODE.SUCCESS, result)