How to use the smartmin.csv_imports.models.ImportTask function in smartmin

To help you get started, we’ve selected a few smartmin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nyaruka / smartmin / test_runner / blog / tests.py View on Github external
self.assertTrue(200, response.status_code)
            self.assertEquals(response.request['PATH_INFO'], import_url)

            csv_file = open('test_runner/blog/test_files/posts.csv', 'rb')
            post_data = dict(csv_file=csv_file)

            response = self.client.post(import_url, post_data, follow=True)
            self.assertEqual(200, response.status_code)

            task = ImportTask.objects.get()
            self.assertEqual(json.loads(task.import_results), dict(records=4, errors=0, error_messages=[]))

            # new posts should all have a new tag
            self.assertEqual(Post.objects.filter(tags="new").count(), 4)

            ImportTask.objects.all().delete()

            csv_file = open('test_runner/blog/test_files/posts.xls', 'rb')
            post_data = dict(csv_file=csv_file)

            response = self.client.post(import_url, post_data, follow=True)
            self.assertEqual(200, response.status_code)

            task = ImportTask.objects.get()
            self.assertEqual(json.loads(task.import_results), dict(records=4, errors=0, error_messages=[]))

            ImportTask.objects.all().delete()

            with patch('test_runner.blog.models.Post.create_instance') as mock_create_instance:
                mock_create_instance.side_effect = SmartImportRowError('foo')

                csv_file = open('test_runner/blog/test_files/posts.csv', 'rb')
github nyaruka / smartmin / smartmin / csv_imports / tests.py View on Github external
def test_generate_file_path(self):
        self.assertEquals(generate_file_path(ImportTask(), 'allo.csv'), 'csv_imports/allo.csv')
        self.assertEquals(generate_file_path(ImportTask(), 'allo.xlsx'), 'csv_imports/allo.xlsx')
        self.assertEquals(generate_file_path(ImportTask(), 'allo.foo.bar'), 'csv_imports/allo.foo.bar')

        long_name = 'foo' * 100

        test_file_name = '%s.xls.csv' % long_name
        self.assertEquals(len(generate_file_path(ImportTask(), test_file_name)), 100)
        self.assertEquals(generate_file_path(ImportTask(), test_file_name), 'csv_imports/%s.csv' % long_name[:84])

        test_file_name = '%s.abc.xlsx' % long_name
        self.assertEquals(len(generate_file_path(ImportTask(), test_file_name)), 100)
        self.assertEquals(generate_file_path(ImportTask(), test_file_name), 'csv_imports/%s.xlsx' % long_name[:83])
github rapidpro / casepro / casepro / msgs / views.py View on Github external
# Override the ImportTask start method so we can use our self-defined task
def override_start(self, org):  # pragma: no cover
    from .tasks import faq_csv_import

    self.log("Queued import at %s" % now())
    self.save(update_fields=("import_log",))

    # trigger task
    result = faq_csv_import.delay(org.id, self.id)

    self.task_id = result.task_id
    self.save(update_fields=("task_id",))


ImportTask.start = override_start


class LabelCRUDL(SmartCRUDL):
    actions = ("create", "update", "read", "delete", "list", "watch", "unwatch")
    model = Label

    class Create(RuleFormMixin, OrgPermsMixin, SmartCreateView):
        form_class = LabelForm

        def get_form_kwargs(self):
            kwargs = super(LabelCRUDL.Create, self).get_form_kwargs()
            kwargs["org"] = self.request.org
            kwargs["is_create"] = True
            return kwargs

        def derive_initial(self):
github nyaruka / smartmin / smartmin / views.py View on Github external
elif action == 'list':
                if 'read' in self.actions:
                    options['link_url'] = 'id@%s' % self.url_name_for_action('read')
                elif 'update' in self.actions:
                    options['link_url'] = 'id@%s' % self.url_name_for_action('update')
                else:
                    options['link_fields'] = ()

                if 'create' in self.actions:
                    options['add_button'] = True

                view = type(str("%sListView" % self.model_name), (SmartListView,), options)

            elif action == 'csv_import':
                options['model'] = ImportTask
                view = type(str("%sCSVImportView" % self.model_name), (SmartCSVImportView,), options)

        if not view:
            # couldn't find a view?  blow up
            raise Exception("No view found for action: %s" % action)

        # set the url name for this view
        view.url_name = self.url_name_for_action(action)

        # no template set for it?  set one based on our action and app name
        if not getattr(view, 'template_name', None):
            view.template_name = self.template_for_action(action)

        view.crudl = self

        return view
github nyaruka / smartmin / smartmin / csv_imports / tasks.py View on Github external
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 = import_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: