Skip to content

Commit

Permalink
plupdatemtime -> plupdatefile
Browse files Browse the repository at this point in the history
this script now also changes file name according to title_slug

fixed deleting media when file path is invalid
  • Loading branch information
martinKupec committed Apr 25, 2012
1 parent 2c385d3 commit c8bee69
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 54 deletions.
19 changes: 3 additions & 16 deletions photologue/management/commands/plrefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from datetime import datetime, timedelta
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.files.base import File
from django.core.files.storage import FileSystemStorage
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import now, is_aware, make_aware, get_current_timezone
from django.template.defaultfilters import slugify

from photologue.models import MediaSize, MediaModel, GalleryItemBase, Video, Photo
from photologue.default_settings import *
from photologue.utils.video import video_sizes
from photologue.utils.upload import move_file

try:
import Image
Expand All @@ -29,14 +28,6 @@ class Command(BaseCommand):
def handle(self, *args, **options):
return refresh_media()

class TemporaryFile(File):
def temporary_file_path(self):
return self.file.name

class OverrideStorage(FileSystemStorage):
def get_available_name(self, name):
return name

def refresh_media():
"""
Scan media upload folder and add any missing gallery items.
Expand Down Expand Up @@ -101,13 +92,9 @@ def refresh_media():
else:
raise Exception("Unknown file type")

file_root = os.path.join(settings.MEDIA_ROOT, get_storage_path(item, ''))
prefix = os.path.commonprefix([file_root , full])
url = full[len(prefix):]
# This will just update path in file entry
move_file(item, full, full)

item.file.storage.__class__ = OverrideStorage
item.file.save(url, TemporaryFile(open(full, 'rb')), save=False)
item.save()
if abs(item.date_taken - item.date_added) < timedelta(seconds=3):
item.date_taken = date_taken
item.save()
Expand Down
66 changes: 66 additions & 0 deletions photologue/management/commands/plupdatefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import time
from datetime import datetime, timedelta
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.files.base import File
from django.core.files.move import file_move_safe
from django.core.files.storage import FileSystemStorage
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import now, is_aware, make_aware, get_current_timezone, localtime
from django.template.defaultfilters import slugify

from photologue.models import MediaSize, MediaModel, GalleryItemBase, Video, Photo, MediaSizeCache
from photologue.default_settings import *
from photologue.utils.video import video_sizes
from photologue.utils.upload import move_file
from photologue.models.video import poster_unconverted

class Command(BaseCommand):
help = _('Set name and mtime of all files associated to media items to title_slug and date_taken.')

requires_model_validation = True
can_import_settings = True

def handle(self, *args, **options):
return update_files()

def update_item(item, newname):
path = item.file.path
root, name = os.path.split(path)
base, ext = os.path.splitext(name)
renamed = os.path.join(root, u'%s%s' % (newname, ext))

cache = {}
for size in MediaSizeCache().sizes.values():
func = getattr(item, "get_%s_filename" % size.name, None)
if func:
sizename = func()
if os.path.exists(sizename) and sizename.startswith(settings.MEDIA_ROOT):
cache[func] = func()

# No need to regenerate caches
item.prevent_cache_clear = True
# Do the move and save
move_file(item, path, renamed)

for key, value in cache.items():
print key(), value
file_move_safe(value, key())

taken = item.date_taken
taken = taken.astimezone(get_current_timezone())
mtime = int(time.mktime(taken.timetuple()))
os.utime(item.file.path, (mtime, mtime))


def update_files():
"""
Set name and mtime of all files associated to media items to title_slug and date_taken.
"""

for item in GalleryItemBase.objects.all():
update_item(item, item.title_slug)
if hasattr(item, 'poster'):
if not poster_unconverted(item.poster):
update_item(item.poster, item.title_slug)
35 changes: 0 additions & 35 deletions photologue/management/commands/plupdatemtime.py

This file was deleted.

7 changes: 4 additions & 3 deletions photologue/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def add_accessor_methods(self, *args, **kwargs):
ok = True
if not ok:
continue
#if not hasattr(self, related_model):
# continue
setattr(self, 'get_%s_size' % size.name,
curry(self._get_SIZE_size, size=size.name))
setattr(self, 'get_%s_mediasize' % size.name,
Expand Down Expand Up @@ -160,7 +158,10 @@ def save(self, *args, **kwargs):
def delete(self):
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
self.clear_cache()
os.remove(self.file.path)
try:
os.remove(self.file.path)
except:
pass
super(MediaModel, self).delete()

class MediaOverride(MediaModel):
Expand Down
24 changes: 24 additions & 0 deletions photologue/utils/upload.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import os
from datetime import timedelta
from cStringIO import StringIO
from django.conf import settings
from django.core.files.temp import NamedTemporaryFile
from django.core.files.base import ContentFile, File
from django.core.files.storage import FileSystemStorage
from django.core.files.base import File
from django.utils.timezone import make_aware, get_current_timezone
from django.template.defaultfilters import slugify
from photologue.utils.video import video_sizes
from photologue.utils.libmc import get_moi_details, set_mpg_dar
from photologue.models import Photo, Video, GalleryItemBase
from photologue.default_settings import *

try:
import Image
Expand Down Expand Up @@ -136,3 +140,23 @@ def upload_file(name, original_name, content, date_taken=None, retrieve_another=
break
count = count + 1
return count

class TemporaryFile(File):
def temporary_file_path(self):
# This is needed. When we move the file, this will not exist and
# django will complain
self._size = os.path.getsize(self.file.name)
return self.file.name

class OverrideStorage(FileSystemStorage):
def get_available_name(self, name):
return name

def move_file(item, orig, to):
file_root = os.path.join(settings.MEDIA_ROOT, get_storage_path(item, ''))
prefix = os.path.commonprefix([file_root , to])
url = to[len(prefix):]

item.file.storage.__class__ = OverrideStorage
# This also saves the entry
item.file.save(url, TemporaryFile(open(orig, 'rb')))

0 comments on commit c8bee69

Please sign in to comment.