from errno import EEXIST, ENOENT
from fcntl import flock, LOCK_EX
from os import mkdir, path, unlink
+from urllib.parse import urljoin
from zipfile import ZipFile
+from django.apps import apps
from django.conf import settings
from django.core.files.storage import DefaultStorage
from django.core.files.uploadedfile import UploadedFile
# @task
-def create_zip(paths, zip_slug):
+def create_zip(paths, zip_slug, file_contents=None):
"""
Creates a zip in MEDIA_ROOT/zip directory containing files from path.
Resulting archive filename is ${zip_slug}.zip
if arcname is None:
arcname = path.basename(p)
zipf.write(p, arcname)
+ if file_contents:
+ for arcname, content in file_contents.items():
+ zipf.writestr(arcname, content)
finally:
zipf.close()
return '%s%s%s/' % (settings.MEDIA_URL, settings.IMAGE_DIR, slug)
+def absolute_url(url):
+ Site = apps.get_model('sites', 'Site')
+ site = Site.objects.get_current()
+ base_url = '%s://%s' % (
+ 'https' if settings.SESSION_COOKIE_SECURE else 'http',
+ site.domain
+ )
+ return urljoin(base_url, url)
+
+
def get_mp3_length(path):
from mutagen.mp3 import MP3
return int(MP3(path).info.length)