def get_context_data(self, *args, **kwargs): registration_provider = self.get_object() registration_provider_attributes = model_to_dict(registration_provider) registration_provider_attributes['default_license'] = registration_provider.default_license.name if registration_provider.default_license else None # compile html list of licenses_acceptable so we can render them as a list licenses_acceptable = list(registration_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>' registration_provider_attributes['licenses_acceptable'] = licenses_html # compile html list of subjects subject_ids = registration_provider.all_subjects.values_list('id', flat=True) kwargs['registration_provider'] = registration_provider_attributes kwargs['subject_ids'] = list(subject_ids) subject_html = '<ul class="subjects-list">' for parent in registration_provider.top_level_subjects: if parent.id in subject_ids: mapped_text = '' if parent.bepress_subject and parent.text != parent.bepress_subject.text: mapped_text = ' (mapped from {})'.format(parent.bepress_subject.text) hash_id = abs(hash(parent.text)) subject_html = subject_html + '<li data-id={}>{}'.format(hash_id, parent.text) + mapped_text + '</li>' child_html = '<ul class="three-cols" data-id={}>'.format(hash_id) 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>' registration_provider_attributes['subjects'] = subject_html fields = model_to_dict(registration_provider) kwargs['show_taxonomies'] = False if registration_provider.subjects.exists() else True kwargs['form'] = RegistrationProviderForm(initial=fields) kwargs['import_form'] = ImportFileForm() kwargs['taxonomy_form'] = RegistrationProviderCustomTaxonomyForm() # set api key for tinymce kwargs['tinymce_apikey'] = settings.TINYMCE_APIKEY return kwargs
def post(self, request, *args, **kwargs): # Import here to avoid test DB access errors when importing registration provider views from osf.management.commands.populate_custom_taxonomies import validate_input, migrate provider_form = RegistrationProviderCustomTaxonomyForm(request.POST) if provider_form.is_valid(): provider = RegistrationProvider.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, provider_type='osf.registrationprovider', 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, provider_type='osf.registrationprovider', add_missing=provider_form.cleaned_data['add_missing']) return redirect('registration_providers:detail', registration_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)