<commit_before>from StringIO import StringIO
import json
import zipfile

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.views.generic import View
from lxml import etree
from lxml.etree import XMLSyntaxError

from website.apps.ts_om.models import Scenario, ExperimentFile
from website.apps.ts_om.submit import submit
from website.apps.ts_om.views.ScenarioValidationView import rest_validate


class ScenarioView(View):
    pass


def duplicate_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

    if not scenario:
        return

    xml = scenario.xml

    try:
        tree = etree.parse(StringIO(str(xml)))
    except XMLSyntaxError:
        return
    else:
        tree.getroot().set('name', scenario.name + " (duplicate)")
        xml = etree.tostring(tree.getroot(), encoding='UTF-8')

    new_scenario = Scenario.objects.create(xml=xml, start_date=scenario.start_date, user=request.user)
    new_scenario.save()

    return HttpResponseRedirect(reverse('ts_om.summary2', kwargs={'scenario_id': new_scenario.id}))


def download_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

    if not scenario:
        return

    f = StringIO(str(scenario.xml))
    parser = etree.XMLParser(remove_blank_text=True)
    tree = etree.parse(f, parser)
    xml = etree.tostring(tree.getroot(), encoding='UTF-8', pretty_print=True)

    return HttpResponse(xml)


def download_experiment_scenario(request, scenario_id, index):
    scenario_id = int(scenario_id)
    index = int(index)
    xml = ""

    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    experiment = ExperimentFile.objects.get(user=request.user, id=int(scenario_id))

    if not experiment:
        return

    experiment_file = experiment.file
    proj_path = getattr(settings, "PROJECT_PATH", None)
    full_path = proj_path + experiment_file.url

    if zipfile.is_zipfile(experiment_file):
        exp_zip = zipfile.ZipFile(full_path)
        name_lst = exp_zip.namelist()

        n = [name for name in name_lst if name.endswith(".xml")][index]
        with exp_zip.open(n) as exp_file:
            xml = exp_file.read()
    else:
        if experiment_file.url.endswith(".xml"):
            xml = experiment_file.read()

    return HttpResponse(xml, content_type="text/xml")


def save_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    xml_file = request.read()
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)<commit_msg>Fix individual scenario download links in experiment view.<commit_after>from StringIO import StringIO
import json
import zipfile

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.views.generic import View
from lxml import etree
from lxml.etree import XMLSyntaxError

from website.apps.ts_om.models import Scenario, ExperimentFile
from website.apps.ts_om.submit import submit
from website.apps.ts_om.views.ScenarioValidationView import rest_validate


class ScenarioView(View):
    pass


def duplicate_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

    if not scenario:
        return

    xml = scenario.xml

    try:
        tree = etree.parse(StringIO(str(xml)))
    except XMLSyntaxError:
        return
    else:
        tree.getroot().set('name', scenario.name + " (duplicate)")
        xml = etree.tostring(tree.getroot(), encoding='UTF-8')

    new_scenario = Scenario.objects.create(xml=xml, start_date=scenario.start_date, user=request.user)
    new_scenario.save()

    return HttpResponseRedirect(reverse('ts_om.summary2', kwargs={'scenario_id': new_scenario.id}))


def download_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

    if not scenario:
        return

    f = StringIO(str(scenario.xml))
    parser = etree.XMLParser(remove_blank_text=True)
    tree = etree.parse(f, parser)
    xml = etree.tostring(tree.getroot(), encoding='UTF-8', pretty_print=True)

    return HttpResponse(xml)


def download_experiment_scenario(request, scenario_id, index):
    scenario_id = int(scenario_id)
    index = int(index)
    xml = ""

    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    experiment = ExperimentFile.objects.get(user=request.user, id=int(scenario_id))

    if not experiment:
        return

    experiment_file = experiment.file
    proj_path = getattr(settings, "MEDIA_ROOT", None)
    full_path = proj_path + "/" + experiment_file.url

    if zipfile.is_zipfile(experiment_file):
        exp_zip = zipfile.ZipFile(full_path)
        name_lst = exp_zip.namelist()

        n = [name for name in name_lst if name.endswith(".xml")][index]
        with exp_zip.open(n) as exp_file:
            xml = exp_file.read()
    else:
        if experiment_file.url.endswith(".xml"):
            xml = experiment_file.read()

    return HttpResponse(xml, content_type="text/xml")


def save_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    xml_file = request.read()
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)
{'fraction_non_alphanumeric': 0.09670953733088358, 'fraction_numerical': 0.0023383998663771507, 'mean_word_length': 3.6854460093896715, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221558', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1839, 'n_words': 422}