def create_osm_export(username, id_string, export_id, **options): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state export = _get_export_object(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_osm_export(Export.OSM_EXPORT, username, id_string, export_id, options, xform=export.xform) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception( "OSM Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_external_export(username, id_string, export_id, query=None, token=None, meta=None): export = Export.objects.get(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_external_export( Export.EXTERNAL_EXPORT, username, id_string, export_id, token, query, meta ) except (Exception, NoRecordsFoundError, ConnectionError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("External Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_zip_export(username, id_string, export_id, **options): export = _get_export_object(id=export_id) try: gen_export = generate_attachments_zip_export( Export.ZIP_EXPORT, username, id_string, export_id, options, xform=export.xform) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception("Zip Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e) raise else: if not settings.TESTING_MODE: delete_export.apply_async( (), {'export_id': gen_export.id}, countdown=settings.ZIP_EXPORT_COUNTDOWN) return gen_export.id
def create_csv_export(username, id_string, export_id, query=None, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state export = Export.objects.get(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_export( Export.CSV_EXPORT, 'csv', username, id_string, export_id, query, group_delimiter, split_select_multiples, binary_select_multiples) except NoRecordsFoundError: # not much we can do but we don't want to report this as the user # should not even be on this page if the survey has no records export.internal_status = Export.FAILED export.save() except Exception as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("CSV Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_sav_zip_export(username, id_string, export_id, query=None, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False): export = Export.objects.get(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_export( Export.SAV_ZIP_EXPORT, 'zip', username, id_string, export_id, query, group_delimiter, split_select_multiples, binary_select_multiples ) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("SAV ZIP Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_kml_export(username, id_string, export_id, query=None): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state export = Export.objects.get(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_kml_export( Export.KML_EXPORT, 'kml', username, id_string, export_id, query) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("KML Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_xls_export(username, id_string, export_id, **options): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state force_xlsx = options.get("force_xlsx", True) options["extension"] = 'xlsx' if force_xlsx else 'xls' try: export = _get_export_object(id=export_id) except Export.DoesNotExist: # no export for this ID return None. return None # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery try: gen_export = generate_export(Export.XLS_EXPORT, export.xform, export_id, options) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception( "XLS Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) # Raise for now to let celery know we failed # - doesnt seem to break celery` raise else: return gen_export.id
def create_csv_export(username, id_string, export_id, **options): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state export = _get_export_object(id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_export(Export.CSV_EXPORT, export.xform, export_id, options) except NoRecordsFoundError: # not much we can do but we don't want to report this as the user # should not even be on this page if the survey has no records export.internal_status = Export.FAILED export.save() except Exception as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception( "CSV Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_xls_export(username, id_string, export_id, query=None, force_xlsx=True, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state ext = 'xls' if not force_xlsx else 'xlsx' export = Export.objects.get(id=export_id) # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery try: gen_export = generate_export( Export.XLS_EXPORT, ext, username, id_string, export_id, query, group_delimiter, split_select_multiples, binary_select_multiples) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("XLS Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) #raise for now to let celery know we failed # - doesnt seem to break celery` raise else: return gen_export.id
def test_report_exception_with_exc_info(self): e = Exception("A test exception") try: raise e except Exception as e: exc_info = sys.exc_info() try: report_exception(subject="Test report exception", info=e, exc_info=exc_info) except Exception as e: raise AssertionError("%s" % e)
def create_db_export(username, id_string, export_id, query=None, force_xlsx=False, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False, show_label=False): # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state ext = 'txt' if not force_xlsx else 'txt' try: logger = create_db_export.get_logger() logger.info("Enter to get export_id") # print 'Enter to get export_id' export = Export.objects.get(id=export_id) except Export.DoesNotExist: # no export for this ID return None. return None # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery try: gen_export = generate_export(Export.DB_EXPORT, ext, username, id_string, export_id, query, group_delimiter, split_select_multiples, binary_select_multiples, show_label) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception( "DB Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) # Raise for now to let celery know we failed # - doesnt seem to break celery` raise else: return gen_export.id
def set_project_perms_to_xform_async(xform_id, project_id): try: xform = XForm.objects.get(id=xform_id) project = Project.objects.get(id=project_id) except (Project.DoesNotExist, XForm.DoesNotExist): pass else: try: if len(getattr(settings, 'SLAVE_DATABASES', [])): from multidb.pinning import use_master with use_master: set_project_perms_to_xform(xform, project) else: set_project_perms_to_xform(xform, project) except Exception as e: msg = '%s: Setting project %d permissions to form %d failed.' % ( type(e), project_id, xform_id) report_exception(msg, e, sys.exc_info())
def create_analyser_export(username, id_string, export_id, query=None): # Mostly a serving of copy pasta based on the above `create_xls_export()`. Enjoy. # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state ext = 'xlsx' try: export = Export.objects.get(id=export_id) except Export.DoesNotExist: # no export for this ID return None. return None # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery try: gen_export = generate_export(Export.ANALYSER_EXPORT, ext, username, id_string, export_id, query, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception( "Analyser Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) # Raise for now to let celery know we failed # - doesnt seem to break celery` raise else: return gen_export.id
def create_sav_zip_export(username, id_string, export_id, **options): export = _get_export_object(id=export_id) options["extension"] = Export.ZIP_EXPORT try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_export(Export.SAV_ZIP_EXPORT, export.xform, export_id, options) except (Exception, NoRecordsFoundError, TypeError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception("SAV ZIP Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id
def create_zip_export(username, id_string, export_id, query=None): export = Export.objects.get(id=export_id) try: gen_export = generate_attachments_zip_export( Export.ZIP_EXPORT, 'zip', username, id_string, export_id, query) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("Zip Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e) raise else: if not settings.TESTING_MODE: delete_export.apply_async( (), {'export_id': gen_export.id}, countdown=settings.ZIP_EXPORT_COUNTDOWN) return gen_export.id
def create_analyser_export(username, id_string, export_id, query=None): # Mostly a serving of copy pasta based on the above `create_xls_export()`. Enjoy. # we re-query the db instead of passing model objects according to # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state ext = 'xlsx' try: export = Export.objects.get(id=export_id) except Export.DoesNotExist: # no export for this ID return None. return None # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery try: gen_export = generate_export(Export.ANALYSER_EXPORT, ext, username, id_string, export_id, query, group_delimiter='/', split_select_multiples=True, binary_select_multiples=False) except (Exception, NoRecordsFoundError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = { 'export_id': export_id, 'username': username, 'id_string': id_string } report_exception("Analyser Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) # Raise for now to let celery know we failed # - doesnt seem to break celery` raise else: return gen_export.id
def create_external_export(username, id_string, export_id, **options): export = get_object_or_404(Export, id=export_id) try: # though export is not available when for has 0 submissions, we # catch this since it potentially stops celery gen_export = generate_external_export(Export.EXTERNAL_EXPORT, username, id_string, export_id, options, xform=export.xform) except (Exception, NoRecordsFoundError, ConnectionError) as e: export.internal_status = Export.FAILED export.save() # mail admins details = _get_export_details(username, id_string, export_id) report_exception( "External Export Exception: Export ID - " "%(export_id)s, /%(username)s/%(id_string)s" % details, e, sys.exc_info()) raise else: return gen_export.id