Esempio n. 1
0
def start_view(request, pk):
    paper = get_object_or_404(Paper.objects.prefetch_related('oairecord_set'),
                              pk=pk)
    repositories_protocol = get_all_repositories_and_protocols(
        paper, request.user)
    used_protocols = set([proto for repo, proto in repositories_protocol])
    available_repositories = sorted(
        [repo for repo, proto in repositories_protocol],
        key=lambda r: r.name.lower())

    # select the most appropriate repository
    userprefs = UserPreferences.get_by_user(request.user)
    preselected_repository = userprefs.get_preferred_or_last_repository()
    preselected_protocol = None
    if preselected_repository:
        preselected_protocol = {
            repo.id: proto
            for repo, proto in repositories_protocol
        }.get(preselected_repository.id, None)
    # If the preferred repository is not available for this paper, pick any
    if not preselected_protocol:
        for repo, protocol in repositories_protocol:
            if protocol is not None:
                preselected_repository = repo
                preselected_protocol = protocol
                break

    breadcrumbs = paper.breadcrumbs()
    breadcrumbs.append((_('Deposit'), ''))
    context = {
        'paper':
        paper,
        'max_file_size':
        settings.DEPOSIT_MAX_FILE_SIZE,
        'available_repositories':
        available_repositories,
        'selected_repository':
        preselected_repository,
        'selected_protocol':
        preselected_protocol,
        'is_owner':
        paper.is_owned_by(request.user, flexible=True),
        'breadcrumbs':
        breadcrumbs,
        'repositoryForm':
        None,
        'paper_form':
        PaperDepositForm(initial={'radioUploadType': request.GET.get('type')}),
        'collapse_doctype':
        request.GET.get('type') in ['preprint', 'postprint', 'pdfversion'],
        'used_protocols':
        used_protocols,
    }
    return render(request, 'deposit/start.html', context)
Esempio n. 2
0
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
Esempio n. 3
0
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