from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from os import environ import itertools import math import time import pandas import numpy from ipware import get_client_ip import ipinfo author = 'FAU_CET' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'cet_pre_screen' players_per_group = None #whats this? num_rounds = 100 do_checks = False #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_screen/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") def creating_session(self): #print("creating sesh") if self.round_number == 1: for p in self.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_screen/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) p.session.vars['myIDtool'] = itertools.cycle(p.session.vars['notstarted']) #with open("./cet_pre_screen/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_screen/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_screen/lats.txt", "r") as file_lats: #file_lats = open("./cet_pre_screen/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_screen/longs.txt", "r") as file_longs: #file_longs = open("./cet_pre_screen/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_screen/ip_roots.txt", "r") as file_ip_roots: #file_ip_roots = open("./cet_pre_screen/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']) pass class Group(BaseGroup): print("group") pass class Player(BasePlayer): def get_client_ip_my(self, request): ip, is_routable = get_client_ip(request) if 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(self, 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(self, lat, long, ip): if C.DO_CHECKS: lats = self.session.vars['lats'] longs = self.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 self.participant.vars['failed_bec'] = "loc" failcount = failcount + 1 print("failed1: same loc") else: self.session.vars['lats'].append(round(lat, 3)) self.session.vars['longs'].append(round(long, 3)) ip_roots = self.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 self.participant.vars['failed_bec'] = "ip" failcount = failcount + 1 print("failed1: same ip") else: self.session.vars['ip_roots'].append(ip_root) if failcount is 2: self.participant.vars['failed'] = 1 self.participant.vars['failed_bec'] = "ip_and_loc" print("aka failed both") else: pass def append_loc(self, lat, long, ip): print("inside append lock") keepgoing = True while keepgoing: try: with open("./cet_pre_screen/lats.txt", "a+") as file_lats: #file_lats = open("./cet_pre_screen/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_screen/longs.txt", "a+") as file_longs: #file_longs = open("./cet_pre_screen/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_screen/ip_roots.txt", "a+") as file_ip_roots: # file_ip_roots = open("./cet_pre_screen/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(self): return self.CV_viewing in [0, 21, 22] def standardvars(self): return { 'initial': self.participant.vars.get('initial'), 'submitted_all': self.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': self.participant.vars.get('failed') == 1 } def specialvars(self): partvars = self.participant.vars return { 'image_path': 'Resumes/Group{1}/Resume_{0}_T1_Output.jpg'.format( self.CV_viewing, partvars['myID']), 'html_path': 'data/Group{1}/ScrapedData{0}.html'.format( self.CV_viewing, partvars['myID']) } def varsfortemplate(self): if self.round_number == 1: self.CV_viewing = 21 if self.round_number != 1 and self.CV_viewing not in [0, 21, 22]: return ({**self.specialvars(), **self.standardvars()}) else: return self.standardvars() def setup(self): print("setup") selfplayer = self 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_screen/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_screen/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(self): self.participant.vars['last_active'] = time.time() self.participant_vars_dump = str(self.participant.vars) ##@@ this is full of repeated code, could make multiple functions def beforenextpage(self): selfplayer = self selfplayer.time_clicked = time.time() selfplayer.participant.vars['last_active'] = time.time() if self.round_number != 1: if selfplayer.CV_viewing == 1: try: selfplayer.TimeSpentCV_1 = selfplayer.TimeSpentCV_1 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_1 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing == 2: try: selfplayer.TimeSpentCV_2 = selfplayer.TimeSpentCV_2 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_2 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing == 3: try: selfplayer.TimeSpentCV_3 = selfplayer.TimeSpentCV_3 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_3 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing == 4: try: selfplayer.TimeSpentCV_4 = selfplayer.TimeSpentCV_4 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_4 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing == 5: try: selfplayer.TimeSpentCV_5 = selfplayer.TimeSpentCV_5 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_5 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing == 6: try: selfplayer.TimeSpentCV_6 = selfplayer.TimeSpentCV_6 + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) except: selfplayer.TimeSpentCV_6 = ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) elif selfplayer.CV_viewing in [0, 21, 22]: selfplayer.TimeSpentInstruc = selfplayer.TimeSpentInstruc + ( selfplayer.time_clicked - self.in_round(self.round_number - 1).time_clicked) else: print('@@@@@@@@@@@@@@@@@@@@@@@ ERROR ERROR NO CV_VIEWING @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@') else: selfplayer.TimeSpentInstruc = (selfplayer.time_clicked - self.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 = self.participant.vars nextselfplayer = self.in_round(self.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.CV_1 nextselfplayer.CV_2 = selfplayer.CV_2 nextselfplayer.CV_3 = selfplayer.CV_3 nextselfplayer.CV_4 = selfplayer.CV_4 nextselfplayer.CV_5 = selfplayer.CV_5 nextselfplayer.CV_6 = selfplayer.CV_6 nextselfplayer.Notes_1 = selfplayer.Notes_1 nextselfplayer.Notes_2 = selfplayer.Notes_2 nextselfplayer.Notes_3 = selfplayer.Notes_3 nextselfplayer.Notes_4 = selfplayer.Notes_4 nextselfplayer.Notes_5 = selfplayer.Notes_5 nextselfplayer.Notes_6 = selfplayer.Notes_6 if selfplayer.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 self.participant.vars['initial'] = False if not selfvars['submitted_all']: if selfplayer.subreq is True and (None not in [selfplayer.CV_1, selfplayer.CV_2, selfplayer.CV_3, selfplayer.CV_4, selfplayer.CV_5, selfplayer.CV_6] or not C.DO_CHECKS): self.participant.vars['initial'] = True self.participant.vars['submitted_all'] = True #print("print the information") #print(selfplayer.ip) #print(selfplayer.loc) def isint1(self): return not self.CV_1 == None and self.CV_1.is_integer() def isint2(self): return not self.CV_2 == None and self.CV_2.is_integer() def isint3(self): return not self.CV_3 == None and self.CV_3.is_integer() def isint4(self): return not self.CV_4 == None and self.CV_4.is_integer() def isint5(self): return not self.CV_5 == None and self.CV_5.is_integer() def isint6(self): return not self.CV_6 == None and self.CV_6.is_integer() 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, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 1"})) Notes_2 = models.LongStringField(blank=True, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 2"})) Notes_3 = models.LongStringField(blank=True, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 3"})) Notes_4 = models.LongStringField(blank=True, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 4"})) Notes_5 = models.LongStringField(blank=True, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 5"})) Notes_6 = models.LongStringField(blank=True, widget=widgets.Textarea(attrs={'placeholder': "Use this box for any notes regarding Résumé 6"})) 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)