def test_args_from_database(self, mock_send):
        # Nothing in the database, should default to disabled
        with self.assertRaisesRegex(CommandError,
                                    'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01',
                         '--args-from-database')

        # Add a config
        config = NotifyCredentialsConfig.current()
        config.arguments = '--start-date 2017-03-01'
        config.enabled = True
        config.save()

        # Not told to use config, should ignore it
        call_command(Command(), '--start-date', '2017-01-01')
        self.assertEqual(len(mock_send.call_args[0][0]), 3)

        # Told to use it, and enabled. Should use config in preference of command line
        call_command(Command(), '--start-date', '2017-01-01',
                     '--args-from-database')
        self.assertEqual(len(mock_send.call_args[0][0]), 1)

        config.enabled = False
        config.save()

        # Explicitly disabled
        with self.assertRaisesRegex(CommandError,
                                    'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01',
                         '--args-from-database')
Exemple #2
0
    def test_args_from_database(self, mock_task):
        # Nothing in the database, should default to disabled
        with self.assertRaisesRegex(CommandError,
                                    'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01',
                         '--args-from-database')

        # Add a config
        config = NotifyCredentialsConfig.current()
        config.arguments = '--start-date "2017-03-01 00:00:00"'
        config.enabled = True
        config.save()

        # Not told to use config, should ignore it
        self.expected_options['start_date'] = '2017-01-01T00:00:00Z'
        call_command(Command(), '--start-date', '2017-01-01')
        assert mock_task.called
        assert mock_task.call_args[0][0] == self.expected_options

        # Told to use it, and enabled. Should use config in preference of command line
        self.expected_options['start_date'] = '2017-03-01T00:00:00Z'
        self.expected_options['skip_checks'] = False
        call_command(Command(), '--start-date', '2017-01-01',
                     '--args-from-database')
        assert mock_task.called
        assert mock_task.call_args[0][0] == self.expected_options

        config.enabled = False
        config.save()

        # Explicitly disabled
        with self.assertRaisesRegex(CommandError,
                                    'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01',
                         '--args-from-database')
    def test_args_from_database(self, mock_send):
        # Nothing in the database, should default to disabled
        with self.assertRaisesRegex(CommandError, 'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01', '--args-from-database')

        # Add a config
        config = NotifyCredentialsConfig.current()
        config.arguments = '--start-date 2017-03-01'
        config.enabled = True
        config.save()

        # Not told to use config, should ignore it
        call_command(Command(), '--start-date', '2017-01-01')
        self.assertEqual(len(mock_send.call_args[0][0]), 3)

        # Told to use it, and enabled. Should use config in preference of command line
        call_command(Command(), '--start-date', '2017-01-01', '--args-from-database')
        self.assertEqual(len(mock_send.call_args[0][0]), 1)

        config.enabled = False
        config.save()

        # Explicitly disabled
        with self.assertRaisesRegex(CommandError, 'NotifyCredentialsConfig is disabled.*'):
            call_command(Command(), '--start-date', '2017-01-01', '--args-from-database')
    def get_args_from_database(self):
        """ Returns an options dictionary from the current NotifyCredentialsConfig model. """
        config = NotifyCredentialsConfig.current()
        if not config.enabled:
            raise CommandError('NotifyCredentialsConfig 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', 'notify_credentials')
        return parser.parse_args(argv).__dict__   # we want a dictionary, not a non-iterable Namespace object
    def get_args_from_database(self):
        """ Returns an options dictionary from the current NotifyCredentialsConfig model. """
        config = NotifyCredentialsConfig.current()
        if not config.enabled:
            raise CommandError('NotifyCredentialsConfig is disabled, but --args-from-database was requested.')

        # This split will allow for quotes to wrap datetimes, like "2020-10-20 04:00:00" and other
        # arguments as if it were the command line
        argv = shlex.split(config.arguments)
        parser = self.create_parser('manage.py', 'notify_credentials')
        return parser.parse_args(argv).__dict__   # we want a dictionary, not a non-iterable Namespace object
    def get_args_from_database(self):
        """ Returns an options dictionary from the current NotifyCredentialsConfig model. """
        config = NotifyCredentialsConfig.current()
        if not config.enabled:
            raise CommandError(
                'NotifyCredentialsConfig 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', 'notify_credentials')
        return parser.parse_args(
            argv
        ).__dict__  # we want a dictionary, not a non-iterable Namespace object