def api_dataset_structure_code(request):
    '''
        This API is needed just by another OKS and it is not meant to be public
        It's goal is to provide another OKS with the information needed to generate
        and migrate the models within a structure. TODO: Models that are external references
        are not included.
        The information is provided in the form of the code of the classes in a dictionary
        that groups them by APP/MODULE
        CAN BE CACHED
    '''
    DataSetStructure_UKCL = request.GET['UKCL']
    dss = DataSetStructure.retrieve_locally(
        urllib.parse.unquote(DataSetStructure_UKCL).replace("%2F", "/"))
    try:
        classes_code = dss.classes_code()
        return render(
            request,
            'knowledge_server/export.json', {
                'json': ApiResponse(ApiResponse.success, "",
                                    classes_code).json()
            },
            content_type="application/json")
    except Exception as ex:
        return render(
            request,
            'knowledge_server/export.json',
            {'json': ApiResponse(ApiResponse.failure, str(ex)).json()},
            content_type="application/json")
def api_datasets(request, DataSetStructure_UKCL=None, response_format=None):
    '''
        http://redmine.davide.galletti.name/issues/64
        all the released datasets of a given structure/type
        
        parameter:
        * response_format { 'XML' | 'JSON' }
        * DataSetStructure_UKCL: UKCL of the DataSetStructure encoded
        
        Implementation:
        # it fetches the structure from the DB, looks for all the datasets
        # with that structure; if it is not a view only those that are released; 
        CAN BE CACHED
    '''
    if not DataSetStructure_UKCL:
        DataSetStructure_UKCL = request.GET['UKCL']
    if response_format:
        ar = ApiResponse(format=response_format)
    else:
        # if not specified I get it from the request object
        ar = ApiResponse(request=request)
    dss = DataSetStructure.retrieve_locally(
        urllib.parse.unquote(DataSetStructure_UKCL).replace("%2F", "/"))

    # Now I need to get all the released DataSet of the DataSetStructure passed as a parameter
    if dss.is_a_view:
        # version_released is not relevant for views
        released_dataset = DataSet.objects.filter(dataset_structure=dss)
    else:
        released_dataset = DataSet.objects.filter(dataset_structure=dss,
                                                  version_released=True)
    serialized = ""
    comma = ""
    dataset_list = []
    for dataset in released_dataset:
        if ar.response_format == 'JSON':
            dataset_list.append(
                dataset.export(export_format="DICT",
                               force_external_reference=True))
        else:
            serialized += dataset.export(export_format=ar.response_format,
                                         force_external_reference=True)
    if ar.response_format == 'XML':
        ar.status = ApiResponse.success
        ar.content = "<DataSets>" + serialized + "</DataSets>"
        return render(request,
                      'knowledge_server/export.xml', {'xml': ar.xml()},
                      content_type="application/xhtml+xml")
    if ar.response_format == 'JSON':
        ar.content = {"DataSets": dataset_list}
        ar.status = ApiResponse.success
        return render(request,
                      'knowledge_server/export.json', {'json': ar.json()},
                      content_type="application/json")
def api_dataset_structure_code(request, DataSetStructure_UKCL):
    '''
        This API is needed just by another OKS and it is not meant to be public
        It's goal is to provide another OKS with the information needed to generate
        and migrate the models within a structure. TODO: Models that are external references
        are not included.
        The information is provided in the form of the code of the classes in a dictionary
        that groups them by APP/MODULE
        CAN BE CACHED
    '''
    dss = DataSetStructure.retrieve_locally(urllib.parse.unquote(DataSetStructure_UKCL).replace("%2F","/"))
    try:
        classes_code = dss.classes_code()
        return render(request, 'knowledge_server/export.json', {'json': ApiResponse(ApiResponse.success, "", classes_code).json()}, content_type="application/json")
    except Exception as ex:
        return render(request, 'knowledge_server/export.json', {'json': ApiResponse(ApiResponse.failure, str(ex)).json()}, content_type="application/json")
def api_datasets(request, DataSetStructure_UKCL, response_format):
    '''
        http://redmine.davide.galletti.name/issues/64
        all the released datasets of a given structure/type
        
        parameter:
        * response_format { 'XML' | 'JSON' }
        * DataSetStructure_UKCL: UKCL of the DataSetStructure encoded
        
        Implementation:
        # it fetches the structure from the DB, looks for all the datasets
        # with that structure; if it is not a view only those that are released; 
        CAN BE CACHED
    '''
    ar = ApiResponse()
    response_format = response_format.upper()
    dss = DataSetStructure.retrieve_locally(urllib.parse.unquote(DataSetStructure_UKCL).replace("%2F","/"))
    
    # Now I need to get all the released DataSet of the DataSetStructure passed as a parameter
    if dss.is_a_view:
        # version_released is not relevant for views
        released_dataset = DataSet.objects.filter(dataset_structure = dss)
    else:
        released_dataset = DataSet.objects.filter(dataset_structure = dss, version_released=True)
    serialized = ""
    comma = ""
    dataset_list = []
    for dataset in released_dataset:
        if response_format == 'JSON':
            dataset_list.append(dataset.export(export_format = "DICT", force_external_reference=True))
        else:
            serialized += dataset.export(export_format = response_format, force_external_reference=True)
    if response_format == 'XML':
        ar.status = ApiResponse.success
        ar.content = "<DataSets>" + serialized + "</DataSets>"
        return render(request, 'knowledge_server/export.xml', {'xml': ar.xml()}, content_type="application/xhtml+xml")
    if response_format == 'JSON':
        ar.content = { "DataSets": dataset_list }
        ar.status = ApiResponse.success 
        return render(request, 'knowledge_server/export.json', {'json': ar.json()}, content_type="application/json")