def update_my_registry(request): if request.method == 'POST': #UPDATE the registry try: #Get all form information if 'name' in request.POST: name = request.POST.get('name') else: name = '' if 'enable_harvesting' in request.POST: enable_harvesting = True else: enable_harvesting = False #Call the API to update the registry try: req = update_my_registry_model(name, enable_harvesting) #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 registry edition template = loader.get_template('oai_pmh/admin/form_my_registry_edit.html') try: information = OaiSettings.objects.get() data = {'name': information.repositoryName, 'repo_identifier': information.repositoryIdentifier, 'enable_harvesting': information.enableHarvesting} registry_form= MyRegistryForm(data) except: registry_form = MyRegistryForm() context = RequestContext(request, { 'registry_form': registry_form, }) return HttpResponse(json.dumps({'template': template.render(context)}), content_type='application/javascript')
def update_my_registry(request): """ PUT http://localhost/oai_pmh/api/update/my-registry PUT data query='{"repositoryName":"value", "enableHarvesting":"True or False"}' """ try: #Serialization of the input data serializer = UpdateMyRegistrySerializer(data=request.DATA) #If it's valid if serializer.is_valid(): repositoryName = request.DATA['repositoryName'] enableHarvesting = request.DATA['enableHarvesting'] == 'True' return update_my_registry_model(repositoryName, enableHarvesting) 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)