Python django.core.management.call_command() Examples

The following are 30 code examples of django.core.management.call_command(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.core.management , or try the search function .
Example #1
Source File: tasks.py    From palanaeum with GNU Affero General Public License v3.0 9 votes vote down vote up
def backup_palanaeum():
    """
    Create a ZIP package containing result of dumpdata command and the `media` folder.
    """
    # TODO lock the site while the backup is running
    logger.info("Starting backup process.")
    with tempfile.TemporaryDirectory() as d:
        dump_path = os.path.join(d, 'dump.json')
        logger.info("Starting data dump...")
        management.call_command('dumpdata', natural_foreign=True, output=dump_path)
        logger.info("Data dumped.")
        logger.info("Copying MEDIA_ROOT to %s...", os.path.join(d, 'media'))
        shutil.copytree(settings.MEDIA_ROOT, os.path.join(d, 'media'))
        logger.info('Copy done.')
        backup_path = os.path.join(settings.BACKUP_DIR, datetime.date.today().strftime("%Y-%m-%d.zip"))
        with zipfile.ZipFile(backup_path, mode='w') as backup_zip:
            for root, dirs, files in os.walk(d):
                for file in files:
                    filepath = os.path.join(root, file)
                    logger.info("Compressing {}...".format(filepath))
                    backup_zip.write(filepath,
                                     arcname=os.path.relpath(filepath, d))
            logger.info("{} created.".format(backup_path)) 
Example #2
Source File: v3_migration_cli_tests.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_v3migration_002_generate_rm_configs(self):
        """Test the generation of resource model config file."""

        # copy in the resource model files, to mimic a user creating and
        # then exporting them into this package.
        shutil.copytree(os.path.join(self.pkg_fixture, "graphs", "resource_models"), os.path.join(self.pkg, "graphs", "resource_models"))

        # now run the management command
        management.call_command("v3", "generate-rm-configs", target=self.pkg)

        # test that the file has been created, and that it has the correct
        # number of entries to match the number of resource models.
        self.assertTrue(os.path.isfile(os.path.join(self.pkg, "v3data", "rm_configs.json")))
        num_rms = len(glob(os.path.join(self.pkg, "graphs", "resource_models", "*.json")))
        with open(os.path.join(self.pkg, "v3data", "rm_configs.json"), "rb") as conf:
            data = json.loads(conf.read())
            self.assertEqual(num_rms, len(list(data.keys()))) 
Example #3
Source File: migration_linter.py    From django-migration-linter with Apache License 2.0 6 votes vote down vote up
def get_sql(self, app_label, migration_name):
        logger.info(
            "Calling sqlmigrate command {} {}".format(app_label, migration_name)
        )
        dev_null = open(os.devnull, "w")
        try:
            sql_statement = call_command(
                "sqlmigrate",
                app_label,
                migration_name,
                database=self.database,
                stdout=dev_null,
            )
        except (ValueError, ProgrammingError):
            logger.warning(
                (
                    "Error while executing sqlmigrate on (%s, %s). "
                    "Continuing execution with empty SQL."
                ),
                app_label,
                migration_name,
            )
            sql_statement = ""
        return sql_statement.splitlines() 
Example #4
Source File: conftest.py    From django-click with MIT License 6 votes vote down vote up
def call_command():
    from django.core.management import call_command

    class CallCommand(object):
        def __init__(self):
            self.io = BytesIO()

        def __call__(self, *args, **kwargs):
            self.io = BytesIO()
            stdout = sys.stdout
            try:
                sys.stdout = self.io
                call_command(*args, **kwargs)
            finally:
                sys.stdout = stdout
            return self

        @property
        def stdout(self):
            return self.io.getvalue()

    return CallCommand() 
Example #5
Source File: fixturize.py    From django-template with MIT License 6 votes vote down vote up
def reset_db():
    """
    Reset database to a blank state by removing all the tables and recreating them.
    """
    with connection.cursor() as cursor:
        cursor.execute("select tablename from pg_tables where schemaname = 'public'")
        tables = [row[0] for row in cursor.fetchall()]

        # Can't use query parameters here as they'll add single quotes which are not
        # supported by postgres
        for table in tables:
            cursor.execute('drop table "' + table + '" cascade')

    # Call migrate so that post-migrate hooks such as generating a default Site object
    # are run
    management.call_command("migrate", "--noinput", stdout=StringIO()) 
Example #6
Source File: v3_migration_cli_tests.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_v3migration_001_create_v3_data_dir(self):
        """Test the creation of a v3data directory within an existing package."""

        if os.path.isdir(self.pkg):
            shutil.rmtree(self.pkg)

        # make some of the standard v4 package directory structure
        # these are used later, but may as well be created here
        os.mkdir(self.pkg)
        os.mkdir(os.path.join(self.pkg, "reference_data"))

        management.call_command("v3", "start-migration", target=self.pkg, overwrite=True)

        self.assertTrue(os.path.isdir(os.path.join(self.pkg, "v3data", "business_data")))
        self.assertTrue(os.path.isdir(os.path.join(self.pkg, "v3data", "graph_data")))
        self.assertTrue(os.path.isdir(os.path.join(self.pkg, "v3data", "reference_data")))
        self.assertTrue(os.path.isfile(os.path.join(self.pkg, "reference_data", "v3topconcept_lookup.json")))

        # now that its existence is tested, update the topconcept lookup
        # file with the real one, whose contents is expected later
        fixture_lookup = os.path.join(self.pkg_fixture, "reference_data", "v3topconcept_lookup.json")
        shutil.copyfile(fixture_lookup, os.path.join(self.pkg, "reference_data", "v3topconcept_lookup.json")) 
Example #7
Source File: test_commands.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def test_inherit_perms(self):
        out, err = StringIO(), StringIO()

        with self.settings(
            FLOW_PROCESSES_DIRS=[os.path.join(PROCESSES_DIR, "first_version")]
        ):
            call_command("register", stdout=out, stderr=err)

        process = Process.objects.latest()
        assign_perm("view_process", self.user, process)

        out, err = StringIO(), StringIO()

        with self.settings(
            FLOW_PROCESSES_DIRS=[os.path.join(PROCESSES_DIR, "second_version")]
        ):
            call_command("register", stdout=out, stderr=err)

        process = Process.objects.latest()

        self.assertEqual(UserObjectPermission.objects.filter(user=self.user).count(), 2)
        self.assertTrue(self.user.has_perm("flow.view_process", process)) 
Example #8
Source File: test_chores.py    From desec-stack with MIT License 6 votes vote down vote up
def test_inactive_user_cleanup(self):
        def create_users(kind):
            logintime = timezone.now() + timezone.timedelta(seconds=5)
            kwargs_list = [
                dict(email=f'user1+{kind}@example.com', is_active=False, last_login=None),
                dict(email=f'user2+{kind}@example.com', is_active=True, last_login=None),
                dict(email=f'user3+{kind}@example.com', is_active=False, last_login=logintime),
                dict(email=f'user4+{kind}@example.com', is_active=True, last_login=logintime),
            ]
            return (User.objects.create(**kwargs) for kwargs in kwargs_list)

        # Old users
        faketime = timezone.now() - settings.VALIDITY_PERIOD_VERIFICATION_SIGNATURE - timezone.timedelta(seconds=1)
        with mock.patch('django.db.models.fields.timezone.now', return_value=faketime):
            expired_user, _, _, _ = create_users('old')

        # New users
        create_users('new')

        all_users = set(User.objects.all())

        management.call_command('chores')
        # Check that only the expired user was deleted
        self.assertEqual(all_users - set(User.objects.all()), {expired_user}) 
Example #9
Source File: models.py    From anytask with MIT License 6 votes vote down vote up
def test_management_command(self):
        """
        The ``cleanupregistration`` management command properly
        deletes expired accounts.
        
        """
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
                                                                    **self.user_info)
        expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
                                                                        username='bob',
                                                                        password='secret',
                                                                        email='bob@example.com')
        expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
        expired_user.save()

        management.call_command('cleanupregistration')
        self.assertEqual(RegistrationProfile.objects.count(), 1)
        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob') 
