# drEffects, demographics from otree.api import Currency as c, currency_range from . import models from ._builtin import Page, WaitPage from .models import Constants from django.conf import settings import time import random import math class introduction(Page): def is_displayed(self): return self.subsession.round_number == 1 def vars_for_template(self): return { 'conversion_rate' : self.session.config['real_world_currency_per_point'], 'participation_fee' : self.session.config['participation_fee'] } class demographics(Page): form_model = models.Player form_fields = ['age', 'gender', 'ethnicity', 'residence', 'income', 'degreeLevel', 'degree', 'religious', 'vote', 'vote_election', 'vote_party', 'left_right', 'efficacy_1', 'efficacy_2','political_knowledge'] # form_fields = ['age', 'gender'] def is_displayed(self): return self.subsession.round_number == 1 def before_next_page(self): self.player.participant.vars['gender'] = self.player.gender class Instructions(Page): def is_displayed(self): return self.subsession.round_number == 1 def vars_for_template(self): return { 'num_choices': len(self.participant.vars['mpl_choices']), 'lottery_a_lo': Constants.lottery_a_lo, 'lottery_a_hi': Constants.lottery_a_hi, 'lottery_b_lo': Constants.lottery_b_lo, 'lottery_b_hi': Constants.lottery_b_hi } class Decision(Page): form_model = 'player' def get_form_fields(self): form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] if Constants.one_choice_per_page: page = self.subsession.round_number return [form_fields[page - 1]] else: return form_fields def vars_for_template(self): total = len(self.participant.vars['mpl_choices']) page = self.subsession.round_number progress = page / total * 100 if Constants.one_choice_per_page: return { 'num_choices': len(self.participant.vars['mpl_choices']), 'page': page, 'total': total, 'progress': progress, 'choices': [self.player.participant.vars['mpl_choices'][page - 1]], 'lottery_a_lo': Constants.lottery_a_lo, 'lottery_a_hi': Constants.lottery_a_hi, 'lottery_b_lo': Constants.lottery_b_lo, 'lottery_b_hi': Constants.lottery_b_hi } else: return { 'num_choices': len(self.participant.vars['mpl_choices']), 'choices': self.player.participant.vars['mpl_choices'], 'lottery_a_lo': Constants.lottery_a_lo, 'lottery_a_hi': Constants.lottery_a_hi, 'lottery_b_lo': Constants.lottery_b_lo, 'lottery_b_hi': Constants.lottery_b_hi } def before_next_page(self): round_number = self.subsession.round_number form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] indices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][0] index = indices[round_number - 1] if Constants.one_choice_per_page: current_choice = getattr(self.player, form_fields[round_number - 1]) self.participant.vars['mpl_choices_made'][index - 1] = current_choice if index == self.player.participant.vars['mpl_index_to_pay']: self.player.set_payoffs() if round_number == Constants.num_choices: self.player.set_consistency() self.player.set_switching_row() if not Constants.one_choice_per_page: for j, choice in zip(indices, form_fields): choice_i = getattr(self.player, choice) self.participant.vars['mpl_choices_made'][j - 1] = choice_i self.player.set_payoffs() self.player.set_consistency() self.player.set_switching_row() class Results(Page): def is_displayed(self): if Constants.one_choice_per_page: return self.subsession.round_number == Constants.num_rounds else: return True def vars_for_template(self): choices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])] indices = choices[0] index_to_pay = self.player.participant.vars['mpl_index_to_pay'] round_to_pay = indices.index(index_to_pay) + 1 choice_to_pay = self.participant.vars['mpl_choices'][round_to_pay - 1] if Constants.one_choice_per_page: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.in_round(round_to_pay).option_to_pay, 'payoff': self.player.in_round(round_to_pay).payoff, 'lottery_a_lo': Constants.lottery_a_lo, 'lottery_a_hi': Constants.lottery_a_hi, 'lottery_b_lo': Constants.lottery_b_lo, 'lottery_b_hi': Constants.lottery_b_hi } else: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.option_to_pay, 'payoff': self.player.payoff, 'lottery_a_lo': Constants.lottery_a_lo, 'lottery_a_hi': Constants.lottery_a_hi, 'lottery_b_lo': Constants.lottery_b_lo, 'lottery_b_hi': Constants.lottery_b_hi } page_sequence = [ introduction, demographics, Decision ] if Constants.instructions: page_sequence.insert(2, Instructions) if Constants.results: page_sequence.append(Results)