Exemplo n.º 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')
                         }))
Exemplo n.º 2
0
    def get_context_data(self, *args, **kwargs):
        preprint_provider = self.get_object()
        subject_ids = preprint_provider.all_subjects.values_list('id', flat=True)

        preprint_provider_attributes = model_to_dict(preprint_provider)
        kwargs.setdefault('page_number', self.request.GET.get('page', '1'))

        licenses_acceptable = list(preprint_provider.licenses_acceptable.values_list('name', flat=True))
        licenses_html = '<ul>'
        for license in licenses_acceptable:
            licenses_html += '<li>{}</li>'.format(license)
        licenses_html += '</ul>'
        preprint_provider_attributes['licenses_acceptable'] = licenses_html

        subject_html = '<ul class="three-cols">'
        for parent in preprint_provider.top_level_subjects:
            mapped_text = ''
            if parent.bepress_subject and parent.text != parent.bepress_subject.text:
                mapped_text = ' (mapped from {})'.format(parent.bepress_subject.text)
            subject_html = subject_html + '<li>{}'.format(parent.text) + mapped_text + '</li>'
            child_html = '<ul>'
            for child in parent.children.all():
                grandchild_html = ''
                if child.id in subject_ids:
                    child_mapped_text = ''
                    if child.bepress_subject and child.text != child.bepress_subject.text:
                        child_mapped_text = ' (mapped from {})'.format(child.bepress_subject.text)
                    child_html = child_html + '<li>{}'.format(child.text) + child_mapped_text + '</li>'
                    grandchild_html = '<ul>'
                    for grandchild in child.children.all():
                        if grandchild.id in subject_ids:
                            grandchild_mapped_text = ''
                            if grandchild.bepress_subject and grandchild.text != grandchild.bepress_subject.text:
                                grandchild_mapped_text = ' (mapped from {})'.format(grandchild.bepress_subject.text)
                            grandchild_html = grandchild_html + '<li>{}'.format(grandchild.text) + grandchild_mapped_text + '</li>'
                    grandchild_html += '</ul>'
                child_html += grandchild_html

            child_html += '</ul>'
            subject_html += child_html

        subject_html += '</ul>'
        preprint_provider_attributes['subjects_acceptable'] = subject_html
        preprint_provider_attributes['lower_name'] = preprint_provider._id

        kwargs['preprint_provider_id'] = preprint_provider._id
        kwargs['preprint_provider'] = preprint_provider_attributes
        kwargs['subject_ids'] = list(subject_ids)
        kwargs['logo'] = preprint_provider.get_asset_url('square_color_no_transparent')
        fields = model_to_dict(preprint_provider)
        fields['toplevel_subjects'] = list(subject_ids)
        fields['subjects_chosen'] = ', '.join(str(i) for i in subject_ids)
        kwargs['show_taxonomies'] = False if preprint_provider.subjects.exists() else True
        kwargs['form'] = PreprintProviderForm(initial=fields)
        kwargs['taxonomy_form'] = PreprintProviderCustomTaxonomyForm()
        kwargs['import_form'] = ImportFileForm()
        kwargs['tinymce_apikey'] = settings.TINYMCE_APIKEY
        return kwargs
Exemplo n.º 3
0
 def get(self, request, *args, **kwargs):
     data = self.get_subjects(request)
     return render(
         request,
         self.template_name,
         {
             'preprint_provider_id': self.kwargs.get('preprint_provider_id'),
             'subject_ids': data['subject_ids'],
             'taxonomy_form': PreprintProviderCustomTaxonomyForm()
         }
     )
Exemplo n.º 4
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
                }
            )
        )
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def get(self, request, *args, **kwargs):
     data = self.get_subjects(request)
     preprint_provider = PreprintProvider.objects.get(
         id=int(self.kwargs.get('preprint_provider_id')))
     return render(
         request, self.template_name, {
             'preprint_provider_id':
             self.kwargs.get('preprint_provider_id'),
             'subject_ids':
             data['subject_ids'],
             'taxonomy_form':
             PreprintProviderCustomTaxonomyForm(),
             'taxonomies_created':
             False if not preprint_provider.subjects.exists() else True
         })