<commit_before>import json
import traceback

from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import Http404
from django import http
from django.shortcuts import redirect
from django.views import generic

import reversion

from editor.forms import NewQuestionForm, QuestionForm
from editor.models import NewQuestion, Extension, Resource
import editor.views.generic
import editor.views.editoritem

from accounts.models import UserProfile

class PreviewView(editor.views.editoritem.PreviewView):
    
    """Compile question as a preview and return its URL."""
    
    model = NewQuestion
    
    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),
            }
            return http.HttpResponseServerError(json.dumps(status),
                                           content_type='application/json')
        else:
            try:
                profile = UserProfile.objects.get(user=request.user)
                q.locale = profile.language
            except UserProfile.ObjectDoesNotExist:
                pass

            return self.preview(q.editoritem)


class ZipView(editor.views.editoritem.ZipView):

    """Compile a question as a SCORM package and return the .zip file"""

    model = NewQuestion

    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
            scorm = 'scorm' in request.GET
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),
            }
            return http.HttpResponseServerError(json.dumps(status),
                                           content_type='application/json')
        else:
            try:
                profile = UserProfile.objects.get(user=request.user)
                q.locale = profile.language
            except UserProfile.ObjectDoesNotExist:
                pass

            return self.download(q.editoritem, scorm)


class SourceView(editor.views.editoritem.SourceView):

    """Compile a question as a SCORM package and return the .zip file"""

    model = NewQuestion

    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),}<commit_msg>catch TypeError when anonymous user trying to preview<commit_after>import json
import traceback

from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import Http404
from django import http
from django.shortcuts import redirect
from django.views import generic

import reversion

from editor.forms import NewQuestionForm, QuestionForm
from editor.models import NewQuestion, Extension, Resource
import editor.views.generic
import editor.views.editoritem

from accounts.models import UserProfile

class PreviewView(editor.views.editoritem.PreviewView):
    
    """Compile question as a preview and return its URL."""
    
    model = NewQuestion
    
    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),
            }
            return http.HttpResponseServerError(json.dumps(status),
                                           content_type='application/json')
        else:
            try:
                profile = UserProfile.objects.get(user=request.user)
                q.locale = profile.language
            except (UserProfile.ObjectDoesNotExist,TypeError):
                pass

            return self.preview(q.editoritem)


class ZipView(editor.views.editoritem.ZipView):

    """Compile a question as a SCORM package and return the .zip file"""

    model = NewQuestion

    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
            scorm = 'scorm' in request.GET
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),
            }
            return http.HttpResponseServerError(json.dumps(status),
                                           content_type='application/json')
        else:
            try:
                profile = UserProfile.objects.get(user=request.user)
                q.locale = profile.language
            except (UserProfile.ObjectDoesNotExist,TypeError):
                pass

            return self.download(q.editoritem, scorm)


class SourceView(editor.views.editoritem.SourceView):

    """Compile a question as a SCORM package and return the .zip file"""

    model = NewQuestion

    def get(self, request, *args, **kwargs):
        try:
            q = self.get_object()
        except (NewQuestion.DoesNotExist, TypeError) as err:
            status = {
                "result": "error",
                "message": str(err),
                "traceback": traceback.format_exc(),}
{'fraction_non_alphanumeric': 0.08873239436619719, 'fraction_numerical': 0.001056338028169014, 'mean_word_length': 2.041220556745182, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84468', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1405, 'n_words': 388}