Example #10
Source File: models.py    From anytask with MIT License 6 votes vote down vote up
def test_management_command(self):
        """
        The ``cleanupregistration`` management command properly
        deletes expired accounts.
        
        """
        new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
                                                                    **self.user_info)
        expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
                                                                        username='bob',
                                                                        password='secret',
                                                                        email='bob@example.com')
        expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
        expired_user.save()

        management.call_command('cleanupregistration')
        self.assertEqual(RegistrationProfile.objects.count(), 1)
        self.assertRaises(User.DoesNotExist, User.objects.get, username='bob') 
Example #11
Source File: start.py    From DeerU with GNU General Public License v3.0 6 votes vote down vote up
def handle(self, *args, **options):
        self.type = options['type']
        self.name = options['name']
        management.call_command('startapp', self.name)
        dir_name = ['management', Path('management/commands')]
        self.mk_dir(dir_name)

        if self.type == 'theme':
            dir_name = ['static', Path('static/' + self.name), 'templates', Path('templates/' + self.name)]
            self.mk_dir(dir_name)

        context = Context({
            'app_name': self.name,
            'app_camel_name': self.name[0].upper() + self.name[1:],
            'app_upper_name': self.name.upper(),
            'deeru_type': self.type
        }, autoescape=False)

        for template_name, new_file in self.get_app_templates():
            template = Engine().from_string(self.get_template_str(template_name))
            content = template.render(context)
            new_file.write_text(content) 
