import datetime import itertools import math import time from os import environ from otree.api import * author = 'FAU_CET' doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'cet_pre_screenProlific' PLAYERS_PER_GROUP = None # whats this? NUM_ROUNDS = 100 DO_CHECKS = True # environ.get('DO_CHECKS') ### warning: this goes out of notfinished now # idkey = '1,5,2,6,3,4' #'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24' IDREDO = environ.get('IDredo') # idtoremove = "121, 15" with open("./cet_pre_screenProlific2/notstarted.txt", "r+") as FILE_NOTSTARTED: # FILE_NOTSTARTED = open("./cet_resume/NOTSTARTED.txt", "r+") NOTSTARTED = FILE_NOTSTARTED.read() try: NOTSTARTED_LIST = [int(i) for i in NOTSTARTED.split(",")] print("import to session IDs:") print(NOTSTARTED_LIST) except: print("failed error.... NOTSTARTED list empty") # if IDREDO then add to list # iterate through list MYIDTOOL = itertools.cycle(NOTSTARTED_LIST) class Subsession(BaseSubsession): print("subsession") pass class Group(BaseGroup): print("group") pass class Player(BasePlayer): agreed = models.BooleanField() # def role(self): # if self.id_in_group == 1: # return 'Treat1_1' # if self.id_in_group == 2: # return 'Treat1_2' # if self.id_in_group == 3: # return 'Con_1' # if self.id_in_group == 4: # return 'Con_2' # if self.id_in_group == 5: # return 'Treat2_1' # if self.id_in_group == 6: # return 'Treat2_2' participant_vars_dump = models.StringField() TimeSpentInstruc = models.FloatField() TimeSpentCV_1 = models.FloatField() TimeSpentCV_2 = models.FloatField() TimeSpentCV_3 = models.FloatField() TimeSpentCV_4 = models.FloatField() TimeSpentCV_5 = models.FloatField() TimeSpentCV_6 = models.FloatField() # mygroup = models.IntegerField() CV_1 = models.FloatField(min=0, max=10, blank=True) CV_2 = models.FloatField(min=0, max=10, blank=True) CV_3 = models.FloatField(min=0, max=10, blank=True) CV_4 = models.FloatField(min=0, max=10, blank=True) CV_5 = models.FloatField(min=0, max=10, blank=True) CV_6 = models.FloatField(min=0, max=10, blank=True) Notes_1 = models.LongStringField( blank=True, ) Notes_2 = models.LongStringField( blank=True, ) Notes_3 = models.LongStringField( blank=True, ) Notes_4 = models.LongStringField( blank=True, ) Notes_5 = models.LongStringField( blank=True, ) Notes_6 = models.LongStringField( blank=True, ) CV_viewing = models.IntegerField(blank=True) CV_to_view = models.IntegerField(blank=True) time_clicked = models.FloatField() subreq = models.BooleanField(blank=True, initial=False) # backreq = models.BooleanField(blank=True, initial=False) loc = models.StringField(max_length=100, blank=True) ip = models.StringField(max_length=100, blank=True) email = models.LongStringField( initial="""Hi Emma, We discussed about meeting next Friday, but now I have a doubt about this. Could we prepone it to tomorrow instead? So you either need to revert back to me and suggest me another time, or book the room for tomorrow. Kindly do the needful. Best, John """ ) drag = models.StringField(blank=True) dragtime = models.StringField(blank=True) CVscroll = models.StringField(blank=True) CVscrolltime = models.StringField(blank=True) SMscroll = models.StringField(blank=True) SMscrolltime = models.StringField(blank=True) # FUNCTIONS def creating_session(subsession: Subsession): # print("creating sesh") if subsession.round_number == 1: for p in subsession.get_players(): p.participant.vars['initial'] = True p.participant.vars['submitted_all'] = False p.participant.vars['finished'] = False p.participant.vars['last_active'] = time.time() p.participant.vars['myID'] = 0 p.participant.vars['failed'] = 0 p.participant.vars['ip2'] = 0 p.participant.vars['lat'] = 0 p.participant.vars['long'] = 0 p.participant.vars['city'] = None p.participant.vars['failed_bec'] = "not" with open("./cet_pre_screenProlific2/notstarted.txt", "r") as file_notstarted: notstarted = file_notstarted.read() p.session.vars['notstarted'] = [ int(i) for i in notstarted.split(",") ] # list(C.MYPARTICIPTABLE_DF.index) print("the not started participants are") print(p.session.vars['notstarted']) p.session.vars['myIDtool'] = itertools.cycle(p.session.vars['notstarted']) # with open("./cet_pre_screenProlific2/notstarted.txt", "r+") as file_notstarted: # # file_notstarted = open("./cet_resume/notstarted.txt", "r+") # notstarted = file_notstarted.read() # try: # notstarted_list = [int(i) for i in notstarted.split(",")] # print("import to session IDs:") # print(notstarted_list) # except: # print("failed error.... notstarted list empty") # if idredo then add to list # iterate through list with open("./cet_pre_screenProlific2/notfinished.txt", "r") as file_notfinished: notfinished = file_notfinished.read() p.session.vars['notfinished'] = [ int(i) for i in notfinished.split(",") ] # list(C.MYPARTICIPTABLE_DF.index) with open("./cet_pre_screenProlific2/lats.txt", "r") as file_lats: # file_lats = open("./cet_pre_screenProlific2/lats.txt", "r") lats = file_lats.read() p.session.vars['lats'] = [float(i) for i in lats.split(",")] # print(p.session.vars['lats']) with open("./cet_pre_screenProlific2/longs.txt", "r") as file_longs: # file_longs = open("./cet_pre_screenProlific2/longs.txt", "r") longs = file_longs.read() p.session.vars['longs'] = [float(i) for i in longs.split(",")] # print(p.session.vars['longs']) with open("./cet_pre_screenProlific2/ip_roots.txt", "r") as file_ip_roots: # file_ip_roots = open("./cet_pre_screenProlific2/ip_roots.txt", "r") ip_roots = file_ip_roots.read() p.session.vars['ip_roots'] = [str(i) for i in ip_roots.split(", ")] # print(p.session.vars['ip_roots']) def get_client_ip_my(player: Player, request): ip, is_routable = get_client_ip(request) if field_maybe_none('ip') is None: # Unable to get the client's IP address return None else: # We got the client's IP address if is_routable: # The client's IP address is publicly routable on the Internet return ip else: return "not routable:" + ip def get_ip_info(player: Player, ip): access_token = '1ac551076210cc' handler = ipinfo.getHandler(access_token) details = handler.getDetails(ip) return details ## function check lat and long from Constants and from list here. return failed if repeated ## def check_loc(player: Player, lat, long, ip): if C.DO_CHECKS: lats = player.session.vars['lats'] longs = player.session.vars['longs'] latindex = [i for i, v in enumerate(lats) if v == round(lat, 3)] longindex = [i for i, v in enumerate(longs) if v == round(long, 3)] failcount = 0 if [i for i in latindex if i in longindex]: ################ LOC CHECK TURNED OFF # self.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "loc" failcount = failcount + 1 print("failed1: same loc") else: player.session.vars['lats'].append(round(lat, 3)) player.session.vars['longs'].append(round(long, 3)) ip_roots = player.session.vars['ip_roots'] # ip = "104.131.19.234" ip_root = '.'.join(ip.split(".")[:-1]) if ip_root in ip_roots: # self.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "ip" failcount = failcount + 1 print("failed1: same ip") else: player.session.vars['ip_roots'].append(ip_root) if failcount == 2: player.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "ip_and_loc" print("aka failed both") else: pass def append_loc(player: Player, lat, long, ip): print("inside append lock") keepgoing = True while keepgoing: try: with open("./cet_pre_screenProlific2/lats.txt", "a+") as file_lats: # file_lats = open("./cet_pre_screenProlific2/lats.txt", "a+") file_lats.write(", " + str(round(lat, 3))) print("written " + str(round(lat, 3))) keepgoing = False except: pass keepgoing = True while keepgoing: try: with open("./cet_pre_screenProlific2/longs.txt", "a+") as file_longs: # file_longs = open("./cet_pre_screenProlific2/longs.txt", "a+") file_longs.write(", " + str(round(long, 3))) # file_longs.close() print("written " + str(round(long, 3))) keepgoing = False except: pass ip_root = '.'.join(ip.split(".")[:-1]) keepgoing = True while keepgoing: try: with open("./cet_pre_screenProlific2/ip_roots.txt", "a+") as file_ip_roots: # file_ip_roots = open("./cet_pre_screenProlific2/longs.txt", "a+") file_ip_roots.write(", " + str(ip_root)) # file_ip_roots.close() print("written " + str(ip_root)) keepgoing = False except: pass def ins_viewing(player: Player): return player.CV_viewing in [0, 21, 22] def standardvars(player: Player): return { 'initial': player.participant.vars.get('initial'), 'submitted_all': player.participant.vars.get('submitted_all', 0), #'treated1': self.participant.vars.get('treated1'), #'treated2': self.participant.vars.get('treated2'), #'type_1': self.participant.vars.get('type_1'), 'failed': player.participant.vars.get('failed') == 1, } def specialvars(player: Player): partvars = player.participant.vars print(partvars['myID']) return { 'image_path': 'Resumes/Group{1}/Resume_{0}_T1_Output.jpg'.format( player.CV_viewing, partvars['myID'] ), 'html_path': 'data/Group{1}/ScrapedData{0}.html'.format( player.CV_viewing, partvars['myID'] ), } def varsfortemplate(player: Player): if player.round_number == 1: player.CV_viewing = 21 if player.round_number != 1 and player.CV_viewing not in [0, 21, 22]: return {**specialvars(player), **standardvars(player)} else: return standardvars(player) def setup(player: Player): print("setup") selfplayer = player selfplayer.participant.vars['last_active'] = time.time() selfplayer.participant.vars['timeleftfirstpage'] = time.time() ##pick ID from next in line in notstarted unless notstarted is empty, ## in which case, choose next in line from notfinished, if empty throw an error. off = False while not off: # remo = 2 # try: me = next(selfplayer.session.vars['myIDtool']) # next(C.MYIDTOOL) print("remove") print(me) list_IDs = selfplayer.session.vars['notstarted'] while me not in list_IDs: me = next(selfplayer.session.vars['myIDtool']) selfplayer.session.vars['notstarted'] = [x for x in list_IDs if x > me] # selfplayer.session.vars['notstarted'].remove(me) selfplayer.participant.vars['myID'] = me print(selfplayer.session.vars['notstarted']) off = True # except: # print("failed on ID assignment") with open("./cet_pre_screenProlific2/notstarted.txt", "r+") as file_notstarted: file_notstarted.seek(0) if len(selfplayer.session.vars['notstarted']) > 0: file_notstarted.write(str(int(selfplayer.session.vars['notstarted'][0]))) if len(selfplayer.session.vars['notstarted']) > 1: for i in selfplayer.session.vars['notstarted'][1:]: file_notstarted.write(", " + str(int(i))) file_notstarted.truncate() # file_notstarted.close() if not off: # remo = 2 with open("./cet_pre_screenProlific2/notfinished.txt", "r+") as file_notfinished: # file_notfinished = open("./cet_resume/notfinished.txt", "r+") notfinished = file_notfinished.read() newlist = [int(i) for i in notfinished.split(",")] # newlist = list(self.session.vars.get('notfinished')) try: me = newlist[0] selfplayer.participant.vars['myID'] = me print("all started so remove") print(me) print("leaving") print(newlist) off = True except: pass # self.session.vars['notfinished'] = newlist # print(self.session.vars['notfinished']) if len(newlist) > 0: file_notfinished.seek(0) file_notfinished.write(str(int(newlist[0]))) if len(newlist) > 1: for i in newlist[1:]: file_notfinished.write(", " + str(int(i))) file_notfinished.truncate() # file_notfinished.close() def instructbeforenextpage(player: Player): player.participant.vars['last_active'] = time.time() player.participant_vars_dump = str(player.participant.vars) ##@@ this is full of repeated code, could make multiple functions def beforenextpage(player: Player): selfplayer = player selfplayer.time_clicked = time.time() selfplayer.participant.vars['last_active'] = time.time() if player.round_number != 1: if selfplayer.CV_viewing == 1: try: selfplayer.TimeSpentCV_1 = selfplayer.TimeSpentCV_1 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_1 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing == 2: try: selfplayer.TimeSpentCV_2 = selfplayer.TimeSpentCV_2 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_2 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing == 3: try: selfplayer.TimeSpentCV_3 = selfplayer.TimeSpentCV_3 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_3 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing == 4: try: selfplayer.TimeSpentCV_4 = selfplayer.TimeSpentCV_4 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_4 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing == 5: try: selfplayer.TimeSpentCV_5 = selfplayer.TimeSpentCV_5 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_5 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing == 6: try: selfplayer.TimeSpentCV_6 = selfplayer.TimeSpentCV_6 + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) except: selfplayer.TimeSpentCV_6 = ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) elif selfplayer.CV_viewing in [0, 21, 22]: selfplayer.TimeSpentInstruc = selfplayer.TimeSpentInstruc + ( selfplayer.time_clicked - player.in_round(player.round_number - 1).time_clicked ) else: print( '@@@@@@@@@@@@@@@@@@@@@@@ ERROR ERROR NO CV_VIEWING @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' ) else: selfplayer.TimeSpentInstruc = selfplayer.time_clicked - player.participant.vars.get( 'timelefttreatpage' ) selfplayer.TimeSpentCV_1 = 0 selfplayer.TimeSpentCV_2 = 0 selfplayer.TimeSpentCV_3 = 0 selfplayer.TimeSpentCV_4 = 0 selfplayer.TimeSpentCV_5 = 0 selfplayer.TimeSpentCV_6 = 0 selfvars = player.participant.vars nextselfplayer = player.in_round(player.round_number + 1) selfplayer.participant_vars_dump = str(selfvars) nextselfplayer.TimeSpentInstruc = selfplayer.TimeSpentInstruc nextselfplayer.TimeSpentCV_1 = selfplayer.TimeSpentCV_1 nextselfplayer.TimeSpentCV_2 = selfplayer.TimeSpentCV_2 nextselfplayer.TimeSpentCV_3 = selfplayer.TimeSpentCV_3 nextselfplayer.TimeSpentCV_4 = selfplayer.TimeSpentCV_4 nextselfplayer.TimeSpentCV_5 = selfplayer.TimeSpentCV_5 nextselfplayer.TimeSpentCV_6 = selfplayer.TimeSpentCV_6 nextselfplayer.CV_1 = selfplayer.field_maybe_none('CV_1') nextselfplayer.CV_2 = selfplayer.field_maybe_none('CV_2') nextselfplayer.CV_3 = selfplayer.field_maybe_none('CV_3') nextselfplayer.CV_4 = selfplayer.field_maybe_none('CV_4') nextselfplayer.CV_5 = selfplayer.field_maybe_none('CV_5') nextselfplayer.CV_6 = selfplayer.field_maybe_none('CV_6') nextselfplayer.Notes_1 = selfplayer.field_maybe_none('Notes_1') nextselfplayer.Notes_2 = selfplayer.field_maybe_none('Notes_2') nextselfplayer.Notes_3 = selfplayer.field_maybe_none('Notes_3') nextselfplayer.Notes_4 = selfplayer.field_maybe_none('Notes_4') nextselfplayer.Notes_5 = selfplayer.field_maybe_none('Notes_5') nextselfplayer.Notes_6 = selfplayer.field_maybe_none('Notes_6') if selfplayer.field_maybe_none('CV_to_view') != None: nextselfplayer.CV_viewing = selfplayer.CV_to_view else: nextselfplayer.CV_viewing = selfplayer.CV_viewing # nextselfplayer.mygroup = selfvars['mygroup'] ##@@ lots of repeated code here, should make function player.participant.vars['initial'] = False if not selfvars['submitted_all']: if selfplayer.subreq is True and ( None not in [ selfplayer.field_maybe_none('CV_1'), selfplayer.field_maybe_none('CV_2'), selfplayer.field_maybe_none('CV_3'), selfplayer.field_maybe_none('CV_4'), selfplayer.field_maybe_none('CV_5'), selfplayer.field_maybe_none('CV_6'), ] or not C.DO_CHECKS ): player.participant.vars['initial'] = True player.participant.vars['submitted_all'] = True # print("print the information") # print(selfplayer.ip) # print(selfplayer.loc) def isint1(player: Player): return not player.field_maybe_none('CV_1') == None and player.CV_1.is_integer() def isint2(player: Player): return not player.field_maybe_none('CV_2') == None and player.CV_2.is_integer() def isint3(player: Player): return not player.field_maybe_none('CV_3') == None and player.CV_3.is_integer() def isint4(player: Player): return not player.field_maybe_none('CV_4') == None and player.CV_4.is_integer() def isint5(player: Player): return not player.field_maybe_none('CV_5') == None and player.CV_5.is_integer() def isint6(player: Player): return not player.field_maybe_none('CV_6') == None and player.CV_6.is_integer() # PAGES class Instructions0(Page): print("instructions") @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' form_fields = ['ip', 'loc'] class Instructions1(Page): print("instructions") @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' form_fields = ['email'] @staticmethod def vars_for_template(player: Player): return standardvars(player) @staticmethod def before_next_page(player: Player, timeout_happened): print("try to append") player.append_loc( player.participant.vars['lat'], player.participant.vars['long'], player.participant.vars['ip2'], ) print("out of append") print(player.email) matches = ['prepone', 'revert', 'needful', 'do the needed'] matches2 = ['suggest me', 'have a doubt '] if len(player.email) < 100: print("failed: email too short") if C.DO_CHECKS: player.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "email_short" elif any(x in player.email for x in matches): print("failed: email major") if C.DO_CHECKS: player.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "email_major" elif all(x in player.email for x in matches2): print("not failed: email two minor") if C.DO_CHECKS: #### turn of minor fail # self.player.participant.vars['failed'] = 1 player.participant.vars['failed_bec'] = "email_minor" if player.participant.vars['failed'] == 0: return setup(player) player.participant_vars_dump = str(player.participant.vars) class Instructions2(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' @staticmethod def vars_for_template(player: Player): return standardvars(player) @staticmethod def before_next_page(player: Player, timeout_happened): print("try to setup") setup(player) return instructbeforenextpage(player) class Instructions3(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' @staticmethod def vars_for_template(player: Player): return standardvars(player) @staticmethod def before_next_page(player: Player, timeout_happened): return instructbeforenextpage(player) class Instructions4(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' @staticmethod def vars_for_template(player: Player): return standardvars(player) @staticmethod def before_next_page(player: Player, timeout_happened): return instructbeforenextpage(player) class Instructions5(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' @staticmethod def vars_for_template(player: Player): return standardvars(player) @staticmethod def before_next_page(player: Player, timeout_happened): player.participant.vars['timelefttreatpage'] = time.time() # self.participant.vars['last_active'] = time.time() return instructbeforenextpage(player) class Resume_Eval2(Page): @staticmethod def is_displayed(player: Player): if player.round_number == 1: return True else: return not player.participant.vars.get('submitted_all') form_model = 'player' form_fields = [ 'CV_1', 'CV_2', 'CV_3', 'CV_4', 'CV_5', 'CV_6', 'CV_to_view', 'subreq', 'Notes_1', 'Notes_2', 'Notes_3', 'Notes_4', 'Notes_5', 'Notes_6', 'ip', 'loc', 'drag', 'dragtime', 'CVscroll', 'CVscrolltime', 'SMscroll', 'SMscrolltime', ] ##@@ add in treatment2 # remember the order in the group is Treat1_1, treat1_1, con_1, con_2, treat2_1, treat2_2 , 2nd numuber is type @staticmethod def vars_for_template(player: Player): return varsfortemplate(player) @staticmethod def before_next_page(player: Player, timeout_happened): return beforenextpage(player) @staticmethod def error_message(player: Player, values): if C.DO_CHECKS: selfvars = player.participant.vars selfplayer = player CV_scores = list( [ values['CV_1'], values['CV_2'], values['CV_3'], values['CV_4'], values['CV_5'], values['CV_6'], ] ) print(CV_scores) if len(list(filter(None, CV_scores))) != len(set(list(filter(None, CV_scores)))): return ( "Please give each résumé a unique score. I.e. Always say that one résumé is better or worse than " "another résumé" ) if not selfvars.get('submitted_all'): if values['subreq'] is True and None in CV_scores: return 'You must fill in a score for each résumé before you can submit' # if values['subreq'] is True and min(CV_scores) != float(0): # return "Please rate the worst résumé with a score of 0" # if values['subreq'] is True and max(CV_scores) != float(10): # return "Please rate the best résumé with a score of 10" pass ##@@ add in more pages page_sequence = [ Instructions0, # Instructions1, Instructions2, Instructions3, Instructions4, Instructions5, Resume_Eval2, ]