cleanup poster update

This commit is contained in:
j 2012-09-25 19:00:10 +02:00
parent 4daf115dad
commit c413d3526b

View file

@ -118,7 +118,8 @@ def get_item(info, user=None, async=False):
item = existing_item item = existing_item
except Item.DoesNotExist: except Item.DoesNotExist:
item.oxdbId = item.oxdb_id() item.oxdbId = item.oxdb_id()
item.save() p = item.save()
if not p:
tasks.update_poster.delay(item.itemId) tasks.update_poster.delay(item.itemId)
else: else:
qs = Item.objects.filter(find__key='title', find__value=info['title']) qs = Item.objects.filter(find__key='title', find__value=info['title'])
@ -128,7 +129,8 @@ def get_item(info, user=None, async=False):
item = Item() item = Item()
item.data = item_data item.data = item_data
item.user = user item.user = user
item.save() p = item.save()
if not p:
tasks.update_poster.delay(item.itemId) tasks.update_poster.delay(item.itemId)
return item return item
@ -265,8 +267,11 @@ class Item(models.Model):
response = external_data('getData', {'id': self.itemId}) response = external_data('getData', {'id': self.itemId})
if response['status']['code'] == 200: if response['status']['code'] == 200:
self.external_data = response['data'] self.external_data = response['data']
p = self.save()
if p:
p.wait()
else:
self.make_poster(True) self.make_poster(True)
self.save()
def expand_connections(self): def expand_connections(self):
c = self.get('connections') c = self.get('connections')
@ -1058,19 +1063,6 @@ class Item(models.Model):
tasks.load_subtitles.delay(self.itemId) tasks.load_subtitles.delay(self.itemId)
get_sequences.delay(self.itemId) get_sequences.delay(self.itemId)
def delete_poster(self):
if self.poster:
path = self.poster.path
try:
self.poster.delete()
except:
self.poster.name = None
else:
poster= self.path('poster.jpg')
path = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
for f in glob(path.replace('.jpg', '*.jpg')):
os.unlink(f)
def save_poster(self, data): def save_poster(self, data):
self.poster.name = self.path('poster.jpg') self.poster.name = self.path('poster.jpg')
poster = self.poster.path poster = self.poster.path
@ -1101,7 +1093,6 @@ class Item(models.Model):
def make_poster(self, force=False): def make_poster(self, force=False):
ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path())) ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path()))
if not self.poster or force: if not self.poster or force:
self.delete_poster()
poster = self.make_siteposter() poster = self.make_siteposter()
url = self.prefered_poster_url() url = self.prefered_poster_url()
if url: if url:
@ -1112,6 +1103,13 @@ class Item(models.Model):
data = f.read() data = f.read()
if data: if data:
self.save_poster(data) self.save_poster(data)
poster = self.poster.path
for f in glob(poster.replace('.jpg', '*.jpg')):
if f != poster:
try:
os.unlink(f)
except OSError:
pass
def make_siteposter(self): def make_siteposter(self):
poster = self.path('siteposter.jpg') poster = self.path('siteposter.jpg')
@ -1194,12 +1192,12 @@ class Item(models.Model):
p = subprocess.Popen(cmd) p = subprocess.Popen(cmd)
p.wait() p.wait()
#remove cached versions #remove cached versions
icons = os.path.abspath(os.path.join(settings.MEDIA_ROOT, icon)) icon = os.path.abspath(os.path.join(settings.MEDIA_ROOT, icon))
icons = glob(icons.replace('.jpg', '*.jpg')) for f in glob(icon.replace('.jpg', '*.jpg')):
for f in filter(lambda p: not p.endswith('/icon.jpg'), icons): if f != icon:
try: try:
os.unlink(f) os.unlink(f)
except: except OSError:
pass pass
return icon return icon