Example #12
Source File: conftest.py    From dissemin with GNU Affero General Public License v3.0 6 votes vote down vote up
def load_test_data(request, db, django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', 'test_dump.json')
        self = request.cls
        self.i = Institution.objects.get(name='ENS')
        self.d = Department.objects.get(name='Chemistry dept')
        self.di = Department.objects.get(name='Comp sci dept')

        self.r1 = get_researcher_by_name('Isabelle', 'Aujard')
        self.r2 = get_researcher_by_name('Ludovic', 'Jullien')
        self.r3 = get_researcher_by_name('Antoine', 'Amarilli')
        self.r4 = get_researcher_by_name('Antonin', 'Delpeuch')
        self.r5 = get_researcher_by_name('Terence', 'Tao')
        self.hal = OaiSource.objects.get(identifier='hal')
        self.arxiv = OaiSource.objects.get(identifier='arxiv')
        self.lncs = Journal.objects.get(issn='0302-9743')
        self.acm = Journal.objects.get(issn='1529-3785').publisher 
Example #13
Source File: test_commands.py    From civet with Apache License 2.0 6 votes vote down vote up
def test_dump_latest(self):
        out = StringIO()
        management.call_command("dump_latest", stdout=out)
        self.assertIn("Dumping 0 events", out.getvalue())

        ev = utils.create_event()
        management.call_command("dump_latest", stdout=out)
        self.assertIn("Dumping 1 events", out.getvalue())

        with open("out.json", "r") as f:
            data = f.read()
        out = json.loads(data)
        count = 0
        for entry in out:
            if entry["model"] == "ci.event":
                self.assertEqual(ev.pk, entry["pk"])
                count = 1
        self.assertEqual(count, 1) 
Example #14
Source File: test_commands.py    From civet with Apache License 2.0 6 votes vote down vote up
def test_user_access(self, mock_get):
        out = StringIO()
        mock_get.return_value = utils.Response(status_code=404)
        with self.assertRaises(CommandError):
            management.call_command("user_access", stdout=out)
        with self.assertRaises(models.GitUser.DoesNotExist):
            management.call_command("user_access", "--master", "nobody", stdout=out)
        with self.assertRaises(CommandError):
            management.call_command("user_access", "--master", self.owner.name, stdout=out)

        out = StringIO()
        management.call_command("user_access", "--master", self.build_user.name, stdout=out)

        repo1 = {'name': 'repo1', 'owner': {'login': 'owner'} }
        repo2 = {'name': 'repo2', 'owner': {'login': 'owner'} }
        mock_get.side_effect = [utils.Response([repo1]), utils.Response([repo2])]

        out = StringIO()
        management.call_command("user_access", "--master", self.build_user.name, "--user", "owner", stdout=out) 
Example #15
Source File: initialize_development.py    From lego with MIT License 5 votes vote down vote up
def run(self, *args, **options):
        self.stdout.write("Migrating...")
        management.call_command("migrate", verbosity=self.verbosity)
        self.stdout.write("Migrating search...")
        management.call_command("migrate_search", verbosity=self.verbosity)
        self.stdout.write("Rebuilding indices...")
        management.call_command("rebuild_index", verbosity=self.verbosity)
        self.stdout.write("Loading fixtures...")
        management.call_command(
            "load_fixtures", verbosity=self.verbosity, development=True
        )
        self.stdout.write("All done!") 
Example #16
Source File: test_mgt_commands.py    From mangaki with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_anilist_tags_to_json(self):
        responses.add(
            responses.GET,
            AniList.BASE_URL,
            body=self.anilist_fixture,
            status=200,
            content_type='application/json'
        )
        management.call_command('anilist_tags_to_json', self.anime.id,
                                stdout=self.stdout)
        self.assertIn('---', self.stdout.getvalue()) 
Example #17
Source File: test_mgt_commands.py    From mangaki with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_anidb_tags_to_json(self):
        responses.add(
            responses.GET,
            AniDB.BASE_URL,
            body=self.anidb_fixture,
            status=200,
            content_type='application/xml'
        )
        management.call_command('anidb_tags_to_json', self.anime.id,
                                stdout=self.stdout)
        self.assertIn('---', self.stdout.getvalue()) 
Example #18
Source File: test_mgt_commands.py    From mangaki with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_anidb(self):
        responses.add(
            responses.GET,
            AniDB.BASE_URL,
            body=self.anidb_fixture,
            status=200,
            content_type='application/xml'
        )
        management.call_command('add_anidb', self.anime.anidb_aid,
                                stdout=self.stdout)
        self.assertEquals(self.stdout.getvalue(),
                          "Successfully added Sangatsu no Lion\n") 
Example #19
Source File: test_manage.py    From django-ftpserver with MIT License 5 votes vote down vote up
def test_createftpusergroup(self):
        random_name = ''.join(random.choice('abcde') for _ in range(10))
        management.call_command('createftpusergroup', random_name) 
Example #20
Source File: test_manage.py    From django-ftpserver with MIT License 5 votes vote down vote up
def test_run_ftpserver(self):
        with pytest.raises(CommandError):
            # Test that management commands work - but without actually running one
            management.call_command('ftpserver', '--passive-ports=fake') 
Example #21
Source File: environment.py    From django-river with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def before_scenario(context, scenario):
    management.call_command('flush', interactive=False) 
Example #22
Source File: test_commands.py    From civet with Apache License 2.0 5 votes vote down vote up
def test_load_recipes(self):
        with utils.RecipeDir():
            management.call_command("load_recipes", "--install-webhooks") 
Example #23
Source File: test_commands.py    From civet with Apache License 2.0 5 votes vote down vote up
def test_disable_repo(self):
        out = StringIO()
        with self.assertRaises(CommandError):
            management.call_command("disable_repo", "--dry-run", stdout=out)
        with self.assertRaises(CommandError):
            management.call_command("disable_repo", "--dry-run", "--owner", "foo", stdout=out)

        repo = utils.create_repo()

        with self.assertRaises(CommandError):
            management.call_command("disable_repo", "--dry-run", "--owner", repo.user.name, "--repo", "<repo>", stdout=out)

        repo.active = True
        repo.save()
        branch = utils.create_branch(repo=repo)
        branch.status = models.JobStatus.SUCCESS
        branch.save()
        pr = utils.create_pr(repo=repo)
        pr.closed = False
        pr.save()

        management.call_command("disable_repo", "--dry-run", "--owner", repo.user.name, "--repo", repo.name, stdout=out)
        repo.refresh_from_db()
        self.assertIs(repo.active, True)
        branch.refresh_from_db()
        self.assertEqual(branch.status, models.JobStatus.SUCCESS)
        pr.refresh_from_db()
        self.assertIs(pr.closed, False)

        management.call_command("disable_repo", "--owner", repo.user.name, "--repo", repo.name, stdout=out)
        repo.refresh_from_db()
        self.assertIs(repo.active, False)
        branch.refresh_from_db()
        self.assertEqual(branch.status, models.JobStatus.NOT_STARTED)
        pr.refresh_from_db()
        self.assertIs(pr.closed, True) 
Example #24
Source File: conftest.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def rebuild_index(request):
    rebuild_index = (
        lambda: call_command('rebuild_index', interactive=False)
    )
    rebuild_index()
    request.addfinalizer(rebuild_index) 
Example #25
Source File: tasks.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def import_business_data(self, data_source="", overwrite="", bulk_load=False, create_concepts=False, create_collections=False):
    management.call_command("packages", operation="import_business_data", source=data_source, overwrite=True) 
Example #26
Source File: tests.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def test_job_day(self):
    # test low level pull ( no worker involved )
    self.assertIsNotNone(self.recipe_today.get_task())
    self.assertIsNone(self.recipe_not_today.get_task())

    assertRecipeNotDone(self, self.recipe_today)
    assertRecipeDone(self, self.recipe_not_today)

    # first loop through manager ( use short timeout )
    management.call_command('job_worker', worker='TEST_WORKER', jobs=5, timeout=60, verbose=True, test=True)

    recipes = Recipe.objects.filter(worker_uid='TEST_WORKER')
    self.assertEqual(len(recipes), 1)
    self.assertEqual(recipes[0].name, 'RECIPE_TODAY') 
Example #27
Source File: tests.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def test_manager_timeout(self):

    # first loop through manager ( use short timeout )
    management.call_command('job_worker', worker='TEST_WORKER', jobs=5, timeout=5, verbose=True, test=True)

    jobs = Recipe.objects.filter(worker_uid='TEST_WORKER')
    self.assertEqual(len(jobs), 1)
    status = jobs[0].get_status()
    self.assertEqual(len(status['tasks']), 2)
    self.assertEqual(status['tasks'][0]['script'], 'hello')
    self.assertEqual(status['tasks'][0]['instance'], 1)
    self.assertEqual(status['tasks'][0]['hour'], 0)
    self.assertEqual(status['tasks'][0]['event'], 'JOB_TIMEOUT')
    self.assertEqual(status['tasks'][1]['script'], 'hello')
    self.assertEqual(status['tasks'][1]['instance'], 2)
    self.assertEqual(status['tasks'][1]['hour'], 0)
    self.assertEqual(status['tasks'][1]['event'], 'JOB_PENDING')

    # advance time, since current jobs need to expire, artificially ping to keep out of queue
    sleep(WORKER_LOOKBACK_EXPIRE/1000)

    # second loop through manager ( use normal timeout )
    management.call_command('job_worker', worker='TEST_WORKER', jobs=5, timeout=60*60*1, verbose=True, test=True)

    jobs = Recipe.objects.filter(worker_uid='TEST_WORKER')
    self.assertEqual(len(jobs), 1)
    status = jobs[0].get_status()
    self.assertEqual(len(status['tasks']), 2)
    self.assertEqual(status['tasks'][0]['script'], 'hello')
    self.assertEqual(status['tasks'][0]['instance'], 1)
    self.assertEqual(status['tasks'][0]['hour'], 0)
    self.assertEqual(status['tasks'][0]['event'], 'JOB_TIMEOUT')
    self.assertEqual(status['tasks'][1]['script'], 'hello')
    self.assertEqual(status['tasks'][1]['instance'], 2)
    self.assertEqual(status['tasks'][1]['hour'], 0)
    self.assertEqual(status['tasks'][1]['event'], 'JOB_END')

    # check if recipe is removed from worker lookup ( job_done=True )
    assertRecipeDone(self, self.recipe) 
Example #28
Source File: permission_tests.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUpClass(cls):
        test_pkg_path = os.path.join(test_settings.TEST_ROOT, "fixtures", "testing_prj", "testing_prj", "pkg")
        management.call_command("packages", operation="load_package", source=test_pkg_path, yes=True)
        delete_resource_relations_index()
        prepare_resource_relations_index(create=True)
        cls.add_users() 
Example #29
Source File: tests.py    From django-composite-foreignkey with GNU General Public License v3.0 5 votes vote down vote up
def test_app_not_exists(self):
        self.assertRaises(CommandError, call_command, "graph_datas", "doesnotexistsapp") 
Example #30
Source File: test_mgt_commands.py    From mangaki with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_fit_algo(self):
        management.call_command('fit_algo', 'zero', stdout=self.stdout)
        self.assertEquals(self.stdout.getvalue(),
                          "Successfully fit zero (0.0 MB)\n")