6 from django.core.management.base import BaseCommand
7 from django.utils.encoding import force_text
9 from emissions.utils import get_duration
10 from ...models import Track
13 class Command(BaseCommand):
14 def add_arguments(self, parser):
16 '--recent', action='store_true', default=False,
17 help='Only do recent files')
19 '--force', action='store_true', default=False,
20 help='Recompute existing durations')
22 def handle(self, verbosity, **kwargs):
23 qs = Track.objects.all()
24 if not kwargs.get('force'):
25 qs = qs.filter(duration__isnull=True)
26 if kwargs.get('recent'):
27 qs = qs.filter(creation_timestamp__gt=datetime.date.today() - datetime.timedelta(days=10))
30 for i, track in enumerate(qs):
32 print('[%s/%s] %s' % (i, total, track))
34 file_path = track.file_path()
35 except AttributeError:
37 if file_path is None or not os.path.exists(file_path):
39 duration = get_duration(file_path)
41 track.duration = datetime.timedelta(seconds=float(duration))