<commit_before>"""Django views implementing the XBlock workbench.

This code is in the Workbench layer.

"""

import logging
import mimetypes
import pkg_resources
from StringIO import StringIO

from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.views.decorators.csrf import ensure_csrf_cookie

from .runtime import Usage, create_xblock, MEMORY_KVS
from .scenarios import SCENARIOS
from .request import webob_to_django_response, django_to_webob_request


# --- Set up an in-memory logger

LOG_STREAM = None

def setup_logging():
    global LOG_STREAM
    LOG_STREAM = StringIO()

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(LOG_STREAM)
    handler.setFormatter(logging.Formatter("<p>%(asctime)s %(name)s %(levelname)s: %(message)s</p>"))
    root_logger.addHandler(handler)

setup_logging()

log = logging.getLogger(__name__)


# We don't really have authentication and multiple students, just accept their
# id on the URL.
def get_student_id(request):
    student_id = int(request.GET.get('student', '98'))
    return student_id


#---- Views -----

def index(request):
    return render_to_response('index.html', {
        'scenarios': [(i, scenario.description) for i, scenario in enumerate(SCENARIOS)]
    })


@ensure_csrf_cookie
def show_scenario(request, scenario_id):
    student_id = get_student_id(request)
    log.info("Start show_scenario %s for %s", scenario_id, student_id)
    scenario = SCENARIOS[int(scenario_id)]
    usage = scenario.usage
    usage.store_initial_state()
    block = create_xblock(usage, "student%s" % student_id)
    frag = block.runtime.render(block, {}, 'student_view')
    log.info("End show_scenario %s", scenario_id)
    return render_to_response('block.html', {
        'scenario': scenario,
        'block': block,
        'body': frag.body_html(),
        'database': MEMORY_KVS,
        'head_html': frag.head_html(),
        'foot_html': frag.foot_html(),<commit_msg>Use a more reasonable default value for student id<commit_after>"""Django views implementing the XBlock workbench.

This code is in the Workbench layer.

"""

import logging
import mimetypes
import pkg_resources
from StringIO import StringIO

from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.views.decorators.csrf import ensure_csrf_cookie

from .runtime import Usage, create_xblock, MEMORY_KVS
from .scenarios import SCENARIOS
from .request import webob_to_django_response, django_to_webob_request


# --- Set up an in-memory logger

LOG_STREAM = None

def setup_logging():
    global LOG_STREAM
    LOG_STREAM = StringIO()

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(LOG_STREAM)
    handler.setFormatter(logging.Formatter("<p>%(asctime)s %(name)s %(levelname)s: %(message)s</p>"))
    root_logger.addHandler(handler)

setup_logging()

log = logging.getLogger(__name__)


# We don't really have authentication and multiple students, just accept their
# id on the URL.
def get_student_id(request):
    student_id = int(request.GET.get('student', '1'))
    return student_id


#---- Views -----

def index(request):
    return render_to_response('index.html', {
        'scenarios': [(i, scenario.description) for i, scenario in enumerate(SCENARIOS)]
    })


@ensure_csrf_cookie
def show_scenario(request, scenario_id):
    student_id = get_student_id(request)
    log.info("Start show_scenario %s for %s", scenario_id, student_id)
    scenario = SCENARIOS[int(scenario_id)]
    usage = scenario.usage
    usage.store_initial_state()
    block = create_xblock(usage, "student%s" % student_id)
    frag = block.runtime.render(block, {}, 'student_view')
    log.info("End show_scenario %s", scenario_id)
    return render_to_response('block.html', {
        'scenario': scenario,
        'block': block,
        'body': frag.body_html(),
        'database': MEMORY_KVS,
        'head_html': frag.head_html(),
        'foot_html': frag.foot_html(),
{'fraction_non_alphanumeric': 0.11046228710462287, 'fraction_numerical': 0.0021897810218978104, 'mean_word_length': 4.359843546284225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7086623', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1284, 'n_words': 317}