Commits

Jason Goldstein committed 28d7a43

Simplified with helpers, and started on experimental mixins

Comments (0)

Files changed (5)

 from django.contrib.contenttypes.models import ContentType
 from cropper.models import Crop
 
-ROTATE_OPTIONS = (
-    (0, ''),
-    (90, '90 degrees clockwise'),
-    (270, '90 degrees counter-clockwise'),
-    (180, '180 degrees'),
-)
+# ROTATE_OPTIONS = (
+#     (0, ''),
+#     (90, '90 degrees clockwise'),
+#     (270, '90 degrees counter-clockwise'),
+#     (180, '180 degrees'),
+# )
 
 
 class CropImageForm(forms.Form):

cropper/helpers.py

+from cropper.models import Crop
+from django.contrib.contenttypes.models import ContentType
+
+def get_or_create_crop(obj, field_name):
+    # Get any existing crop coordinates to pass along
+    crop = Crop.objects.get_or_create(object_id = obj.id, 
+            content_type = ContentType.objects.get_for_model(obj),
+            field = field_name,
+        )[0]
+    return crop
+
+def delete_crop(obj, field_name):    
+    crop = Crop.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj), field=field_name)
+    if not crop:
+        return False
+
+    crop.delete()
+    return True

cropper/models.py

 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 
+
+# class CroppableImageMixin(models.Model):
+#     """ 
+#     EXPERTIMENTAL: DO NOT USE.
+
+#     Handles post save logic for models that have croppable image fields.
+
+#     """
+
+#     class Meta:
+#         abstract = True
+
+#     def clear_old_crops(self, *args, **kwargs):
+#         """ Delete any crops if an image changes. """
+#         from cropper.helpers import delete_crop
+
+#         if self.id: # only existing versions
+#             fields = self._meta.fields
+#             croppable_fields = [field for field in fields if field.__class__.__name__ == "ImageField"]
+
+#             old = self.__class__.objects.get(id=self.id)  # Before this save
+#             for field in croppable_fields:
+#                 if getattr(self, field.name) != getattr(old, field.name): # If the file is different
+#                     delete_crop(self, field)
+
+
 class Crop(models.Model):
     """ A cropped version of an imagefield from another model. """
     content_type = models.ForeignKey(ContentType)
 from django.shortcuts import render, get_object_or_404, redirect
-from django.contrib.contenttypes.models import ContentType
 from django.contrib import messages
 from django.db.models.loading import get_model
-
-from cropper.models import Crop
 from cropper.forms import CropImageForm
+from cropper.helpers import get_or_create_crop, delete_crop
 
 
 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.
+    """
+    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
     view or decorator that ensures it's okay for the user to access it.
 
     # So we can reuse this view, passing 'delete' in GET will kill it and abort
     if 'delete' in request.POST:
-        crop = Crop.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj), field=field)
-        crop.delete()
+        delete_crop(obj, field)
         messages.add_message(request, messages.SUCCESS, 'No crop? No problem..')
         return redirect(post_save_redirect)
 
     # Get any existing crop coordinates to pass along
-    coordinates = Crop.objects.get_or_create(object_id = obj.id, 
-            content_type = ContentType.objects.get_for_model(obj),
-            field = field,
-        )[0].coordinates
+    coordinates = get_or_create_crop(obj, field).coordinates
 
     form = CropImageForm(request.POST or None, coordinates=coordinates, model=obj, field=field)
 
     if request.POST:
         if form.is_valid():
-            crop = form.save()
-            coordinates = crop.coordinates
+            form.save()
             messages.add_message(request, messages.SUCCESS, 'Crop saved! That was easy.')
             return redirect(post_save_redirect)
         else:
 
 setup(
     name='Scruffy Cropper',
-    version="0.2.2",
+    version="0.2.3",
     author='Jason Goldstein',
     author_email='jason@betheshoe.com',
     url='https://bitbucket.org/whatisjasongoldstein/django-cropper',