mirror of
https://github.com/pinry/pinry.git
synced 2026-01-15 19:52:06 +01:00
Too lax unique constraints for Thumbnail coped with "on demand" thumbnail generation may leave database in inconsistent state where two thumbnail for the same size are saved. We should be able to prevent that from happening by generating all thumbnails when we save the image. Should fix #24, but I can't figure out a way to actually test it.
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from django.http import HttpResponseRedirect
|
|
from django.conf import settings
|
|
from django.core.urlresolvers import reverse
|
|
from django.views.generic import CreateView
|
|
from django_images.models import Image
|
|
|
|
from braces.views import JSONResponseMixin, LoginRequiredMixin
|
|
from django_images.models import Thumbnail
|
|
|
|
from .forms import ImageForm
|
|
|
|
|
|
class CreateImage(JSONResponseMixin, LoginRequiredMixin, CreateView):
|
|
template_name = None # JavaScript-only view
|
|
model = Image
|
|
form_class = ImageForm
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
if not request.is_ajax():
|
|
return HttpResponseRedirect(reverse('core:recent-pins'))
|
|
return super(CreateImage, self).get(request, *args, **kwargs)
|
|
|
|
def form_valid(self, form):
|
|
image = form.save()
|
|
for size in settings.IMAGE_SIZES.keys():
|
|
Thumbnail.objects.get_or_create_at_size(image.pk, size)
|
|
return self.render_json_response({
|
|
'success': {
|
|
'id': image.id
|
|
}
|
|
})
|
|
|
|
def form_invalid(self, form):
|
|
return self.render_json_response({'error': form.errors})
|