Example #1
0
def export_html(request, article, article_files):
    with translation.override(settings.LANGUAGE_CODE):
        html_file_path = files.create_temp_file(
            render_to_string('export/export.html',
                             context={
                                 'article': article,
                                 'journal': request.journal,
                                 'files': article_files,
                             }), '{code}-{pk}.html'.format(
                                 code=article.journal.code,
                                 pk=article.pk,
                             ))

    zip_file_name = 'export_{}_{}_html.zip'.format(article.journal.code,
                                                   article.pk)
    zip_path = os.path.join(files.TEMP_DIR, zip_file_name)
    zip_file = zipfile.ZipFile(zip_path, mode='w')
    zip_file.write(html_file_path, 'article_data.html')

    for file in article_files:
        zip_file.write(
            file.self_article_path(),
            file.original_filename,
        )

    zip_file.close()

    return files.serve_temp_file(zip_path, zip_file_name)
Example #2
0
def serve_failed_rows(request, tmp_file_name):
    if not tmp_file_name.startswith(utils.TMP_PREFIX):
        raise Http404
    filepath = files.get_temp_file_path_from_name(tmp_file_name)
    if not os.path.exists(filepath):
        raise Http404
    return files.serve_temp_file(filepath, 'failed_rows.csv')
Example #3
0
def prepare_export_for_issue(request, file=False):
    """
    Prepares an export for an issue
    :param request: HttpRequest object
    :param file: Boolean, returns a file path rather than a HttpRequest
    :return: Streaming zip file
    """
    issue_id = request.POST.get('export-issue', None)
    issue = get_object_or_404(models.Issue,
                              pk=issue_id,
                              journal=request.journal)

    temp_folder, folder_string = prepare_temp_folder(request, issue=issue)

    print('Processing {issue}'.format(issue=issue))

    for article in issue.articles.all():
        prepare_article(request, article, temp_folder)

    zip_portico_folder(temp_folder)

    if file:
        return [
            '{folder}.zip'.format(folder=temp_folder),
            '{filename}.zip'.format(filename=folder_string)
        ]

    return files.serve_temp_file(
        '{folder}.zip'.format(folder=temp_folder),
        '{filename}.zip'.format(filename=folder_string))
Example #4
0
def serve_csv_file(revenue_by_month):
    filename = '{0}.csv'.format(datetime.datetime.now())
    full_path = os.path.join(settings.BASE_DIR, 'files', 'temp', filename)
    with open(full_path, 'w') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')

        for month, value in revenue_by_month.items():
            csv_writer.writerow([month, value])

    return files.serve_temp_file(full_path, filename)
Example #5
0
def export_csv(rows):
    filename = '{0}.csv'.format(timezone.now())
    full_path = os.path.join(settings.BASE_DIR, 'files', 'temp', filename)

    with open(full_path, 'w') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')
        for row in rows:
            csv_writer.writerow(row)

    return serve_temp_file(full_path, filename)
Example #6
0
def csv_example(request):
    """
    Serves up an example metadata csv
    :param request: HttpRequest
    :return: CSV File
    """
    filepath = files.get_temp_file_path_from_name('metadata.csv')

    with open(filepath, "w") as f:
        wr = csv.writer(f)
        wr.writerow(utils.CSV_HEADER_ROW.split(","))
        wr.writerow(utils.CSV_MAURO.split(","))

        return files.serve_temp_file(filepath, 'metadata.csv')
Example #7
0
def export_institutions(modeladmin, request, queryset):
    headers = ['Name', 'Country', 'Active', 'First Name', 'Last Name', 'Email']
    filename = '{uuid}.csv'.format(uuid=uuid.uuid4())
    filepath = os.path.join(settings.BASE_DIR, 'files', 'temp', filename)
    with open(filepath, 'w') as file:
        writer = csv.writer(file, quoting=csv.QUOTE_ALL)
        writer.writerow(headers)

        for inst in queryset:
            writer.writerow([
                inst.name, inst.country, inst.active, inst.first_name,
                inst.last_name, inst.email_address
            ])

    return files.serve_temp_file(filepath, filename)
Example #8
0
def prepare_export_for_article(request):
    """
    Prepares a single article for export
    :param request: HttpRequest
    :return: Streaming zip file
    """
    article_id = request.POST.get('export-article')
    article = get_object_or_404(submission_models.Article,
                                pk=article_id,
                                journal=request.journal)

    issue = article.primary_issue if article.primary_issue else article.issue
    temp_folder, folder_string = prepare_temp_folder(request,
                                                     issue=issue,
                                                     article=article)
    prepare_article(request, article, temp_folder, article_only=True)
    zip_folder(temp_folder)

    return files.serve_temp_file(
        '{folder}.zip'.format(folder=temp_folder),
        '{filename}.zip'.format(filename=folder_string))
Example #9
0
def serve_review_file(assignment):
    """
    Produces a word document representing the review form.
    :param assignment: ReviewAssignment object
    :return: HttpStreamingResponse
    """
    elements = assignment.form.elements.all()
    document = Document()
    document.add_heading('Review #{pk}'.format(pk=assignment.pk), 0)
    document.add_heading('Review of `{article_title}` by {reviewer}'.format(
        article_title=assignment.article.title,
        reviewer=assignment.reviewer.full_name()),
                         level=1)
    document.add_paragraph()
    document.add_paragraph(
        'Complete the form below, then upload it under the "FILE UPLOAD" section on your review page'
        '. There is no need to complete the form on the web page if you are uploading this '
        'document.')
    document.add_paragraph()

    for element in elements:
        document.add_heading(element.name, level=2)
        document.add_paragraph(element.help_text)
        if element.choices:
            choices = render_choices(element.choices)
            table = document.add_table(rows=1, cols=2)
            hdr_cells = table.rows[0].cells
            hdr_cells[0].text = 'Choice'
            hdr_cells[1].text = 'Indication'

            for choice in element.choices.split('|'):
                row_cells = table.add_row().cells
                row_cells[0].text = str(choice)
        document.add_paragraph()

    filename = '{uuid}.docx'.format(uuid=uuid4())
    filepath = os.path.join(settings.BASE_DIR, 'files', 'temp', filename)
    document.save(filepath)
    return files.serve_temp_file(filepath, filename)
Example #10
0
def export_csv(request, article, article_files):
    elements = [
        'general.html',
        'authors.html',
        'files.html',
        'dates.html',
        'funding.html',
    ]

    context = {
        'article': article,
        'journal': request.journal,
        'files': article_files,
    }

    html = ''
    for element in elements:
        html = html + render_to_string(
            'export/elements/{element}'.format(element=element),
            context,
        )
    csv_file_path = html_table_to_csv(html)

    zip_file_name = 'export_{}_{}_csv.zip'.format(article.journal.code,
                                                  article.pk)
    zip_path = os.path.join(files.TEMP_DIR, zip_file_name)
    zip_file = zipfile.ZipFile(zip_path, mode='w')

    zip_file.write(csv_file_path, 'article_data.csv')

    for file in article_files:
        zip_file.write(
            file.self_article_path(),
            file.original_filename,
        )

    zip_file.close()

    return files.serve_temp_file(zip_path, zip_file_name)