def _parse_date_input(date_input, default_offset=0):
    """Parses a date input."""
    if date_input:
        try:
            return parse_date_input(date_input)
        except ValueError as err:
            raise CommandError(force_text(err))
    else:
        return get_midnight() - timedelta(days=default_offset)
    def handle(self, **options):
        verbosity = options['verbosity']

        limit = options['limit']
        if limit:
            try:
                limit = parse_date_input(limit)
            except ValueError as err:
                raise CommandError(force_text(err))
        else:
            limit = get_midnight() - timedelta(days=DEFAULT_OFFSET)

        CSPReport.objects.filter(created__lt=limit).delete()
        if verbosity >= 2:
            self.stdout.write(
                "Deleted all reports created before {}.".format(limit))
Exemplo n.º 3
0
 def test_invalid_input(self):
     with self.assertRaisesMessage(ValueError, 'is not a valid date.'):
         parse_date_input('INVALID')
Exemplo n.º 4
0
 def test_invalid_date(self):
     with self.assertRaisesMessage(ValueError, 'is not a valid date.'):
         parse_date_input('2016-13-25')
Exemplo n.º 5
0
 def test_naive(self):
     with self.settings(USE_TZ=False):
         self.assertEqual(parse_date_input('2016-05-25'),
                          datetime(2016, 5, 25))
Exemplo n.º 6
0
 def test_aware(self):
     with self.settings(USE_TZ=True, TIME_ZONE='Europe/Prague'):
         self.assertEqual(parse_date_input('2016-05-25'),
                          timezone.make_aware(datetime(2016, 5, 25)))