def submitDeposit(request, pk): paper = get_object_or_404(Paper, pk=pk) context = {'status': 'error'} form = PaperDepositForm(data=request.POST) if not form.is_valid(): context['form'] = form.errors return context, 400 # This validation could take place in the form (but we need access to the # paper and user?) repository = form.cleaned_data['radioRepository'] protocol = repository.protocol_for_deposit(paper, request.user) if protocol is None: context['radioRepository'] = _( "This repository cannot be used for this paper.") return context, 400 repositoryForm = protocol.get_bound_form(request.POST) if not repositoryForm.is_valid(): request_context = RequestContext(request) form_html = render_crispy_form(repositoryForm, context=request_context) context['form_html'] = form_html return context, 400 # Store the repository as in the user preferences userprefs = UserPreferences.get_by_user(request.user) userprefs.last_repository = repository userprefs.save(update_fields=['last_repository']) # Check that the paper has been uploaded by the same user pdf = form.cleaned_data['file_id'] if pdf.user != request.user: context['message'] = _('Access to the PDF was denied.') return context, 400 # Submit the paper to the repository path = os.path.join(settings.MEDIA_ROOT, pdf.file.name) # Create initial record d = DepositRecord(paper=paper, user=pdf.user, repository=repository, upload_type=form.cleaned_data['radioUploadType'], file=pdf) d.save() submitResult = protocol.submit_deposit_wrapper(path, repositoryForm) d.request = submitResult.logs if submitResult.status == 'failed': context['message'] = submitResult.message d.save() # Send the failed deposit as error to sentry msg = "Deposit failed for id %s for paper %s \n\n" % (d.pk, paper.pk) logger.error(msg + submitResult.logs) return context, 400 d.identifier = submitResult.identifier d.additional_info = submitResult.additional_info d.status = submitResult.status d.oairecord = submitResult.oairecord d.license = submitResult.license d.save() paper.update_availability() paper.save() paper.update_index() context['status'] = 'success' # TODO change this (we don't need it) context['upload_id'] = d.id return context
def submitDeposit(request, pk): paper = get_object_or_404(Paper, pk=pk) context = {'status': 'error'} form = PaperDepositForm(data=request.POST) if not form.is_valid(): context['form'] = form.errors return context, 400 # This validation could take place in the form (but we need access to the # paper and user?) repository = form.cleaned_data['radioRepository'] protocol = repository.protocol_for_deposit(paper, request.user) if protocol is None: context['radioRepository'] = _( "This repository cannot be used for this paper.") return context, 400 repositoryForm = protocol.get_bound_form(request.POST) if not repositoryForm.is_valid(): request_context = RequestContext(request) form_html = render_crispy_form(repositoryForm, context=request_context) context['form_html'] = form_html return context, 400 # Store the repository as in the user preferences userprefs = UserPreferences.get_by_user(request.user) userprefs.last_repository = repository userprefs.save(update_fields=['last_repository']) # Check that the paper has been uploaded by the same user pdf = form.cleaned_data['file_id'] if pdf.user != request.user: context['message'] = _('Access to the PDF was denied.') return context, 400 # Submit the paper to the repository path = os.path.join(settings.MEDIA_ROOT, pdf.file.name) # Create initial record d = DepositRecord( paper=paper, user=pdf.user, repository=repository, upload_type=form.cleaned_data['radioUploadType'], file=pdf) d.save() submitResult = protocol.submit_deposit_wrapper(path, repositoryForm) d.request = submitResult.logs if submitResult.status == 'failed': context['message'] = submitResult.message d.save() return context, 400 d.identifier = submitResult.identifier d.additional_info = submitResult.additional_info d.status = submitResult.status d.oairecord = submitResult.oairecord d.save() paper.update_availability() paper.save() paper.update_index() context['status'] = 'success' # TODO change this (we don't need it) context['upload_id'] = d.id return context