1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # Clean/Clear the cached tiles
|
---|
5 | #
|
---|
6 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
7 | #
|
---|
8 | from django.core.management.base import BaseCommand, CommandError
|
---|
9 | from optparse import OptionParser, make_option
|
---|
10 | from gheat.models import TileCache
|
---|
11 | import datetime
|
---|
12 | import sys
|
---|
13 |
|
---|
14 | class Command(BaseCommand):
|
---|
15 | option_list = BaseCommand.option_list + (
|
---|
16 | make_option('-d', '--datum', dest='datum', default=None, help="""Provide date
|
---|
17 | in following format: %Y-%m-%d[-%H%M], to delete all time generated before
|
---|
18 | this time the filename"""),)
|
---|
19 |
|
---|
20 | def handle(self, *args, **options):
|
---|
21 | if options['datum'] == None:
|
---|
22 | datum = datetime.datetime.now()
|
---|
23 | else:
|
---|
24 | # Try parsing different formats
|
---|
25 | for fmt in ['%Y-%m-%d-%H%M', '%Y-%m-%d']:
|
---|
26 | try:
|
---|
27 | datum = datetime.datetime.strptime(options['datum'],fmt)
|
---|
28 | break
|
---|
29 | except ValueError:
|
---|
30 | datum = None
|
---|
31 | if not datum:
|
---|
32 | raise CommandError("Invalid date '%s'\n" % options['datum'])
|
---|
33 |
|
---|
34 | obj = TileCache.objects.filter(creation_time__range=(datetime.datetime.fromtimestamp(0), datum))
|
---|
35 | print "#INFO: Deleting '%s' objects" % obj.count()
|
---|
36 | obj.delete()
|
---|
37 |
|
---|