Merge branch 'delete-previews'

This commit is contained in:
Dave Dash 2011-01-06 07:37:52 -08:00
Родитель 169df2846f 5bb6811494
Коммит ca30f25fbc
10 изменённых файлов: 106 добавлений и 2 удалений

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

@ -8,7 +8,8 @@ from datetime import datetime, timedelta
from django.conf import settings
from django.db import models, transaction
from django.db.models import Q, Max, signals as dbsignals
from django.db.models import Q, Sum, Max, signals as dbsignals
from django.dispatch import receiver
from django.utils.translation import trans_real as translation
import caching.base as caching
@ -1123,6 +1124,13 @@ class Preview(amo.models.ModelBase):
return self._image_path(settings.PREVIEW_FULL_PATH)
# Use pre_delete since we need to know what we want to delete in the fs.
@receiver(dbsignals.pre_delete, sender=Preview)
def delete_preview_handler(sender, instance, **kwargs):
from . import tasks
tasks.delete_preview_files.delay(instance.id)
class AppSupport(amo.models.ModelBase):
"""Cache to tell us if an add-on's current version supports an app."""
addon = models.ForeignKey(Addon)

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

@ -7,7 +7,7 @@ from celeryutils import task
import amo
from amo.decorators import write
from . import cron # Pull in tasks from cron.
from .models import Addon
from .models import Addon, Preview
log = logging.getLogger('z.task')
@ -57,3 +57,15 @@ def update_appsupport(ids):
# All our updates were sql, so invalidate manually.
Addon.objects.invalidate(*addons)
@task
def delete_preview_files(id):
log.info('[1@None] Removing preview with id of %s.' % id)
p = Preview(id=id)
for f in (p.thumbnail_path, p.image_path):
try:
os.remove(f)
except Exception, e:
log.error('Error deleting preview file: %s' % f)

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

@ -473,6 +473,13 @@ class PreviewForm(happyforms.ModelForm):
def save(self, addon, commit=True):
if self.cleaned_data:
self.instance.addon = addon
if self.cleaned_data.get('DELETE'):
# Existing preview.
if self.instance.id:
self.instance.delete()
# User has no desire to save this preview.
return
super(PreviewForm, self).save(commit=commit)
if self.cleaned_data['upload_hash']:

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

@ -68,8 +68,14 @@
{% endif %}
</div>
{{ form.id|safe }}
<span class="js-hidden delete">{{ form.DELETE|safe }}{{ form.DELETE.label_tag()|safe }}</span>
<label>{{ _('Please provide a caption for this screen shot:') }}</label>
{{ form.caption|safe }}
<p class="preview-delete">
<a href="#">
{{ _('Delete this Screen Shot') }}
</a>
</p>
<div class="preview_extra">
{{ form.upload_hash|safe }}
</div>

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

@ -88,10 +88,16 @@
{% endif %}
</div>
{{ form.id|safe }}
<span class="js-hidden delete">{{ form.DELETE|safe }}{{ form.DELETE.label_tag()|safe }}</span>
<label data-for="{{ form.prefix }}-{{ form.caption.name }}" class="above-the-field">
{{ _('Please provide a caption for this screen shot:') }}
</label>
{{ form.caption|safe }}
<p class="preview-delete">
<a href="#">
{{ _('Delete this Screen Shot') }}
</a>
</p>
<div class="preview_extra">
{{ form.upload_hash|safe }}
</div>

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

@ -1653,6 +1653,20 @@ class TestEdit(test_utils.TestCase):
eq_(str(self.get_addon().previews.all()[0].caption), 'bye')
eq_(len(self.get_addon().previews.all()), 1)
def test_edit_media_preview_delete(self):
self.preview_add()
preview = self.get_addon().previews.get()
edited = {'DELETE': 'checked',
'upload_hash': '',
'id': preview.id,
'file_upload': None}
data_formset = self.formset_media(edited, initial_count=1)
self.client.post(self.get_url('media', True), data_formset)
eq_(len(self.get_addon().previews.all()), 0)
def test_edit_media_preview_add_another(self):
self.preview_add()
self.preview_add()

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

@ -304,6 +304,20 @@ form .char-count b {
display: none;
}
#file-list .preview .preview-delete {
margin-right: 1.6em;
text-align: right;
}
#file-list .preview .preview-delete a {
color: red;
text-decoration: none;
}
.html-rtl #file-list .preview .preview-delete {
margin-right: 0;
}
#file-list .preview .preview_extra {
display: none;
}

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

@ -349,6 +349,12 @@ function initUploadPreview() {
});
$("#edit-addon-media, #submit-media").delegate(".preview-delete a", "click", function(e){
e.preventDefault();
var row = $(this).closest(".preview");
row.find(".delete input").attr("checked", "checked");
row.hide();
});
}
function initUploadIcon() {

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

@ -898,6 +898,22 @@ asyncTest('form errors are cleared', function() {
});
});
module('preview_edit', {
setup: function() {
this.sandbox = tests.createSandbox('#preview-list');
initUploadPreview();
},
teardown: function() {
this.sandbox.remove();
}
});
test('Clicking delete screenshot marks checkbox.', function() {
$(".preview-delete a", this.sandbox).trigger('click');
equals($(".delete input", this.sandbox).attr("checked"), true);
equals($(".preview:visible", this.sandbox).length, 0);
});
module('addon platform chooser', {
setup: function() {

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

@ -59,6 +59,21 @@
<li><a href="#id">Bahasa Indonesia</a></li>
</ul>
</div>
<div id="preview-list">
<div id="edit-addon-media">
<div class="preview">
<span class="js-hidden delete">
<input type="checkbox" id="id_files-0-DELETE" name="files-0-DELETE"><label for="id_files-0-DELETE">Delete</label></span>
<p class="preview-delete">
<a href="#">
Delete this Screen Shot
</a>
</p>
</div>
</div>
</div>
<div id="addon-upload-template">
<div class="upload-status hidden">
<strong id="upload-status-text"></strong>