def inner_post(self, **kwargs): session = create_session(**kwargs) room_name = kwargs.get('room_name') if room_name: channel_utils.sync_group_send_wrapper( type='room_session_ready', group=channel_utils.room_participants_group_name(room_name), event={}, ) return HttpResponse(session.code)
def inner_post(self, **kwargs): ''' Notes: - This allows you to pass parameters that did not exist in the original config, as well as params that are blocked from editing in the UI, either because of datatype. I can't see any specific problem with this. ''' session = create_session(**kwargs) room_name = kwargs.get('room_name') if room_name: channel_utils.sync_group_send_wrapper( type='room_session_ready', group=channel_utils.room_participants_group_name(room_name), event={}, ) return HttpResponse(session.code)
def create_session(message): group = Group(message['channels_group_name']) kwargs = message['kwargs'] try: session = otree.session.create_session(**kwargs) if message['use_browser_bots']: otree.bots.browser.initialize_session( session_pk=session.pk, case_number=None ) session.ready_for_browser = True session.save() except Exception as e: # full error message is printed to console (though sometimes not?) error_message = 'Failed to create session: "{}"'.format(e) traceback_str = traceback.format_exc() group.send( {'text': json.dumps( { 'error': error_message, 'traceback': traceback_str, })} ) FailedSessionCreation.objects.create( pre_create_id=kwargs['pre_create_id'], message=error_message[:FAILURE_MESSAGE_MAX_LENGTH], traceback=traceback_str ) raise group.send( {'text': json.dumps( {'status': 'ready'})} ) if 'room_name' in kwargs: Group(channel_utils.room_participants_group_name(kwargs['room_name'])).send( {'text': json.dumps( {'status': 'session_ready'})} )
def create_session(message): group = Group(message['channels_group_name']) kwargs = message['kwargs'] # because it's launched through web UI kwargs['honor_browser_bots_config'] = True try: otree.session.create_session(**kwargs) except Exception as e: # full error message is printed to console (though sometimes not?) error_message = 'Failed to create session: "{}"'.format(e) traceback_str = traceback.format_exc() group.send( {'text': json.dumps( { 'error': error_message, 'traceback': traceback_str, })} ) FailedSessionCreation.objects.create( pre_create_id=kwargs['pre_create_id'], message=error_message[:FAILURE_MESSAGE_MAX_LENGTH], traceback=traceback_str ) raise group.send( {'text': json.dumps( {'status': 'ready'})} ) if 'room_name' in kwargs: Group(channel_utils.room_participants_group_name(kwargs['room_name'])).send( {'text': json.dumps( {'status': 'session_ready'})} )
def group_name(self, room_name, participant_label, tab_unique_id): return channel_utils.room_participants_group_name(room_name)
async def post_receive_json(self, form_data: dict): form = CreateSessionForm(data=form_data) if not form.is_valid(): await self.send_json({'validation_errors': form.errors}) return session_config_name = form.cleaned_data['session_config'] is_mturk = form.cleaned_data['is_mturk'] config = SESSION_CONFIGS_DICT[session_config_name] num_participants = form.cleaned_data['num_participants'] if is_mturk: num_participants *= settings.MTURK_NUM_PARTICIPANTS_MULTIPLE edited_session_config_fields = {} for field in config.editable_fields(): html_field_name = config.html_field_name(field) old_value = config[field] # to allow concise unit tests, we can simply omit any fields we don't # want to change. this allows us to write more concise # unit tests. # EXCEPT for boolean fields -- omitting # it means we turn it off. # ideally we could interpret omitted boolean fields as unchanged # and False as unchecked, but HTML & serializeArray omits # unchecked checkboxes from form data. if isinstance(old_value, bool): new_value = bool(form_data.get(html_field_name)) if old_value != new_value: edited_session_config_fields[field] = new_value else: new_value_raw = form_data.get(html_field_name, '') if new_value_raw != '': # don't use isinstance because that will catch bool also if type(old_value) is int: # in case someone enters 1.0 instead of 1 new_value = int(float(new_value_raw)) else: new_value = type(old_value)(new_value_raw) if old_value != new_value: edited_session_config_fields[field] = new_value use_browser_bots = edited_session_config_fields.get( 'use_browser_bots', config.get('use_browser_bots', False)) # if room_name is missing, it will be empty string room_name = form.cleaned_data['room_name'] or None await self.create_session_then_send_start_link( session_config_name=session_config_name, num_participants=num_participants, is_demo=False, is_mturk=is_mturk, edited_session_config_fields=edited_session_config_fields, use_browser_bots=use_browser_bots, room_name=room_name, ) if room_name: await channel_utils.group_send_wrapper( type='room_session_ready', group=channel_utils.room_participants_group_name(room_name), event={}, )
async def post_receive_json(self, form_data: dict): # when i passed in data= as a dict, InputRequired failed. # i guess it looks in formdata to see if an input was made. form = CreateSessionForm(formdata=FormData(form_data)) if not form.validate(): await self.send_json({'validation_errors': form.errors}) return session_config_name = form.session_config.data is_mturk = form.is_mturk.data config = SESSION_CONFIGS_DICT[session_config_name] num_participants = form.num_participants.data if is_mturk: num_participants *= settings.MTURK_NUM_PARTICIPANTS_MULTIPLE modified_session_config_fields = {} for field in config.editable_fields(): html_field_name = config.html_field_name(field) old_value = config[field] # to allow concise unit tests, we can simply omit any fields we don't # want to change. this allows us to write more concise # unit tests. # EXCEPT for boolean fields -- omitting # it means we turn it off. # ideally we could interpret omitted boolean fields as unchanged # and False as unchecked, but HTML & serializeArray omits # unchecked checkboxes from form data. if isinstance(old_value, bool): new_value = bool(form_data.get(html_field_name)) if old_value != new_value: modified_session_config_fields[field] = new_value else: new_value_raw = form_data.get(html_field_name, '') if new_value_raw != '': # don't use isinstance because that will catch bool also if type(old_value) is int: # in case someone enters 1.0 instead of 1 new_value = int(float(new_value_raw)) else: new_value = type(old_value)(new_value_raw) if old_value != new_value: modified_session_config_fields[field] = new_value use_browser_bots = modified_session_config_fields.get( 'use_browser_bots', config.get('use_browser_bots', False)) # if room_name is missing, it will be empty string room_name = form.room_name.data or None await self.create_session_then_send_start_link( session_config_name=session_config_name, num_participants=num_participants, is_demo=False, is_mturk=is_mturk, modified_session_config_fields=modified_session_config_fields, use_browser_bots=use_browser_bots, room_name=room_name, ) if room_name: await channel_utils.group_send( group=channel_utils.room_participants_group_name(room_name), data=SESSION_READY_PAYLOAD, )