from otree.api import Currency as c, currency_range, Page as oTreePage from ._builtin import WaitPage from otree.currency import RealWorldCurrency from .forms import TaskForm, AllocationForm from .abstract_pages import TaskPage, Page, GeneralCQMixin, BackPage from string import ascii_lowercase from typing import List from .models import Constants from django.db.models import Sum def vars_for_all_templates(self): nums = [2, 5, 1, 9, 0, 3, 7, 8, 6, 4] return {'task_example': list(zip(ascii_lowercase[:10], nums))} class Intro(BackPage): title = 'Introduction' section = 'Introduction' next_btn_seconds = 3 both_btns = False def vars_for_template(self) -> dict: currency_per_point = self.session.config['real_world_currency_per_point'] # ending = ' ' if int(currency_per_point * 100) == 1 else ' ' return {'currency_per_point': currency_per_point} class Consent(BackPage): section = 'Introduction' next_btn_seconds = 3 class InstructionsOverview(BackPage): title = "Instructions - overview" section = 'Instructions' pass class InstructionsVote(BackPage): section = 'Instructions' title = 'Your group and the task' def vars_for_template(self) -> dict: data = self.subsession.allocation_params option_list = [i['name'] for i in data] user_data = [list(i['user'].items()) for i in data] user_data = list(zip(*user_data)) final_data = [] for u in user_data: i = {} i['type_name'] = u[0][0] i['data'] = [x[1] for x in u] final_data.append(i) return dict(option_list=option_list, final_data=final_data) class InstructionsTax(BackPage): title = 'Tax rates and distribution of the tax revenue' section = 'Instructions' pass class InstructionsTask(BackPage): title = 'Stage 2: Task-solving' section = 'Instructions' pass class InstructionsPayoff(BackPage): section = 'Instructions' title = 'Stage 3. Tax and payoff calculation' def vars_for_template(self) -> dict: params = self.subsession.allocation_params allocation_yes = params[1]['user'] # TODO PLEASE REDO! high_tasks = allocation_yes['B']['tasks'] low_tasks = allocation_yes['A']['tasks'] return dict(high_tasks=high_tasks, low_tasks=low_tasks, params=Constants.for_vue[self.subsession.get_partial_display()]) class CQ(GeneralCQMixin, Page): def vars_for_template(self) -> dict: return dict(tot_wrong=self.player.cqs.all().aggregate(tot_wrong=Sum('counter'))['tot_wrong']) section = 'Questions' title = 'Comprehension questions' class NotEligible(oTreePage): def is_displayed(self) -> bool: return not self.player.eligible class BeforeStudyBegins(Page): section = 'Study' class Practice(TaskPage): section = 'Study' title = 'Practice task' practice = True def post(self): print(self.request.POST) return super().post() class Reallocation(Page): title = 'Vote' section = 'Study' form_model = 'player' def vars_for_template(self): taxes = [self.subsession.tax_rate_1, self.subsession.tax_rate_2, self.subsession.tax_rate_3] form = self.get_form() data = list(zip(taxes, form)) return {'data': data} def get_form_fields(self): return [f'reallocation_tax{i}' for i in range(1, 4)] class AfterReallocationWP(WaitPage): def after_all_players_arrive(self): self.group.set_allocation() class DictatorShown(Page): section = 'Study' pass class Signal(Page): section = 'Study' form_model = 'player' form_fields = ['signal'] def is_displayed(self) -> bool: return self.subsession.signalling and self.player.role() == 'A' and self.group.get_chosen_allocation() class AfterSignalWP(WaitPage): def after_all_players_arrive(self): for p in self.group.get_players(): p.create_tasks() class Task(TaskPage): section = 'Study' pass class BeforeResultsWP(WaitPage): def after_all_players_arrive(self): self.group.set_payoffs() class Results(Page): section = 'Study' pass page_sequence = [ Intro, Consent, InstructionsOverview, InstructionsVote, InstructionsTax, InstructionsTask, InstructionsPayoff, CQ, NotEligible, BeforeStudyBegins, Reallocation, AfterReallocationWP, DictatorShown, Signal, AfterSignalWP, Task, BeforeResultsWP, Results ]