SciPost Code Repository

Skip to content
Snippets Groups Projects
views.py 45.8 KiB
Newer Older
    # create a doi_batch_id
    salt = ""
    for i in range(5):
        salt = salt + random.choice(string.ascii_letters)
    salt = salt.encode('utf8')
    idsalt = str(_object)[:10]
    idsalt = idsalt.encode('utf8')
    timestamp=timezone.now().strftime('%Y%m%d%H%M%S')
    doi_batch_id = hashlib.sha1(salt+idsalt).hexdigest()
    metadata_xml = (
        '<?xml version="1.0" encoding="UTF-8"?>\n'
        '<doi_batch version="4.4.0" xmlns="http://www.crossref.org/schema/4.4.0" '
        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
        'xmlns:fr="http://www.crossref.org/fundref.xsd" '
        'xsi:schemaLocation="http://www.crossref.org/schema/4.4.0 '
        'http://www.crossref.org/shema/deposit/crossref4.4.0.xsd" '
        'xmlns:ai="http://www.crossref.org/AccessIndicators.xsd">\n'
        '<head>\n'
        '<doi_batch_id>' + str(doi_batch_id) + '</doi_batch_id>\n'
        '<timestamp>' + timestamp + '</timestamp>\n'
        '<depositor>\n'
        '<depositor_name>scipost</depositor_name>\n'
        '<email_address>admin@scipost.org</email_address>\n'
        '</depositor>\n'
        '<registrant>scipost</registrant>\n'
        '</head>\n'
        '<body>\n'
        '<database><dataset>\n'
        '<dataset_type>component<\dataset_type>\n'
        '<doi_data><doi>' + _object.doi_string + '</doi>\n'
        '<resource>' + _object.get_absolute_url() + '</resource>\n'
        '</dataset></database>\n'
        '</body></doi_batch>'
        )
    url = 'http://doi.crossref.org/servlet/deposit'
    params = {
        'operation': 'doMDUpload',
        'login_id': settings.CROSSREF_LOGIN_ID,
        'login_passwd': settings.CROSSREF_LOGIN_PASSWORD,
        }
    files = {'fname': ('metadata.xml', metadata_xml, 'multipart/form-data')}
    r = requests.post(url, params=params, files=files)
    deposit = GenericDOIDeposit(content_type=ContentType.objects.get_for_model(_object),
                                object_id=object_id,
                                content_object=_object,
                                timestamp=timestamp,
                                doi_batch_id=doi_batch_id,
                                metadata_xml=metadata_xml,
                                deposition_date=timezone.now(),
                                response=r.text)
    deposit.save()
    context = {
        'response_headers': r.headers,
        'response_text': r.text,
    }
    return render(request, 'journals/generic_metadata_xml_deposit.html', context)


@permission_required('scipost.can_publish_accepted_submission', return_403=True)
def mark_generic_deposit_success(request, deposit_id, success):
    deposit = get_object_or_404(GenericDOIDeposit, pk=deposit_id)
    if success == '1':
        deposit.deposit_successful = True
    elif success == '0':
        deposit.deposit_successful = False
    deposit.save()
    if deposit.content_type.name == 'report':
        return redirect(reverse('journals:manage_report_metadata'))
    else:
        return redirect(reverse('journals:manage_comment_metadata'))


def publication_detail(request, doi_label):
    publication = Publication.objects.get_published(doi_label=doi_label)
    journal = publication.in_issue.in_volume.in_journal

    context = {
        'publication': publication,
        'journal': journal
    return render(request, 'journals/publication_detail.html', context)


def publication_detail_pdf(request, doi_label):
    publication = Publication.objects.get_published(doi_label=doi_label)
    response = HttpResponse(publication.pdf_file.read(), content_type='application/pdf')
    response['Content-Disposition'] = ('filename='
                                       + publication.doi_label.replace('.', '_') + '.pdf')
    return response