<commit_before>from pprint import pprint
import simplejson as json
from collections import defaultdict

from django import forms
from django.forms.util import ErrorList
from . import spec as d
from .forms import AdviseField, DatePickerWidget, MonthYearField, PostCodeField

def parse_specification(spec, survey_class='Survey'):
    vars = {'d': d}
    exec(spec, vars)

    klass = vars[survey_class]
    return klass

class Specification(object):
    def __init__(self, survey):
        '''
        @param spec.Survey survey
        '''
        self.survey = survey
        self._id_mapping = None
        self._questions = None
        self._modifier = None
        self._modified = None
        self._responses = None
        self._profiles = None
        self._prefills = None
        self._conditions = None

        d.validate_rules(survey.rules)

    def get_questions(self, root=None, id_mapping={}):
        if self._questions is not None and root is None:
            return self._questions

        if root is None:
            root = self.survey.rules

        questions = []
        for item in root:
            if isinstance(item, d.Branch):
                questions += self.get_questions(item.rules, id_mapping)
            elif issubclass(item, d.Question):
                obj = item()
                questions.append(obj)
                id_mapping.update({item.__name__: obj.id})

        if root == self.survey.rules:
            self._questions = questions
            self._id_mapping = id_mapping

        return questions

    questions = property(get_questions)

    def get_id_mapping(self):
        self.get_questions()
        return self._id_mapping

    id_mapping = property(get_id_mapping)

    def get_usage(self, name):
        res = {}
        for question in self.questions:
            modifier = question.get_usage(name)
            if len(modifier) > 0:
                res[question.id] = modifier

        return res

    def get_modifier(self):
        if self._modifier is None:
            self._modifier = self.get_usage('question')
        return self._modifier

    def get_modified(self):
        if self._modified is not None:
            return self._modified

        res = defaultdict(list)
        for question, modifier in self.get_modifier().items():
            for mod in modifier:
                res[mod].append(question)

        self._modified = dict(res)
        return self._modified

    def get_profiles(self):
        if self._profiles is None:
            usage = self.get_usage('profile')
            ids = list(set([id for items in usage.values() for id in items]))
            self._profiles = ids
        return self._profiles

    def get_responses(self):
        if self._responses is None:
            usage = self.get_usage('response')
            response_ids = list(set([id for items in usage.values() for id in items]))

            keys = self.survey.prefill.keys()
            self.get_questions() # make sure the mapping is finished
            prefill_ids = [self._id_mapping[key.__name__] for key in keys]

            ids = response_ids + prefill_ids

            self._responses = ids
        return self._responses

    def get_conditions(self):
        if self._conditions is None:
            res = {}
            for question in self.questions:
                condition = question.get_condition()
                res[question.id] = condition
            self._conditions = res
        return self._conditions

    def get_prefills(self):
        if self._prefills is None:
            res = {}
            self.get_questions() # make sure the mapping is finished
            for key, value in self.survey.prefill.items():
                id = self._id_mapping[key.__name__]
                res[id] = value
            self._prefills = res
        return self._prefills

def get_template_context(spec, context):
    """Get and return a context containing the date and responses to the last
    completed survey, as well a other contextual information required by the
    questionnaires.
    """
    try:
        from utils import get_last_response
        lsd = get_last_response(context.user)

        from models import LastResponse
        last_response = LastResponse.objects.get(user=context.user)
        lsd['DATE'] = last_response.participation.date
    except LastResponse.DoesNotExist:
        lsd = {}

    def season(delta=0):
        from datetime import datetime
        now = datetime.now()
        year = now.year + delta
        if now.month > 6:
            return '%d-%d' % (year, year+1)
        else:
            return '%d-%d' % (year-1, year)

    d = { 'LAST_SURVEY': lsd,
          'SEASON': season(),
          'LAST_SEASON': season(-1) }

    return d 

class SurveyFormBase(forms.Form):
    def clean(self):
        from copy import copy
        data = copy(self.cleaned_data)
        values = copy(data)<commit_msg>Fix bug where text of the questions in the GSQ was not displayed.<commit_after>from pprint import pprint
import simplejson as json
from collections import defaultdict

