Esempio n. 1
0
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)
Esempio n. 2
0
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})
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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})
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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)