from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage import settings from .models import Constants import time import boto3 class StartPage(Page): live_method = 'live_method' # timeout_seconds = 120 def js_vars(self): return dict( data_received=self.player.data_received, ) def vars_for_template(self): mturk = self.session.config.get('mturk', False) return { 'mturk': mturk, 'points100' : self.session.config.get('real_world_currency_per_point', 1)*100, 'exchange_rate': c(int(round(1 / self.session.config['real_world_currency_per_point']))), 'participation_fee': self.session.config['participation_fee'], 'experiment_starting_time': self.session.config.get('experiment_starting_time',''), 'late_minutes_allowed': self.session.config.get('late_minutes_allowed', 3), 'experimental_currency': getattr(settings, 'POINTS_CUSTOM_NAME', 'tokens'), 'real_currency': getattr(settings, 'REAL_WORLD_CURRENCY_CODE', '$'), } def is_displayed(self): self.player.workerid = self.participant.label return True def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['experiment_starting_time'] = time.time() class Consent(Page): # timeout_seconds = 120 form_model = 'player' form_fields = ['workerid'] live_method = 'live_method' def js_vars(self): return dict( data_received=self.player.data_received, ) def vars_for_template(self): mturk = self.session.config.get('mturk', False) return { 'mturk': mturk, } def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['waitpage_arrival_time'] = time.time() self.participant.vars['started'] = 1 ## after consent, qualification value set to 0. if not self.timeout_happened: self.player.consent = 1 if not self.session.config.get('debug',False): access_key_id = self.session.config['access_key_id'] secret_access_key = self.session.config['secret_access_key'] if self.session.config.get('sandbox',False): # sandbox Qid = self.session.config['Qid_sandbox'] Qid_qual = self.session.config['Qid_qual_sandbox'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', endpoint_url="https://mturk-requester-sandbox.us-east-1.amazonaws.com", ) else: # live production Qid = self.session.config['Qid'] Qid_qual = self.session.config['Qid_qual'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', ) try: mturk.associate_qualification_with_worker( QualificationTypeId=Qid, WorkerId=self.player.workerid, IntegerValue=0, SendNotification=False ) mturk.associate_qualification_with_worker( QualificationTypeId=Qid_qual, WorkerId=self.player.workerid, IntegerValue=0, SendNotification=False ) print('qualification granted:',self.player.workerid) except Exception as e: # print(e, value, Qid,) return 'Worker ID not Valid.' class WorkerID(Page): # timeout_seconds = 0 form_model = 'player' form_fields = ['workerid'] def before_next_page(self): if not self.timeout_happened: if not self.session.config.get('debug',False): access_key_id = self.session.config['access_key_id'] secret_access_key = self.session.config['secret_access_key'] if self.session.config.get('sandbox',False): # sandbox Qid = self.session.config['Qid_sandbox'] Qid_qual = self.session.config['Qid_qual_sandbox'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', endpoint_url="https://mturk-requester-sandbox.us-east-1.amazonaws.com", ) else: # live production Qid = self.session.config['Qid'] Qid_qual = self.session.config['Qid_qual'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', ) mturk.associate_qualification_with_worker( QualificationTypeId=Qid, WorkerId=self.player.workerid, IntegerValue=0, SendNotification=False ) mturk.associate_qualification_with_worker( QualificationTypeId=Qid_qual, WorkerId=self.player.workerid, IntegerValue=0, SendNotification=False ) self.participant.vars['workerid'] = self.player.workerid def is_displayed(self): self.player.workerid = self.participant.label return self.session.config.get('mturk', False) page_sequence = [ # WorkerID, StartPage, Consent, ]