You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

35 line
1.1 KiB

  1. from django.http import HttpResponseRedirect
  2. from django.conf import settings
  3. from django.core.urlresolvers import reverse
  4. from django.views.generic import CreateView
  5. from django_images.models import Image
  6. from braces.views import JSONResponseMixin, LoginRequiredMixin
  7. from django_images.models import Thumbnail
  8. from .forms import ImageForm
  9. class CreateImage(JSONResponseMixin, LoginRequiredMixin, CreateView):
  10. template_name = None # JavaScript-only view
  11. model = Image
  12. form_class = ImageForm
  13. def get(self, request, *args, **kwargs):
  14. if not request.is_ajax():
  15. return HttpResponseRedirect(reverse('core:recent-pins'))
  16. return super(CreateImage, self).get(request, *args, **kwargs)
  17. def form_valid(self, form):
  18. image = form.save()
  19. for size in settings.IMAGE_SIZES:
  20. Thumbnail.objects.get_or_create_at_size(image.pk, size)
  21. return self.render_json_response({
  22. 'success': {
  23. 'id': image.id
  24. }
  25. })
  26. def form_invalid(self, form):
  27. return self.render_json_response({'error': form.errors})