from django import forms
from django.forms.util import ErrorList
from . import spec as d
from .forms import AdviseField, DatePickerWidget, MonthYearField, PostCodeField

def parse_specification(spec, survey_class='Survey'):
    vars = {'d': d}
    exec(spec, vars)

    klass = vars[survey_class]
    return klass

class Specification(object):
    def __init__(self, survey):
        '''
        @param spec.Survey survey
        '''
        self.survey = survey
        self._id_mapping = None
        self._questions = None
        self._modifier = None
        self._modified = None
        self._responses = None
        self._profiles = None
        self._prefills = None
        self._conditions = None

        d.validate_rules(survey.rules)

    def get_questions(self, root=None, id_mapping={}):
        if self._questions is not None and root is None:
            return self._questions

        if root is None:
            root = self.survey.rules

        questions = []
        for item in root:
            if isinstance(item, d.Branch):
                questions += self.get_questions(item.rules, id_mapping)
            elif issubclass(item, d.Question):
                obj = item()
                questions.append(obj)
                id_mapping.update({item.__name__: obj.id})

        if root == self.survey.rules:
            self._questions = questions
            self._id_mapping = id_mapping

        return questions

    questions = property(get_questions)

    def get_id_mapping(self):
        self.get_questions()
        return self._id_mapping

    id_mapping = property(get_id_mapping)

    def get_usage(self, name):
        res = {}
        for question in self.questions:
            modifier = question.get_usage(name)
            if len(modifier) > 0:
                res[question.id] = modifier

        return res

    def get_modifier(self):
        if self._modifier is None:
            self._modifier = self.get_usage('question')
        return self._modifier

    def get_modified(self):
        if self._modified is not None:
            return self._modified

        res = defaultdict(list)
        for question, modifier in self.get_modifier().items():
            for mod in modifier:
                res[mod].append(question)

        self._modified = dict(res)
        return self._modified

    def get_profiles(self):
        if self._profiles is None:
            usage = self.get_usage('profile')
            ids = list(set([id for items in usage.values() for id in items]))
            self._profiles = ids
        return self._profiles

    def get_responses(self):
        if self._responses is None:
            usage = self.get_usage('response')
            response_ids = list(set([id for items in usage.values() for id in items]))

            keys = self.survey.prefill.keys()
            self.get_questions() # make sure the mapping is finished
            prefill_ids = [self._id_mapping[key.__name__] for key in keys]

            ids = response_ids + prefill_ids

            self._responses = ids
        return self._responses

    def get_conditions(self):
        if self._conditions is None:
            res = {}
            for question in self.questions:
                condition = question.get_condition()
                res[question.id] = condition
            self._conditions = res
        return self._conditions

    def get_prefills(self):
        if self._prefills is None:
            res = {}
            self.get_questions() # make sure the mapping is finished
            for key, value in self.survey.prefill.items():
                id = self._id_mapping[key.__name__]
                res[id] = value
            self._prefills = res
        return self._prefills

def get_template_context(spec, context):
    """Get and return a context containing the date and responses to the last
    completed survey, as well a other contextual information required by the
    questionnaires.
    """
    try:
        from utils import get_last_response
        lsd = get_last_response(context.user) or {}

        from models import LastResponse
        last_response = LastResponse.objects.get(user=context.user)
        if last_response.participation and date in last_response.participation:
            lsd['DATE'] = last_response.participation.date
    except LastResponse.DoesNotExist:
        lsd = {}

    def season(delta=0):
        from datetime import datetime
        now = datetime.now()
        year = now.year + delta
        if now.month > 6:
            return '%d-%d' % (year, year+1)
        else:
            return '%d-%d' % (year-1, year)

    d = { 'LAST_SURVEY': lsd,
          'SEASON': season(),
          'LAST_SEASON': season(-1) }

    return d 

class SurveyFormBase(forms.Form):
    def clean(self):
        from copy import copy
        data = copy(self.cleaned_data)
        values = copy(data)
{'fraction_non_alphanumeric': 0.08509788254095085, 'fraction_numerical': 0.0011985617259288853, 'mean_word_length': 2.0087139423076925, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7298555', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2673, 'n_words': 776}