Example #1
0
    def post(self, request, *args, **kwargs):
        # Import here to avoid test DB access errors when importing preprint provider views
        from osf.management.commands.populate_custom_taxonomies import validate_input, migrate

        provider_form = PreprintProviderCustomTaxonomyForm(request.POST)
        if provider_form.is_valid():
            provider = PreprintProvider.objects.get(
                id=request.POST.get('provider_id'))
            try:
                taxonomy_json = json.loads(
                    provider_form.cleaned_data['custom_taxonomy_json'])
                if request.is_ajax():
                    # An ajax request is for validation only, so run that validation!
                    response_data = validate_input(
                        custom_provider=provider,
                        data=taxonomy_json,
                        add_missing=provider_form.cleaned_data['add_missing'])
                    if response_data:
                        added_subjects = [
                            subject.text for subject in response_data
                        ]
                        messages.success(
                            f'Custom taxonomy validated with added subjects: {added_subjects}'
                        )
                else:
                    # Actually do the migration of the custom taxonomies
                    migrate(
                        provider=provider._id,
                        data=taxonomy_json,
                        add_missing=provider_form.cleaned_data['add_missing'])
                    return redirect('preprint_providers:detail',
                                    preprint_provider_id=provider.id)
            except (ValueError, RuntimeError, AssertionError) as error:
                messages.error(
                    request,
                    f'There is an error with the submitted JSON or the provider. Here are some details: {str(error)}'
                )
        else:
            for key, value in provider_form.errors.items():
                messages.error(request, f'{key}: {value}')

        return redirect(
            reverse_lazy('preprint_providers:process_custom_taxonomy',
                         kwargs={
                             'preprint_provider_id':
                             kwargs.get('preprint_provider_id')
                         }))
Example #2
0
    def post(self, request, *args, **kwargs):
        # Import here to avoid test DB access errors when importing preprint provider views
        from osf.management.commands.populate_custom_taxonomies import validate_input, migrate

        provider_form = PreprintProviderCustomTaxonomyForm(request.POST)
        if provider_form.is_valid():
            provider = PreprintProvider.objects.get(id=provider_form.cleaned_data['provider_id'])
            try:
                taxonomy_json = json.loads(provider_form.cleaned_data['custom_taxonomy_json'])
                if request.is_ajax():
                    # An ajax request is for validation only, so run that validation!
                    try:
                        response_data = validate_input(custom_provider=provider, data=taxonomy_json, add_missing=provider_form.cleaned_data['add_missing'])
                        if response_data:
                            added_subjects = [subject.text for subject in response_data]
                            response_data = {'message': 'Custom taxonomy validated with added subjects: {}'.format(added_subjects), 'feedback_type': 'success'}
                    except (RuntimeError, AssertionError) as script_feedback:
                        response_data = {'message': script_feedback.message, 'feedback_type': 'error'}
                    if not response_data:
                        response_data = {'message': 'Custom taxonomy validated!', 'feedback_type': 'success'}
                else:
                    # Actually do the migration of the custom taxonomies
                    migrate(provider=provider._id, data=taxonomy_json, add_missing=provider_form.cleaned_data['add_missing'])
                    return redirect('preprint_providers:detail', preprint_provider_id=provider.id)
            except (ValueError, RuntimeError, AssertionError) as error:
                messages.error(request, f'There is an error with the submitted JSON or the provider. Here are some details: {str(error)}')
        else:
            messages.error(request, f'There is a problem with the form. Here are some details:  {provider_form.errors}')

        return redirect(
            reverse_lazy(
                'preprint_providers:process_custom_taxonomy',
                kwargs={
                    'preprint_provider_id': provider.id
                }
            )
        )
Example #3
0
    def post(self, request, *args, **kwargs):
        # Import here to avoid test DB access errors when importing preprint provider views
        from osf.management.commands.populate_custom_taxonomies import validate_input, migrate

        provider_form = PreprintProviderCustomTaxonomyForm(request.POST)
        if provider_form.is_valid():
            provider = PreprintProvider.objects.get(id=provider_form.cleaned_data['provider_id'])
            try:
                taxonomy_json = json.loads(provider_form.cleaned_data['custom_taxonomy_json'])
                if request.is_ajax():
                    # An ajax request is for validation only, so run that validation!
                    try:
                        response_data = validate_input(custom_provider=provider, data=taxonomy_json, add_missing=provider_form.cleaned_data['add_missing'])
                        if response_data:
                            added_subjects = [subject.text for subject in response_data]
                            response_data = {'message': 'Custom taxonomy validated with added subjects: {}'.format(added_subjects), 'feedback_type': 'success'}
                    except (RuntimeError, AssertionError) as script_feedback:
                        response_data = {'message': script_feedback.message, 'feedback_type': 'error'}
                    if not response_data:
                        response_data = {'message': 'Custom taxonomy validated!', 'feedback_type': 'success'}
                else:
                    # Actually do the migration of the custom taxonomies
                    migrate(provider=provider._id, data=taxonomy_json, add_missing=provider_form.cleaned_data['add_missing'])
                    return redirect('preprint_providers:detail', preprint_provider_id=provider.id)
            except (ValueError, RuntimeError) as error:
                response_data = {
                    'message': 'There is an error with the submitted JSON or the provider. Here are some details: ' + error.message,
                    'feedback_type': 'error'
                }
        else:
            response_data = {
                'message': 'There is a problem with the form. Here are some details: ' + unicode(provider_form.errors),
                'feedback_type': 'error'
            }
        # Return a JsonResponse with the JSON error or the validation error if it's not doing an actual migration
        return JsonResponse(response_data)