<commit_before>import csv
import io

import xlsxwriter
from flask import (
    abort,
    Blueprint,
    make_response,
    redirect,
    render_template,
    url_for
)
from flask.ext.login import current_user, login_required

from ..models import Hearing

hearing = Blueprint(
    name='hearing',
    import_name=__name__,
    url_prefix=''
)


# Redirects to the first hearing before the real index page is implemented.
@hearing.route('/kuulemiset')
def index():
    hearing = (
        Hearing.query
        .filter(Hearing.published)
        .first()
    )
    if not hearing:
        return redirect(url_for('frontpage.index'))

    return redirect(url_for('hearing.show', slug=hearing.slug))


@hearing.route('/<slug>')
def show(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not hearing:
        return abort(404)

    if not (
        hearing.published or
        current_user.is_authenticated() and
        (current_user.is_official or current_user.is_admin)
    ):
        return abort(404)

    commentable_sections_string = hearing.get_commentable_sections_string()

    return render_template(
        'hearing/show.html',
        hearing=hearing,
        commentable_sections_string=commentable_sections_string,
        hearing_page_active=True
    )


# This route is only for demo and preview purposes.
@hearing.route('/hameentie-ennakko')
@login_required
def hameentie():
    if not (current_user.is_official or current_user.is_admin):
        return abort(404)

    HAMEENTIE_HEARING_ID = 11
    hearing = Hearing.query.get(HAMEENTIE_HEARING_ID)

    if not hearing:
        return abort(404)

    commentable_sections_string = hearing.get_commentable_sections_string()

    return render_template(
        'hearing/show.html',
        hearing=hearing,
        commentable_sections_string=commentable_sections_string,
        hearing_page_active=True
    )


@hearing.route('/<slug>/raportti.csv')
def report_as_csv(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not (hearing and hearing.published):
        return abort(404)

    output = io.StringIO()
    writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)

    writer.writerow(hearing.report_headers)
    for comment in hearing.comments_for_report:
        writer.writerow(comment.csv_value_array)

    csv_as_string = output.getvalue()
    response = make_response(csv_as_string)
    response.headers['Content-Disposition'] = (
        'attachment; filename={filename}.csv'.format(
            filename=hearing.report_filename
        )
    )
    return response


@hearing.route('/<slug>/raportti.xlsx')
def report_as_xlsx(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not (hearing and hearing.published):
        return abort(404)

    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()

    worksheet.write_row(0, 0, hearing.report_headers)
    for index, comment in enumerate(hearing.comments_for_report, start=1):
        worksheet.write_row(index, 0, comment.csv_value_array)

    workbook.close()

    xlsx_data = output.getvalue()
    response = make_response(xlsx_data)
    response.headers['Content-Disposition'] = (
        'attachment; filename={filename}.xlsx'.format(
            filename=hearing.report_filename
        )
    )
    return response<commit_msg>Add formatting to xlsx report cells<commit_after>import csv
import io

import xlsxwriter
from flask import (
    abort,
    Blueprint,
    make_response,
    redirect,
    render_template,
    url_for
)
from flask.ext.login import current_user, login_required

from ..models import Hearing

hearing = Blueprint(
    name='hearing',
    import_name=__name__,
    url_prefix=''
)


# Redirects to the first hearing before the real index page is implemented.
@hearing.route('/kuulemiset')
def index():
    hearing = (
        Hearing.query
        .filter(Hearing.published)
        .first()
    )
    if not hearing:
        return redirect(url_for('frontpage.index'))

    return redirect(url_for('hearing.show', slug=hearing.slug))


@hearing.route('/<slug>')
def show(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not hearing:
        return abort(404)

    if not (
        hearing.published or
        current_user.is_authenticated() and
        (current_user.is_official or current_user.is_admin)
    ):
        return abort(404)

    commentable_sections_string = hearing.get_commentable_sections_string()

    return render_template(
        'hearing/show.html',
        hearing=hearing,
        commentable_sections_string=commentable_sections_string,
        hearing_page_active=True
    )


# This route is only for demo and preview purposes.
@hearing.route('/hameentie-ennakko')
@login_required
def hameentie():
    if not (current_user.is_official or current_user.is_admin):
        return abort(404)

    HAMEENTIE_HEARING_ID = 11
    hearing = Hearing.query.get(HAMEENTIE_HEARING_ID)

    if not hearing:
        return abort(404)

    commentable_sections_string = hearing.get_commentable_sections_string()

    return render_template(
        'hearing/show.html',
        hearing=hearing,
        commentable_sections_string=commentable_sections_string,
        hearing_page_active=True
    )


@hearing.route('/<slug>/raportti.csv')
def report_as_csv(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not (hearing and hearing.published):
        return abort(404)

    output = io.StringIO()
    writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)

    writer.writerow(hearing.report_headers)
    for comment in hearing.comments_for_report:
        writer.writerow(comment.csv_value_array)

    csv_as_string = output.getvalue()
    response = make_response(csv_as_string)
    response.headers['Content-Disposition'] = (
        'attachment; filename={filename}.csv'.format(
            filename=hearing.report_filename
        )
    )
    return response


@hearing.route('/<slug>/raportti.xlsx')
def report_as_xlsx(slug):
    hearing = (
        Hearing.query
        .filter(Hearing.slug == slug)
        .first()
    )

    if not (hearing and hearing.published):
        return abort(404)

    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()

    # Cell formatting.
    bolded = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 30)
    worksheet.set_column('B:D', 20)
    worksheet.set_column('E:E', 10)
    worksheet.set_column('F:F', 80)

    worksheet.write_row(0, 0, hearing.report_headers, bolded)
    for index, comment in enumerate(hearing.comments_for_report, start=1):
        worksheet.write_row(index, 0, comment.csv_value_array)

    workbook.close()

    xlsx_data = output.getvalue()
    response = make_response(xlsx_data)
    response.headers['Content-Disposition'] = (
        'attachment; filename={filename}.xlsx'.format(
            filename=hearing.report_filename
        )
    )
    return response
{'fraction_non_alphanumeric': 0.10233958133807634, 'fraction_numerical': 0.007661786838144753, 'mean_word_length': 2.962059620596206, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7188508', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2243, 'n_words': 432}