def priority_user_import(sender, **kwargs):
    user = kwargs['instance']
    if user.priority == user.PRIORITY_IMMEDIATE:
        post_save.disconnect(priority_user_import, sender=User)

        try:
            imp = Import(priority=user.priority, csv_type='user')
            imp.save()

            user.priority = user.PRIORITY_DEFAULT
            user.queue_id = imp.pk
            user.save()

            try:
                imp.csv_path = UserBuilder([user]).build()
            except Exception:
                imp.csv_errors = traceback.format_exc()

            if imp.csv_path:
                imp.import_csv()
            else:
                user.queue_id = None
                user.priority = user.PRIORITY_HIGH
                user.save()
                imp.delete()

        except Exception as err:
            log.error('Immediate user provision failed: {}'.format(err))
            user.priority = user.PRIORITY_HIGH
            user.save()

        post_save.connect(priority_user_import, sender=User)
def priority_user_import(sender, **kwargs):
    user = kwargs['instance']
    if user.priority == PRIORITY_IMMEDIATE:
        post_save.disconnect(priority_user_import, sender=User)

        try:
            imp = Import(priority=user.priority, csv_type='user')
            imp.save()

            user.priority = PRIORITY_DEFAULT
            user.queue_id = imp.pk
            user.save()

            try:
                imp.csv_path = UserBuilder([user]).build()
            except:
                imp.csv_errors = traceback.format_exc()

            if imp.csv_path:
                imp.import_csv()
            else:
                user.queue_id = None
                user.priority = PRIORITY_HIGH
                user.save()
                imp.delete()

        except Exception, err:
            log.error('Immediate user provision failed: %s' % (err))
            user.priority = PRIORITY_HIGH
            user.save()

        post_save.connect(priority_user_import, sender=User)
    def handle(self, *args, **options):
        imp = Import(csv_type="account")
        try:
            imp.csv_path = AccountBuilder().build()
        except Exception:
            imp.csv_errors = traceback.format_exc()

        imp.save()

        if imp.csv_path:
            imp.import_csv()

        self.update_job()