add in size onto preview, change api and job to backfill old data (bug 664889)

This commit is contained in:
Andy McKay 2011-06-22 12:09:18 -07:00
Родитель 1e982d7ca9
Коммит 428e989410
8 изменённых файлов: 85 добавлений и 20 удалений

Просмотреть файл

@ -6,14 +6,15 @@ from django.db.models import Q
from addons.models import Addon
from addons.tasks import fix_get_satisfaction
from amo.utils import chunked
from devhub.tasks import flag_binary
from devhub.tasks import flag_binary, get_preview_sizes
tasks = {
'flag_binary': {'method': flag_binary, 'qs': []},
'fix_get_satisfaction': {
'method': fix_get_satisfaction,
'qs': [Q(get_satisfaction_company__startswith='http')],
}
},
'get_preview_sizes': {'method': get_preview_sizes, 'qs': []}
}

Просмотреть файл

@ -14,6 +14,7 @@ from django.utils.translation import trans_real as translation
import caching.base as caching
import commonware.log
import json_field
from tower import ugettext_lazy as _
import amo.models
@ -1328,6 +1329,7 @@ class Preview(amo.models.ModelBase):
caption = TranslatedField()
position = models.IntegerField(default=0)
sizes = json_field.JSONField(max_length=25)
class Meta:
db_table = 'previews'
@ -1371,6 +1373,14 @@ class Preview(amo.models.ModelBase):
def image_path(self):
return self._image_path(settings.PREVIEW_FULL_PATH)
@property
def thumbnail_size(self):
return self.sizes.get('thumbnail', []) if self.sizes else []
@property
def image_size(self):
return self.sizes.get('image', []) if self.sizes else []
# Use pre_delete since we need to know what we want to delete in the fs.
@receiver(dbsignals.pre_delete, sender=Preview)

Просмотреть файл

@ -297,7 +297,7 @@ Image.Image.load = patched_load
def resize_image(src, dst, size, remove_src=True):
"""Resizes and image from src, to dst."""
"""Resizes and image from src, to dst. Returns width and height."""
if src == dst:
raise Exception("src and dst can't be the same: %s" % src)
@ -313,6 +313,8 @@ def resize_image(src, dst, size, remove_src=True):
if remove_src:
os.remove(src)
return im.size
def remove_icons(destination):
for size in ADDON_ICON_SIZES:

Просмотреть файл

@ -53,10 +53,10 @@
<previews>
{%- for preview in addon.all_previews -%}
<preview position="{{ preview.position|int }}">
<full type="{{ preview.filetype }}">
<full type="{{ preview.filetype }}"{% with sizes=preview.image_size -%}{% if sizes %} width="{{ sizes[0] }}" height="{{ sizes[1] }}"{% endif %}{% endwith -%}>
{{ preview.image_url|urlparams(src='api') }}
</full>
<thumbnail type="{{ preview.thumbtype }}">
<thumbnail type="{{ preview.thumbtype }}"{% with sizes=preview.thumbnail_size -%}{% if sizes %} width="{{ sizes[0] }}" height="{{ sizes[1] }}"{% endif %}{% endwith -%}>
{{ preview.thumbnail_url|urlparams(src='api') }}
</thumbnail>
{%- if preview.caption -%}

Просмотреть файл

@ -7,15 +7,15 @@ from django.core.cache import cache
from django.conf import settings
from django.test.client import Client
from pyquery import PyQuery as pq
import jingo
from test_utils import TestCase
from nose.tools import eq_
from pyquery import PyQuery as pq
from test_utils import TestCase
import amo
import api
import api.utils
from addons.models import Addon, AddonCategory, Category, Feature
from addons.models import Addon, AddonCategory, Category, Feature, Preview
from amo import helpers
from amo.urlresolvers import reverse
from search.tests import SphinxTestCase
@ -406,6 +406,17 @@ class APITest(TestCase):
self.assertContains(make_call('addon/5299'), '<icon></icon>')
def test_thumbnail_size(self):
addon = Addon.objects.get(pk=5299)
preview = Preview.objects.create(addon=addon)
preview.sizes = {'thumbnail': [200, 150]}
preview.save()
result = make_call('addon/5299', version=1.5)
self.assertContains(result, '<full type="">')
self.assertContains(result,
'<thumbnail type="" width="200" height="150">')
class ListTest(TestCase):
"""Tests the list view with various urls."""
fixtures = ['base/apps', 'base/addon_3615', 'base/featured']

