def export_sessions_csv_task(self, event_id): sessions = db.session.query(Session).filter_by(event_id=event_id) try: filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/') if not os.path.isdir(filedir): os.makedirs(filedir) filename = "sessions-{}.csv".format(uuid.uuid1().hex) file_path = os.path.join(filedir, filename) with open(file_path, "w") as temp_file: writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_sessions_csv content = export_sessions_csv(sessions) for row in content: writer.writerow(row) sessions_csv_file = UploadedFile(file_path=file_path, filename=filename) sessions_csv_url = upload( sessions_csv_file, UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier='')) result = {'download_url': sessions_csv_url} except Exception as e: result = {'__error': True, 'result': str(e)} logging.error('Error in exporting sessions as CSV') return result
def export_sessions_csv_task(self, event_id): sessions = db.session.query(Session).filter_by(event_id=event_id) try: filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/') if not os.path.isdir(filedir): os.makedirs(filedir) filename = "sessions-{}.csv".format(uuid.uuid1().hex) file_path = os.path.join(filedir, filename) with open(file_path, "w") as temp_file: writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_sessions_csv content = export_sessions_csv(sessions) for row in content: writer.writerow(row) sessions_csv_file = UploadedFile(file_path=file_path, filename=filename) sessions_csv_url = upload(sessions_csv_file, UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier='')) result = { 'download_url': sessions_csv_url } except Exception as e: print(traceback.format_exc()) result = {'__error': True, 'result': str(e)} return result
def export_sessions_csv_task(self, event_id, status='all'): if status not in [ 'all', 'pending', 'accepted', 'confirmed', 'rejected', 'withdrawn', 'canceled', ]: status = 'all' if status == 'all': sessions = Session.query.filter( Session.event_id == event_id, Session.deleted_at.is_(None) ).all() else: sessions = Session.query.filter( Session.state == status, Session.event_id == event_id, Session.deleted_at.is_(None), ).all() try: filedir = os.path.join(current_app.config.get('BASE_DIR'), 'static/uploads/temp/') if not os.path.isdir(filedir): os.makedirs(filedir) filename = f"sessions-{uuid.uuid1().hex}.csv" file_path = os.path.join(filedir, filename) with open(file_path, "w") as temp_file: writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_sessions_csv content = export_sessions_csv(sessions) for row in content: writer.writerow(row) sessions_csv_file = UploadedFile(file_path=file_path, filename=filename) sessions_csv_url = upload( sessions_csv_file, UPLOAD_PATHS['exports-temp']['csv'].format(event_id=event_id, identifier=''), ) result = {'download_url': sessions_csv_url} except Exception as e: result = {'__error': True, 'result': str(e)} logging.exception('Error in exporting sessions as CSV') return result