33from app import consent , alert , experiment , complete , error
44from .io import write_metadata
55from .utils import gen_code
6- __version__ = '0.9.8 '
6+ __version__ = '1.0 '
77
88## Define root directory.
99ROOT_DIR = os .path .dirname (os .path .realpath (__file__ ))
2020reject_dir = os .path .join (ROOT_DIR , cfg ['IO' ]['REJECT' ])
2121if not os .path .isdir (reject_dir ): os .makedirs (reject_dir )
2222
23- ## Check Flask password .
24- if cfg ['FLASK' ][ 'SECRET_KEY' ] == "PLEASE_CHANGE_THIS" :
25- msg = "WARNING: Flask password is currently default. This should be changed prior to production."
26- warnings .warn (msg )
23+ ## Check Flask mode; if debug mode, clear session variable .
24+ debug = cfg ['FLASK' ]. getboolean ( 'DEBUG' )
25+ if debug :
26+ warnings .warn ("WARNING: Flask currently in debug mode. This should be changed prior to production." )
2727
28- ## Check Flask mode .
29- if cfg ['FLASK' ]['DEBUG' ] != "FALSE" :
30- msg = "WARNING: Flask currently in debug mode. This should be changed prior to production."
31- warnings .warn (msg )
28+ ## Check Flask password .
29+ secret_key = cfg ['FLASK' ]['SECRET_KEY' ]
30+ if secret_key == "PLEASE_CHANGE_THIS" :
31+ warnings .warn ("WARNING: Flask password is currently default. This should be changed prior to production." )
3232
3333## Initialize Flask application.
3434app = Flask (__name__ )
35- app .secret_key = cfg [ 'FLASK' ][ 'SECRET_KEY' ]
35+ app .secret_key = secret_key
3636
3737## Apply blueprints to the application.
3838app .register_blueprint (consent .bp )
4545@app .route ('/' )
4646def index ():
4747
48+ ## Debug mode: clear session.
49+ if debug :
50+ session .clear ()
51+
4852 ## Store directories in session object.
4953 session ['data' ] = data_dir
5054 session ['metadata' ] = meta_dir
5155 session ['reject' ] = reject_dir
52- session ['debug' ] = cfg ['FLASK' ]['DEBUG' ] != "FALSE"
5356
5457 ## Record incoming metadata.
5558 info = dict (
@@ -63,6 +66,7 @@ def index():
6366 tp_b = request .args .get ('tp_b' ), # TurkPrime metadata
6467 c = request .args .get ('c' ), # TurkPrime metadata
6568 tp_c = request .args .get ('tp_c' ), # TurkPrime metadata
69+ address = request .remote_addr , # NivTurk metadata
6670 browser = request .user_agent .browser , # User metadata
6771 platform = request .user_agent .platform , # User metadata
6872 version = request .user_agent .version , # User metadata
@@ -71,49 +75,82 @@ def index():
7175 ## Case 1: workerId absent.
7276 if info ['workerId' ] is None :
7377
74- ## Redirect participant to error (admin error ).
78+ ## Redirect participant to error (missing workerId ).
7579 return redirect (url_for ('error.error' , errornum = 1000 ))
7680
7781 ## Case 2: mobile user.
7882 elif info ['platform' ] in ['android' ,'iphone' ,'ipad' ,'wii' ]:
7983
80- ## Redirect participant to error (admin error).
84+ ## Redirect participant to error (platform error).
8185 return redirect (url_for ('error.error' , errornum = 1001 ))
8286
83- ## Case 3: repeat visit, manually changed workerId.
87+ ## Case 3: repeat visit, preexisting log but no session data.
88+ elif not 'workerId' in session and info ['workerId' ] in os .listdir (meta_dir ):
89+
90+ ## Consult log file.
91+ with open (os .path .join (session ['metadata' ], info ['workerId' ]),'r' ) as f :
92+ logs = f .read ()
93+
94+ ## Case 3a: previously started experiment.
95+ if 'experiment' in logs :
96+
97+ ## Update metadata.
98+ session ['workerId' ] = info ['workerId' ]
99+ session ['ERROR' ] = '1004: Suspected incognito user.'
100+ session ['complete' ] = 'error'
101+ write_metadata (session , ['ERROR' ,'complete' ], 'a' )
102+
103+ ## Redirect participant to error (previous participation).
104+ return redirect (url_for ('error.error' , errornum = 1004 ))
105+
106+ ## Case 3b: no previous experiment starts.
107+ else :
108+
109+ ## Update metadata.
110+ for k , v in info .items (): session [k ] = v
111+ session ['WARNING' ] = "Assigned new subId."
112+ write_metadata (session , ['subId' ,'WARNING' ], 'a' )
113+
114+ ## Redirect participant to consent form.
115+ return redirect (url_for ('consent.consent' ))
116+
117+ ## Case 4: repeat visit, manually changed workerId.
84118 elif 'workerId' in session and session ['workerId' ] != info ['workerId' ]:
85119
86120 ## Update metadata.
87- session ['ERROR' ] = '1002: workerId tampering detected.'
88- write_metadata (session , ['ERROR' ], 'a' )
121+ session ['ERROR' ] = '1005: workerId tampering detected.'
122+ session ['complete' ] = 'error'
123+ write_metadata (session , ['ERROR' ,'complete' ], 'a' )
89124
90125 ## Redirect participant to error (unusual activity).
91- return redirect (url_for ('error.error' , errornum = 1002 ))
126+ return redirect (url_for ('error.error' , errornum = 1005 ))
92127
93- ## Case 4: repeat visit, preexisting activity.
94- elif 'workerId' in session or info ['workerId' ] in os .listdir (meta_dir ):
128+ ## Case 5: repeat visit, previously completed experiment.
129+ elif 'complete' in session :
130+
131+ ## Update metadata.
132+ session ['WARNING' ] = "Revisited home."
133+ write_metadata (session , ['WARNING' ], 'a' )
134+
135+ ## Redirect participant to complete page.
136+ return redirect (url_for ('complete.complete' ))
137+
138+ ## Case 6: repeat visit, preexisting activity.
139+ elif 'workerId' in session :
95140
96141 ## Update metadata.
97- for k , v in info .items (): session [k ] = v
98142 session ['WARNING' ] = "Revisited home."
99143 write_metadata (session , ['WARNING' ], 'a' )
100144
101145 ## Redirect participant to consent form.
102146 return redirect (url_for ('consent.consent' ))
103147
104- ## Case 5 : first visit, workerId present.
148+ ## Case 7 : first visit, workerId present.
105149 else :
106150
107151 ## Update metadata.
108152 for k , v in info .items (): session [k ] = v
109- write_metadata (session , ['workerId' ,'hitId' ,'assignmentId' ,'subId' ,'browser' ,'platform' ,'version' ], 'w' )
153+ write_metadata (session , ['workerId' ,'hitId' ,'assignmentId' ,'subId' ,'address' , ' browser' ,'platform' ,'version' ], 'w' )
110154
111155 ## Redirect participant to consent form.
112156 return redirect (url_for ('consent.consent' ))
113-
114- ## DEV NOTE:
115- ## The following route is strictly for development purpose and should be commented out before deployment.
116- # @app.route('/clear')
117- # def clear():
118- # session.clear()
119- # return 'Complete!'
0 commit comments