local changes from server
authorJan Szejko <jan.szejko@gmail.com>
Thu, 21 Jul 2016 12:32:18 +0000 (14:32 +0200)
committerJan Szejko <jan.szejko@gmail.com>
Thu, 21 Jul 2016 12:32:18 +0000 (14:32 +0200)
apps/archive/forms.py
apps/archive/models.py
apps/archive/tasks.py
apps/archive/views.py
audiobooks/wsgi.py [new file with mode: 0644]

index cdc416e..81568c6 100644 (file)
@@ -5,6 +5,7 @@ import os.path
 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
@@ -33,9 +34,9 @@ class AudiobookForm(forms.ModelForm):
                 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()
index 4d3f838..a78eb52 100644 (file)
@@ -2,11 +2,13 @@
 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.
 
@@ -72,6 +74,30 @@ class Audiobook(models.Model):
     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:
@@ -85,7 +111,7 @@ class Audiobook(models.Model):
                     u" finansowanego przez %s" % self.project.sponsors if self.project.sponsors else "",
                     ADVERT)
 
-        return {
+        tags = {
             'album': PROJECT,
             'albumartist': ORGANIZATION,
             'artist': self.artist,
@@ -99,7 +125,11 @@ class Audiobook(models.Model):
             '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
+
index 1b2be74..d9c3f5e 100644 (file)
@@ -46,8 +46,11 @@ class AudioFormatTask(Task):
 
     @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()
 
@@ -173,8 +176,11 @@ class Mp3Task(AudioFormatTask):
 
     @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:]))
index d40f633..b5850a0 100644 (file)
@@ -111,15 +111,22 @@ def remove_to_archive(request, aid):
     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)
 
@@ -246,7 +253,7 @@ def file_managed(request, id):
     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)
diff --git a/audiobooks/wsgi.py b/audiobooks/wsgi.py
new file mode 100644 (file)
index 0000000..5e0ddaa
--- /dev/null
@@ -0,0 +1,28 @@
+"""
+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)