From de277d9df5292ab2f5b36f4ed1500dd67d486115 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Sun, 9 Feb 2014 20:10:45 +0000 Subject: [PATCH] Rewrote orphan mp3 management command --- spa/management/commands/deleteorphanmp3.py | 29 +++++++++++++++++----- spa/views.py | 2 +- templates/base.html | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/spa/management/commands/deleteorphanmp3.py b/spa/management/commands/deleteorphanmp3.py index 32c1084..8130e71 100755 --- a/spa/management/commands/deleteorphanmp3.py +++ b/spa/management/commands/deleteorphanmp3.py @@ -1,14 +1,31 @@ +from dircache import listdir +import logging import os import shutil +from django.core.exceptions import ObjectDoesNotExist from django.core.management.base import NoArgsCommand +from os.path import isfile, join +from dss import settings from spa.models import Mix +logger = logging.getLogger('spa') + class Command(NoArgsCommand): def handle(self, *args, **options): - candidates = Mix.objects.all() - for mix in candidates: - file = mix.get_absolute_path(prefix="expired/") - if os.path.exists(file): - new = mix.get_absolute_path() - shutil.move(file, new) + try: + mixes_path = join(settings.MEDIA_ROOT, "mixes") + expired_path = join(settings.MEDIA_ROOT, "mixes/expired") + files = [f for f in listdir(mixes_path) if isfile(join(mixes_path, f))] + + for f in files: + uid = os.path.splitext(f)[0] + try: + Mix.objects.get(uid=uid) + except Mix.DoesNotExist: + new_file = os.path.join(expired_path, os.path.basename(f)) + os.rename(f, new_file) + print "Moved %s to %s" % (f, new_file) + except Exception, ex: + logger.log(logger.WARNING, "Error: %s" % ex.message) + diff --git a/spa/views.py b/spa/views.py index d172928..ccad870 100755 --- a/spa/views.py +++ b/spa/views.py @@ -31,7 +31,7 @@ def app(request): context['ua_html'] = \ """
Hello Firefox user. If you are having any problems with the site, please do a diff --git a/templates/base.html b/templates/base.html index e1f05cc..ebc5084 100755 --- a/templates/base.html +++ b/templates/base.html @@ -41,7 +41,7 @@
-
{{ ua_html|safe }}
+
{{ ua_html|safe }}