Commits

Jason Goldstein committed 3181d62

Post save redirect improvements

  • Participants
  • Parent commits 152165d

Comments (0)

Files changed (4)

File cropper/templates/cropper/crop.html

             {{ form.as_p }}
         </div>
         <div class="section controls">
+            <input type="hidden" name="post-save-redirect" value="{{ post_save_redirect }}">
             <input class="button" name="save" type="submit" value="Save Crop" default>
             
             {% if coordinates %}            

File cropper/views.py

 from django.shortcuts import render, get_object_or_404, redirect
 from django.contrib.contenttypes.models import ContentType
-from django.contrib.contenttypes import generic
 from django.contrib import messages
 from django.db.models.loading import get_model
 
 from cropper.forms import CropImageForm
 
 
-def create_crop(request, app, model_name, object_id, field, template='cropper/crop.html', post_save_redirect=None):
+def create_crop(request, app, model_name, object_id, field, template='cropper/crop.html', post_save_redirect="/"):
     """ Create a crop, or edit an existing one. This is an example of a wrapping view.
 
     Notice there is no security around this view. I would not call it directly without a wrapper
         crop = Crop.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj), field=field)
         crop.delete()
         messages.add_message(request, messages.SUCCESS, 'No crop? No problem..')
-        return redirect(post_save_redirect or '/')
+        return redirect(post_save_redirect)
 
     # Get any existing crop coordinates to pass along
     coordinates = Crop.objects.get_or_create(object_id = obj.id, 
             crop = form.save()
             coordinates = crop.coordinates
             messages.add_message(request, messages.SUCCESS, 'Crop saved! That was easy.')
-            return redirect(post_save_redirect or '/')
+            return redirect(post_save_redirect)
         else:
             messages.add_message(request, messages.ERROR, "Not a valid crop.")
 
     return render(request, template, {
             'form': form,
             'coordinates': coordinates,
+            'post_save_redirect': post_save_redirect,
         })

File cropper/widgets.py

 from django.forms import ClearableFileInput, CheckboxInput
 from django.utils.safestring import mark_safe
+from django.template.defaultfilters import urlencode
 from django.template import Template, Context
 
 def _snippet(template="", context={}):
 
     def _crop_button(self, substitutions):
         link = self.attrs.get('data-crop-url')
+        post_save_link = urlencode(self.attrs.get('post-save-redirect') or "")
+
         if not link:
             return ""
-        return """<a class="button" href="%s">Crop</a>""" % link
+        return """<a class="button" href="%s?post-save-redirect=%s">Crop</a>""" % (link, post_save_link)
 
     def render(self, name, value, attrs=None):
 
 
 setup(
     name='Scruffy Cropper',
-    version="0.1.2",
+    version="0.1.3",
     author='Jason Goldstein',
     author_email='jason@betheshoe.com',
     url='https://bitbucket.org/whatisjasongoldstein/django-cropper',