def augment_session_config(session_config): new_session_config = {'doc': ''} new_session_config.update(settings.SESSION_CONFIG_DEFAULTS) new_session_config.update(session_config) # look up new_session_config # 2015-05-14: why do we strip? the doc can have line breaks in the middle # anyways new_session_config['doc'] = new_session_config['doc'].strip() # TODO: fixed_pay is deprecated as of 2015-05-07, # in favor of participation_fee. make this required at some point. if (('participation_fee' not in new_session_config) and ('fixed_pay' in new_session_config)): deprecate.dwarning( '"fixed_pay" is deprecated; ' 'you should rename it to "participation_fee".' ) new_session_config['participation_fee'] = ( new_session_config['fixed_pay']) new_session_config['participation_fee'] = RealWorldCurrency( new_session_config['participation_fee']) # normalize to decimal so we can do multiplications, etc # quantize because the original value may be a float, # which when converted to Decimal may have some 'decimal junk' # like 0.010000000000000000208166817... new_session_config['real_world_currency_per_point'] = Decimal( new_session_config['real_world_currency_per_point'] ).quantize(Decimal('0.00001')) validate_session_config(new_session_config) return new_session_config
def augment_session_config(session_config): new_session_config = {"doc": ""} new_session_config.update(settings.SESSION_CONFIG_DEFAULTS) new_session_config.update(session_config) # look up new_session_config # 2015-05-14: why do we strip? the doc can have line breaks in the middle # anyways new_session_config["doc"] = new_session_config["doc"].strip() # TODO: fixed_pay is deprecated as of 2015-05-07, # in favor of participation_fee. make this required at some point. if ("participation_fee" not in new_session_config) and ("fixed_pay" in new_session_config): deprecate.dwarning('"fixed_pay" is deprecated; ' 'you should rename it to "participation_fee".') new_session_config["participation_fee"] = new_session_config["fixed_pay"] new_session_config["participation_fee"] = RealWorldCurrency(new_session_config["participation_fee"]) # normalize to decimal so we can do multiplications, etc # quantize because the original value may be a float, # which when converted to Decimal may have some 'decimal junk' # like 0.010000000000000000208166817... new_session_config["real_world_currency_per_point"] = Decimal( new_session_config["real_world_currency_per_point"] ).quantize(Decimal("0.00001")) validate_session_config(new_session_config) return new_session_config
def test_dwarning(self): with warnings.catch_warnings(record=True) as warns: msg = self.random_string() deprecate.dwarning(msg) dw = self.extract_otree_dwarn(warns) self.assertTrue(dw) self.assertEqual(force_text(dw.message), msg) with warnings.catch_warnings(record=True) as warns: warnings.simplefilter("ignore", deprecate.OTreeDeprecationWarning) msg = self.random_string() deprecate.dwarning(msg) dw = self.extract_otree_dwarn(warns) self.assertFalse(dw) with warnings.catch_warnings(record=True) as warns: warnings.simplefilter("error", deprecate.OTreeDeprecationWarning) msg = self.random_string() with self.assertRaises(deprecate.OTreeDeprecationWarning) as cm: deprecate.dwarning(msg) self.assertEqual(force_text(cm.exception), msg)
def post(self, request, *args, **kwargs): form = self.get_form(data=request.POST, files=request.FILES) if not form.is_valid(): return self.form_invalid(form) session = self.session in_sandbox = 'in_sandbox' in form.data # session can't be created if (not self.in_public_domain(request, *args, **kwargs) and not in_sandbox): msg = ('<h1>Error: ' 'oTree must run on a public domain for Mechanical Turk' '</h1>') return HttpResponseServerError(msg) with MTurkConnection(self.request, in_sandbox) as mturk_connection: mturk_settings = session.config['mturk_hit_settings'] qualification_id = mturk_settings.get('grant_qualification_id', None) # verify that specified qualification type # for preventing retakes exists on mturk server if qualification_id: try: mturk_connection.get_qualification_type(qualification_id) except MTurkRequestError as e: code = 'AWS.MechanicalTurk.QualificationTypeDoesNotExist' if e.error_code == code: msg = ("In settings.py you specified qualification id " " '%s' which doesn't exist on mturk server. " "Please verify its validity.") msg = msg.format('grant_qualification_id') messages.error(request, msg) return HttpResponseRedirect( reverse('session_create_hit', args=(session.pk, ))) else: session.mturk_qualification_type_id = qualification_id url_landing_page = self.request.build_absolute_uri( reverse('mturk_landing_page', args=(session.code, ))) # updating schema from http to https # this is compulsory for MTurk exteranlQuestion secured_url_landing_page = urlunparse( urlparse(url_landing_page)._replace(scheme='https')) # TODO: validate, that the server support https # (heroku does support by default) # TODO: validate that there is enought money for the hit reward = boto.mturk.price.Price( amount=float(form.data['money_reward'])) # creating external questions, that would be passed to the hit external_question = boto.mturk.question.ExternalQuestion( secured_url_landing_page, mturk_settings['frame_height']) qualifications = mturk_settings.get('qualification_requirements') # deprecated summer 2015: remove this if (not qualifications and hasattr(settings, 'MTURK_WORKER_REQUIREMENTS')): deprecate.dwarning( 'The MTURK_WORKER_REQUIREMENTS setting has been ' 'deprecated. You should instead use ' '"qualification_requirements" as shown here: ' 'https://github.com/oTree-org/oTree/blob/master/' 'settings.py') qualifications = settings.MTURK_WORKER_REQUIREMENTS mturk_hit_parameters = { 'title': form.cleaned_data['title'], 'description': form.cleaned_data['description'], 'keywords': [k.strip() for k in form.cleaned_data['keywords'].split(',')], 'question': external_question, 'max_assignments': form.cleaned_data['assignments'], 'reward': reward, 'response_groups': ('Minimal', 'HITDetail'), 'qualifications': Qualifications(qualifications), } if form.cleaned_data['minutes_allotted_per_assignment']: mturk_hit_parameters['duration'] = datetime.timedelta(minutes=( form.cleaned_data['minutes_allotted_per_assignment'])) if form.cleaned_data['expiration_hours']: mturk_hit_parameters['lifetime'] = datetime.timedelta( hours=form.cleaned_data['expiration_hours']) hit = mturk_connection.create_hit(**mturk_hit_parameters) session.mturk_HITId = hit[0].HITId session.mturk_HITGroupId = hit[0].HITGroupId session.mturk_sandbox = in_sandbox session.save() return HttpResponseRedirect( reverse('session_create_hit', args=(session.pk, )))
def post(self, request, *args, **kwargs): form = self.get_form( data=request.POST, files=request.FILES ) if not form.is_valid(): return self.form_invalid(form) session = self.session in_sandbox = 'in_sandbox' in form.data # session can't be created if (not self.in_public_domain(request, *args, **kwargs) and not in_sandbox): msg = ( '<h1>Error: ' 'oTree must run on a public domain for Mechanical Turk' '</h1>') return HttpResponseServerError(msg) with MTurkConnection(self.request, in_sandbox) as mturk_connection: mturk_settings = session.config['mturk_hit_settings'] qualification_id = mturk_settings.get( 'grant_qualification_id', None) # verify that specified qualification type # for preventing retakes exists on mturk server if qualification_id: try: mturk_connection.get_qualification_type(qualification_id) except MTurkRequestError as e: code = 'AWS.MechanicalTurk.QualificationTypeDoesNotExist' if e.error_code == code: msg = ( "In settings.py you specified qualification id " " '%s' which doesn't exist on mturk server. " "Please verify its validity.") msg = msg.format('grant_qualification_id') messages.error(request, msg) return HttpResponseRedirect( reverse('session_create_hit', args=(session.pk,))) else: session.mturk_qualification_type_id = qualification_id url_landing_page = self.request.build_absolute_uri( reverse('mturk_landing_page', args=(session.code,))) # updating schema from http to https # this is compulsory for MTurk exteranlQuestion secured_url_landing_page = urlparse.urlunparse( urlparse.urlparse(url_landing_page)._replace(scheme='https')) # TODO: validate, that the server support https # (heroku does support by default) # TODO: validate that there is enought money for the hit reward = boto.mturk.price.Price( amount=float(form.data['money_reward'])) # creating external questions, that would be passed to the hit external_question = boto.mturk.question.ExternalQuestion( secured_url_landing_page, mturk_settings['frame_height']) qualifications = mturk_settings.get('qualification_requirements') # deprecated summer 2015: remove this if (not qualifications and hasattr(settings, 'MTURK_WORKER_REQUIREMENTS')): deprecate.dwarning( 'The MTURK_WORKER_REQUIREMENTS setting has been ' 'deprecated. You should instead use ' '"qualification_requirements" as shown here: ' 'https://github.com/oTree-org/oTree/blob/master/' 'settings.py') qualifications = settings.MTURK_WORKER_REQUIREMENTS mturk_hit_parameters = { 'title': form.cleaned_data['title'], 'description': form.cleaned_data['description'], 'keywords': [ k.strip() for k in form.cleaned_data['keywords'].split(',') ], 'question': external_question, 'max_assignments': form.cleaned_data['assignments'], 'reward': reward, 'response_groups': ('Minimal', 'HITDetail'), 'qualifications': Qualifications(qualifications), } if form.cleaned_data['minutes_allotted_per_assignment']: mturk_hit_parameters['duration'] = datetime.timedelta( minutes=( form.cleaned_data['minutes_allotted_per_assignment'])) if form.cleaned_data['expiration_hours']: mturk_hit_parameters['lifetime'] = datetime.timedelta( hours=form.cleaned_data['expiration_hours']) hit = mturk_connection.create_hit(**mturk_hit_parameters) session.mturk_HITId = hit[0].HITId session.mturk_HITGroupId = hit[0].HITGroupId session.mturk_sandbox = in_sandbox session.save() return HttpResponseRedirect( reverse('session_create_hit', args=(session.pk,)))