<commit_before>from django.shortcuts import render_to_response, HttpResponse
from django.http import HttpResponseServerError
from django.template import RequestContext
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth.decorators import login_required
from models import SurveyDraft
from django.forms.models import model_to_dict
from rest_framework import viewsets
from rest_framework.decorators import action
from serializers import SurveyDraftSerializer
import json
import utils


def csv_to_xform(request):
    csv_data = request.POST.get('txtImport')

    survey = utils.create_survey_from_csv_text(csv_data)

    response = HttpResponse(survey.to_xml(),
                            mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=survey.xml'

    return response


def export_form_to_xform(request, id):
    try:
        survey = utils.create_survey_from_csv_text(SurveyDraft.objects.get(pk=id).body)

        response = HttpResponse(survey.to_xml(),
                                mimetype='application/force-download')

        response['Content-Disposition'] = 'attachment; filename=survey.xml'
    except Exception, e:
        response = HttpResponseServerError(json.dumps({
            u'error': e.message
        }))
    return response


@login_required
@ensure_csrf_cookie
def spa(request):
    if request.user.is_authenticated():
        user_details = {u'name': request.user.email,
                        u'gravatar': utils.gravatar_url(request.user.email)}
    else:
        user_details = {}
    return render_to_response("index.html",
                              context_instance=
                              RequestContext(request, {'user_details': json.dumps(user_details)}))


@login_required
def survey_drafts(request, sdid=0):
    if request.method == 'GET':
        if sdid > 0:
            return read_survey_draft(request, sdid)
        else:
            return list_survey_drafts(request)<commit_msg>#242 - breaks when exporting with empty optionset - removed initial patch (friendly error message)<commit_after>from django.shortcuts import render_to_response, HttpResponse
from django.http import HttpResponseServerError
from django.template import RequestContext
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth.decorators import login_required
from models import SurveyDraft
from django.forms.models import model_to_dict
from rest_framework import viewsets
from rest_framework.decorators import action
from serializers import SurveyDraftSerializer
import json
import utils


def csv_to_xform(request):
    csv_data = request.POST.get('txtImport')

    survey = utils.create_survey_from_csv_text(csv_data)

    response = HttpResponse(survey.to_xml(),
                            mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=survey.xml'

    return response


def export_form_to_xform(request, id):
    survey = utils.create_survey_from_csv_text(SurveyDraft.objects.get(pk=id).body)

    response = HttpResponse(survey.to_xml(),
                            mimetype='application/force-download')

    response['Content-Disposition'] = 'attachment; filename=survey.xml'
    return response


@login_required
@ensure_csrf_cookie
def spa(request):
    if request.user.is_authenticated():
        user_details = {u'name': request.user.email,
                        u'gravatar': utils.gravatar_url(request.user.email)}
    else:
        user_details = {}
    return render_to_response("index.html",
                              context_instance=
                              RequestContext(request, {'user_details': json.dumps(user_details)}))


@login_required
def survey_drafts(request, sdid=0):
    if request.method == 'GET':
        if sdid > 0:
            return read_survey_draft(request, sdid)
        else:
            return list_survey_drafts(request)
{'fraction_non_alphanumeric': 0.08511709896751447, 'fraction_numerical': 0.0017627801561319568, 'mean_word_length': 3.331515812431843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6923840', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1098, 'n_words': 233}