コード例 #1
0
def add_registry(request):
    if request.method == 'POST':
        form = RegistryForm(request.POST)
        #Check the Form
        if form.is_valid():
            try:
                try:
                    url = request.POST.get('url')
                except ValueError:
                    return HttpResponseBadRequest('Please provide a URL.')
                #We retrieve information from the form
                harvestrate = request.POST.get('harvestrate', "")
                if 'harvest' in request.POST:
                    harvest = True
                else:
                    harvest = False
                #Call to the API to add the registry
                try:
                    req = add_registry_model(url, harvestrate, harvest)
                    #If the status is OK, sucess message
                    if req.status_code == status.HTTP_201_CREATED:
                        messages.add_message(request, messages.SUCCESS, 'Data provider added with success.')
                        return HttpResponse('CREATED')
                    #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.')
        else:
            return HttpResponseBadRequest('Bad entries. Please enter a valid URL and a positive integer')
コード例 #2
0
ファイル: views.py プロジェクト: Huchikoma/internship_MMQ
def add_registry(request):
    """
    POST http://localhost/oai_pmh/api/add/registry
    POST data query='{"url":"value","harvestrate":"number", "harvest":"True or False"}'
    """
    try:
        #Serialization of the input data
        serializer = AddRegistrySerializer(data=request.DATA)
        #If all fields are okay
        if serializer.is_valid():
            #Check the URL
            url = request.DATA['url']
            harvestrate = request.DATA['harvestrate']
            harvest = request.DATA['harvest'] == 'True'
            return add_registry_model(url, 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)