from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, safe_json ) from otree.views import mturk from django.db import models as dj_models import csv import random import math from django.utils import timezone from django import forms from django.utils.encoding import force_text from django.utils.safestring import mark_safe from django.core.validators import MaxValueValidator author = 'Alexander Max Bauer und Jan K. Romann' doc = """ Umfrage zum Thema Bedarfsgerechtigkeit, die auf Basis von csv-Dateien erstellt wird. """ class Constants(BaseConstants): name_in_url = 'bedarfsgerechtigkeit' players_per_group = None with open('bedarfsgerechtigkeit/mono.csv') as f: questions_mono = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/sen.csv') as f: questions_sen = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/ueberunter.csv') as f: questions_ueberunter = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/skatrans1.csv') as f: questions_skatrans1 = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/skatrans2.csv') as f: questions_skatrans2 = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/verteil.csv') as f: questions_verteil = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/ein.csv') as f: questions_ein = list(csv.DictReader(f)) with open('bedarfsgerechtigkeit/ein.csv') as f: questions_aus = list(csv.DictReader(f)) # Treatment 1 total_questions_mono = range(len(questions_mono)) total_questions_sen = range(len(questions_sen)) total_questions_ueberunter = range(len(questions_ueberunter)) total_questions_skatrans1 = range(len(questions_skatrans1)) total_questions_skatrans2 = range(len(questions_skatrans2)) total_questions_verteil = range(len(questions_verteil)) num_total_questions_mono = max(total_questions_mono) + 1 num_total_questions_sen = max(total_questions_sen) + 1 num_total_questions_ueberunter = max(total_questions_ueberunter) + 1 num_total_questions_skatrans1 = max(total_questions_skatrans1) + 1 num_total_questions_skatrans2 = max(total_questions_skatrans2) + 1 num_total_questions_verteil = max(total_questions_verteil) + 1 # Treatment 2+3 total_questions_ein = range(len(questions_ein)) total_questions_aus = range(len(questions_aus)) num_total_questions_ein = max(total_questions_ein) + 1 num_total_questions_aus = max(total_questions_ein) + 1 num_rounds = 1 class Subsession(BaseSubsession): def creating_session(self): players = self.get_players() num_players = len(players) num_randomization = math.ceil(num_players / 3) random_numbers = [] i = [1, 2, 3, ] random_counter = 0 for p in self.get_players(): p.payoff = 0 p.participant.vars['value_sen'] = 0 p.participant.vars['num_question_ein'] = 1 p.participant.vars['num_question_aus'] = 1 p.participant.vars['num_question_mono'] = 1 p.participant.vars['num_question_sen'] = 1 p.participant.vars['num_question_ueberunter'] = 1 p.participant.vars['num_question_skatrans1'] = 1 p.participant.vars['num_question_skatrans2'] = 1 p.participant.vars['num_question_verteil'] = 1 for x in range(0, num_randomization): random.shuffle(i) random_numbers.extend(i) if self.round_number == 1: for p in self.get_players(): p.randomise_question_data_to_participant_vars('questions_ein', Constants.questions_ein) p.randomise_question_data_to_participant_vars('questions_aus', Constants.questions_aus) p.randomise_question_data_to_participant_vars('questions_mono', Constants.questions_mono) p.randomise_question_data_to_participant_vars('questions_sen', Constants.questions_sen) p.randomise_question_data_to_participant_vars('questions_ueberunter', Constants.questions_ueberunter) p.randomise_question_data_to_participant_vars('questions_skatrans1', Constants.questions_skatrans1) p.randomise_question_data_to_participant_vars('questions_skatrans2', Constants.questions_skatrans2) p.randomise_question_data_to_participant_vars('questions_verteil', Constants.questions_verteil) p.treatment_number = random_numbers[random_counter] random_counter += 1 p.participant.vars['num_uebersicht'] = 0 for p in self.get_players(): p.participant.vars['situation_id_sen'] = p.current_question_sen()['situation_id'] p.participant.vars['situation_id_ueberunter'] = p.current_question_ueberunter()['situation_id'] p.participant.vars['situation_id_skatrans1'] = p.current_question_skatrans1()['situation_id'] p.participant.vars['situation_id_skatrans2'] = p.current_question_skatrans2()['situation_id'] p.participant.vars['situation_id_verteil'] = p.current_question_verteil()['situation_id'] p.participant.vars['situation_id_ein'] = p.current_question_ein()['situation_id'] p.participant.vars['situation_id_aus'] = p.current_question_aus()['situation_id'] p.randomise_questions_to_participant_vars(['Frei', 'Wuerde', 'Gleich', 'Viel', 'Verdienst', 'Bedarf'], 'staat') p.randomise_questions_to_participant_vars( ['CDU', 'SPD', 'GRÜNE', 'DIELINKE', 'CSU', 'FDP', 'PIRATEN', 'AfD', 'NPD'], 'partei') p.randomise_questions_to_participant_vars(['Familien', 'Arbeitslos', 'Krank', 'Alter', 'Pflege'], 'verant') self.session.config['participation_fee'] = c(10) class Group(BaseGroup): pass class Player(BasePlayer): time_started = dj_models.DateTimeField() time_ended = dj_models.DateTimeField() treatment_number = models.PositiveIntegerField() reihenfolge_sen = models.LongStringField(initial='') reihenfolge_verteil = models.LongStringField(initial='') reihenfolge_ueberunter = models.LongStringField(initial='') reihenfolge_aus = models.LongStringField(initial='') reihenfolge_ein = models.LongStringField(initial='') verteilfrage = models.LongStringField(label='Was verstehen Sie unter einer gerechten Güterverteilung?', max_length=500, widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) # Treatment 1 if Constants.num_total_questions_mono - 1 >= 0: for i in range(0, (Constants.num_total_questions_mono) * 200, 200): locals()["MonoSen" + str(i)] = models.PositiveIntegerField(choices=range(0, 101, 1), initial=None, label='', widget=widgets.Slider()) locals()["MonoSenSicher" + str(i)] = models.StringField(initial=None, choices=[('1', 'Sicher'), ('2', 'Unsicher')], label='Bei meiner Gerechtigkeitseinschätzung war ich mir...', widget=widgets.RadioSelectHorizontal) del i monofrei = models.LongStringField(max_length=500, label='Wie sind Sie zu Ihren Entscheidungen gekommen?', widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) if Constants.num_total_questions_sen - 1 >= 0: for i in range(1, Constants.num_total_questions_sen + 1, 1): locals()["Sen" + str(i)] = models.StringField(initial=None, choices=[('1', 'Szenario 1'), ('2', 'Szenario 2'), ('3', 'Beide Szenarien sind gleich gerecht beziehungsweise gleich ungerecht')], label='Welches Szenario ist gerechter?', widget=widgets.RadioSelect) locals()["Sen" + str(i) + "value"] = models.PositiveIntegerField(choices=range(0, 12, 1), initial=None, label='', widget=forms.RadioSelect()) locals()["Sen" + str(i) + "sicher"] = models.StringField(initial=None, choices=[('1', 'sicher'), ('2', 'unsicher'), ], label='Bei meiner Gerechtigkeitseinschätzung war ich mir...', widget=forms.RadioSelect()) del i senfrei = models.LongStringField(max_length=500, label='Wie sind Sie zu Ihren Gerechtigkeitseinschätzungen gekommen?', widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) if Constants.num_total_questions_ueberunter - 1 >= 0: for i in range(1, Constants.num_total_questions_ueberunter + 1): for s in range(1, 3): locals()[str(i) + "ueberunter" + str(s)] = models.PositiveIntegerField(choices=range(0, 101, 1), initial=None, label='', widget=widgets.Slider()) locals()[str(i) + "ueberuntersicher" + str(s)] = models.StringField(initial=None, choices=[('1', 'sicher'), ('2', 'unsicher'), ], label='Bei meiner Gerechtigkeitseinschätzung war ich mir...', widget=forms.RadioSelect()) del s del i ueberunterfrei = models.LongStringField(max_length=500, label='Wie sind Sie zu Ihren Entscheidungen gekommen?', widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) if Constants.num_total_questions_skatrans1 - 1 >= 0: for i in range(200, (Constants.num_total_questions_skatrans1 + 1) * 200, 200): locals()["1skatrans" + str(i)] = models.PositiveIntegerField(choices=range(0, 2001, 1), initial=None, label='Wieviel soll Haushalt B bekommen, damit er gleich gerecht beziehungsweise gleich ungerecht versorgt ist wie Haushalt A?', widget=forms.TextInput()) del i if Constants.num_total_questions_skatrans2 - 1 >= 0: for i in range(200, (Constants.num_total_questions_skatrans1 + 1) * 200, 200): locals()["2skatrans" + str(i)] = models.PositiveIntegerField(choices=range(0, 2001, 1), initial=None, label='Wieviel soll Haushalt A bekommen, damit er gleich gerecht beziehungsweise gleich ungerecht versorgt ist wie Haushalt A?', widget=forms.TextInput()) del i skatransfrei = models.LongStringField(max_length=500, label='Mit welcher Überlegung haben Sie Wohnraum verteilt?', widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) if Constants.num_total_questions_verteil - 1 >= 0: for i in range(1, Constants.num_total_questions_verteil + 1): locals()[str(i) + "verteil1"] = models.PositiveIntegerField( initial=None, label='Haushalt A soll bekommen:', widget=forms.TextInput()) locals()[str(i) + "verteil2"] = models.PositiveIntegerField( initial=None, label='Haushalt B soll bekommen:', widget=forms.TextInput()) del i verteilfrei = models.LongStringField(max_length=500, label='Mit welcher Überlegung haben Sie Wohnraum verteilt?', widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) # Treatment 2+3 if Constants.num_total_questions_aus - 1 >= 0: for i in range(1, Constants.num_total_questions_aus + 1): locals()["aus" + str(i) + "frage"] = models.StringField(initial=None, choices=[('1', 'Szenario 1'), ('2', 'Szenario 2')], label='', widget=widgets.RadioSelectHorizontal) ausfrei = models.LongStringField(max_length=500, widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) if Constants.num_total_questions_ein - 1 >= 0: for i in range(1, Constants.num_total_questions_ein + 1): for s in range(1, 3): locals()["ein" + str(i) + "frageS" + str(s)] = models.PositiveIntegerField(choices=range(0, 101, 1), initial=None, label='', widget=widgets.Slider()) locals()["ein" + str(i) + "sicherS" + str(s)] = models.StringField(initial=None, choices=[('1', 'Sicher'), ('2', 'Unsicher')], label='Wie sicher waren Sie sich bei Ihrer Entscheidung?', ) del s del i einfrei = models.LongStringField(max_length=500, widget=forms.Textarea(attrs={ 'style': 'width: 30em;'}), label='Bitte beschreiben Sie kurz, wie Sie zu Ihren Entscheidungen gekommen sind') # General questions and Demographics bedarffrage = models.LongStringField(label='Was verstehen Sie unter einer bedarfsgerechten Güterverteilung?', max_length=500, widget=forms.Textarea(attrs={ 'style': 'width: 30em;'})) assoquadrat = models.PositiveIntegerField(validators=[MaxValueValidator(300)], label='Wievielen Quadratmetern entsprechen 1.000 regionale Größeneinheiten in Ihrer Vorstellung?', widget=forms.TextInput) assopersonen = models.PositiveIntegerField(validators=[MaxValueValidator(50)], label='Wieviele Personen leben in Ihrer Vorstellung in einem Haushalt, der 1.000 regionale Größeneinheiten Wohnraum braucht, um in Würde leben zu können?', widget=forms.TextInput) geschlecht = models.StringField(choices=[('1', 'männlich'), ('2', 'weiblich'), ('-oth-', 'anderes')], label='Welches Geschlecht haben Sie?', widget=widgets.RadioSelectHorizontal) geschlechtother = models.LongStringField(blank=True, label='Wenn Sie "anderes" angegeben haben: Welche Selbstbezeichnung würden Sie stattdessen wählen?', widget=forms.TextInput(attrs={ 'style': 'width: 30em;'})) alter = models.PositiveIntegerField(label='In welchem Jahr wurden Sie geboren?', choices=range(1900, 2001), initial=None) muttersprache = models.StringField( choices=[('1', 'deutsch'), ('-oth-', 'andere')], label='Welche Staatsbürgerschaft besitzen Sie?', widget=widgets.RadioSelectHorizontal) mutterspracheother = models.LongStringField(blank=True, label='Wenn Sie "andere" angegeben haben: Welche Staatsbürgerschaft besitzen Sie?', widget=forms.TextInput(attrs={ 'style': 'width: 30em;'})) abschluss = models.StringField(choices=[('1', 'kein Schulabschluss'), ('2', 'Hauptschul- oder Realschulabschluss'), ('3', 'Abitur beziehungsweise allgemeine Hochschulreife oder Fachhochschulreife'), ('4', 'abgeschlossene betriebliche oder schulische Berufsausbildung'), ('5', 'Bachelor'), ('6', 'Master, Promotion oder Vergleichbares'), ('7', 'anderer allgemeiner Schulabschluss oder beruflicher Abschluss')], label='Was ist der höchste Bildungsabschluss, den Sie erreicht haben?', widget=widgets.RadioSelect) beschaeftigung = models.StringField(choices=[('1', 'Schüler oder Student'), ('2', 'Angestellter oder Beamter'), ('3', 'selbstständig Erwerbstätiger'), ('4', 'geringfügig Beschäftigter'), ('5', 'arbeitslos oder arbeitssuchend'), ], label='Welcher Beschäftigung gehen Sie aktuell hauptsächlich nach?', widget=widgets.RadioSelect) studienfach = models.LongStringField(blank=True, label='Wenn Sie studieren/studiert haben: Welches ist Ihr Studienfach?', widget=forms.TextInput(attrs={ 'style': 'width: 30em;'})) wohnsituation = models.StringField(initial=None, choices=[('1', 'alleine lebend'), ('2', 'in einer Wohngemeinschaft lebend'), ('3', 'bei den Eltern oder bei anderen Familienmitgliedern lebend'), ('4', 'in einer Partnerschaft oder mit der eigenen Familie lebend'), ], label='Wie ist Ihre Wohnsituation?', widget=widgets.RadioSelect) hausflaeche = models.PositiveIntegerField( label='Wie viele Quadratmeter Wohnraum stehen Ihrem Haushalt insgesamt zur Verfügung?', widget=forms.TextInput(attrs={ 'style': 'width: 30em;'})) hausgroesse = models.PositiveIntegerField( label='Wie viele Personen leben insgesamt – Sie eingeschlossen – in Ihrem Haushalt?', widget=forms.TextInput(attrs={ 'style': 'width: 30em;'})) soziodemozustaendVerantFamilien = models.PositiveIntegerField(choices=range(1, 6, 1), ) soziodemozustaendVerantArbeitslos = models.PositiveIntegerField(choices=range(1, 6, 1), ) soziodemozustaendVerantKrank = models.PositiveIntegerField(choices=range(1, 6, 1), ) soziodemozustaendVerantAlter = models.PositiveIntegerField(choices=range(1, 6, 1), ) soziodemozustaendVerantPflege = models.PositiveIntegerField(choices=range(1, 6, 1), ) # Links-Rechts-Selbsteinschätzung linksrechtsselbst = models.PositiveIntegerField(choices=range(1, 10, 1), ) # Links-Rechts-Einschätzung Parteien linksrechtsparteiCDU = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiSPD = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiGRÜNE = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiDIELINKE = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiCSU = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiFDP = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiPIRATEN = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiAfD = models.PositiveIntegerField(choices=range(1, 10, 1), ) linksrechtsparteiNPD = models.PositiveIntegerField(choices=range(1, 10, 1), ) # alte Gerechtigkeitsskala zustimmungStaatFrei = models.PositiveIntegerField(choices=range(1, 8, 1), ) zustimmungStaatWuerde = models.PositiveIntegerField(choices=range(1, 8, 1), ) zustimmungStaatGleich = models.PositiveIntegerField(choices=range(1, 8, 1), ) zustimmungStaatViel = models.PositiveIntegerField(choices=range(1, 8, 1), ) zustimmungStaatVerdienst = models.PositiveIntegerField(choices=range(1, 8, 1), ) zustimmungStaatBedarf = models.PositiveIntegerField(choices=range(1, 8, 1), ) risiko = models.StringField(initial=None, choices=[('1', 'sehr risikofreudig'), ('2', 'eher risikofreudig'), ('3', 'risikoneutral'), ('4', 'eher risikoscheu'), ('5', 'sehr risikoscheu'), ], label='Bitte geben Sie an, ob Sie sich eher als risikofreudig oder risikoscheu beschreiben würden.', widget=widgets.RadioSelect) def current_question_mono(self): return self.participant.vars['questions_mono'][self.participant.vars['num_question_mono'] - 1] def current_question_ueberunter(self): return self.participant.vars['questions_ueberunter'][self.participant.vars['num_question_ueberunter'] - 1] def current_question_skatrans1(self): return self.participant.vars['questions_skatrans1'][self.participant.vars['num_question_skatrans1'] - 1] def current_question_skatrans2(self): return self.participant.vars['questions_skatrans2'][self.participant.vars['num_question_skatrans2'] - 1] def current_question_verteil(self): return self.participant.vars['questions_verteil'][self.participant.vars['num_question_verteil'] - 1] def current_question_sen(self): return self.participant.vars['questions_sen'][self.participant.vars['num_question_sen'] - 1] def current_question_ein(self): return self.participant.vars['questions_ein'][self.participant.vars['num_question_ein'] - 1] def current_question_aus(self): return self.participant.vars['questions_aus'][self.participant.vars['num_question_aus'] - 1] def get_question_data_mono_as_list(self): list_of_situation_ids = [] for x in self.participant.vars['questions_mono']: list_of_situation_ids.append(x['situation_id']) list_of_need = [] for x in self.participant.vars['questions_mono']: list_of_need.append(x['need']) list_of_endowment = [] for x in self.participant.vars['questions_mono']: list_of_endowment.append(x['endowment']) return list(zip(list_of_situation_ids, list_of_need, list_of_endowment)) def get_tuple_of_mono_form_fields(self, infix): list = [] for x in self.participant.vars['questions_mono']: list.append('MonoSen' + infix + '{}'.format(x['endowment'])) return tuple(list) def get_question_data_skatrans_as_list(self, which_skatrans): list_of_situation_ids = [] for x in self.participant.vars['questions_skatrans' + which_skatrans]: list_of_situation_ids.append(x['situation_id']) list_of_scen_1_need = [] for x in self.participant.vars['questions_skatrans' + which_skatrans]: list_of_scen_1_need.append(x['scen_1_need']) list_of_scen_1_endowment = [] for x in self.participant.vars['questions_skatrans' + which_skatrans]: list_of_scen_1_endowment.append(x['scen_1_endowment']) list_of_scen_2_need = [] for x in self.participant.vars['questions_skatrans' + which_skatrans]: list_of_scen_2_need.append(x['scen_2_need']) list_of_scen_2_endowment = [] for x in self.participant.vars['questions_skatrans' + which_skatrans]: list_of_scen_2_endowment.append(x['scen_2_endowment']) return list(zip(list_of_situation_ids, list_of_scen_1_need, list_of_scen_1_endowment, list_of_scen_2_need, list_of_scen_2_endowment)) def get_tuple_of_skatrans_form_fields(self, infix): list = [] for x in self.participant.vars['questions_skatrans1']: list.append(infix + 'skatrans{}'.format(x['scen_2_need'])) return tuple(list) def get_question_data_ueberunter_as_list(self): list_of_situation_ids = [] for x in self.participant.vars['questions_ueberunter']: list_of_situation_ids.append(x['situation_id']) list_of_scen_1_household_A_need = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_1_household_A_need.append(x['scen_1_household_A_need']) list_of_scen_1_household_A_endowment = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_1_household_A_endowment.append(x['scen_1_household_A_endowment']) list_of_scen_2_household_A_need = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_2_household_A_need.append(x['scen_2_household_A_need']) list_of_scen_2_household_A_endowment = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_2_household_A_endowment.append(x['scen_2_household_A_endowment']) list_of_scen_2_household_B_need = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_2_household_B_need.append(x['scen_2_household_B_need']) list_of_scen_2_household_B_endowment = [] for x in self.participant.vars['questions_ueberunter']: list_of_scen_2_household_B_endowment.append(x['scen_2_household_B_endowment']) return list(zip(list_of_situation_ids, list_of_scen_1_household_A_need, list_of_scen_1_household_A_endowment, list_of_scen_2_household_A_need, list_of_scen_2_household_A_endowment, list_of_scen_2_household_B_need, list_of_scen_2_household_B_endowment)) def randomise_questions_to_participant_vars(self, items, dictionary_key): self.participant.vars[dictionary_key] = random.sample(items, len(items)) def randomise_question_data_to_participant_vars(self, participant_var, constant): self.participant.vars[participant_var] = random.sample(constant, len(constant)) def display_if_treatment_1_or_4(self): return self.treatment_number == 1 or self.treatment_number == 4 def display_if_treatment_2_or_3(self): return self.treatment_number == 2 or self.treatment_number == 3