async def post_receive_json(self, content: dict): ''' if an app name is given, export the app. otherwise, export all the data (wide). don't need time_spent or chat yet, they are quick enough ''' app_name = content.get('app_name') is_custom = content.get('is_custom') iso_date = datetime.date.today().isoformat() with io.StringIO() as fp: # Excel requires BOM; otherwise non-english characters are garbled if content.get('for_excel'): fp.write('\ufeff') if app_name: if is_custom: fxn = custom_export_app else: fxn = export_app fxn(app_name, fp) file_name_prefix = app_name else: export_wide(fp) file_name_prefix = 'all_apps_wide' data = fp.getvalue() file_name = f'{file_name_prefix}_{iso_date}.csv' content.update(file_name=file_name, data=data, mime_type='text/csv') # this doesn't go through channel layer, so it is probably safer # in terms of sending large data await self.send_json(content)
def post_receive(self, content: dict): ''' if an app name is given, export the app. otherwise, export all the data (wide). don't need time_spent or chat yet, they are quick enough ''' # authenticate # maybe it should be is_superuser or something else more specific # but this is to be consistent with the rest of Django's login if settings.AUTH_LEVEL and (not self.message.user.is_authenticated or not self.message.user.is_superuser): logger.warning( 'rejected access to data export through non-authenticated ' 'websocket') return file_extension = content['file_extension'] app_name = content.get('app_name') if file_extension == 'xlsx': mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" IOClass = io.BytesIO else: mime_type = 'text/csv' IOClass = io.StringIO iso_date = datetime.date.today().isoformat() with IOClass() as fp: if app_name: export_app(app_name, fp, file_extension=file_extension) file_name_prefix = app_name else: export_wide(fp, file_extension=file_extension) file_name_prefix = 'all_apps_wide' data = fp.getvalue() file_name = '{}_{}.{}'.format(file_name_prefix, iso_date, file_extension) if file_extension == 'xlsx': data = base64.b64encode(data).decode('utf-8') content.update({ 'file_name': file_name, 'data': data, 'mime_type': mime_type, }) self.send(content)
def post_receive(self, content: dict): ''' if an app name is given, export the app. otherwise, export all the data (wide). don't need time_spent or chat yet, they are quick enough ''' # authenticate # maybe it should be is_superuser or something else more specific # but this is to be consistent with the rest of Django's login if settings.AUTH_LEVEL and not self.message.user.is_authenticated: logger.warning( 'rejected access to data export through non-authenticated ' 'websocket' ) return file_extension = content['file_extension'] app_name = content.get('app_name') if file_extension == 'xlsx': mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" IOClass = io.BytesIO else: mime_type = 'text/csv' IOClass = io.StringIO iso_date = datetime.date.today().isoformat() with IOClass() as fp: if app_name: export_app(app_name, fp, file_extension=file_extension) file_name_prefix = app_name else: export_wide(fp, file_extension=file_extension) file_name_prefix = 'all_apps_wide' data = fp.getvalue() file_name = '{}_{}.{}'.format( file_name_prefix, iso_date, file_extension) if file_extension == 'xlsx': data = base64.b64encode(data).decode('utf-8') content.update({ 'file_name': file_name, 'data': data, 'mime_type': mime_type, }) self.send(content)
def post_receive_json(self, content: dict): ''' if an app name is given, export the app. otherwise, export all the data (wide). don't need time_spent or chat yet, they are quick enough ''' file_extension = content['file_extension'] app_name = content.get('app_name') if file_extension == 'xlsx': mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" IOClass = io.BytesIO else: mime_type = 'text/csv' IOClass = io.StringIO iso_date = datetime.date.today().isoformat() with IOClass() as fp: if app_name: export_app(app_name, fp, file_extension=file_extension) file_name_prefix = app_name else: export_wide(fp, file_extension=file_extension) file_name_prefix = 'all_apps_wide' data = fp.getvalue() file_name = '{}_{}.{}'.format(file_name_prefix, iso_date, file_extension) if file_extension == 'xlsx': data = base64.b64encode(data).decode('utf-8') content.update(file_name=file_name, data=data, mime_type=mime_type) # this doesn't go through channel layer, so it is probably safer # in terms of sending large data self.send_json(content)
def _all_data(): from otree import export fp = io.StringIO() export.export_wide(fp, file_extension='csv') fp.seek(0) return fp
def wide(self, fp): fext = self._fext(fp) export.export_wide(fp, file_extension=fext)