<commit_before>from datetime import datetime
from pprint import pformat

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render
from django.views.generic.edit import CreateView, UpdateView
from django.utils.decorators import method_decorator

from fjord.base.utils import analyzer_required, check_new_user
from fjord.heartbeat.forms import SurveyCreateForm
from fjord.heartbeat.models import Answer, Survey
from fjord.journal.models import Record


@check_new_user
@analyzer_required
def hb_data(request, answerid=None):
    """View for hb data that shows one or all of the answers"""
    VALID_SORTBY_FIELDS = ('id', 'received_ts', 'updated_ts')

    ALL_FIELDS = [
        'id', 'received_ts', 'updated_ts', 'experiment_version',
        'response_version', 'person_id', 'survey_id',
        'flow_id', 'question_id', 'question_text', 'variation_id',
        'score', 'max_score', 'flow_began_ts', 'flow_offered_ts',
        'flow_voted_ts', 'flow_engaged_ts', 'platform', 'channel',
        'version', 'locale', 'country', 'build_id', 'partner_id',
        'profile_age', 'profile_usage', 'addons', 'extra'
    ]

    DEFAULT_FIELDS = [
        'id', 'received_ts', 'updated_ts', 'experiment_version',
        'response_version', 'person_id', 'survey_id', 'flow_id',
        'question_id', 'variation_id', 'score', 'max_score',
        'flow_began_ts', 'flow_voted_ts', 'platform', 'channel',
        'locale'
    ]

    sortby = 'id'
    answer = None
    answers = []
    survey = None
    showdata_filter = None

    if answerid is not None:
        answer = Answer.objects.get(id=answerid)

    else:
        # Get fields and split on comma, then get rid of empty fields.
        fields = request.GET.get('fields', '').split(',')

        # Make sure all the fields
        fields = [field for field in fields if field and field in ALL_FIELDS]

        # If we have no fields, then use default_fields.
        fields = fields or DEFAULT_FIELDS

        sortby = request.GET.get('sortby', sortby)
        if sortby not in VALID_SORTBY_FIELDS:
            sortby = 'id'

        page = request.GET.get('page')
        answers = Answer.objects.order_by('-' + sortby)
        survey_filter = request.GET.get('survey', survey)
        showdata_filter = request.GET.get('showdata', None)

        if showdata_filter:<commit_msg>[bug 1213340] Fix unfinished comment<commit_after>from datetime import datetime
from pprint import pformat

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render
from django.views.generic.edit import CreateView, UpdateView
from django.utils.decorators import method_decorator

from fjord.base.utils import analyzer_required, check_new_user
from fjord.heartbeat.forms import SurveyCreateForm
from fjord.heartbeat.models import Answer, Survey
from fjord.journal.models import Record


@check_new_user
@analyzer_required
def hb_data(request, answerid=None):
    """View for hb data that shows one or all of the answers"""
    VALID_SORTBY_FIELDS = ('id', 'received_ts', 'updated_ts')

    ALL_FIELDS = [
        'id', 'received_ts', 'updated_ts', 'experiment_version',
        'response_version', 'person_id', 'survey_id',
        'flow_id', 'question_id', 'question_text', 'variation_id',
        'score', 'max_score', 'flow_began_ts', 'flow_offered_ts',
        'flow_voted_ts', 'flow_engaged_ts', 'platform', 'channel',
        'version', 'locale', 'country', 'build_id', 'partner_id',
        'profile_age', 'profile_usage', 'addons', 'extra'
    ]

    DEFAULT_FIELDS = [
        'id', 'received_ts', 'updated_ts', 'experiment_version',
        'response_version', 'person_id', 'survey_id', 'flow_id',
        'question_id', 'variation_id', 'score', 'max_score',
        'flow_began_ts', 'flow_voted_ts', 'platform', 'channel',
        'locale'
    ]

    sortby = 'id'
    answer = None
    answers = []
    survey = None
    showdata_filter = None

    if answerid is not None:
        answer = Answer.objects.get(id=answerid)

    else:
        # Get fields and split on comma, then get rid of empty fields.
        fields = request.GET.get('fields', '').split(',')

        # Nix any fields that don't exist
        fields = [field for field in fields if field and field in ALL_FIELDS]

        # If we have no fields, then use default_fields.
        fields = fields or DEFAULT_FIELDS

        sortby = request.GET.get('sortby', sortby)
        if sortby not in VALID_SORTBY_FIELDS:
            sortby = 'id'

        page = request.GET.get('page')
        answers = Answer.objects.order_by('-' + sortby)
        survey_filter = request.GET.get('survey', survey)
        showdata_filter = request.GET.get('showdata', None)

        if showdata_filter:
{'fraction_non_alphanumeric': 0.11417483660130719, 'fraction_numerical': 0.0014297385620915032, 'mean_word_length': 3.4356884057971016, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6180652', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1532, 'n_words': 406}