Exemplo n.º 1
0
def update_registry_harvest(request):
    if request.method == 'POST':
        try:
            #Get all form information
            if 'id' in request.POST:
                id = request.POST.get('id')
            if 'metadataFormats' in request.POST:
                metadataFormats = request.POST.getlist('metadataFormats')
            else:
                metadataFormats = []
            if 'sets' in request.POST:
                sets = request.POST.getlist('sets')
            else:
                sets = []
            #Call the API to update the harvest configuration
            try:
                req = update_registry_harvest_model(id, sets, metadataFormats)
                #If the status is OK, sucess message
                if req.status_code == status.HTTP_200_OK:
                    messages.add_message(request, messages.INFO, 'Data provider edited with success.')
                    return HttpResponse(json.dumps({}), content_type='application/javascript')
                #Else, we return a bad request response with the message provided by the API
                else:
                    data = req.data
                    return HttpResponseBadRequest(data[APIMessage.label])
            except OAIAPIException as e:
                return HttpResponseBadRequest(e.message)
            except Exception as e:
                return HttpResponseBadRequest('An error occurred. Please contact your administrator.')
        except Exception as e:
            return HttpResponseBadRequest('An error occurred. Please contact your administrator.')
    elif request.method == 'GET':
        #Build the template to render for the harvest configuration
        template = loader.get_template('oai_pmh/admin/form_harvest.html')
        registry_id = request.GET['registry_id']
        try:
            #Configure if we have to harvest all metadataFormats and all sets
            harvest_form = SettingHarvestForm(id=registry_id)
        except:
            harvest_form = SettingHarvestForm()

        context = RequestContext(request, {
            'harvest_form': harvest_form,
        })
        return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')
Exemplo n.º 2
0
def update_registry_harvest(request):
    """
    PUT http://localhost/oai_pmh/api/update/registry-harvest
    PUT data query='{"id":"value", "metadataFormats":["id1", "id2"..], "sets":["id1", "id2"..]}'
    id: string
    """
    try:
        #Serialization of the input data
        serializer = UpdateRegistryHarvestSerializer(data=request.DATA)
        #If it's valid
        if serializer.is_valid():
            id = request.DATA['id']
            sets = request.DATA.get('sets')
            metadataFormats = request.DATA.get('metadataFormats')
            return update_registry_harvest_model(id, sets, metadataFormats)
        else:
            raise OAIAPISerializeLabelledException(errors=serializer.errors,
                                          status=status.HTTP_400_BAD_REQUEST)
    except OAIAPIException as e:
        return e.response()
    except Exception as e:
        content = APIMessage.getMessageLabelled('Unable to update the harvest configuration for the registry.')
        return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)