from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random import re #from captcha.fields import ReCaptchaField class Consent(Page): def vars_for_template(self): return {'participation_fee': Constants.participation_fee, 'max_bonus': self.session.config['max_bonus'], 'redemption_code': self.participant.code } class Captcha(Page): _allow_custom_attributes = True form_model = 'player' form_fields = [] # captcha does not get in here #def get_form(self, data=None, files=None, **kwargs): # frm = super().get_form(data, files, **kwargs) # frm.fields['captcha'] = ReCaptchaField() # return frm class Warning(Page): pass class InstructionsRating(Page): def vars_for_template(self): max_bonus = self.session.config['max_bonus'] if self.player.cp_error: return {'message': 'Please, re-read the instructions!', 'participation_fee': Constants.participation_fee, 'max_bonus': self.session.config['max_bonus']} else: return {'participation_fee': Constants.participation_fee, 'max_bonus': self.session.config['max_bonus']} class UQ(Page): form_model = 'player' form_fields = ['uq2', 'uq3', 'uq4'] # Function to redirect to instructions page if the comprehension answers are wrong: def before_next_page(self): p = self.player p.cp_error = False p.flagged = False for k, v in Constants.correct_answers.items(): if getattr(p, k) != v and p.error1 is not True: p.cp_error = True p.error1 = True break elif getattr(p, k) != v and p.error1 is True and p.error2 is not True: p.cp_error = True p.error2 = True break elif getattr(p, k) != v and p.error1 is True and p.error2 is True and p.error3 is not True: p.cp_error = False p.error3 = True break elif getattr(p, k) != v and p.error1 is True and p.error2 is True and p.error3 is True: p.cp_error = False p.flagged = True break if p.cp_error: for f in self.form_fields: setattr(p, f, None) self._is_frozen = False self._index_in_pages -= 2 self.participant._index_in_pages -= 2 class Beliefs(Page): form_model = 'player' random.seed(11235) def get_form_fields(self): all_fields = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9'] random.shuffle(all_fields) return all_fields def vars_for_template(self): image = self.participant.vars['images'] return {'img_to_show': image} class Perception(Page): form_model = 'player' random.seed(11235) def get_form_fields(self): all_fields = ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9'] random.shuffle(all_fields) return all_fields def vars_for_template(self): image = self.participant.vars['images'] return {'img_to_show': image} class Demographics(Page): form_model = 'player' form_fields = ['year_of_birth', 'gender', 'income', 'hhincome', 'marital_status', 'children', 'education', 'height', 'weight', 'health', 'race', 'westernized', 'country', 'zip'] class Issues(Page): form_model = 'player' form_fields = ['issues', 'comments'] def before_next_page(self): self.group.set_payoffs() class ResultsUganda(Page): form_model = 'player' form_fields = ['phone_number', 'payment_method'] def vars_for_template(self): if self.player.phone_error: return {"msg": "Invalid mobile phone number!", "participation_fee": Constants.participation_fee, "max_bonus": Constants.maxbonus, "payoff_round1": self.participant.vars["payoff_round1"], "payoff_round2": self.participant.vars["payoff_round2"], "total_payoff": self.participant.payoff_plus_participation_fee() } else: return {"participation_fee": Constants.participation_fee, "max_bonus": Constants.maxbonus, "total_payoff": self.participant.payoff_plus_participation_fee() } def before_next_page(self): """ Raise a ValidationError if the value looks like a mobile telephone number. """ rule = re.compile(r'^(?:\+?256)?[07]\d{9,13}$') if not rule.search(self.player.phone_number): self.player.phone_error = True else: self.player.phone_error = False if self.player.phone_error: self._is_frozen = False self._index_in_pages -= 1 self.participant._index_in_pages -= 1 page_sequence = [ Consent, #Captcha, Warning, InstructionsRating, Beliefs, Perception, Demographics, Issues, ResultsUganda ]