コード例 #1
0
    def test_args_from_database(self):
        """Test management command arguments injected from config model."""
        # Nothing in the database, should default to disabled
        with self.assertRaisesRegex(
                CommandError, 'SimulateCourseConfigPublish is disabled.*'):
            call_command('simulate_publish', '--args-from-database')

        # Add a config
        config = SimulateCoursePublishConfig.current()
        config.arguments = '--delay 20 --dry-run'
        config.enabled = True
        config.save()

        with LogCapture(LOGGER_NAME) as log:
            call_command('simulate_publish')

            log.check_present(
                (LOGGER_NAME, 'INFO',
                 "simulate_publish starting, dry-run={}, delay={} seconds".
                 format('False', '0')), )

        with LogCapture(LOGGER_NAME) as log:
            call_command('simulate_publish', '--args-from-database')

            log.check_present(
                (LOGGER_NAME, 'INFO',
                 "simulate_publish starting, dry-run={}, delay={} seconds".
                 format('True', '20')), )
コード例 #2
0
    def get_args_from_database(self):
        """ Returns an options dictionary from the current SimulateCoursePublishConfig model. """

        config = SimulateCoursePublishConfig.current()
        if not config.enabled:
            raise CommandError(
                'SimulateCourseConfigPublish is disabled, but --args-from-database was requested.'
            )

        # We don't need fancy shell-style whitespace/quote handling - none of our arguments are complicated
        argv = config.arguments.split()

        parser = self.create_parser('manage.py', 'simulate_publish')
        return parser.parse_args(
            argv
        ).__dict__  # we want a dictionary, not a non-iterable Namespace object