def csv_import(task_id): # pragma: no cover task_obj = ImportTask.objects.get(pk=task_id) log = StringIO() task_obj.task_id = csv_import.request.id task_obj.task_status = ImportTask.RUNNING task_obj.log("Started import at %s" % timezone.now()) task_obj.log("--------------------------------") task_obj.save() try: with transaction.atomic(): model = class_from_string(task_obj.model_class) records = model.import_csv(task_obj, log) task_obj.task_status = ImportTask.SUCCESS task_obj.save() task_obj.log(log.getvalue()) task_obj.log("Import finished at %s" % timezone.now()) task_obj.log("%d record(s) added." % len(records)) except Exception as e: import traceback traceback.print_exc(e) task_obj.task_status = ImportTask.FAILURE task_obj.log("\nError: %s\n" % e) task_obj.log(log.getvalue()) task_obj.save() raise e return task_obj
def csv_import(task_id): #pragma: no cover from django.db import transaction # there is a possible race condition between this task starting # so we have a bit of loop here to fetch the task tries = 0 task = None while tries < 5 and not task: try: task = ImportTask.objects.get(pk=task_id) except Exception as e: # this object just doesn't exist yet, sleep a bit then try again tries+=1 if tries >= 5: raise e else: sleep(1) transaction.enter_transaction_management() transaction.managed() log = StringIO.StringIO() try: task.task_id = csv_import.request.id task.log("Started import at %s" % timezone.now()) task.log("--------------------------------") task.save() transaction.commit() model = class_from_string(task.model_class) records = model.import_csv(task, log) task.log(log.getvalue()) task.log("Import finished at %s" % timezone.now()) task.log("%d record(s) added." % len(records)) transaction.commit() except Exception as e: transaction.rollback() import traceback traceback.print_exc(e) task.log("\nError: %s\n" % e) task.log(log.getvalue()) transaction.commit() raise e finally: transaction.leave_transaction_management() return task
def csv_import(task): #pragma: no cover from django.db import transaction transaction.enter_transaction_management() transaction.managed() log = StringIO.StringIO() try: task.task_id = csv_import.request.id task.log("Started import at %s" % datetime.now()) task.log("--------------------------------") task.save() transaction.commit() model = class_from_string(task.model_class) records = model.import_csv(task.csv_file.file, task.created_by, log) task.log(log.getvalue()) task.log("Import finished at %s" % datetime.now()) task.log("%d record(s) added." % len(records)) transaction.commit() except Exception as e: transaction.rollback() import traceback traceback.print_exc(e) task.log("\nError: %s\n" % e) task.log(log.getvalue()) transaction.commit() raise e finally: transaction.leave_transaction_management() return task
def find(cls, kls): """ Finds the message handler from the fully qualified name that is passed in """ from smartmin import class_from_string return class_from_string(kls)
def __unicode__(self): return "%s Import" % class_from_string( self.model_class)._meta.verbose_name.title()
def __unicode__(self): return "%s Import" % class_from_string(self.model_class)._meta.verbose_name.title()
def csv_import(task_id): # pragma: no cover from django.db import transaction # there is a possible race condition between this task starting # so we have a bit of loop here to fetch the task tries = 0 task = None while tries < 5 and not task: try: task = ImportTask.objects.get(pk=task_id) except Exception as e: # this object just doesn't exist yet, sleep a bit then try again tries += 1 if tries >= 5: raise e else: sleep(1) log = StringIO() if StrictVersion(django.get_version()) < StrictVersion('1.6'): transaction.enter_transaction_management() transaction.managed() try: task.task_id = csv_import.request.id task.log("Started import at %s" % timezone.now()) task.log("--------------------------------") task.save() transaction.commit() model = class_from_string(task.model_class) records = model.import_csv(task, log) task.save() task.log(log.getvalue()) task.log("Import finished at %s" % timezone.now()) task.log("%d record(s) added." % len(records)) transaction.commit() except Exception as e: transaction.rollback() import traceback traceback.print_exc(e) task.log("\nError: %s\n" % e) task.log(log.getvalue()) transaction.commit() raise e finally: transaction.leave_transaction_management() else: task.task_id = csv_import.request.id task.log("Started import at %s" % timezone.now()) task.log("--------------------------------") task.save() try: with transaction.atomic(): model = class_from_string(task.model_class) records = model.import_csv(task, log) task.save() task.log(log.getvalue()) task.log("Import finished at %s" % timezone.now()) task.log("%d record(s) added." % len(records)) except Exception as e: import traceback traceback.print_exc(e) task.log("\nError: %s\n" % e) task.log(log.getvalue()) raise e return task
def get_type(self, obj): return class_from_string(obj.model_class)._meta.verbose_name_plural.title()