Просмотреть файл

@ -546,8 +546,7 @@ class PreviewForm(happyforms.ModelForm):
upload_hash = self.cleaned_data['upload_hash']
upload_path = path.path(settings.TMP_PATH) / 'preview' / upload_hash
tasks.resize_preview.delay(str(upload_path),
self.instance.thumbnail_path,
self.instance.image_path,
self.instance,
set_modified_on=[self.instance])
class Meta:

Просмотреть файл

@ -15,6 +15,8 @@ from amo.utils import resize_image
from files.models import FileUpload, File, FileValidation
from applications.management.commands import dump_apps
from PIL import Image
log = logging.getLogger('z.devhub.task')
@ -146,17 +148,43 @@ def resize_icon(src, dst, size, **kw):
@task
@set_modified_on
def resize_preview(src, thumb_dst, full_dst, **kw):
def resize_preview(src, instance, **kw):
"""Resizes preview images."""
thumb_dst, full_dst = instance.thumbnail_path, instance.image_path
sizes = {}
log.info('[1@None] Resizing preview: %s' % thumb_dst)
try:
# Generate the thumb.
size = amo.ADDON_PREVIEW_SIZES[0]
resize_image(src, thumb_dst, size, remove_src=False)
# Resize the original.
size = amo.ADDON_PREVIEW_SIZES[1]
resize_image(src, full_dst, size, remove_src=True)
sizes['thumbnail'] = resize_image(src, thumb_dst,
amo.ADDON_PREVIEW_SIZES[0],
remove_src=False)
sizes['image'] = resize_image(src, thumb_dst,
amo.ADDON_PREVIEW_SIZES[1],
remove_src=False)
instance.sizes = sizes
instance.save()
return True
except Exception, e:
log.error("Error saving preview: %s" % e)
@task
@write
def get_preview_sizes(ids, **kw):
log.info('[%s@%s] Getting preview sizes for addons starting at id: %s...'
% (len(ids), get_preview_sizes.rate_limit, ids[0]))
addons = Addon.objects.filter(pk__in=ids).no_transforms()
for addon in addons:
previews = addon.previews.all()
log.info('Found %s previews for: %s' % (previews.count(), addon.pk))
for preview in previews:
try:
log.info('Getting size for preview: %s' % preview.pk)
sizes = {
'thumbnail': Image.open(preview.thumbnail_path).size,
'image': Image.open(preview.image_path).size,
}
preview.update(sizes=sizes)
except Exception, err:
log.error('Failed to find size of preview: %s, error: %s'
% (addon.pk, err))

Просмотреть файл

@ -10,7 +10,7 @@ import amo
from amo.tests.test_helpers import get_image_path
import paypal
import test_utils
from applications.models import Application, AppVersion
from applications.models import AppVersion
from addons.models import Addon, Charity
from devhub import forms
from files.models import FileUpload
@ -95,7 +95,7 @@ class TestPreviewForm(test_utils.TestCase):
def test_preview_modified(self, update_mock):
addon = Addon.objects.get(pk=3615)
name = 'transparent.png'
form = forms.PreviewForm({'caption': 'test','upload_hash': name,
form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
'position': 1})
dest = path.path(settings.TMP_PATH) / 'preview'
if not os.path.exists(dest):
@ -104,3 +104,17 @@ class TestPreviewForm(test_utils.TestCase):
assert form.is_valid()
form.save(addon)
assert update_mock.called
def test_preview_size(self):
addon = Addon.objects.get(pk=3615)
name = 'non-animated.gif'
form = forms.PreviewForm({'caption': 'test', 'upload_hash': name,
'position': 1})
dest = path.path(settings.TMP_PATH) / 'preview'
if not os.path.exists(dest):
os.makedirs(dest)
shutil.copyfile(get_image_path(name), dest / name)
assert form.is_valid()
form.save(addon)
eq_(addon.previews.all()[0].sizes,
{u'image': [250, 297], u'thumbnail': [126, 150]})