Ejemplo n.º 1
0
def add_my_set(request):
    if request.method == 'POST':
        try:
            #We retrieve information from the form
            if 'setSpec' in request.POST:
                setSpec = request.POST.get('setSpec')
            if 'setName' in request.POST:
                setName = request.POST.get('setName')
            if 'description' in request.POST:
                description = request.POST.get('description')
            if 'templates' in request.POST:
                templates = request.POST.getlist('templates')
            else:
                templates = []

            #Call to the API to add the set
            try:
                req = add_my_set_model(setSpec, setName, templates, description)
                #If the status is OK, sucess message
                if req.status_code == status.HTTP_201_CREATED:
                    messages.add_message(request, messages.SUCCESS, 'Set 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.')
Ejemplo n.º 2
0
def add_my_set(request):
    """
    PUT http://localhost/oai_pmh/api/add/my-set
    PUT data query='{"setSpec":"value", "setName":"value", templates:["id1", "id2"..]}'
    optional: {'description':'value'}"
    """
    try:
        #Serialization of the input data
        serializer = MySetSerializer(data=request.DATA)
        #If it's valid
        if serializer.is_valid():
            setSpec = request.DATA['setSpec']
            setName = request.DATA['setName']
            description = request.DATA.get('description', None)
            templates = request.DATA.getlist('templates')
            return add_my_set_model(setSpec, setName, templates, description)
        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)