<commit_before>import io
import csv
import json
from guardian.shortcuts import get_objects_for_user
from guardian.mixins import PermissionRequiredMixin
from django.shortcuts import redirect

from studies.models import Study
from exp.views.mixins import ExperimenterLoginRequiredMixin
import get_study_attachments

class StudyResponsesMixin(ExperimenterLoginRequiredMixin, PermissionRequiredMixin, object):
    """
    Mixin with shared items for StudyResponsesList, StudyResponsesAll, and
    StudyAttachments Views
    """
    model = Study
    permission_required = 'studies.can_view_study_responses'
    raise_exception = True

    def build_responses(self, responses):
        """
        Builds the JSON response data for the researcher to download
        """
        return [json.dumps({
            'sequence': resp.sequence,
            'conditions': resp.conditions,
            'exp_data': resp.exp_data,
            'participant_id': resp.child.user.id,
            'global_event_timings': resp.global_event_timings,
            'child_id': resp.child.id,
            'completed': resp.completed,
            'study_id': resp.study.id,
            'response_id': resp.id,
            'demographic_id': resp.demographic_snapshot.id
            }, indent=4) for resp in responses]

    def csv_output_and_writer(self):
        output = io.StringIO()
        return output, csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)

    def csv_row_data(self, resp):
        """
        Builds individual row for csv responses
        """
        return [resp.sequence, resp.conditions, resp.exp_data, resp.child.user.id,
        resp.global_event_timings, resp.child.id, resp.completed, resp.study.id, resp.id,
        resp.demographic_snapshot.id]

    def get_csv_headers(self):
        """
        Returns header row for csv data
        """
        return ['sequence', 'conditions', 'exp_data', 'participant_id', 'global_event_timings',
            'child_id', 'completed', 'study_id', 'response_id', 'demographic_id']

    def post(self, request, *args, **kwargs):
        '''
        Downloads study video
        '''
        attachment = self.request.POST.get('attachment')
        if attachment:
            download_url = get_study_attachments.get_download_url(attachment)
            return redirect(download_url)

        return HttpResponseRedirect(reverse('exp:study-responses-list', kwargs=dict(pk=self.get_object().pk)))
<commit_msg>WIP add participant data to json response.<commit_after>import io
import csv
import json
from guardian.shortcuts import get_objects_for_user
from guardian.mixins import PermissionRequiredMixin
from django.shortcuts import redirect

from studies.models import Study
from exp.views.mixins import ExperimenterLoginRequiredMixin
import get_study_attachments

class StudyResponsesMixin(ExperimenterLoginRequiredMixin, PermissionRequiredMixin, object):
    """
    Mixin with shared items for StudyResponsesList, StudyResponsesAll, and
    StudyAttachments Views
    """
    model = Study
    permission_required = 'studies.can_view_study_responses'
    raise_exception = True

    def build_responses(self, responses):
        """
        Builds the JSON response data for the researcher to download
        """
        json_responses = []
        for resp in responses:
            latest_dem = resp.demographic_snapshot
            json_responses.append(json.dumps({
                'sequence': resp.sequence,
                'conditions': resp.conditions,
                'exp_data': resp.exp_data,
                'participant_id': resp.child.user.id,
                'global_event_timings': resp.global_event_timings,
                'child_id': resp.child.id,
                'completed': resp.completed,
                'study_id': resp.study.id,
                'response_id': resp.id,
                'participant': {
                    "participant_id": resp.child.user.id,
                    "demographics_info": {
                        "demographic_id": latest_dem.id,
                        "number_of_children": latest_dem.number_of_children,
                        "languages_spoken_at_home": latest_dem.languages_spoken_at_home,
                        "number_of_guardians": latest_dem.number_of_guardians,
                        "number_of_guardians_explanation": latest_dem.number_of_guardians_explanation,
                        "race_identification": latest_dem.race_identification,
                        "age": latest_dem.age,
                        "gender": latest_dem.gender,
                        "education_level": latest_dem.gender,
                        "spouse_education_level": latest_dem.spouse_education_level,
                        "annual_income": latest_dem.annual_income,
                        "number_of_books": latest_dem.number_of_books,
                        "additional_comments": latest_dem.additional_comments,
                        "country": latest_dem.country.name,
                        "state": latest_dem.state.name,
                        "density": latest_dem.density,
                        "extra": latest_dem.extra
                    }

                }}, indent=4))
        return json_responses

    def csv_output_and_writer(self):
        output = io.StringIO()
        return output, csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)

    def csv_row_data(self, resp):
        """
        Builds individual row for csv responses
        """
        return [resp.sequence, resp.conditions, resp.exp_data, resp.child.user.id,
        resp.global_event_timings, resp.child.id, resp.completed, resp.study.id, resp.id,
        resp.demographic_snapshot.id]

    def get_csv_headers(self):
        """
        Returns header row for csv data
        """
        return ['sequence', 'conditions', 'exp_data', 'participant_id', 'global_event_timings',
            'child_id', 'completed', 'study_id', 'response_id', 'demographic_id']

    def post(self, request, *args, **kwargs):
        '''
        Downloads study video
        '''
        attachment = self.request.POST.get('attachment')
        if attachment:
            download_url = get_study_attachments.get_download_url(attachment)
            return redirect(download_url)

        return HttpResponseRedirect(reverse('exp:study-responses-list', kwargs=dict(pk=self.get_object().pk)))

{'fraction_non_alphanumeric': 0.09166535865641187, 'fraction_numerical': 0.0003139224611520954, 'mean_word_length': 2.409309791332263, 'pattern_counts': {'":': 19, '<': 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': '2958702', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1709, 'n_words': 354}