Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_rows_for_csv(app_name):
# need to use app_name and not app_label because the app might have been
# removed from SESSION_CONFIGS
models_module = otree.common.get_models_module(app_name)
Player = models_module.Player
Group = models_module.Group
Subsession = models_module.Subsession
columns_for_models = {
Model.__name__.lower(): get_field_names_for_csv(Model)
for Model in [Player, Group, Subsession, Participant, Session]
}
participant_ids = Player.objects.values_list('participant_id', flat=True)
session_ids = Subsession.objects.values_list('session_id', flat=True)
players = Player.objects.order_by('id').values()
value_dicts = {
'group': {row['id']: row for row in Group.objects.values()},
def _Constants(self):
return get_models_module(self._meta.app_config.name).Constants
participant_values = session.participant_set.order_by('id').values('code', 'id')
ParticipantLockModel.objects.bulk_create(
[
ParticipantLockModel(participant_code=participant['code'])
for participant in participant_values
]
)
participant_to_player_lookups = []
page_index = 0
for app_name in session_config['app_sequence']:
views_module = common.get_pages_module(app_name)
models_module = get_models_module(app_name)
Constants = models_module.Constants
num_subsessions += Constants.num_rounds
round_numbers = list(range(1, Constants.num_rounds + 1))
Subsession = models_module.Subsession
Group = models_module.Group
Player = models_module.Player
Subsession.objects.bulk_create(
[
Subsession(round_number=round_number, session=session)
for round_number in round_numbers
]
)
def get_subsessions(self):
lst = []
app_sequence = self.config['app_sequence']
for app in app_sequence:
models_module = otree.common.get_models_module(app)
subsessions = models_module.Subsession.objects.filter(
session=self
).order_by('round_number')
lst.extend(list(subsessions))
return lst
)
run_bots(session, case_number=case_number)
logger.info('Bots completed session')
if export_path:
now = datetime.datetime.now()
if export_path == AUTO_NAME_BOTS_EXPORT_FOLDER:
# oTree convention to prefix __temp all temp folders.
export_path = now.strftime('__temp_bots_%b%d_%Hh%Mm%S.%f')[:-5] + 's'
os.makedirs(export_path, exist_ok=True)
for app in settings.INSTALLED_OTREE_APPS:
model_module = otree.common.get_models_module(app)
if model_module.Player.objects.exists():
fpath = Path(export_path, "{}.csv".format(app))
with fpath.open("w", encoding="utf8") as fp:
otree.export.export_app(app, fp, file_extension='csv')
fpath = Path(export_path, "all_apps_wide.csv")
with fpath.open("w", encoding="utf8") as fp:
otree.export.export_wide(fp, 'csv')
logger.info('Exported CSV to folder "{}"'.format(export_path))
else:
logger.info(
'Tip: Run this command with the --export flag'
' to save the data generated by bots.'
# was removed from SESSION_CONFIGS.
app_names_with_data = set()
for session in sessions:
for app_name in session.config['app_sequence']:
app_names_with_data.add(app_name)
apps_not_in_popular_sequence = [
app for app in app_names_with_data if app not in most_common_app_sequence
]
order_of_apps = list(most_common_app_sequence) + apps_not_in_popular_sequence
rounds_per_app = OrderedDict()
for app_name in order_of_apps:
try:
models_module = get_models_module(app_name)
except ModuleNotFoundError:
# this should only happen with devserver because on production server,
# you would need to resetdb after renaming an app.
logger.warning(
f'Cannot export data for app {app_name}, which existed when the session was run '
f'but no longer exists.'
)
continue
agg_dict = models_module.Subsession.objects.all().aggregate(Max('round_number'))
highest_round_number = agg_dict['round_number__max']
if highest_round_number is not None:
rounds_per_app[app_name] = highest_round_number
for app_name in rounds_per_app:
for round_number in range(1, rounds_per_app[app_name] + 1):
new_rows = get_rows_for_wide_csv_round(app_name, round_number, sessions)
def get_rows_for_wide_csv_round(app_name, round_number, sessions):
models_module = otree.common.get_models_module(app_name)
Player = models_module.Player
Group = models_module.Group
Subsession = models_module.Subsession
rows = []
group_cache = {row['id']: row for row in Group.objects.values()}
columns_for_models = {
Model.__name__.lower(): get_field_names_for_csv(Model)
for Model in [Player, Group, Subsession]
}
model_order = ['player', 'group', 'subsession']
header_row = []