def update_my_set(request): """ PUT http://localhost/oai_pmh/api/update/my-set PUT data query='{"id":"value", "setSpec":"value","setName":"value"}' optional: '{"description":"value", "templates":["id1", "id2"..]}' """ try: #Serialization of the input data serializer = UpdateMySetSerializer(data=request.DATA) #If it's valid if serializer.is_valid(): #We retrieve all information id = request.DATA['id'] setSpec = request.DATA['setSpec'] setName = request.DATA['setName'] description = request.DATA.get('description', None) templates = request.DATA.get('templates', None) return update_my_set_model(id, 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)
def update_my_set(request): if request.method == 'POST': #UPDATE the set try: #Get all form information if 'id' in request.POST: id = request.POST.get('id') 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 = [] try: req = update_my_set_model(id, setSpec, setName, templates, description) #If the status is OK, sucess message if req.status_code == status.HTTP_200_OK: messages.add_message(request, messages.INFO, 'Set 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 set edition template = loader.get_template('oai_pmh/admin/form_my_set_edit.html') set_id = request.GET['set_id'] try: set_form = UpdateMySetForm(id = set_id) except: set_form = UpdateMySetForm() context = RequestContext(request, { 'set_form': set_form, }) return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')