from django import forms
from django.utils.translation import ugettext_lazy as _
import mutagen
+from django.utils.encoding import force_bytes
from archive.models import Audiobook
from archive.settings import FILES_PATH, NEW_PATH
path,
ExistingFile(abs_path))
- f = open(m.source_file.path)
- m.source_sha1 = sha1_file(f)
- f.close()
+# f = open(force_bytes(m.source_file.path))
+# m.source_sha1 = sha1_file(f)
+# f.close()
if commit:
m.save()
import os.path
from django.db import models
+from time import sleep
from jsonfield.fields import JSONField
+from django.utils.encoding import force_bytes
from django.utils.translation import ugettext_lazy as _
from archive.constants import status
from archive.settings import FILES_SAVE_PATH, ADVERT, LICENSE, ORGANIZATION, PROJECT
-from archive.utils import OverwriteStorage
+from archive.utils import OverwriteStorage, sha1_file
# Create your models here.
def published(self):
return self.mp3_published and self.ogg_published
+ def get_source_sha1(self):
+ source_sha1 = self.source_sha1
+ if self.pk:
+ source_sha1 = type(self).objects.get(pk=self.pk).source_sha1
+ while source_sha1 == 'wait':
+ sleep(10)
+ if not source_sha1:
+ self.source_sha1 = 'wait'
+ if self.pk:
+ type(self).objects.filter(pk=self.pk).update(source_sha1='wait')
+ try:
+ f = open(force_bytes(self.source_file.path))
+ source_sha1 = sha1_file(f)
+ self.source_sha1 = source_sha1
+ if self.pk:
+ type(self).objects.filter(pk=self.pk).update(source_sha1=source_sha1)
+ except:
+ self.source_sha1 = ''
+ if self.pk:
+ type(self).objects.filter(pk=self.pk).update(source_sha1='')
+ return None
+ return source_sha1
+
+
def new_publish_tags(self):
title = self.title
if self.translator:
u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
ADVERT)
- return {
+ tags = {
'album': PROJECT,
'albumartist': ORGANIZATION,
'artist': self.artist,
'license': LICENSE,
'organization': ORGANIZATION,
'title': title,
- 'flac_sha1': self.source_sha1,
+ #'flac_sha1': self.get_source_sha1(),
'project': self.project.name,
'funded_by': self.project.sponsors,
}
+ if self.source_sha1 and self.source_sha1 != 'wait':
+ tags['flac_sha1'] = self.source_sha1
+ return tags
+
@classmethod
def set_tags(cls, audiobook, file_name):
+ tags = getattr(audiobook, "%s_tags" % cls.ext)['tags']
+ if not tags.get('flac_sha1'):
+ tags['flac_sha1'] = audiobook.get_source_sha1()
audio = File(file_name)
- for k, v in getattr(audiobook, "%s_tags" % cls.ext)['tags'].items():
+ for k, v in tags.items():
audio[k] = v
audio.save()
@classmethod
def set_tags(cls, audiobook, file_name):
+ mp3_tags = audiobook.mp3_tags['tags']
+ if not mp3_tags.get('flac_sha1'):
+ mp3_tags['flac_sha1'] = audiobook.get_source_sha1()
audio = id3.ID3(file_name)
- for k, v in audiobook.mp3_tags['tags'].items():
+ for k, v in mp3_tags.items():
factory_tuple = cls.TAG_MAP[k]
factory, tagtype = factory_tuple[:2]
audio.add(factory(tagtype, v, *factory_tuple[2:]))
if not os.path.isfile(old_path):
raise Http404
- try:
- os.link(old_path, new_path)
- except OSError:
- # destination file exists, don't overwrite it
- # TODO: this should probably be more informative
- return redirect(file_new, filename)
- else:
- os.unlink(old_path)
- audiobook.delete()
+ success = False
+ try_new_path = new_path
+ try_number = 0
+ while not success:
+ try:
+ os.link(old_path, try_new_path)
+ except OSError:
+ # destination file exists, don't overwrite it
+ try_number += 1
+ parts = new_path.rsplit('.', 1)
+ parts[0] += '_%d' % try_number
+ try_new_path = ".".join(parts)
+ else:
+ os.unlink(old_path)
+ audiobook.delete()
+ success = True
return redirect(list_unmanaged)
path = audiobook.source_file.path[len(settings.FILES_PATH):].lstrip('/')
# for tags update
- tags = mutagen.File(audiobook.source_file.path)
+ tags = mutagen.File(audiobook.source_file.path.encode('utf-8'))
if not tags:
tags = {}
form = AudiobookForm(instance=audiobook)
--- /dev/null
+"""
+WSGI config for edumed project.
+
+This module contains the WSGI application used by Django's development server
+and any production WSGI deployments. It should expose a module-level variable
+named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
+this application via the ``WSGI_APPLICATION`` setting.
+
+Usually you will have the standard Django WSGI application here, but it also
+might make sense to replace the whole Django WSGI application with a custom one
+that later delegates to the Django one. For example, you could introduce WSGI
+middleware here, or combine a Django application with an application of another
+framework.
+
+"""
+import os
+
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "audiobooks.settings")
+
+# This application object is used by any WSGI server configured to use this
+# file. This includes Django's development server, if the WSGI_APPLICATION
+# setting points here.
+from django.core.wsgi import get_wsgi_application
+application = get_wsgi_application()
+
+# Apply WSGI middleware here.
+# from helloworld.wsgi import HelloWorldApplication
+# application = HelloWorldApplication(application)