def update_registry(request):
    if request.method == 'POST':
        #UPDATE the registry
        try:
            #Get the ID
            if 'id' in request.POST:
                id = request.POST.get('id')
            else:
                return HttpResponseBadRequest('Please provide an ID in order to edit the data provider.')
            #Get all form information
            if 'harvestrate' in request.POST:
                harvestrate = request.POST.get('harvestrate')
            else:
                harvestrate = ''
            if 'edit_harvest' in request.POST:
                harvest = True
            else:
                harvest = False
            #Call the API to update the registry
            try:
                req = update_registry_model(id, harvestrate, harvest)
                #If the status is OK, sucess message
                if req.status_code == status.HTTP_200_OK:
                    messages.add_message(request, messages.INFO, 'Data Provider successfully edited.')
                    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 registry edition
        template = loader.get_template('oai_pmh/admin/form_registry_edit.html')
        registry_id = request.GET['registry_id']
        try:
            registry = OaiRegistry.objects.get(pk=registry_id)
            data = {'id': registry.id, 'harvestrate': registry.harvestrate,
                    'edit_harvest': registry.harvest}
            registry_form= UpdateRegistryForm(data)
        except:
            registry_form = UpdateRegistryForm()

        context = RequestContext(request, {
            'registry_form': registry_form,
        })

        return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')
Beispiel #2
0
def update_registry(request):
    """
    PUT http://localhost/oai_pmh/api/update/registry
    PUT data query='{"id":"value", "harvestrate":"value", "harvest":"True or False"}'
    id: string
    """
    try:
        #Serialization of the input data
        serializer = UpdateRegistrySerializer(data=request.DATA)
        #If it's valid
        if serializer.is_valid():
            #We retrieve all information
            id = request.DATA['id']
            harvestrate = request.DATA['harvestrate']
            harvest = request.DATA['harvest'] == 'True'
            return update_registry_model(id, harvestrate, harvest)
        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(e.message)
        return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)