<commit_before>import datetime
import os
import shutil
import tempfile

from flask import render_template, redirect, url_for
from flask.ext.security import login_required, current_user

from . import report
from .forms import BasicReportForm, ProblemIdsReportForm
from ..args import BasicReport, ProblemIdsReport, SendEmail
from edx_data_research import reporting, tasks


@report.route('/')
@login_required
def index():
    return render_template('report/index.html')


@report.route('/problemids', methods=['GET', 'POST'])
@login_required
def report_problem_ids():
    form = ProblemIdsReportForm()
    if form.validate_on_submit():
        try:
            temp_dir = tempfile.mkdtemp()
            course = form.course.data
            anonymize = form.anonymize.data
            problem_ids = form.problem_ids.data
            problem_ids = [problem_id.strip() for problem_id in problem_ids.split(',')]
            final_attempt = form.final_attempt.data
            include_email = form.include_email.data
            #end_date = datetime.datetime.today().date() - datetime.timedelta(days=1)
            end_date = datetime.datetime(2016, 03, 06).date() - datetime.timedelta(days=1)
            start_date = end_date - datetime.timedelta(days=6)
            args = ProblemIdsReport(course, 'localhost', problem_ids, final_attempt,
                                    include_email, start_date, end_date, temp_dir, 10000,
                                    anonymize, None)
            edx_obj = reporting.ProblemIds(args)
            edx_obj.problem_ids()
            
            attachments = [os.path.join(temp_dir, _file)
                           for _file in os.listdir(temp_dir)]
            from_address = os.environ['FROM_EMAIL_ADDRESS']
            password = os.environ['FROM_EMAIL_PASSWORD']
            to_address = [current_user.email]
            subject = 'Weekly Report for {0} - {1}'.format(course.capitalize(),
                                                           datetime.datetime.today()
                                                           .date())
            args = SendEmail(from_address, None, password, to_address, None,
                             subject, attachments)
            email = tasks.Email(args)
            email.do()
        finally:
            shutil.rmtree(temp_dir)
        return redirect(url_for('report.report_problem_ids'))
    return render_template('report/problem_ids.html', form=form)


@report.route('/basic', methods=['GET', 'POST'])
@login_required
def report_basic():
    form = BasicReportForm()
    if form.validate_on_submit():
        try:
            temp_dir = tempfile.mkdtemp()
            course = form.course.data
            report_name = form.report.data
            anonymize = form.anonymize.data
            args = BasicReport(course, 'localhost', report_name, anonymize,
                               temp_dir, 10000)
            edx_obj = reporting.Basic(args)
            getattr(edx_obj, report_name)()

            attachments = [os.path.join(temp_dir, _file)
                           for _file in os.listdir(temp_dir)]
            from_address = os.environ['FROM_EMAIL_ADDRESS']
            password = <PASSWORD>['FROM_EMAIL_PASSWORD']
            to_address = [current_user.email]
            subject = 'Weekly Report for {0} - {1}'.format(course.capitalize(),
                                                           datetime.datetime.today()
                                                           .date())
            args = SendEmail(from_address, None, password, to_address, None,
                             subject, attachments)
            email = tasks.Email(args)
            email.do()
        finally:
            shutil.rmtree(temp_dir)
        return redirect(url_for('report.report_basic'))
    return render_template('report/basic.html', form=form)<commit_msg>Refactor code<commit_after>import datetime
import os
import shutil
import tempfile

from flask import render_template, redirect, url_for
from flask.ext.security import login_required, current_user

from . import report
from .forms import BasicReportForm, ProblemIdsReportForm
from ..args import BasicReport, ProblemIdsReport, SendEmail
from ..utils import send_email, temp_dir_context
from edx_data_research import reporting, tasks


@report.route('/')
@login_required
def index():
    return render_template('report/index.html')


@report.route('/problemids', methods=['GET', 'POST'])
@login_required
def report_problem_ids():
    form = ProblemIdsReportForm()
    if form.validate_on_submit():
        with temp_dir_context() as temp_dir:
            course = form.course.data
            anonymize = form.anonymize.data
            problem_ids = form.problem_ids.data
            problem_ids = [problem_id.strip() for problem_id in problem_ids.split(',')]
            final_attempt = form.final_attempt.data
            include_email = form.include_email.data
            #end_date = datetime.datetime.today().date() - datetime.timedelta(days=1)
            end_date = datetime.datetime(2016, 03, 06).date() - datetime.timedelta(days=1)
            start_date = end_date - datetime.timedelta(days=6)
            args = ProblemIdsReport(course, 'localhost', problem_ids, final_attempt,
                                    include_email, start_date, end_date, temp_dir, 10000,
                                    anonymize, None)
            edx_obj = reporting.ProblemIds(args)
            edx_obj.problem_ids()
            
            attachments = [os.path.join(temp_dir, _file)
                           for _file in os.listdir(temp_dir)]
            subject = 'ProblemIds Report for {0} - {1}'.format(course.capitalize(),
                                                               datetime.datetime.today()
                                                               .date())
            send_email(current_user.email, subject, attachments)
        return redirect(url_for('report.report_problem_ids'))
    return render_template('report/problem_ids.html', form=form)


@report.route('/basic', methods=['GET', 'POST'])
@login_required
def report_basic():
    form = BasicReportForm()
    if form.validate_on_submit():
        with temp_dir_context() as temp_dir:
            course = form.course.data
            report_name = form.report.data
            anonymize = form.anonymize.data
            args = BasicReport(course, 'localhost', report_name, anonymize,
                               temp_dir, 10000)
            edx_obj = reporting.Basic(args)
            getattr(edx_obj, report_name)()

            attachments = [os.path.join(temp_dir, _file)
                           for _file in os.listdir(temp_dir)]
            subject = '{0} Report for {1} - {2}'.format(report_name.capitalize(),
                                                        course.capitalize(),
                                                        datetime.datetime.today()
                                                        .date())
            send_email(current_user.email, subject, attachments)
        return redirect(url_for('report.report_basic'))
    return render_template('report/basic.html', form=form)
{'fraction_non_alphanumeric': 0.09658617818484597, 'fraction_numerical': 0.007077435470441299, 'mean_word_length': 1.946443172526574, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6601171', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1857, 'n_words': 390}